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.apache.commons.lang3.StringUtils;
import org.jeecg.common.system.query.QueryGenerator;
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.vo.ProjectImportVo;
import org.jeecg.modules.stm.project.create.vo.StProjectGatherBatchVo;
import org.jeecg.modules.stm.project.create.vo.StProjectGatherVo;
import org.jeecg.modules.stm.project.create.vo.XlsStProjectGather;
import org.jeecg.modules.system.entity.SysCategory;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysCategoryService;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.ModelAndView;
import org.jeecg.modules.stm.base.domain.entity.StDomain;
import org.jeecg.modules.stm.base.domain.service.IStDomainService;
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.StProjectGather;
import org.jeecg.modules.stm.project.create.mapper.StProjectGatherMapper;
import org.jeecg.modules.stm.project.create.service.IStProjectCreService;
import org.jeecg.modules.stm.project.create.service.IStProjectGatherService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.jeecg.modules.stm.utils.UserUtil;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: st_project_gather
 * @Author: jeecg-boot
 * @Date:   2023-08-21
 * @Version: V1.0
 */
@Service
public class StProjectGatherServiceImpl extends ServiceImpl<StProjectGatherMapper, StProjectGather> implements IStProjectGatherService {

    @Autowired
    private ISysCategoryService iSysCategoryService;
    @Autowired
    private IStDomainService iStDomainService;
    @Autowired
    private ISysDepartService iSysDepartService;

    @Override
    public <E extends IPage<StProjectGather>> E page(E page, Wrapper<StProjectGather> queryWrapper) {
        IPage<StProjectGather> pageList = super.page(page, queryWrapper);
        List<StProjectGather> voList = new ArrayList<>();
        List<StProjectGather> list = pageList.getRecords();
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        for(StProjectGather entity:list) {
            StProjectGatherVo vo = new StProjectGatherVo();
            BeanUtils.copyProperties(entity, vo);
            QueryWrapper<SysCategory> sysCategoryQueryWrapper = new QueryWrapper<>();
            sysCategoryQueryWrapper.eq("code",entity.getCheckDoc());
            SysCategory sysCategory = iSysCategoryService.getOne(sysCategoryQueryWrapper);
            if(sysCategory==null){
                vo.setCheckDocText("-");
            }else{
                vo.setCheckDocText(sysCategory.getName());
            }
            QueryWrapper<StDomain> domainQueryWrapper = new QueryWrapper<>();
            domainQueryWrapper.eq("DOMAIN_CODE",entity.getDomainCode());
            StDomain domain = iStDomainService.getOne(domainQueryWrapper);
            if(domain==null){
                vo.setDomainName("-");
            }else{
                vo.setDomainName(domain.getDomainName());
            }
            if(StringUtils.isNotEmpty(entity.getDutyCode())){
                DutyVo dutyVo = SpringContextUtils.getBean(IStCommonService.class).queryDutyByCode(entity.getDutyCode());
                if(dutyVo!=null){
                    vo.setDutyName(dutyVo.initDutyNames());
                }else{
                    vo.setDutyName("-");
                }
            }else{
                vo.setDutyName("-");
            }
            voList.add(vo);
        }
        pageList.setRecords(voList);
        return (E) pageList;
    }

    @Override
    public void saveInfo(StProjectCheck stProjectCheck,String domainCode) {
        if(StringUtils.isNotEmpty(stProjectCheck.getCheckReview())){
            String userCode = UserUtil.getUserCode();
            StProjectGather stProjectGather = new StProjectGather();
            stProjectGather.setProjectCreId(stProjectCheck.getProjectCreId());
            stProjectGather.setProjectCheckId(stProjectCheck.getId());
            stProjectGather.setCheckDoc(stProjectCheck.getCheckReview());
            stProjectGather.setDomainCode(domainCode);
            stProjectGather.setDutyStatus("0");
            stProjectGather.setCreatedUser(userCode);
            stProjectGather.setUpdatedUser(userCode);
            this.save(stProjectGather);
        }
    }

    @Override
    public void updateInfo(StProjectCheck stProjectCheck, String domainCode) {
        QueryWrapper<StProjectGather> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CHECK_ID",stProjectCheck.getId());
        queryWrapper.eq("PROJECT_CRE_ID",stProjectCheck.getProjectCreId());
        StProjectGather chekGater =  this.getOne(queryWrapper);
        if(chekGater!=null){
            if(StringUtils.isNotEmpty(stProjectCheck.getCheckReview())) {
                String userCode = UserUtil.getUserCode();
                StProjectGather stProjectGather = new StProjectGather();
                stProjectGather.setDomainCode(domainCode);
                stProjectGather.setCheckDoc(stProjectCheck.getCheckReview());
                stProjectGather.setId(chekGater.getId());
                stProjectGather.setUpdatedUser(userCode);
                this.updateById(stProjectGather);
            }else{
                this.removeById(chekGater.getId());
            }
        }else{
            saveInfo(stProjectCheck,domainCode);
        }
    }

