package org.jeecg.modules.stm.project.create.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
  
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.stm.act.check.entity.StCaMainCheck;
import org.jeecg.modules.stm.act.check.service.IStCaMainCheckService;
import org.jeecg.modules.stm.act.collection.entity.StCaMainRelCollection;
import org.jeecg.modules.stm.act.collection.service.IStCaMainRelCollectionService;
import org.jeecg.modules.stm.act.main.entity.StCaMain;
import org.jeecg.modules.stm.act.main.service.IStCaMainService;
import org.jeecg.modules.stm.project.create.entity.StProjectCheck;
import org.jeecg.modules.stm.project.create.entity.StProjectGather;
import org.jeecg.modules.stm.project.create.entity.StProjectInterview;
import org.jeecg.modules.stm.project.create.entity.StProjectPerpetrate;
import org.jeecg.modules.stm.project.create.service.IStProjectCheckService;
import org.jeecg.modules.stm.project.create.service.IStProjectGatherService;
import org.jeecg.modules.stm.project.create.service.IStProjectInterviewService;
import org.jeecg.modules.stm.project.create.service.IStProjectPerpetrateService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.jeecg.modules.stm.base.domain.service.IStDomainService;
import org.jeecg.modules.stm.base.domain.vo.StDomainVo;

import org.jeecg.modules.stm.project.create.mapper.StProjectCheckMapper;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.stm.project.create.vo.ProjectImportVo;
import org.jeecg.modules.stm.project.create.vo.StProjectCheckBatchVo;
import org.jeecg.modules.stm.project.create.vo.StProjectCheckVo;
import org.jeecg.modules.stm.utils.UserUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: st_project_check
 * @Author: jeecg-boot
 * @Date:   2023-08-21
 * @Version: V1.0
 */
@Service
public class StProjectCheckServiceImpl extends ServiceImpl<StProjectCheckMapper, StProjectCheck> implements IStProjectCheckService {

    @Autowired
    private IStCaMainRelCollectionService iStCaMainRelCollectionService;

    @Autowired
    private IStCaMainCheckService iStCaMainCheckService;

    @Autowired
    private IStCaMainService iStCaMainService;

    @Autowired
    private IStProjectPerpetrateService iStProjectPerpetrateService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private IStProjectGatherService iStProjectGatherService;

    @Autowired
    private IStProjectInterviewService iStProjectInterviewService;

    @Autowired
    private IStDomainService iStDomainService;

    @Override
    public <E extends IPage<StProjectCheck>> E page(E page, Wrapper<StProjectCheck> queryWrapper) {
        IPage<StProjectCheck> pageList = super.page(page, queryWrapper);
        List<StProjectCheck> list = pageList.getRecords();
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        List<StProjectCheck> voList = new ArrayList<>();
        for(StProjectCheck entity:list) {
            StProjectCheckVo stProjectCheckVo = new StProjectCheckVo();
            BeanUtils.copyProperties(entity,stProjectCheckVo);
            StCaMain stCaMain2 = iStCaMainService.queryByCaNo(entity.getCaNo());
            if(stCaMain2!=null) {
                stProjectCheckVo.setCaName(stCaMain2.getCaName());
                String domainCode = stCaMain2.getDomainCode();
                StDomainVo stDomainVo = iStDomainService.queryEntityByCode(domainCode);
                if(stDomainVo==null) {
                    stProjectCheckVo.setDomainName("-");
                }else{
                    stProjectCheckVo.setDomainName(stDomainVo.getDomainName());
                }
            }else{
                stProjectCheckVo.setCaName("-");
            }
            if(StringUtils.hasText(entity.getDutyCode())) {
                String dutyCode = entity.getDutyCode();
                QueryWrapper<StProjectPerpetrate> projectPerpetrateQueryWrapper = new QueryWrapper<>();
                projectPerpetrateQueryWrapper.eq("PROJECT_CRE_ID",entity.getProjectCreId());
                projectPerpetrateQueryWrapper.eq("USER_NO",dutyCode);
                StProjectPerpetrate stProjectPerpetrate =iStProjectPerpetrateService.getOne(projectPerpetrateQueryWrapper);
                if(stProjectPerpetrate!=null){
                    SysUser sysUser = iSysUserService.getUserByName(dutyCode);
                    stProjectCheckVo.setDutyName(String.format("%s-%s",stProjectPerpetrate.getProjectRoleName(),sysUser.getRealname()));
                }else{
                    stProjectCheckVo.setDutyName("-");
                }
            }else {
                stProjectCheckVo.setDutyName("-");
            }
            voList.add(stProjectCheckVo);
        }
        pageList.setRecords(voList);
        return (E) pageList;
    }

