package com.cmvr.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmvr.common.core.domain.entity.SysGroup;
import com.cmvr.common.utils.StringUtils;
import com.cmvr.test.enums.RunModeEnum;
import com.cmvr.test.mapper.TeTaskConfigInfoMapper;
import com.cmvr.test.model.domain.TeTaskConfigInfo;
import com.cmvr.test.model.vo.TeTaskConfigInfoVO;
import com.cmvr.test.service.ITeTaskConfigInfoService;
import com.cmvr.test.service.ITeTaskOrchestrationService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 任务流程编排Service业务层处理
 *
 * @author cmvr-iot
 */
@Service
@RequiredArgsConstructor
public class TeTaskConfigInfoServiceImpl extends ServiceImpl<TeTaskConfigInfoMapper, TeTaskConfigInfo> implements ITeTaskConfigInfoService {

    private final ITeTaskOrchestrationService taskOrchestrationService;
    /**
     * 查询任务流程编排
     *
     * @param id 任务流程编排主键
     * @return 任务流程编排
     */
    @Override
    public TeTaskConfigInfo selectTeTaskConfigInfoById(String id) {
        return this.baseMapper.selectById(id);
    }

    /**
     * 查询任务流程编排列表
     *
     * @param teTaskConfigInfo 任务流程编排
     * @return 任务流程编排
     */
    @Override
    public List<TeTaskConfigInfoVO> selectTeTaskConfigInfoList(TeTaskConfigInfo teTaskConfigInfo) {
        MPJLambdaWrapper<TeTaskConfigInfo> queryWrapper = new MPJLambdaWrapper<>();

        queryWrapper.selectAll(TeTaskConfigInfo.class)
                .select(SysGroup::getGroupId)
                .select(SysGroup::getGroupName)
                .leftJoin(SysGroup.class, SysGroup::getGroupId, TeTaskConfigInfo::getGroupId)
                .eq(TeTaskConfigInfo::getRunMode, RunModeEnum.NORMAL) // 只查询普通
                .like(StringUtils.isNotBlank(teTaskConfigInfo.getTaskDesc()), TeTaskConfigInfo::getTaskDesc, teTaskConfigInfo.getTaskDesc())
                .like(StringUtils.isNotBlank(teTaskConfigInfo.getTaskName()), TeTaskConfigInfo::getTaskName, teTaskConfigInfo.getTaskName())
                .eq(StringUtils.isNotBlank(teTaskConfigInfo.getGroupId()), TeTaskConfigInfo::getGroupId, teTaskConfigInfo.getGroupId())
                .eq(StringUtils.isNotBlank(teTaskConfigInfo.getStatus()), TeTaskConfigInfo::getStatus, teTaskConfigInfo.getStatus())
                .orderByDesc(TeTaskConfigInfo::getCreateTime);

        // 执行查询并返回结果
        return this.baseMapper.selectJoinList(TeTaskConfigInfoVO.class, queryWrapper);
    }

    /**
     * 新增任务流程编排
     *
     * @param teTaskConfigInfo 任务流程编排
     * @return 结果
     */
    @Override
    public int insertTeTaskConfigInfo(TeTaskConfigInfo teTaskConfigInfo) {
        teTaskConfigInfo.setRunMode(RunModeEnum.NORMAL.name());
        return this.baseMapper.insert(teTaskConfigInfo);
    }

    /**
     * 修改任务流程编排
     *
     * @param teTaskConfigInfo 任务流程编排
     * @return 结果
     */
    @Override
    public int updateTeTaskConfigInfo(TeTaskConfigInfo teTaskConfigInfo) {
        return this.baseMapper.updateById(teTaskConfigInfo);
    }

    /**
     * 批量删除任务流程编排
     *
     * @param ids 需要删除的任务流程编排主键
     * @return 结果
     */
    @Override
    public int deleteTeTaskConfigInfoByIds(String[] ids) {
        // 删除对应的检测项关系
        taskOrchestrationService.deleteByTaskIds(ids);
        return this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除任务流程编排信息
     *
     * @param id 任务流程编排主键
     * @return 结果
     */
    @Override
    public int deleteTeTaskConfigInfoById(String id) {
        // 删除对应的检测项关系
        taskOrchestrationService.deleteByTaskId(id);
        return this.baseMapper.deleteById(id);
    }

    /**
     * 检查任务配置是否存在
     */
    @Override
    public boolean isTaskExist(String id) {
        LambdaQueryWrapper<TeTaskConfigInfo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TeTaskConfigInfo::getId, id);
        return this.baseMapper.exists(wrapper);
    }
}
