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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.sys.vo.SysEvaluationTemplatePageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.CheckInProcessSequenceDO;
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.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 SysEvaluationTemplatesServiceImpl implements SysEvaluationTemplatesService{

    @Autowired
    private SysEvaluationTemplatesMapper mapper;
    @Autowired
    private CheckInProcessSequenceMapper checkInProcessSequenceMapper;
    @Resource
    private TenantMapper tenantMapper;


    @Override
    public void add(SysEvaluationTemplatesDO templatesDO) {
        templatesDO.setCreatedDate(new Date());
        mapper.insert(templatesDO);
    }

    @Override
    public PageResult<SysEvaluationTemplatesDO> getEvaluationTemplateByPage(SysEvaluationTemplatePageReqVO reqVO) {
        PageResult<SysEvaluationTemplatesDO> pageResult = mapper.selectPage(reqVO);
        for (SysEvaluationTemplatesDO sysEvaluationTemplatesDO : pageResult.getList()) {
            sysEvaluationTemplatesDO.setTenantName(tenantMapper.selectById(sysEvaluationTemplatesDO.getTenantId()).getName());
        }
        return pageResult;
    }

    @Override
    public SysEvaluationTemplatesDO getDetails(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public void edit(SysEvaluationTemplatesDO templatesDO) {
        templatesDO.setUpdateDate(new Date());
        mapper.updateById(templatesDO);
    }

    @Override
    public void delete(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public void updateStatus(SysEvaluationTemplatesDO templatesDO) {
        SysEvaluationTemplatesDO sysEvaluationTemplatesDO = mapper.selectById(templatesDO.getId());
        if (sysEvaluationTemplatesDO != null){
            sysEvaluationTemplatesDO.setStatus(templatesDO.getStatus());
            mapper.updateById(sysEvaluationTemplatesDO);
        }
    }

    @Override
    public List<CheckInProcessSequenceDO> checkInProcessSeqInfo(Integer orgType) {
        return  checkInProcessSequenceMapper.selectList(new LambdaQueryWrapperX<CheckInProcessSequenceDO>()
                        .eqIfPresent(CheckInProcessSequenceDO::getOrgType,orgType)
                .orderByAsc(CheckInProcessSequenceDO::getSort));
    }
}

