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

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

import jakarta.servlet.http.HttpServletRequest;

import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
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.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.ModelAndView;
import org.jeecg.modules.stm.compliane.info.entity.StComplianeInfo;
import org.jeecg.modules.stm.compliane.info.service.IStComplianeInfoService;
import org.jeecg.modules.stm.compliane.item.entity.StComplianeItem;
import org.jeecg.modules.stm.compliane.item.service.IStComplianeItemService;
import org.jeecg.modules.stm.project.create.entity.StProjectCre;
import org.jeecg.modules.stm.project.create.service.IStProjectCheckService;
import org.jeecg.modules.stm.project.create.service.IStProjectCreService;
import org.jeecg.modules.stm.project.exec.entity.StProjectExec;
import org.jeecg.modules.stm.project.exec.mapper.StProjectExecMapper;
import org.jeecg.modules.stm.project.exec.service.IStProjectExecService;
import org.jeecg.modules.stm.project.exec.vo.StProjectExecExVo;
import org.jeecg.modules.stm.project.exec.vo.StProjectExecVo;
import org.jeecg.modules.stm.project.exec.vo.XlsStProjectExecProVo;
import org.jeecg.modules.stm.project.execpro.entity.StProjectExecPro;
import org.jeecg.modules.stm.project.execpro.service.IStProjectExecProService;
import org.jeecg.modules.stm.project.execrisk.entity.StProjectExecRisk;
import org.jeecg.modules.stm.project.execrisk.service.IStProjectExecRiskService;
import org.jeecg.modules.stm.utils.UserUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 项目实施
 * @Author: jeecg-boot
 * @Date:   2023-08-17
 * @Version: V1.0
 */
