package cn.iocoder.yudao.module.system.service.biz;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.sys.vo.EvaluationProcessPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.CheckInProcessSequenceDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.SysEvaluationProcessDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.SysEvaluationTemplatesDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.CheckInProcessSequenceMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.SysEvaluationProcessMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.SysEvaluationTemplatesMapper;
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SysEvaluationProcessServiceImpl implements SysEvaluationProcessService {

    @Autowired
    private SysEvaluationProcessMapper sysEvaluationProcessMapper;
    @Autowired
    private CheckInProcessSequenceMapper checkInProcessSequenceMapper;
    @Autowired
    private SysEvaluationTemplatesMapper sysEvaluationTemplatesMapper;
    @Resource
    private TenantMapper tenantMapper;

    @Override
    public PageResult<SysEvaluationProcessDO> queryPage(EvaluationProcessPageReqVO pageVO) {
        PageResult<SysEvaluationProcessDO> pageResult = sysEvaluationProcessMapper.selectPage(pageVO);
        for (SysEvaluationProcessDO sysEvaluationProcessDO : pageResult.getList()) {
            sysEvaluationProcessDO.setTenantName(tenantMapper.selectById(sysEvaluationProcessDO.getTenantId()).getName());
        }
        return pageResult;
    }

    @Override
    public SysEvaluationProcessDO findOneById(Long evaluationProcessId) {
        SysEvaluationProcessDO sysEvaluationProcess = sysEvaluationProcessMapper.selectById(evaluationProcessId);
        if (sysEvaluationProcess != null) {
            List<CheckInProcessSequenceDO> sequences = checkInProcessSequenceMapper.selectList(new LambdaQueryWrapper<CheckInProcessSequenceDO>().eq(CheckInProcessSequenceDO::getEvaluationProcessId, sysEvaluationProcess.getId()));
            if (CollectionUtil.isNotEmpty(sequences)) {
                for (CheckInProcessSequenceDO sequence : sequences) {
                    SysEvaluationTemplatesDO template = sysEvaluationTemplatesMapper.selectOne(new LambdaQueryWrapper<SysEvaluationTemplatesDO>()
                            .eq(SysEvaluationTemplatesDO::getId, sequence.getTemplateId()));
                    sequence.setTemplate(template);
                }
            }
            sysEvaluationProcess.setTemplates(sequences);
        }
        return sysEvaluationProcess;
    }

    @Override
    public void addEvaluationProcess(SysEvaluationProcessDO processDO) {
        processDO.setCreatedTime(new Date());
        sysEvaluationProcessMapper.insert(processDO);
        List<CheckInProcessSequenceDO> templates = processDO.getTemplates();
        if (templates != null && !templates.isEmpty()) {
            for (CheckInProcessSequenceDO sequenceEntity : templates) {
                sequenceEntity.setEvaluationProcessId(processDO.getId());
                sequenceEntity.setTenantId(TenantContextHolder.getTenantId());
                checkInProcessSequenceMapper.insert(sequenceEntity);
            }
        }
    }

    @Override
    public void edit(SysEvaluationProcessDO processDO) {
        sysEvaluationProcessMapper.updateById(processDO);

        List<CheckInProcessSequenceDO> templates = processDO.getTemplates();
        if (CollectionUtil.isNotEmpty(templates)) {
            checkInProcessSequenceMapper.delete(new LambdaQueryWrapper<CheckInProcessSequenceDO>().eq(CheckInProcessSequenceDO::getEvaluationProcessId, processDO.getId()));

            //重新生成评估表
            for (CheckInProcessSequenceDO sequenceEntity : templates) {
                sequenceEntity.setEvaluationProcessId(processDO.getId());
                checkInProcessSequenceMapper.insert(sequenceEntity);
            }
        }
    }

    @Override
    public void delete(Long evaluationProcessId) {
        sysEvaluationProcessMapper.deleteById(evaluationProcessId);
        checkInProcessSequenceMapper.delete(new LambdaQueryWrapper<CheckInProcessSequenceDO>()
                .eq(CheckInProcessSequenceDO::getEvaluationProcessId, evaluationProcessId));
    }
}