    @Override
    public void batchEditProjectGatherCheck(StProjectGatherBatchVo stProjectGatherBatchVo) {
        String userCode = UserUtil.getUserCode();
        for(String id : stProjectGatherBatchVo.getIds()) {
            StProjectGather stProjectGather = new StProjectGather();
            stProjectGather.setId(Integer.valueOf(id));
            stProjectGather.setUpdatedUser(userCode);
            stProjectGather.setDutyCode(stProjectGatherBatchVo.getDutyCode());
            stProjectGather.setDutyStatus("1");
            SysDepart sysDepart = iSysDepartService.getById(stProjectGatherBatchVo.getDepId());
            if(sysDepart!=null){
                stProjectGather.setDepName(sysDepart.getDepartName());
            }else{
                stProjectGather.setDepName("-");
            }
            stProjectGather.setDes(stProjectGatherBatchVo.getDes());
            this.updateById(stProjectGather);
        }
    }

    @Override
    public ModelAndView exportGatherXls(HttpServletRequest request, StProjectGather stProjectGather) {
        String title = "项目资料清单";
        // Step.1 组装查询条件
        QueryWrapper<StProjectGather> queryWrapper = QueryGenerator.initQueryWrapper(stProjectGather, request.getParameterMap());
        IStProjectCreService iStProjectCreService = SpringContextUtils.getBean(IStProjectCreService.class);
        StProjectCre stProjectCre = iStProjectCreService.getById(stProjectGather.getProjectCreId());
        String projectName = "";
        if(stProjectCre!=null){
            projectName = stProjectCre.getProjectName();
        }
        // Step.2 获取导出数据
        List<StProjectGather> list = this.list(queryWrapper);
        List<XlsStProjectGather> exportList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)) {
            int index = 1;
            for (StProjectGather entity : list) {
                XlsStProjectGather xlsStProjectGather = new XlsStProjectGather();
                xlsStProjectGather.setNo(index);
                QueryWrapper<SysCategory> sysCategoryQueryWrapper = new QueryWrapper<>();
                sysCategoryQueryWrapper.eq("code",entity.getCheckDoc());
                SysCategory sysCategory = iSysCategoryService.getOne(sysCategoryQueryWrapper);
                if(sysCategory==null){
                    xlsStProjectGather.setCheckDoc("-");
                }else{
                    xlsStProjectGather.setCheckDoc(sysCategory.getName());
                }
                QueryWrapper<StDomain> domainQueryWrapper = new QueryWrapper<>();
                domainQueryWrapper.eq("DOMAIN_CODE",entity.getDomainCode());
                StDomain domain = iStDomainService.getOne(domainQueryWrapper);
                if(domain==null){
                    xlsStProjectGather.setDomainName("-");
                }else{
                    xlsStProjectGather.setDomainName(domain.getDomainName());
                }
                if(StringUtils.isNotEmpty(entity.getDutyCode())){
                    DutyVo dutyVo = SpringContextUtils.getBean(IStCommonService.class).queryDutyByCode(entity.getDutyCode());
                    if(dutyVo!=null){
                        xlsStProjectGather.setDutyName(dutyVo.initDutyNames());
                    }else{
                        xlsStProjectGather.setDutyName("-");
                    }
                }else{
                    xlsStProjectGather.setDutyName("-");
                }
                String caName = this.baseMapper.queryCaNameByCheckId(entity.getProjectCheckId());
                if(StringUtils.isNotEmpty(caName)) {
                    xlsStProjectGather.setCaName(caName);
                }else{
                    xlsStProjectGather.setCaName("-");
                }
                xlsStProjectGather.setDepName(entity.getDepName());
                xlsStProjectGather.setDes(entity.getDes());
                exportList.add(xlsStProjectGather);
                index ++;
            }
        }
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, projectName+title);
        mv.addObject(NormalExcelConstants.CLASS, XlsStProjectGather.class);
        //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
        ExportParams exportParams=new ExportParams(projectName+title , "导出人:" + UserUtil.getUserCode(), title);
        //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS,exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }

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