package com.cmvr.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmvr.common.utils.StringUtils;
import com.cmvr.test.enums.RunModeEnum;
import com.cmvr.test.enums.TaskStatusEnum;
import com.cmvr.test.mapper.TeTaskInstMapper;
import com.cmvr.test.model.domain.TeNodeInst;
import com.cmvr.test.model.domain.TeTaskConfigInfo;
import com.cmvr.test.model.domain.TeTaskInst;
import com.cmvr.test.model.vo.TeQueryTaskInstVO;
import com.cmvr.test.model.vo.TeTaskInstVO;
import com.cmvr.test.service.ITeNodeInstService;
import com.cmvr.test.service.ITeTaskInstService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 任务实例Service业务层处理
 *
 * @author cmvr-iot
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TeTaskInstServiceImpl extends ServiceImpl<TeTaskInstMapper, TeTaskInst> implements ITeTaskInstService {

    private final ITeNodeInstService teNodeInstService;

    /**
     * 查询任务实例列表
     */
    @Override
    public List<TeTaskInstVO> selectTeTaskInstList(TeQueryTaskInstVO queryTaskInstVO) {
        MPJLambdaWrapper<TeTaskInst> queryWrapper = new MPJLambdaWrapper<>();

        queryWrapper.selectAll(TeTaskInst.class)
                .select(TeTaskConfigInfo::getTaskName)
                .selectAs(TeTaskInst::getTaskId, TeQueryTaskInstVO::getTaskId)
                .leftJoin(TeTaskConfigInfo.class, TeTaskConfigInfo::getId, TeTaskInst::getTaskId)
                .eq(TeTaskInst::getRunMode, RunModeEnum.NORMAL)
                .eq(StringUtils.isNotBlank(queryTaskInstVO.getStatus()), TeTaskInst::getStatus, queryTaskInstVO.getStatus())
                .like(StringUtils.isNotBlank(queryTaskInstVO.getTaskName()), TeTaskConfigInfo::getTaskName, queryTaskInstVO.getTaskName())
                .ge(StringUtils.isNotBlank(queryTaskInstVO.getStartTime()), TeTaskInst::getCreateTime, queryTaskInstVO.getStartTime())
                .le(StringUtils.isNotBlank(queryTaskInstVO.getEndTime()), TeTaskInst::getCreateTime, queryTaskInstVO.getEndTime())
                .orderByDesc(TeTaskInst::getCreateTime);

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

    @Override
    public boolean updateStatus(String instId, TaskStatusEnum taskStatusEnum) {
        return this.update(
                new LambdaUpdateWrapper<TeTaskInst>()
                        .eq(TeTaskInst::getId, instId)
                        .set(TeTaskInst::getStatus, taskStatusEnum.getCode())
        );
    }

    @Override
    public List<TeNodeInst> detail(String instId) {
        LambdaQueryWrapper<TeNodeInst> wrapper = Wrappers.lambdaQuery(TeNodeInst.class);
        wrapper.eq(TeNodeInst::getInstId, instId)
                .orderByAsc(TeNodeInst::getId);
        return teNodeInstService.list(wrapper);
    }
}
