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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.stm.common.service.IStCommonService;

import org.jeecg.modules.stm.common.vo.DutyVo;
import org.jeecg.modules.stm.project.create.service.*;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.jeecg.modules.stm.project.create.entity.StProjectCheck;
import org.jeecg.modules.stm.project.create.entity.StProjectCre;
import org.jeecg.modules.stm.project.create.entity.StProjectCreApprove;
import org.jeecg.modules.stm.project.create.mapper.StProjectCreMapper;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.stm.project.create.vo.ProjectConfigVo;
import org.jeecg.modules.stm.project.create.vo.ProjectImportVo;
import org.jeecg.modules.stm.project.create.vo.StProjectCreVo;
import org.jeecg.modules.stm.project.create.vo.TwoProjectConfigVo;
import org.jeecg.modules.stm.project.exec.entity.StProjectExec;
import org.jeecg.modules.stm.project.exec.service.IStProjectExecService;
import org.jeecg.modules.stm.utils.UserUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 项目立项
 * @Author: jeecg-boot
 * @Date:   2023-08-13
 * @Version: V1.0
 */
@Service
public class StProjectCreServiceImpl extends ServiceImpl<StProjectCreMapper, StProjectCre> implements IStProjectCreService {

    @Autowired
    private ISysDictService iSysDictService;

    @Autowired
    private IStProjectCreApproveService iStProjectCreApproveService;

    @Autowired
    private IStProjectOrgService iStProjectOrgService;

    @Autowired
    private IStProjectUserService iStProjectUserService;

    @Autowired
    private IStProjectSysService iStProjectSysService;

    @Autowired
    private IStProjectOutsourcerService iStProjectOutsourcerService;

    @Autowired
    private IStProjectXmService iStProjectXmService;

    @Autowired
    private IStProjectActService iStProjectActService;

    @Autowired
    private IStProjectPerpetrateService iStProjectPerpetrateService;

    @Override
    public <E extends IPage<StProjectCre>> E page(E page, Wrapper<StProjectCre> queryWrapper) {
        IPage<StProjectCre> pageList = super.page(page, queryWrapper);
        List<StProjectCre> voList = new ArrayList<>();
        List<StProjectCre> list = pageList.getRecords();
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        for(StProjectCre entity:list) {
            StProjectCreVo stProjectCreVo = new StProjectCreVo();
            BeanUtils.copyProperties(entity, stProjectCreVo);
            DutyVo dutyVo = SpringContextUtils.getBean(IStCommonService.class).queryDutyByCode(entity.getHeadCode());
            if(dutyVo!=null){
                stProjectCreVo.setHeadName(dutyVo.initDutyNames());
                stProjectCreVo.setExecDepName(dutyVo.getDepartName());
            }else{
                stProjectCreVo.setHeadName("-");
            }
            if(StringUtils.isNotEmpty(entity.getApproveUser())){
                DutyVo dutyVo1 = SpringContextUtils.getBean(IStCommonService.class).queryDutyByCode(entity.getApproveUser());
                if(dutyVo1!=null){
                  stProjectCreVo.setApproveUserName(dutyVo1.initDutyNames());
                }else{
                    stProjectCreVo.setApproveUserName(entity.getApproveUser());
                }
            }
            String name = iSysDictService.queryDictTextByKey("projecttype",entity.getProjectType());
            if(StringUtils.isNotEmpty(name)){
                stProjectCreVo.setProjectTypeName(name);
            }else {
                stProjectCreVo.setProjectType("-");
            }
            voList.add(stProjectCreVo);
        }
        pageList.setRecords(voList);
        return (E) pageList;
    }

    @Override
    public boolean save(StProjectCre entity) {
        String userCode = UserUtil.getUserCode();
        entity.setCreatedUser(userCode);
        entity.setUpdatedUser(userCode);
        return super.save(entity);
    }

