package com.jeeplus.finance.projectReportArchive.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.finance.projectRecords.domain.CwProjectRecords;
import com.jeeplus.finance.projectRecords.mapper.CwProjectRecordsMapper;
import com.jeeplus.finance.projectReport.mapper.CwProjectInfoMapper;
import com.jeeplus.finance.projectReportArchive.domain.CwProjectReportArchive;
import com.jeeplus.finance.projectReportArchive.domain.CwProjectReportArchiveFile;
import com.jeeplus.finance.projectReportArchive.mapper.CwProjectReportArchiveFileMapper;
import com.jeeplus.finance.projectReportArchive.mapper.CwProjectReportArchiveMapper;
import com.jeeplus.finance.projectReportArchive.mapper.CwProjectReportArchiveReportMapper;
import com.jeeplus.finance.projectReportArchive.service.dto.CwProjectReportArchiveDTO;
import com.jeeplus.finance.projectReportArchive.service.dto.ReportDTO;
import com.jeeplus.finance.projectReportArchive.service.dto.WorkAttachmentArchiveDto;
import com.jeeplus.finance.projectReportArchive.service.mapstruct.CwProjectReportArchiveWrapper;
import com.jeeplus.flowable.feign.IFlowableApi;
import com.jeeplus.sys.domain.WorkAttachmentInfo;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.feign.IWorkAttachmentApi;
import com.jeeplus.sys.service.dto.UserDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
@Transactional
public class CwProjectReportArchiveService extends ServiceImpl<CwProjectReportArchiveMapper, CwProjectReportArchive> {

    @Resource
    private CwProjectReportArchiveMapper cwProjectReportArchiveMapper;
    @Resource
    private CwProjectReportArchiveReportMapper cwProjectReportArchiveReportMapper;
    @Resource
    private CwProjectReportArchiveFileMapper cwProjectReportArchiveFileMapper;
    @Resource
    private CwProjectRecordsMapper cwProjectRecordsMapper;
//    @Resource
//    private OssService ossService;
//    @Resource
//    private OssServiceMapper ossServiceMapper;
//    @Resource
//    private UserMapper userMapper;
    @Resource
    private CwProjectInfoMapper cwProjectInfoMapper;
//    @Resource
//    private SerialnumTplService serialnumTplService;
    @Resource
    private IFlowableApi flowTaskService;

