package org.dromara.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.chat.domain.ChatModel;
import org.dromara.chat.domain.bo.ChatModelBo;
import org.dromara.chat.domain.vo.ChatModelVo;
import org.dromara.chat.mapper.ChatModelMapper;
import org.dromara.chat.service.ChatModelService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 聊天模型Service业务层处理
 *
 * @author pengles
 */
@RequiredArgsConstructor
@Service
public class ChatModelServiceImpl extends ServiceImpl<ChatModelMapper, ChatModel> implements ChatModelService {

    /**
     * 查询聊天模型
     */
    @Override
    public ChatModelVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询聊天模型列表
     */
    @Override
    public TableDataInfo<ChatModelVo> queryPageList(ChatModelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ChatModel> lqw = buildQueryWrapper(bo);
        Page<ChatModelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询聊天模型列表
     */
    @Override
    public List<ChatModelVo> queryList(ChatModelBo bo) {
        LambdaQueryWrapper<ChatModel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ChatModel> buildQueryWrapper(ChatModelBo bo) {
        LambdaQueryWrapper<ChatModel> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCategory()), ChatModel::getCategory, bo.getCategory());
        lqw.like(StringUtils.isNotBlank(bo.getModelName()), ChatModel::getModelName, bo.getModelName());
        lqw.like(StringUtils.isNotBlank(bo.getModelDescribe()), ChatModel::getModelDescribe, bo.getModelDescribe());
        lqw.eq(StringUtils.isNotBlank(bo.getModelType()), ChatModel::getModelType, bo.getModelType());
        lqw.eq(StringUtils.isNotBlank(bo.getModelShow()), ChatModel::getModelShow, bo.getModelShow());
        return lqw;
    }

    /**
     * 新增聊天模型
     */
    @Override
    public Boolean insertByBo(ChatModelBo bo) {
        ChatModel add = MapstructUtils.convert(bo, ChatModel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改聊天模型
     */
    @Override
    public Boolean updateByBo(ChatModelBo bo) {
        ChatModel update = MapstructUtils.convert(bo, ChatModel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ChatModel entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除聊天模型
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据模型分类查询模型列表
     */
    @Override
    public List<ChatModelVo> queryListByCategory(String category) {
        LambdaQueryWrapper<ChatModel> lqw = Wrappers.lambdaQuery();
        lqw.eq(ChatModel::getCategory, category);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据模型名称查询模型
     */
    @Override
    public ChatModelVo queryByModelName(String modelName) {
        LambdaQueryWrapper<ChatModel> lqw = Wrappers.lambdaQuery();
        lqw.eq(ChatModel::getModelName, modelName);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 查询显示的模型列表
     */
    @Override
    public List<ChatModelVo> queryShowModelList() {
        LambdaQueryWrapper<ChatModel> lqw = Wrappers.lambdaQuery();
        lqw.eq(ChatModel::getModelShow, "1");
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据模型类型查询模型列表
     */
    @Override
    public List<ChatModelVo> queryListByModelType(String modelType) {
        LambdaQueryWrapper<ChatModel> lqw = Wrappers.lambdaQuery();
        lqw.eq(ChatModel::getModelType, modelType);
        return baseMapper.selectVoList(lqw);
    }

}