    @Override
    public boolean updateById(StProjectCre entity) {
        String userCode = UserUtil.getUserCode();
        entity.setUpdatedUser(userCode);
        return super.updateById(entity);
    }

    @Override
    public void approve(StProjectCre entity) {
        Date date = new Date();
        String userCode = UserUtil.getUserCode();
        StProjectCre stProjectCreEntity = new StProjectCre();
        stProjectCreEntity.setId(entity.getId());
        stProjectCreEntity.setApproveFlag(entity.getApproveFlag());
        stProjectCreEntity.setApproveDes(entity.getApproveDes());
        stProjectCreEntity.setApproveTime(date);
        stProjectCreEntity.setUpdatedUser(userCode);
        super.updateById(stProjectCreEntity);
        StProjectCreApprove stProjectCreApprove = new StProjectCreApprove();
        stProjectCreApprove.setApproveFlag(entity.getApproveFlag());
        stProjectCreApprove.setApproveDes(entity.getApproveDes());
        stProjectCreApprove.setApproveTime(date);
        stProjectCreApprove.setProjectCreId(entity.getId());
        stProjectCreApprove.setApproveUser(entity.getApproveUser());
        stProjectCreApprove.setCreatedUser(userCode);
        stProjectCreApprove.setUpdatedUser(userCode);
        iStProjectCreApproveService.save(stProjectCreApprove);
        // 审核通过 写入负责人信息
        if(StringUtils.equals("1",entity.getApproveFlag())){
            StProjectCre appStProjectCre = this.getById(entity.getId());
            iStProjectPerpetrateService.saveHead(appStProjectCre);
        }
    }

    @Override
    public IPage<StProjectCre> pagePrepar(Page<StProjectCre> page, QueryWrapper<StProjectCre> queryWrapper) {
        queryWrapper.eq("APPROVE_FLAG","1");
        return page(page,queryWrapper);
    }

    @Override
    public IPage<StProjectCre> defPage(Page<StProjectCre> page, QueryWrapper<StProjectCre> queryWrapper) {
        String userCode = UserUtil.getUserCode();
        queryWrapper.and(wrapper->wrapper.eq("CREATED_USER",userCode).or().eq("APPROVE_USER",userCode));
        return page(page,queryWrapper);
    }
    @Override
    public void saveOneStep(ProjectConfigVo projectConfigVo) {
        StProjectCre qData = this.getById(projectConfigVo.getProjectCreId());
        StProjectCre stProjectCre = new StProjectCre();
        stProjectCre.setId(projectConfigVo.getProjectCreId());
        // 第一步完成
        if(qData.getConfigStep()==null || qData.getConfigStep().intValue()==0 || qData.getConfigStep().intValue()==1) {
            stProjectCre.setConfigStep(2);
        }
        stProjectCre.setConfigStatus("0");
        String userCode = UserUtil.getUserCode();
        stProjectCre.setUpdatedUser(userCode);
        this.updateById(stProjectCre);
        if(projectConfigVo.getDepartCheck()!=null){
            iStProjectOrgService.saveInfo(projectConfigVo.getProjectCreId(),projectConfigVo.getDepartCheck());
        }
        if(projectConfigVo.getUserCheck()!=null){
            iStProjectUserService.saveInfo(projectConfigVo.getProjectCreId(),projectConfigVo.getUserCheck());
        }
        if(projectConfigVo.getSysCheck()!=null){
            iStProjectSysService.saveInfo(projectConfigVo.getProjectCreId(),projectConfigVo.getSysCheck());
        }

        if(projectConfigVo.getOuterCheck()!=null){
            iStProjectOutsourcerService.saveInfo(projectConfigVo.getProjectCreId(),projectConfigVo.getOuterCheck());
        }
        if(projectConfigVo.getProjectCheck()!=null) {
            iStProjectXmService.saveInfo(projectConfigVo.getProjectCreId(),projectConfigVo.getProjectCheck());
        }

    }