    /**
     * 项目归档列表信息
     * @param page
     * @param cwProjectReportArchiveDTO
     * @return
     * @throws Exception
     */
    public IPage<CwProjectReportArchiveDTO> findList(Page<CwProjectReportArchiveDTO> page, CwProjectReportArchiveDTO cwProjectReportArchiveDTO) throws Exception{
        QueryWrapper<CwProjectReportArchive> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( CwProjectReportArchiveWrapper.INSTANCE.toEntity(cwProjectReportArchiveDTO), CwProjectReportArchive.class );
        queryWrapper.eq("cw_pa.del_flag","0");
        if (ObjectUtil.isNotEmpty(cwProjectReportArchiveDTO)) {
            if (ObjectUtil.isNotEmpty(cwProjectReportArchiveDTO.getCwProjectRecordsDTO())){
                // 项目名称
                if (StringUtils.isNotBlank(cwProjectReportArchiveDTO.getCwProjectRecordsDTO().getProjectName())){
                    List<CwProjectRecords> cwProjectRecords = cwProjectRecordsMapper.selectList(new QueryWrapper<CwProjectRecords>().lambda().like(CwProjectRecords::getProjectName, cwProjectReportArchiveDTO.getCwProjectRecordsDTO().getProjectName()));
                    List<String> ids = cwProjectRecords.stream().map(CwProjectRecords::getId).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
                    if(CollectionUtil.isNotEmpty(ids)) {
                        queryWrapper.in("cw_pa.project_id",ids);
                    }else{
                        return new Page<>();
                    }
                }
                // 项目经理1
                if (StringUtils.isNotBlank(cwProjectReportArchiveDTO.getCwProjectRecordsDTO().getProjectMasterName())){
//                    List<User> userList = SpringUtil.getBean ( IUserApi.class ).selectListByName(cwProjectReportArchiveDTO.getCwProjectRecordsDTO().getProjectMasterName());
//                    if (CollectionUtils.isNotEmpty(userList)){
//                        List<String> userIdList = userList.stream().map(User::getId).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
//                        if (CollectionUtil.isNotEmpty(userIdList)){
//                            List<CwProjectRecords> cwProjectRecords = cwProjectRecordsMapper.selectList(new QueryWrapper<CwProjectRecords>().lambda().in(CwProjectRecords::getProjectMasterId, userIdList));
//                            List<String> projectIds = cwProjectRecords.stream().map(CwProjectRecords::getId).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
//                            if (CollectionUtil.isNotEmpty(projectIds)){
//                                queryWrapper.in("cw_pa.project_id",projectIds);
//                            }else{
//                                return new Page<>();
//                            }
//                        }else{
//                            return new Page<>();
//                        }
//                    } else {
//                        List<CwProjectRecords> cwProjectRecords = cwProjectRecordsMapper.selectList(new QueryWrapper<CwProjectRecords>().lambda().eq(CwProjectRecords::getProjectMasterId, cwProjectReportArchiveDTO.getCwProjectRecordsDTO().getProjectMasterName()));
//                        List<String> projectIds = cwProjectRecords.stream().map(CwProjectRecords::getId).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
//                        if (CollectionUtil.isNotEmpty(projectIds)){
//                            queryWrapper.in("cw_pa.project_id",projectIds);
//                        }else{
//                            return new Page<>();
//                        }
//                    }
                    queryWrapper.like("su1.name",cwProjectReportArchiveDTO.getCwProjectRecordsDTO().getProjectMasterName());

                }
                // 项目经理2
                if (StringUtils.isNotBlank(cwProjectReportArchiveDTO.getCwProjectRecordsDTO().getRealHeaderName())){
//                    List<User> userList = SpringUtil.getBean ( IUserApi.class ).selectListByName(cwProjectReportArchiveDTO.getCwProjectRecordsDTO().getRealHeaderName());
//                    if (CollectionUtils.isNotEmpty(userList)){
//                        List<String> userIdList = userList.stream().map(User::getId).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
//                        if (CollectionUtil.isNotEmpty(userIdList)){
//                            List<CwProjectRecords> cwProjectRecords = cwProjectRecordsMapper.selectList(new QueryWrapper<CwProjectRecords>().lambda().in(CwProjectRecords::getRealHeader, userIdList));
//                            List<String> projectIds = cwProjectRecords.stream().map(CwProjectRecords::getId).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
//                            if (CollectionUtil.isNotEmpty(projectIds)){
//                                queryWrapper.in("cw_pa.project_id",projectIds);
//                            }else{
//                                return new Page<>();
//                            }
//                        }else{
//                            return new Page<>();
//                        }
//                    } else {
//                        List<CwProjectRecords> cwProjectRecords = cwProjectRecordsMapper.selectList(new QueryWrapper<CwProjectRecords>().lambda().in(CwProjectRecords::getRealHeader, cwProjectReportArchiveDTO.getCwProjectRecordsDTO().getRealHeaderName()));
//                        List<String> projectIds = cwProjectRecords.stream().map(CwProjectRecords::getId).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
//                        if (CollectionUtil.isNotEmpty(projectIds)){
//                            queryWrapper.in("cw_pa.project_id",projectIds);
//                        }else{
//                            return new Page<>();
//                        }
//                    }
                    queryWrapper.like("su2.name",cwProjectReportArchiveDTO.getCwProjectRecordsDTO().getRealHeaderName());

                }
                // 创建时间
                if (ArrayUtil.isNotEmpty(cwProjectReportArchiveDTO.getCreateDates())){
                    queryWrapper.between("cw_pa.create_time", cwProjectReportArchiveDTO.getCreateDates()[0], cwProjectReportArchiveDTO.getCreateDates()[1]);
                }
                // 归档时间
                if (ArrayUtil.isNotEmpty(cwProjectReportArchiveDTO.getAuditDates())){
                    queryWrapper.between("cw_pa.audit_date", cwProjectReportArchiveDTO.getAuditDates()[0], cwProjectReportArchiveDTO.getAuditDates()[1]);
                }
                // 创建人
                if (ObjectUtil.isNotEmpty(cwProjectReportArchiveDTO.getCreateBy())){
                    if (StringUtils.isNotBlank(cwProjectReportArchiveDTO.getCreateBy().getName())){
//                        List<User> userList = SpringUtil.getBean ( IUserApi.class ).selectListByName(cwProjectReportArchiveDTO.getCreateBy().getName());
//                        List<String> ids = userList.stream().map(User::getId).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
//                        if (CollectionUtil.isNotEmpty(ids)){
//                            queryWrapper.in("cw_pa.create_by_id",ids);
//                        }else{
//                            return new Page<>();
//                        }
                        queryWrapper.in("cw_pa.create_by_id",cwProjectReportArchiveDTO.getCreateBy().getName()).or().like("su.name",cwProjectReportArchiveDTO.getCreateBy().getName());
                    }
                }
                // 报告文号
                if (StringUtils.isNotBlank(cwProjectReportArchiveDTO.getReportNo())) {
                    queryWrapper.like("cw_prnl.report_no",cwProjectReportArchiveDTO.getReportNo());
                }

            }
        }
        String isBmzr = "0";
        StringBuilder officeIds = new StringBuilder();
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        List<String> manageOfficeIdList= Lists.newArrayList();

        if (StringUtils.isBlank(cwProjectReportArchiveDTO.getArchiveSta())) {
            cwProjectReportArchiveDTO.setArchiveSta("");
        }

        /*//获取当前登录人所属角色
        //若是评估部门主任，则仅展示部门主任信息
        //若是评估员工，则仅展示自己的数据信息
        List<RoleDTO> roleDTOList = userDTO.getRoleDTOList();
        for (RoleDTO roleDTO : roleDTOList) {
            //根据角色id查询角色信息
            RoleDTO roleDTOById = SpringUtil.getBean(IRoleApi.class).getRoleDTOById(roleDTO.getId());
            if("cwyg".equals(roleDTOById.getEnName()) && "兴光会计员工".equals(roleDTOById.getName())){
                queryWrapper.eq("cpm.user_id", userDTO.getId());
                break;
            }else if("cw_bmzr".equals(roleDTOById.getEnName()) && "兴光会计部门主任".equals(roleDTOById.getName())){
                queryWrapper.eq("sumo.user_id", userDTO.getOfficeDTO().getId());
                break;
            }else if("szfbdagly".equals(roleDTOById.getEnName()) && "苏州分部档案管理员".equals(roleDTOById.getName())){
                queryWrapper.eq("so.code", "szfb");
                break;
            }else if("cwhtgdr".equals(roleDTOById.getEnName()) && "兴光会计报告归档管理员".equals(roleDTOById.getName())){
                queryWrapper.ne("so.code", "szfb");
                break;
            }
        }*/


        IPage<CwProjectReportArchiveDTO> list = cwProjectReportArchiveMapper.findList(page, userDTO, isBmzr, officeIds.toString(), queryWrapper, cwProjectReportArchiveDTO.getArchiveSta());
        List<CwProjectReportArchiveDTO> records = list.getRecords();
        if (null != records && records.size()>0){
            //循环查询出来的数据
            for (CwProjectReportArchiveDTO info : records) {
                //如果归档日期为空，则表示该报告还未归档
                if(null == info.getAuditDate()){
                    //如果归档日期差大于60天，表示超期未归档
                    if(info.getArchiveDay()>60){
                        // 超期天数
                        Integer overdueDay = info.getArchiveDay() - 60;
                        info.setArchiveSta("超期未归档");
                        long n = (long) Math.ceil((double)(overdueDay) / 30);
                        String money = Long.toString(n * 200);
                        info.setOverdueDay(String.valueOf(overdueDay));
                        info.setFineMoney(money);
                    }else{
                        //如果归档日期差小于等于60天，表示暂时未归档
                        info.setArchiveSta("暂时未归档");
                        info.setOverdueDay(String.valueOf(info.getArchiveDay() - 60));
                        info.setFineMoney("0");
                    }
                } else {
                    //如果归档日期不为空，则表示该报告已归档

                    //如果归档日期差大于60天，表示已超期归档
                    if(info.getArchiveDay()>60){
                        // 超期天数
                        Integer overdueDay = info.getArchiveDay() - 60;

                        info.setArchiveSta("已超期归档");
                        long n = (long) Math.ceil((double)(overdueDay) / 30);
                        String money = Long.toString(n * 200);
                        info.setOverdueDay(String.valueOf(overdueDay));
                        info.setFineMoney(money);
                    }else{
                        //如果归档日期差小于等于60天，表示已按时归档
                        info.setArchiveSta("已按时归档");
                        info.setOverdueDay(String.valueOf(info.getArchiveDay() - 60));
                        info.setFineMoney("0");
                    }
                }
                // 报告归档
                if (StringUtils.isNotBlank(info.getTaskId()) && StringUtils.isNotBlank(info.getStatus())) {
                    if ("2".equals(info.getStatus())) { // “审核中”的数据要获取数据审核人
                        info.setAuditUserIds(flowTaskService.getTaskAuditUsers(info.getTaskId()));  // 获取数据审核人
                    }
                }
            }
        }
        return list;
    }

