package org.dromara.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.domain.SenAlgorithmNodes;
import org.dromara.mapper.SenAlgorithmNodesMapper;
import org.springframework.stereotype.Service;
import org.dromara.domain.bo.SenAlgorithmConfigBo;
import org.dromara.domain.vo.SenAlgorithmConfigVo;
import org.dromara.domain.SenAlgorithmConfig;
import org.dromara.mapper.SenAlgorithmConfigMapper;
import org.dromara.service.ISenAlgorithmConfigService;

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

/**
 * 算法仓配置Service业务层处理
 *
 * @author luogang
 * @date 2024-09-12
 */
@RequiredArgsConstructor
@Service
public class SenAlgorithmConfigServiceImpl implements ISenAlgorithmConfigService {

    private final SenAlgorithmConfigMapper baseMapper;
    private final SenAlgorithmNodesMapper algorithmNodesMapper;

    /**
     * 查询算法仓配置
     *
     * @param id 主键
     * @return 算法仓配置
     */
    @Override
    public SenAlgorithmConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询算法仓配置列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 算法仓配置分页列表
     */
    @Override
    public TableDataInfo<SenAlgorithmConfigVo> queryPageList(SenAlgorithmConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SenAlgorithmConfig> lqw = buildQueryWrapper(bo);
        Page<SenAlgorithmConfigVo> result = baseMapper.selectVoPageWrapper(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的算法仓配置列表
     *
     * @param bo 查询条件
     * @return 算法仓配置列表
     */
    @Override
    public List<SenAlgorithmConfigVo> queryList(SenAlgorithmConfigBo bo) {
        LambdaQueryWrapper<SenAlgorithmConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SenAlgorithmConfig> buildQueryWrapper(SenAlgorithmConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SenAlgorithmConfig> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getAlgorithmName()), SenAlgorithmConfig::getAlgorithmName, bo.getAlgorithmName());
        lqw.eq(bo.getAlgorithmCategory() != null, SenAlgorithmConfig::getAlgorithmCategory, bo.getAlgorithmCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SenAlgorithmConfig::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增算法仓配置
     *
     * @param bo 算法仓配置
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SenAlgorithmConfigBo bo) {
        SenAlgorithmConfig add = MapstructUtils.convert(bo, SenAlgorithmConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            //插入关联节点
            SenAlgorithmNodes algorithmNodes =new SenAlgorithmNodes() ;
            algorithmNodes.setAlgorithmId(bo.getId());
            algorithmNodes.setNodeId(bo.getNodeId());
            algorithmNodesMapper.insert(algorithmNodes);
        }
        return flag;
    }

    /**
     * 修改算法仓配置
     *
     * @param bo 算法仓配置
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SenAlgorithmConfigBo bo) {
        SenAlgorithmConfig update = MapstructUtils.convert(bo, SenAlgorithmConfig.class);
        validEntityBeforeSave(update);

        SenAlgorithmNodes algorithmNodes =new SenAlgorithmNodes() ;
        algorithmNodes.setAlgorithmId(bo.getId());
        algorithmNodes.setNodeId(bo.getNodeId());
         algorithmNodesMapper.deleteById(algorithmNodes.getAlgorithmId());
        //插入关联节点
         algorithmNodesMapper.insert(algorithmNodes);

        return baseMapper.updateById(update) > 0;
    }

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

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