    @Override
    public void saveTwoStep(TwoProjectConfigVo twoProjectConfigVo) {
        StProjectCre qData = this.getById(twoProjectConfigVo.getProjectCreId());
        StProjectCre stProjectCre = new StProjectCre();
        stProjectCre.setId(twoProjectConfigVo.getProjectCreId());
        // 第二步完成
        if(qData.getConfigStep()!=null && qData.getConfigStep().intValue()==2) {
            stProjectCre.setConfigStep(3);
        }
        stProjectCre.setConfigStatus("0");
        String userCode = UserUtil.getUserCode();
        stProjectCre.setUpdatedUser(userCode);
        this.updateById(stProjectCre);
        iStProjectActService.saveInfo(twoProjectConfigVo.getProjectCreId(),twoProjectConfigVo.getCollectionNo());
    }

    @Override
    public void add3Step(TwoProjectConfigVo twoProjectConfigVo) {
        StProjectCre qData = this.getById(twoProjectConfigVo.getProjectCreId());
        StProjectCre stProjectCre = new StProjectCre();
        stProjectCre.setId(twoProjectConfigVo.getProjectCreId());
        // 第三步完成
        if(qData.getConfigStep()!=null &&  qData.getConfigStep().intValue()==3) {
            stProjectCre.setConfigStep(4);
        }
        stProjectCre.setConfigStatus("0");
        String userCode = UserUtil.getUserCode();
        stProjectCre.setUpdatedUser(userCode);
        this.updateById(stProjectCre);
    }

    @Override
    public void add4Step(TwoProjectConfigVo twoProjectConfigVo) {
        StProjectCre qData = this.getById(twoProjectConfigVo.getProjectCreId());
        StProjectCre stProjectCre = new StProjectCre();
        stProjectCre.setId(twoProjectConfigVo.getProjectCreId());
        // 第4步完成
        if(qData.getConfigStep()!=null &&  qData.getConfigStep().intValue()==4) {
            stProjectCre.setConfigStep(5);
        }
        stProjectCre.setConfigStatus("0");
        String userCode = UserUtil.getUserCode();
        stProjectCre.setUpdatedUser(userCode);
        this.updateById(stProjectCre);
    }

    @Override
    public void add5Step(TwoProjectConfigVo twoProjectConfigVo) {
        StProjectCre qData = this.getById(twoProjectConfigVo.getProjectCreId());
        StProjectCre stProjectCre = new StProjectCre();
        stProjectCre.setId(twoProjectConfigVo.getProjectCreId());
        // 第5步完成
        if(qData.getConfigStep()!=null &&  qData.getConfigStep().intValue()==5) {
            stProjectCre.setConfigStep(6);
        }
        stProjectCre.setConfigStatus("0");
        String userCode = UserUtil.getUserCode();
        stProjectCre.setUpdatedUser(userCode);
        this.updateById(stProjectCre);

    }

    @Override
    public void add6Step(TwoProjectConfigVo twoProjectConfigVo) {
        StProjectCre qData = this.getById(twoProjectConfigVo.getProjectCreId());
        StProjectCre stProjectCre = new StProjectCre();
        stProjectCre.setId(twoProjectConfigVo.getProjectCreId());
        // 第5步完成
        if(qData.getConfigStep()!=null && qData.getConfigStep().intValue()==6) {
            stProjectCre.setConfigStep(7);
        }
        stProjectCre.setConfigStatus("0");
        String userCode = UserUtil.getUserCode();
        stProjectCre.setUpdatedUser(userCode);
        this.updateById(stProjectCre);

    }

    @Override
    public void add7Step(TwoProjectConfigVo twoProjectConfigVo) {
        StProjectCre qData = this.getById(twoProjectConfigVo.getProjectCreId());
        StProjectCre stProjectCre = new StProjectCre();
        stProjectCre.setId(twoProjectConfigVo.getProjectCreId());
        // 第7步完成
        if(qData.getConfigStep()!=null && qData.getConfigStep().intValue()==7) {
            stProjectCre.setConfigStatus("1");
            stProjectCre.setSubmitStatus("0");
        }
        String userCode = UserUtil.getUserCode();
        stProjectCre.setUpdatedUser(userCode);
        this.updateById(stProjectCre);

    }