    @Override
    public void saveInfo(Integer projectCreId, String collectionNo) {
        QueryWrapper<StProjectCheck> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CRE_ID",projectCreId);
        long count = this.count(queryWrapper);
        if(count>0){
            this.remove(queryWrapper);
            QueryWrapper<StProjectGather> gatherQueryWrapper = new QueryWrapper<>();
            gatherQueryWrapper.eq("PROJECT_CRE_ID",projectCreId);
            iStProjectGatherService.remove(gatherQueryWrapper);
            QueryWrapper<StProjectInterview> interviewQueryWrapper = new QueryWrapper<>();
            interviewQueryWrapper.eq("PROJECT_CRE_ID",projectCreId);
            iStProjectInterviewService.remove(interviewQueryWrapper);
        }
        String userCode = UserUtil.getUserCode();
        QueryWrapper<StCaMainRelCollection> queryRelWrapper = new QueryWrapper<>();
        queryRelWrapper.eq("COLLECTION_NO",collectionNo);
        List<StCaMainRelCollection> relCollectionList = iStCaMainRelCollectionService.list(queryRelWrapper);
        if(!CollectionUtils.isEmpty(relCollectionList)){
            for(StCaMainRelCollection stCaMainRelCollection:relCollectionList){
                String caNo = stCaMainRelCollection.getCaNo();
                StCaMain stCaMain2 = iStCaMainService.queryByCaNo(caNo);
                if(stCaMain2==null){
                    continue;
                }
                QueryWrapper<StCaMainCheck> checkQueryWrapper = new QueryWrapper<>();
                checkQueryWrapper.eq("CA_NO",caNo);
                List<StCaMainCheck> stCaMainCheckList =  iStCaMainCheckService.list(checkQueryWrapper);
                if(!CollectionUtils.isEmpty(stCaMainCheckList)){
                    for(StCaMainCheck stCaMainCheck:stCaMainCheckList){
                        StProjectCheck stProjectCheck = new StProjectCheck();
                        BeanUtils.copyProperties(stCaMainCheck,stProjectCheck);
                        // 修正拷贝过程id复制导致主键冲突
                        stProjectCheck.setId(null);
                        stProjectCheck.setCreatedUser(userCode);
                        stProjectCheck.setUpdatedUser(userCode);
                        stProjectCheck.setProjectCreId(projectCreId);
                        stProjectCheck.setCaNo(caNo);
                        stProjectCheck.setConfigStatus("1");
                        stProjectCheck.setDutyStatus("0");
                        stProjectCheck.setUpdatedTime(null);
                        stProjectCheck.setCreatedTime(null);
                        this.save(stProjectCheck);
                        iStProjectGatherService.saveInfo(stProjectCheck,stCaMain2.getDomainCode());
                        iStProjectInterviewService.saveInfo(stProjectCheck);
                    }
                }else{
                    StProjectCheck stProjectCheck = new StProjectCheck();
                    stProjectCheck.setProjectCreId(projectCreId);
                    stProjectCheck.setCaNo(caNo);
                    stProjectCheck.setCreatedUser(userCode);
                    stProjectCheck.setUpdatedUser(userCode);
                    stProjectCheck.setConfigStatus("0");
                    stProjectCheck.setDutyStatus("0");
                    this.save(stProjectCheck);
                    iStProjectGatherService.saveInfo(stProjectCheck,stCaMain2.getDomainCode());
                    iStProjectInterviewService.saveInfo(stProjectCheck);
                }
            }
        }
    }