    /**
     * 查询审计报告信息列表
     * @param page
     * @param reportDTO
     * @return
     * @throws Exception
     */
    public IPage<ReportDTO> findReportList(Page<ReportDTO> page, ReportDTO reportDTO) throws Exception{
        String repId = "";
        if (ObjectUtil.isNotEmpty(reportDTO)) {
            if (StringUtils.isNotBlank(reportDTO.getAchiveId())) {
                CwProjectReportArchive byId = this.getById(reportDTO.getAchiveId());
                if (ObjectUtil.isNotEmpty(byId)) {
                    if (StringUtils.isNotBlank(byId.getReportId())) {
                        repId = byId.getReportId();
                    }
                }
            }
        }
        String finalRepId = repId;
        List<String> reportIds = this.list().stream().filter(item -> {
            if (StringUtils.isNotBlank(item.getReportId())) {
                if (StringUtils.isNotBlank(finalRepId)) {
                    if (item.getReportId().equals(finalRepId)) {
                        return false;
                    }
                }
                return true;
            }
            return false;
        }).map(CwProjectReportArchive::getReportId).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
        QueryWrapper<ReportDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cw_pr.del_flag","0");
        queryWrapper.orderByDesc("cw_pr.create_time");
        queryWrapper.eq("cw_pr.status", "5");
        if (CollectionUtil.isNotEmpty(reportIds)) {
            queryWrapper.notIn("cw_pr.id",reportIds);
        }
        if (ObjectUtil.isNotEmpty(reportDTO)) {
            if (StringUtils.isNotBlank(reportDTO.getProjectId())) {
                queryWrapper.eq("cw_pr.project_id",reportDTO.getProjectId());
            }
            if (StringUtils.isNotBlank(reportDTO.getReportNo())) {
                queryWrapper.like("cw_prnl.report_no",reportDTO.getReportNo());
            }
        }
        return cwProjectReportArchiveMapper.findReportList(page, queryWrapper);
    }