    @Override
    public void submitPrepate(TwoProjectConfigVo twoProjectConfigVo) {
        //StProjectCre qData = this.getById(twoProjectConfigVo.getProjectCreId());
        StProjectCre stProjectCre = new StProjectCre();
        stProjectCre.setId(twoProjectConfigVo.getProjectCreId());
        String userCode = UserUtil.getUserCode();
        stProjectCre.setUpdatedUser(userCode);
        stProjectCre.setSubmitStatus("1");
        this.updateById(stProjectCre);
        // 提交完成生成下一流程数据
        saveNext(twoProjectConfigVo.getProjectCreId());
    }

    @Override
    public void save2Submit(TwoProjectConfigVo twoProjectConfigVo) {
        //StProjectCre qData = this.getById(twoProjectConfigVo.getProjectCreId());
        StProjectCre stProjectCre = new StProjectCre();
        stProjectCre.setId(twoProjectConfigVo.getProjectCreId());
        stProjectCre.setConfigStatus("1");
        stProjectCre.setSubmitStatus("1");
        String userCode = UserUtil.getUserCode();
        stProjectCre.setUpdatedUser(userCode);
        this.updateById(stProjectCre);
        saveNext(twoProjectConfigVo.getProjectCreId());
    }

    @Override
    public void projectImport(ProjectImportVo projectImportVo) {
        iStProjectOrgService.copy(projectImportVo);
        iStProjectUserService.copy(projectImportVo);
        iStProjectSysService.copy(projectImportVo);
        iStProjectOutsourcerService.copy(projectImportVo);
        iStProjectXmService.copy(projectImportVo);
        iStProjectActService.copy(projectImportVo);
        iStProjectPerpetrateService.copy(projectImportVo);
        IStProjectCheckService iStProjectCheckService = SpringContextUtils.getBean(IStProjectCheckService.class);
        iStProjectCheckService.copy(projectImportVo);
        StProjectCre stProjectCre = new StProjectCre();
        stProjectCre.setId(projectImportVo.getId());
        stProjectCre.setConfigStatus("1");
        stProjectCre.setConfigStep(7);
        stProjectCre.setSubmitStatus("1");
        this.updateById(stProjectCre);
    }

    private void saveNext(Integer projectCreId){
        String userCode = UserUtil.getUserCode();
        IStProjectCheckService iStProjectCheckService = SpringContextUtils.getBean(IStProjectCheckService.class);
        QueryWrapper<StProjectCheck> stProjectCheckQueryWrapper = new QueryWrapper<>();
        stProjectCheckQueryWrapper.select("id");
        stProjectCheckQueryWrapper.eq("PROJECT_CRE_ID",projectCreId);
        List<StProjectCheck> dataList = iStProjectCheckService.list(stProjectCheckQueryWrapper);
        if(!CollectionUtils.isEmpty(dataList)){
            for(StProjectCheck stProjectCheck:dataList) {
                // 提交完成生成下一流程数据
                IStProjectExecService iStProjectExecService = SpringContextUtils.getBean(IStProjectExecService.class);
                StProjectExec stProjectExec = new StProjectExec();
                stProjectExec.setProjectCheckId(stProjectCheck.getId());
                stProjectExec.setCreatedUser(userCode);
                stProjectExec.setUpdatedUser(userCode);
                iStProjectExecService.save(stProjectExec);
            }
        }

    }

    @Override
    public boolean removeById(Serializable id) {
        QueryWrapper<StProjectCreApprove> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CRE_ID",id);
        iStProjectCreApproveService.remove(queryWrapper);
        return super.removeById(id);
    }
}
