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.project.create.vo.ProjectImportVo;
import org.jeecg.modules.stm.project.create.vo.StProjectInterviewBatchVo;
import org.jeecg.modules.stm.project.create.vo.StProjectInterviewVo;
import org.jeecg.modules.stm.project.create.vo.XlsStProjectInterview;
import org.jeecg.modules.system.entity.SysCategory;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysPosition;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysPositionService;
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.util.CollectionUtils;
import org.springframework.web.servlet.ModelAndView;
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.StProjectInterview;
import org.jeecg.modules.stm.project.create.mapper.StProjectInterviewMapper;
import org.jeecg.modules.stm.project.create.service.IStProjectCreService;
import org.jeecg.modules.stm.project.create.service.IStProjectInterviewService;
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_interview
 * @Author: jeecg-boot
 * @Date:   2023-08-21
 * @Version: V1.0
 */
@Service
public class StProjectInterviewServiceImpl extends ServiceImpl<StProjectInterviewMapper, StProjectInterview> implements IStProjectInterviewService {

    @Autowired
    private ISysPositionService iSysPositionService;

    @Autowired
    private ISysDepartService iSysDepartService;

    @Autowired
    private ISysUserService iSysUserService;

    @Override
    public <E extends IPage<StProjectInterview>> E page(E page, Wrapper<StProjectInterview> queryWrapper) {
        IPage<StProjectInterview> pageList = super.page(page, queryWrapper);
        List<StProjectInterview> voList = new ArrayList<>();
        List<StProjectInterview> list = pageList.getRecords();
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        for(StProjectInterview entity:list) {
            StProjectInterviewVo vo = new StProjectInterviewVo();
            BeanUtils.copyProperties(entity, vo);
            SysUser sysUser = iSysUserService.getUserByName(entity.getUserNo());
            if(sysUser!=null) {
                vo.setUserName(sysUser.getRealname());
            }
            voList.add(vo);
        }
        pageList.setRecords(voList);
        return (E) pageList;
    }

    @Override
    public void saveInfo(StProjectCheck stProjectCheck) {
        String checkAsk = stProjectCheck.getCheckAsk();
        if(!StringUtils.isEmpty(checkAsk)) {
            String userCode = UserUtil.getUserCode();
            StProjectInterview stProjectInterview = new StProjectInterview();
            stProjectInterview.setProjectCreId(stProjectCheck.getProjectCreId());
            stProjectInterview.setProjectCheckId(stProjectCheck.getId());
            stProjectInterview.setPostId(checkAsk);
            SysPosition sysPosition = iSysPositionService.getByCode(checkAsk);
            if(sysPosition==null){
                stProjectInterview.setPostName("-");
            }else{
                stProjectInterview.setPostName(sysPosition.getName());
            }
            stProjectInterview.setCreatedUser(userCode);
            stProjectInterview.setUpdatedUser(userCode);
            stProjectInterview.setDutyStatus("0");
            this.save(stProjectInterview);
        }
    }

    @Override
    public void updateInfo(StProjectCheck stProjectCheck) {
        QueryWrapper<StProjectInterview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CHECK_ID",stProjectCheck.getId());
        queryWrapper.eq("PROJECT_CRE_ID",stProjectCheck.getProjectCreId());
        StProjectInterview stProjectInterview =  this.getOne(queryWrapper);
        if(stProjectInterview!=null){
            String userCode = UserUtil.getUserCode();
            StProjectInterview entity = new StProjectInterview();
            entity.setId(stProjectInterview.getId());
            entity.setUpdatedUser(userCode);
            String checkAsk = stProjectCheck.getCheckAsk();
            if(StringUtils.isNotEmpty(checkAsk)) {
                entity.setPostId(checkAsk);
                SysPosition sysPosition = iSysPositionService.getByCode(checkAsk);
                if (sysPosition == null) {
                    entity.setPostName("-");
                } else {
                    entity.setPostName(sysPosition.getName());
                }
                this.updateById(entity);
            }else{
                this.removeById(stProjectInterview.getId());
            }
        }else{
            saveInfo(stProjectCheck);
        }
    }

    @Override
    public void batchEditProjectPlan(StProjectInterviewBatchVo stProjectInterviewBatchVo) {
        String userCode = UserUtil.getUserCode();
        for(String id : stProjectInterviewBatchVo.getIds()) {
            StProjectInterview entity = new StProjectInterview();
            entity.setId(Integer.valueOf(id));
            entity.setUpdatedUser(userCode);
            entity.setInterviewDepId(stProjectInterviewBatchVo.getInterviewDepId());
            entity.setUserNo(stProjectInterviewBatchVo.getUserNo());
            entity.setInterviewContent(stProjectInterviewBatchVo.getInterviewContent());
            entity.setInterviewDate(stProjectInterviewBatchVo.getInterviewDate());
            entity.setDes(stProjectInterviewBatchVo.getDes());
            SysDepart sysDepart = iSysDepartService.getById(stProjectInterviewBatchVo.getInterviewDepId());
            if(sysDepart!=null){
                entity.setInterviewDepName(sysDepart.getDepartName());
            }else{
                entity.setInterviewDepName("-");
            }
            entity.setDutyStatus("1");
            this.updateById(entity);
        }
    }

    @Override
    public ModelAndView exportInterviewXls(HttpServletRequest request, StProjectInterview stProjectInterview) {
        String title = "项目访谈清单";
        // Step.1 组装查询条件
        QueryWrapper<StProjectInterview> queryWrapper = QueryGenerator.initQueryWrapper(stProjectInterview, request.getParameterMap());
        IStProjectCreService iStProjectCreService = SpringContextUtils.getBean(IStProjectCreService.class);
        StProjectCre stProjectCre = iStProjectCreService.getById(stProjectInterview.getProjectCreId());
        String projectName = "";
        if(stProjectCre!=null){
            projectName = stProjectCre.getProjectName();
        }
        // Step.2 获取导出数据
        List<StProjectInterview> list = this.list(queryWrapper);
        List<XlsStProjectInterview> exportList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)) {
            int index = 1;
            for (StProjectInterview entity : list) {
                XlsStProjectInterview xlsData = new XlsStProjectInterview();
                BeanUtils.copyProperties(entity,xlsData);
                xlsData.setNo(index);
                SysUser sysUser = iSysUserService.getUserByName(entity.getUserNo());
                if(sysUser!=null) {
                    xlsData.setUserName(sysUser.getRealname());
                }else{
                    xlsData.setUserName("-");
                }
                exportList.add(xlsData);
                index ++;
            }
        }
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, projectName+title);
        mv.addObject(NormalExcelConstants.CLASS, XlsStProjectInterview.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<StProjectInterview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PROJECT_CRE_ID",projectImportVo.getCopyId());
        queryWrapper.eq("PROJECT_CHECK_ID",oldProjectCheckId);
        List<StProjectInterview> list = this.list(queryWrapper);
        String userCode = UserUtil.getUserCode();
        if(!CollectionUtils.isEmpty(list)){
            for(StProjectInterview entity:list){
                entity.setId(null);
                entity.setProjectCreId(projectImportVo.getId());
                entity.setProjectCheckId(projectCheckId);
                entity.setCreatedUser(userCode);
                entity.setUpdatedUser(userCode);
                this.save(entity);
            }
        }
    }
}