    /**
     * 查询报告是否已归档
     * @param reportId
     * @return
     */
    public boolean findReportUse(String reportId) {
        LambdaQueryWrapper<CwProjectReportArchive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CwProjectReportArchive::getReportId,reportId);
        queryWrapper.eq(CwProjectReportArchive::getDelFlag,"0");
        List<CwProjectReportArchive> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return false;
        }
        return true;
    }

    /**
     * 根据id查询项目详情
     * @param id
     * @return
     */
    public CwProjectReportArchiveDTO queryById(String id) {
        CwProjectReportArchiveDTO d = cwProjectReportArchiveMapper.queryById(id);
        if (null != d){
            List<WorkAttachmentArchiveDto> fileList = Lists.newArrayList();
            // 查询附件信息
            List<WorkAttachmentArchiveDto> dtos = cwProjectReportArchiveMapper.findDtos(id);
            if (CollectionUtils.isNotEmpty(dtos)) {
                for (WorkAttachmentArchiveDto i : dtos) {
                    i.setCreateBy(SpringUtil.getBean ( IUserApi.class ).getById(i.getBy()));
                }
            }
            fileList.addAll(dtos);
            d.setWorkAttachmentDtoList(fileList);
        }
        return d;
    }

    /**
     * 保存项目归档信息以及其他相关信息
     * @param cwProjectReportArchiveDTO
     * @return
     * @throws Exception
     */
    public String saveForm(CwProjectReportArchiveDTO cwProjectReportArchiveDTO) throws Exception{
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        CwProjectReportArchive cwProjectReportArchive = CwProjectReportArchiveWrapper.INSTANCE.toEntity(cwProjectReportArchiveDTO);
        if (ObjectUtil.isNotEmpty(cwProjectReportArchive)) {
            if (StringUtils.isBlank(cwProjectReportArchive.getCreateById())) {
                cwProjectReportArchive.setCreateById(userDTO.getId());
            }
            if (StringUtils.isBlank(cwProjectReportArchive.getId())){
                String fileNumber = SpringUtil.getBean ( IWorkAttachmentApi.class ).genSerialNum(userDTO.getCompanyDTO().getId(), CwProjectReportArchiveDTO.BIZ_CODE, TokenProvider.getCurrentToken());
//                String fileNumber = serialnumTplService.genSerialNum(userDTO.getCompanyDTO().getId(), CwProjectReportArchiveDTO.BIZ_CODE, TokenProvider.getCurrentToken());
                cwProjectReportArchive.setFileNumber(fileNumber);
            }
            cwProjectReportArchive.setAuditMoney(cwProjectReportArchiveDTO.getAuditMoney());
        }
        if(StringUtils.isNotBlank(cwProjectReportArchiveDTO.getConnectReportId())){
            cwProjectReportArchive.setConnectReportId(cwProjectReportArchiveDTO.getConnectReportId());
        }
        if(StringUtils.isNotBlank(cwProjectReportArchive.getId())){ //修改
            cwProjectReportArchiveMapper.updateInfoById(cwProjectReportArchive);
        }else{  //新增
            cwProjectReportArchiveMapper.insert(cwProjectReportArchive);
        }
//        this.saveOrUpdate(cwProjectReportArchive);
        if (ObjectUtil.isNotEmpty(cwProjectReportArchiveDTO)) {
            // 附件
//            ossServiceMapper.delete(new QueryWrapper<WorkAttachment>().lambda().eq(WorkAttachment::getAttachmentId, cwProjectReportArchive.getId()));
            SpringUtil.getBean ( IWorkAttachmentApi.class ).deleteByAttachmentId(cwProjectReportArchive.getId());
            AtomicInteger sort = new AtomicInteger(1);
            cwProjectReportArchiveDTO.getWorkAttachmentDtoList().stream().forEach(item->{
                WorkAttachmentInfo workAttachmentDto = new WorkAttachmentInfo();
                workAttachmentDto.setName(item.getName());
                workAttachmentDto.setSize(item.getSize());
                workAttachmentDto.setUrl(item.getUrl());
//                String fileId = ossService.saveFile(workAttachmentDto,userDTO, cwProjectReportArchive.getId(),"cw_project_report_archive",sort.get());

                Map<String,String> map = new HashMap<>();
                String workAttachmentDtoInfo = JSON.toJSONString(workAttachmentDto);
                String userDTOInfo = JSON.toJSONString(userDTO);
                String attachmentId = cwProjectReportArchive.getId();
                String attachmentFlag = "cw_project_report_archive";
                String sortInfo = Integer.toString(sort.get());
                map.put("workAttachmentDtoInfo",workAttachmentDtoInfo);
                map.put("userDTOInfo",userDTOInfo);
                map.put("attachmentId",attachmentId);
                map.put("attachmentFlag",attachmentFlag);
                map.put("sortInfo",sortInfo);
                String fileId = SpringUtil.getBean ( IWorkAttachmentApi.class ).saveFile(map);

                sort.getAndIncrement();
                CwProjectReportArchiveFile cwProjectReportArchiveFile = new CwProjectReportArchiveFile();
                cwProjectReportArchiveFile.setWorkAttachmentId(fileId);
                cwProjectReportArchiveFile.setRemarks(item.getRemarks());
                cwProjectReportArchiveFile.setFileType(item.getFileType());
                cwProjectReportArchiveFileMapper.insert(cwProjectReportArchiveFile);
            });
        }
        return cwProjectReportArchive.getId();
    }

    /**
     * 保存项目归档信息
     * @param cwProjectReportArchiveDTO
     * @return
     * @throws Exception
     */
    public ResponseEntity save(CwProjectReportArchiveDTO cwProjectReportArchiveDTO) throws Exception{
        this.saveForm(cwProjectReportArchiveDTO);
        return ResponseEntity.ok("保存成功");
    }

    /**
     * 根据id删除项目归档信息
     * @param ids
     * @return
     */
    public ResponseEntity deleteByIds(String ids) {
        String idArray[] =ids.split(",");
        this.removeByIds (Lists.newArrayList (idArray));
        return ResponseEntity.ok ("删除成功");
    }

    /**
     * 根据单个id删除项目归档信息
     * @param id
     * @return
     */
    public ResponseEntity deleteById(String id) {
        this.removeById(id);
        return ResponseEntity.ok ("删除成功");
    }

    /**
     * 修改状态
     * @param cwProjectReportArchiveDTO
     * @return
     */
    public String updateStatusById(CwProjectReportArchiveDTO cwProjectReportArchiveDTO) {
        CwProjectReportArchive cwProjectReportArchive = CwProjectReportArchiveWrapper.INSTANCE.toEntity(cwProjectReportArchiveDTO);
        this.update(cwProjectReportArchive, new QueryWrapper<CwProjectReportArchive>().lambda().eq(CwProjectReportArchive::getId, cwProjectReportArchive.getId()));
        return "操作成功";
    }

    /**
     * 查询暂时未归档报告归档数据
     * @param cwProjectReportArchiveDTO
     * @return
     * @throws Exception
     */
    public void getNotFiledYetList(CwProjectReportArchiveDTO cwProjectReportArchiveDTO) {
        try {
            QueryWrapper<CwProjectReportArchive> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( CwProjectReportArchiveWrapper.INSTANCE.toEntity(cwProjectReportArchiveDTO), CwProjectReportArchive.class );
            queryWrapper.eq("cw_pa.del_flag","0");

            List<CwProjectReportArchiveDTO> list = cwProjectReportArchiveMapper.getNotFiledYetList(queryWrapper);
            //循环查询出来的数据
            for (CwProjectReportArchiveDTO info : list) {
                //如果归档日期为空，则表示该报告还未归档
                if(null == info.getAuditDate()){
                    //如果归档日期差大于60天，表示超期未归档
                    if(info.getArchiveDay()<=60){
                        int restultDay = 60 - info.getArchiveDay();

                        switch (restultDay){
                            case 7:
                            case 14:
                                //获取报告创建人信息（根据id）
                                UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getById(info.getCreateBy().getId());

                                String titleName =userDTO.getName() +  " 的报告文号 " + info.getReportNo() + " 还有" + restultDay + "天归档超期，请及时归档";
                                String actName = "报告归档超期提醒";
                                Map<String,String> map = new HashMap<String,String>();
                                map.put("taskId",info.getCprrProcInsId());
                                map.put("title",titleName);
                                map.put("defId",info.getCprrProcDefId());
                                map.put("taskName",actName);
                                map.put("createUser","管理员");

                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                String nowStr = format.format(new Date());
                                map.put("createTime",nowStr);
                                map.put("noticeName",userDTO.getName());
                                map.put("noticeId",userDTO.getId());
                                map.put("createById",userDTO.getId());
                                SpringUtil.getBean(IFlowableApi.class).add(map);
                                break;
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 根据归档id查询该报告历史归档信息集合
     * @param collectId
     * @return
     */
    public List<CwProjectReportArchiveDTO> findHiById(String collectId) {
        List<CwProjectReportArchiveDTO> hiById = cwProjectReportArchiveMapper.findHiById(collectId);
        return hiById;
    }


    /**
     * 查询最后一条归档并作废的归档信息
     * @param collectId
     * @return
     */
    public CwProjectReportArchiveDTO getLastAuditDateByReportId(String collectId) {
        CwProjectReportArchiveDTO hiById = cwProjectReportArchiveMapper.getLastAuditDateByReportId(collectId);
        return hiById;
    }

    /**
     * 查询财务项目报告归档信息列表
     * @param page
     * @param cwProjectReportArchiveDTO
     * @return
     * @throws Exception
     */
    public IPage<CwProjectReportArchiveDTO> findCompletedList(Page<CwProjectReportArchiveDTO> page, CwProjectReportArchiveDTO cwProjectReportArchiveDTO) throws Exception{
        QueryWrapper<CwProjectReportArchive> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( CwProjectReportArchiveWrapper.INSTANCE.toEntity(cwProjectReportArchiveDTO), CwProjectReportArchive.class );
        queryWrapper.eq("cw_pa.del_flag","0");
        if (ObjectUtil.isNotEmpty(cwProjectReportArchiveDTO)) {
            if (ObjectUtil.isNotEmpty(cwProjectReportArchiveDTO.getCwProjectRecordsDTO())){
                // 归档时间
                if (ArrayUtil.isNotEmpty(cwProjectReportArchiveDTO.getAuditDates())){
                    queryWrapper.ge("cw_pa.audit_date", cwProjectReportArchiveDTO.getAuditDates()[0])
                            .le("cw_pa.audit_date", cwProjectReportArchiveDTO.getAuditDates()[1]);
                }
                // 创建人
                if (ObjectUtil.isNotEmpty(cwProjectReportArchiveDTO.getCreateBy())){
                    if (StringUtils.isNotBlank(cwProjectReportArchiveDTO.getCreateBy().getName())){
                        queryWrapper.nested(qw ->
                                qw.in("cw_pa.create_by_id", cwProjectReportArchiveDTO.getCreateBy().getName())
                                        .or()
                                        .like("su.name", cwProjectReportArchiveDTO.getCreateBy().getName())
                        );

                    }
                }

            }
        }
        String isBmzr = "0";
        StringBuilder officeIds = new StringBuilder();
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        List<String> manageOfficeIdList= Lists.newArrayList();

        if (StringUtils.isBlank(cwProjectReportArchiveDTO.getArchiveSta())) {
            cwProjectReportArchiveDTO.setArchiveSta("");
        }

        IPage<CwProjectReportArchiveDTO> list = cwProjectReportArchiveMapper.findCompletedList(page, userDTO, isBmzr, officeIds.toString(), queryWrapper, cwProjectReportArchiveDTO.getArchiveSta());
        List<CwProjectReportArchiveDTO> records = list.getRecords();
        if (null != records && records.size()>0){
            //循环查询出来的数据
            for (CwProjectReportArchiveDTO info : records) {
                //如果归档日期为空，则表示该报告还未归档
                if(null == info.getAuditDate()){
                    //如果归档日期差大于60天，表示超期未归档
                    if(info.getArchiveDay()>60){
                        // 超期天数
                        Integer overdueDay = info.getArchiveDay() - 60;
                        info.setArchiveSta("超期未归档");
                        long n = (long) Math.ceil((double)(overdueDay) / 30);
                        String money = Long.toString(n * 200);
                        info.setOverdueDay(String.valueOf(overdueDay));
                        info.setFineMoney(money);
                    }else{
                        //如果归档日期差小于等于60天，表示暂时未归档
                        info.setArchiveSta("暂时未归档");
                        info.setOverdueDay(String.valueOf(info.getArchiveDay() - 60));
                        info.setFineMoney("0");
                    }
                } else {
                    //如果归档日期不为空，则表示该报告已归档

                    //如果归档日期差大于60天，表示已超期归档
                    if(info.getArchiveDay()>60){
                        // 超期天数
                        Integer overdueDay = info.getArchiveDay() - 60;

                        info.setArchiveSta("已超期归档");
                        long n = (long) Math.ceil((double)(overdueDay) / 30);
                        String money = Long.toString(n * 200);
                        info.setOverdueDay(String.valueOf(overdueDay));
                        info.setFineMoney(money);
                    }else{
                        //如果归档日期差小于等于60天，表示已按时归档
                        info.setArchiveSta("已按时归档");
                        info.setOverdueDay(String.valueOf(info.getArchiveDay() - 60));
                        info.setFineMoney("0");
                    }
                }
//                // 报告归档
//                if (StringUtils.isNotBlank(info.getTaskId()) && StringUtils.isNotBlank(info.getStatus())) {
//                    if ("2".equals(info.getStatus())) { // “审核中”的数据要获取数据审核人
//                        info.setAuditUserIds(flowTaskService.getTaskAuditUsers(info.getTaskId()));  // 获取数据审核人
//                    }
//                }
            }
        }
        return list;
    }

}