@Service
public class StProjectExecServiceImpl extends ServiceImpl<StProjectExecMapper, StProjectExec> implements IStProjectExecService {
	@Autowired
	IStProjectExecProService stProjectExecProService;
	@Autowired
	IStProjectExecRiskService stProjectExecRiskService;
	@Autowired
	ISysUserService iSysUserService;
	@Autowired
	IStProjectCreService iStProjectCreService;
	@Autowired
	IStProjectCheckService iStProjectCheckService;
	@Autowired
	IStComplianeInfoService iStComplianeInfoService;
	@Autowired
	IStComplianeItemService iStComplianeItemService;
	public IPage<StProjectExecVo> queryStProjectExecVoByPage(IPage<StProjectExecVo> page,
			StProjectExecVo stProjectExecVo) {
		// TODO Auto-generated method stub
		IPage<StProjectExecVo> stProjectExecVoPage=baseMapper.queryStProjectExecVoByPage(page,stProjectExecVo);
		List<StProjectExecVo> stProjectExecVoList=stProjectExecVoPage.getRecords();
		for(StProjectExecVo tempVo:stProjectExecVoList){
			if(!tempVo.getExecLevel().equals("实施完成")){
				String str=tempVo.getExecLevel()+"["+tempVo.getOverCount()+"/"+tempVo.getSumCount()+"]";
				tempVo.setExecLevel(str);				
			}
		}
		return stProjectExecVoPage;
	}
	@Override
	public IPage<StProjectExecExVo> queryStProjectExecExVoByPage(IPage<StProjectExecExVo> page, StProjectExecExVo stProjectExecExVo) {
		// TODO Auto-generated method stub
		IPage<StProjectExecExVo> stProjectExecExVoPage=baseMapper.queryStProjectExecExVoByPage(page,stProjectExecExVo);
		List<StProjectExecExVo> stProjectExecExVoList=stProjectExecExVoPage.getRecords();
		for(StProjectExecExVo tempVo:stProjectExecExVoList){
			if("0003".equals(tempVo.getExecLevelCode())&&stProjectExecExVo.getConfirmUser().equals(tempVo.getConfirmUser())){
				tempVo.setHandleAdviceDis("1");
			}else{
				tempVo.setHandleAdviceDis("0");
			}
			if(tempVo.getId()==null){
				StProjectExec stProjectExec=new StProjectExec();
				stProjectExec.setProjectCheckId(tempVo.getProjectCheckId());
				baseMapper.insert(stProjectExec);
				tempVo.setId(stProjectExec.getId());
			}
			List<StProjectExecPro> stProjectExecPros=new ArrayList<StProjectExecPro>();
			stProjectExecPros=stProjectExecProService.queryStProjectExecProByPage(stProjectExecPros, tempVo);
			tempVo.setStProjectExecPros(stProjectExecPros);
			List<StProjectExecRisk> stProjectExecRisks=new ArrayList<StProjectExecRisk>();
			stProjectExecRisks=stProjectExecRiskService.queryStProjectExecRiskByPage(stProjectExecRisks, tempVo);
			tempVo.setStProjectExecRisks(stProjectExecRisks);
			QueryWrapper<StComplianeItem> queryWrapperItem = new QueryWrapper<>();
			queryWrapperItem.eq("CA_NO", tempVo.getCaNo());
			IPage<StComplianeItem> iPageItem=new Page<StComplianeItem>();
			Page<StComplianeItem> pageItem=new Page<StComplianeItem>();
			List<StComplianeInfo> ListInfo=new ArrayList<StComplianeInfo>();
			iPageItem=iStComplianeItemService.caPage(pageItem, queryWrapperItem);
			for(StComplianeItem stComplianeItem:iPageItem.getRecords()){
				QueryWrapper<StComplianeInfo> queryWrapperInfo = new QueryWrapper<>();
				queryWrapperInfo.eq("COMPLIANE_NO", stComplianeItem.getComplianeNo());
				StComplianeInfo stComplianeInfo=iStComplianeInfoService.getOne(queryWrapperInfo);
				ListInfo.add(stComplianeInfo);
			}
			tempVo.setStComplianeInfos(ListInfo);
			
		}
		
		return stProjectExecExVoPage;
	}
	@Override
	public void submitExec(StProjectExecVo stProjectExecVo) {
		// TODO Auto-generated method stub
		baseMapper.submitExec(stProjectExecVo);
	}
	@Override
	public IPage<StProjectExecExVo> queryStProjectExecPro(IPage<StProjectExecExVo> page,
			StProjectExecExVo stProjectExecExVo) {
		// TODO Auto-generated method stub
		IPage<StProjectExecExVo> stProjectExecExVoPage=baseMapper.queryStProjectExecPro(page,stProjectExecExVo);
		List<StProjectExecExVo> stProjectExecExVoList=stProjectExecExVoPage.getRecords();
		for(StProjectExecExVo tempVo:stProjectExecExVoList){
			QueryWrapper<StComplianeItem> queryWrapperItem = new QueryWrapper<>();
			queryWrapperItem.eq("CA_NO", tempVo.getCaNo());
			IPage<StComplianeItem> iPageItem=new Page<StComplianeItem>();
			Page<StComplianeItem> pageItem=new Page<StComplianeItem>();
			List<StComplianeInfo> ListInfo=new ArrayList<StComplianeInfo>();
			iPageItem=iStComplianeItemService.caPage(pageItem, queryWrapperItem);
			for(StComplianeItem stComplianeItem:iPageItem.getRecords()){
				QueryWrapper<StComplianeInfo> queryWrapperInfo = new QueryWrapper<>();
				queryWrapperInfo.eq("COMPLIANE_NO", stComplianeItem.getComplianeNo());
				StComplianeInfo stComplianeInfo=iStComplianeInfoService.getOne(queryWrapperInfo);
				ListInfo.add(stComplianeInfo);
			}
			tempVo.setStComplianeInfos(ListInfo);
			
		}
		
		return stProjectExecExVoPage;
	}
	private List<StProjectExecExVo> queryStProjectExecProList(List<StProjectExecExVo> List,
			StProjectExecExVo stProjectExecExVo) {
		// TODO Auto-generated method stub
		return baseMapper.queryStProjectExecProList(List,stProjectExecExVo);
	}
	
    @Override
    public ModelAndView exportInterviewXls(HttpServletRequest request,StProjectExecExVo stProjectExecExVo) {
        String title = "项目问题清单";
        // Step.1 组装查询条件
        StProjectCre stProjectCre = iStProjectCreService.getById(stProjectExecExVo.getProjectCreId());
        String projectName = "";
        projectName = stProjectCre.getProjectName();
        projectName=projectName+title;
        // Step.2 获取导出数据
        List<StProjectExecExVo> listm=new ArrayList<StProjectExecExVo>();
        List<StProjectExecExVo> list = this.queryStProjectExecProList(listm,stProjectExecExVo);
        List<XlsStProjectExecProVo> exportList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)) {
            int index = 1;
            for (StProjectExecExVo entity : list) {
            	XlsStProjectExecProVo xlsData = new XlsStProjectExecProVo();
                BeanUtils.copyProperties(entity,xlsData);
                xlsData.setNo(index);
                exportList.add(xlsData);
                index ++;
            }
        }
        SysUser sysUser = iSysUserService.getUserByName( UserUtil.getUserCode());
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, projectName);
        mv.addObject(NormalExcelConstants.CLASS, XlsStProjectExecProVo.class);
        //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
        ExportParams exportParams=new ExportParams(projectName , "导出人:" +sysUser.getRealname(), title);
        //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS,exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }


}
