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.common.service.IStCommonService;

import org.jeecg.modules.stm.common.vo.DutyVo;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.jeecg.modules.stm.project.create.entity.StProjectCre;
import org.jeecg.modules.stm.project.create.entity.StProjectPerpetrate;
import org.jeecg.modules.stm.project.create.mapper.StProjectPerpetrateMapper;
import org.jeecg.modules.stm.project.create.service.IStProjectPerpetrateService;
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.utils.UserUtil;

import java.util.List;

/**
 * @Description: st_project_perpetrate
 * @Author: jeecg-boot
 * @Date:   2023-08-21
 * @Version: V1.0
 */
@Service
public class StProjectPerpetrateServiceImpl extends ServiceImpl<StProjectPerpetrateMapper, StProjectPerpetrate> implements IStProjectPerpetrateService {

    @Autowired
    private ISysDictService iSysDictService;
    @Override
    public <E extends IPage<StProjectPerpetrate>> E page(E page, Wrapper<StProjectPerpetrate> queryWrapper) {
        IPage<StProjectPerpetrate> pageList = super.page(page, queryWrapper);
        List<StProjectPerpetrate> list = pageList.getRecords();
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        for(StProjectPerpetrate entity:list) {
            DutyVo dutyVo = SpringContextUtils.getBean(IStCommonService.class).queryDutyByCode(entity.getUserNo());
            if(dutyVo!=null){
                entity.setUserName(dutyVo.getUname());
                entity.setDepName(dutyVo.getDepartName());
                entity.setPostName(dutyVo.getPostName());
            }else{
                entity.setUserName(dutyVo.getCode());
                entity.setDepName("-");
                entity.setPostName("-");
            }
        }
        return (E) pageList;
    }

    @Override
    public List<StProjectPerpetrate> list(Wrapper<StProjectPerpetrate> queryWrapper) {
        List<StProjectPerpetrate> list = super.list(queryWrapper);
        for(StProjectPerpetrate entity:list) {
            DutyVo dutyVo = SpringContextUtils.getBean(IStCommonService.class).queryDutyByCode(entity.getUserNo());
            if(dutyVo!=null){
                entity.setUserName(dutyVo.getUname());
                entity.setDepName(dutyVo.getDepartName());
                entity.setPostName(dutyVo.getPostName());
            }else{
                entity.setUserName(dutyVo.getCode());
                entity.setDepName("-");
                entity.setPostName("-");
            }
        }
        return list;
    }

    @Override
    public boolean save(StProjectPerpetrate entity) {
        QueryWrapper<StProjectPerpetrate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USER_NO",entity.getUserNo());
        queryWrapper.eq("PROJECT_CRE_ID",entity.getProjectCreId());
        long count = this.count(queryWrapper);
        if(count>0){
            throw new JeecgBootException("已经添加过该人员");
        }
        String name = iSysDictService.queryDictTextByKey("projectrole",entity.getProjectRole());
        if(StringUtils.hasText(name)){
            entity.setProjectRoleName(name);
        }else{
            entity.setProjectRoleName("-");
        }
        String userCode = UserUtil.getUserCode();
        entity.setCreatedUser(userCode);
        entity.setUpdatedUser(userCode);
        return super.save(entity);
    }

    @Override
    public void saveHead(StProjectCre entity) {
        StProjectPerpetrate stProjectPerpetrate = new StProjectPerpetrate();
        stProjectPerpetrate.setProjectCreId(entity.getId());
        stProjectPerpetrate.setUserNo(entity.getHeadCode());
        stProjectPerpetrate.setDepId(entity.getExecDepCode());
        stProjectPerpetrate.setProjectRole("head");
        stProjectPerpetrate.setProjectRoleName("负责人");
        this.save(stProjectPerpetrate);
    }

    @Override
    public void copy(ProjectImportVo projectImportVo) {
        QueryWrapper<StProjectPerpetrate> queryClearWrapper = new QueryWrapper<>();
        queryClearWrapper.eq("PROJECT_CRE_ID",projectImportVo.getId());
        this.remove(queryClearWrapper);
        QueryWrapper<StProjectPerpetrate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CRE_ID",projectImportVo.getCopyId());
        List<StProjectPerpetrate> list = this.list(queryWrapper);
        String userCode = UserUtil.getUserCode();
        if(!CollectionUtils.isEmpty(list)){
            for(StProjectPerpetrate entity:list){
                entity.setId(null);
                entity.setProjectCreId(projectImportVo.getId());
                entity.setCreatedUser(userCode);
                entity.setUpdatedUser(userCode);
                this.save(entity);
            }
        }
    }
}
