package com.emoon.knowledge.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.emoon.common.core.utils.MapstructUtils;
import com.emoon.common.core.utils.StringUtils;
import com.emoon.common.mybatis.core.page.PageQuery;
import com.emoon.common.mybatis.core.page.TableDataInfo;
import com.emoon.knowledge.domain.ThinkModel;
import com.emoon.knowledge.domain.bo.ThinkModelBo;
import com.emoon.knowledge.domain.vo.ThinkModelVo;
import com.emoon.knowledge.mapper.ThinkModelMapper;
import com.emoon.knowledge.service.IThinkModelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * 模型Service业务层处理
 *
 * @author wangkang
 * @date 2025-11-26
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ThinkModelServiceImpl implements IThinkModelService {

    private final ThinkModelMapper baseMapper;

    /**
     * 查询模型
     *
     * @param id 主键
     * @return 模型
     */
    @Override
    public ThinkModelVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询模型列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 模型分页列表
     */
    @Override
    public TableDataInfo<ThinkModelVo> queryPageList(ThinkModelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ThinkModel> lqw = buildQueryWrapper(bo);
        Page<ThinkModelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的模型列表
     *
     * @param bo 查询条件
     * @return 模型列表
     */
    @Override
    public List<ThinkModelVo> queryList(ThinkModelBo bo) {
        LambdaQueryWrapper<ThinkModel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ThinkModel> buildQueryWrapper(ThinkModelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ThinkModel> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ThinkModel::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), ThinkModel::getModel, bo.getModel());
        lqw.eq(bo.getNetwork() != null, ThinkModel::getNetwork, bo.getNetwork());
        lqw.eq(StringUtils.isNotBlank(bo.getCompany()), ThinkModel::getCompany, bo.getCompany());
        lqw.eq(StringUtils.isNotBlank(bo.getIntroduce()), ThinkModel::getIntroduce, bo.getIntroduce());
        lqw.eq(bo.getSort() != null, ThinkModel::getSort, bo.getSort());
        return lqw;
    }

    /**
     * 新增模型
     *
     * @param bo 模型
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ThinkModelBo bo) {
        ThinkModel add = MapstructUtils.convert(bo, ThinkModel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改模型
     *
     * @param bo 模型
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ThinkModelBo bo) {
        ThinkModel update = MapstructUtils.convert(bo, ThinkModel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除模型信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