    @Override
    public void batchEditProjectDutyCheck(StProjectCheckBatchVo stProjectCheckBatchVo) {
        String userCode = UserUtil.getUserCode();
        for(String id : stProjectCheckBatchVo.getIds()) {
            StProjectCheck stProjectCheck = new StProjectCheck();
            stProjectCheck.setId(Integer.valueOf(id));
            stProjectCheck.setUpdatedUser(userCode);
            stProjectCheck.setDutyCode(stProjectCheckBatchVo.getDutyCode());
            stProjectCheck.setDutyStatus("1");
            this.updateById(stProjectCheck);
        }
    }

    @Override
    public void editProjectCheck(StProjectCheck stProjectCheck) {
        String userCode = UserUtil.getUserCode();
        if(stProjectCheck.getId()==null){
            StCaMain stCaMain2 = iStCaMainService.queryByCaNo(stProjectCheck.getCaNo());
            if(stCaMain2==null){
                throw new JeecgBootException(stProjectCheck.getCaNo()+"控制活动不存在");
            }
            stProjectCheck.setCreatedUser(userCode);
            stProjectCheck.setUpdatedUser(userCode);
            stProjectCheck.setConfigStatus("1");
            this.save(stProjectCheck);
            iStProjectGatherService.saveInfo(stProjectCheck,stCaMain2.getDomainCode());
            iStProjectInterviewService.saveInfo(stProjectCheck);
        }else {
            StCaMain stCaMain2 = iStCaMainService.queryByCaNo(stProjectCheck.getCaNo());
            if(stCaMain2==null){
                throw new JeecgBootException(stProjectCheck.getCaNo()+"控制活动不存在");
            }
            stProjectCheck.setUpdatedUser(userCode);
            stProjectCheck.setConfigStatus("1");
            this.updateById(stProjectCheck);
            iStProjectGatherService.updateInfo(stProjectCheck,stCaMain2.getDomainCode());
            iStProjectInterviewService.updateInfo(stProjectCheck);
        }
    }

    @Override
    public void copy(ProjectImportVo projectImportVo) {
        QueryWrapper<StProjectCheck> queryClearWrapper = new QueryWrapper<>();
        queryClearWrapper.eq("PROJECT_CRE_ID",projectImportVo.getId());
        this.remove(queryClearWrapper);
        IStProjectGatherService iStProjectGatherService = SpringContextUtils.getBean(IStProjectGatherService.class);
        IStProjectInterviewService iStProjectInterviewService = SpringContextUtils.getBean(IStProjectInterviewService.class);
        QueryWrapper<StProjectGather> queryClearGatherWrapper = new QueryWrapper<>();
        queryClearWrapper.eq("PROJECT_CRE_ID",projectImportVo.getId());
        iStProjectGatherService.remove(queryClearGatherWrapper);

        QueryWrapper<StProjectInterview> queryClearInterViewWrapper = new QueryWrapper<>();
        queryClearWrapper.eq("PROJECT_CRE_ID",projectImportVo.getId());
        iStProjectInterviewService.remove(queryClearInterViewWrapper);

        QueryWrapper<StProjectCheck> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CRE_ID",projectImportVo.getCopyId());
        List<StProjectCheck> list = this.list(queryWrapper);
        String userCode = UserUtil.getUserCode();
        if(!CollectionUtils.isEmpty(list)){
            for(StProjectCheck entity:list){
                Integer oldCheckId = entity.getId();
                entity.setId(null);
                entity.setProjectCreId(projectImportVo.getId());
                entity.setCreatedUser(userCode);
                entity.setUpdatedUser(userCode);
                this.save(entity);
                iStProjectGatherService.copy(projectImportVo,oldCheckId,entity.getId());
                iStProjectInterviewService.copy(projectImportVo,oldCheckId,entity.getId());
            }
        }
    }
}
