package com.jeeplus.business.project.service;

import cn.hutool.core.collection.CollectionUtil;
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.jeeplus.business.project.domain.*;
import com.jeeplus.business.project.mapper.*;
import com.jeeplus.business.project.service.dto.*;
import com.jeeplus.business.project.service.mapstruct.JyProjectArchiveWrapper;
import com.jeeplus.business.project.service.mapstruct.JyProjectAuditWrapper;
import com.jeeplus.business.project.service.mapstruct.JyProjectEiaQualificationWrapper;
import com.jeeplus.business.project.service.mapstruct.JyProjectOutInstanceWrapper;
import com.jeeplus.business.useSeal.domain.Signet;
import com.jeeplus.business.useSeal.mapper.SignetMapper;
import com.jeeplus.business.workClientInfo.domain.JyWorkClientInfo;
import com.jeeplus.business.workClientInfo.domain.JyWorkClientLinkman;
import com.jeeplus.business.workClientInfo.mapper.JyWorkClientInfoMapper;
import com.jeeplus.business.workClientInfo.mapper.JyWorkClientLinkmanMapper;
import com.jeeplus.business.workContractInfo.domain.JyWorkContractInfo;
import com.jeeplus.business.workContractInfo.mapper.JyWorkContractInfoMapper;
import com.jeeplus.business.workContractInfo.service.JyWorkContractService;
import com.jeeplus.business.workContractInfo.service.dto.JyWorkContractInfoDto;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.flowable.feign.IFlowableApi;
import com.jeeplus.sys.domain.WorkAttachmentInfo;
import com.jeeplus.sys.feign.IDictApi;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.feign.IWorkAttachmentApi;

//import com.jeeplus.sys.mapper.DictValueMapper;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.service.dto.WorkAttachmentInfoDTO;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

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


@Service
public class JyProjectService {

    @Resource
    private JyProjectMapper jyProjectMapper;

    @Resource
    private IFlowableApi flowTaskService;

    @Resource
    private JyWorkContractInfoMapper jyWorkContractInfoMapper;

    @Resource
    private JyWorkClientInfoMapper jyWorkClientInfoMapper;

    @Resource
    private JyProjectMemberMapper jyProjectMemberMapper;


    @Resource
    private JyWorkClientLinkmanMapper jyWorkClientLinkmanMapper;

//    @Autowired
//    private JyWorkContractService jyWorkContractService;

//    @Resource
//    private DictValueMapper dictValueMapper;

    @Resource
    private JyProjectAuditMapper jyProjectAuditMapper;


    @Autowired
    private SignetMapper signetMapper;

    @Resource
    private JyProjectOutinstanceMapper jyProjectOutinstanceMapper;

    @Resource
    private JyProjectOutInstanceFileMapper jyProjectOutInstanceFileMapper;

    @Autowired
    private JyProjectReportsSubmitMapper jyProjectReportsSubmitMapper;

    @Autowired
    private JyProjectArchiveMapper jyProjectArchiveMapper;

    @Autowired
    private JyProjectArchiveFileMapper jyProjectArchiveFileMapper;

    @Autowired
    private JyProjectEiaQualificationMapper jyProjectEiaQualificationMapper;

    @Autowired
    private JyProjectEiaQualificationFileMapper jyProjectEiaQualificationFileMapper;


    /**
     * 项目登记列表
     * @param page
     * @param info
     * @return
     */
    public IPage<JyProject> list(Page<JyProject> page, JyProject info) throws Exception {
        QueryWrapper<JyProject> wrapper = QueryWrapperGenerator.buildQueryCondition(info, JyProject.class);
        if (ObjectUtil.isNotEmpty(info) && StringUtils.isNotBlank(info.getCreateById())) {
            wrapper.eq("a.create_by_id", info.getCreateById());
        }
        wrapper.eq("a.del_flag", "0");
        //创建时间
        if (info.getCreateDates() != null && info.getCreateDates().length > 0) {
            wrapper.between("a.create_time", info.getCreateDates()[0], info.getCreateDates()[1]);
        }
        //一审状态
        if (StringUtils.isNotBlank(info.getFirstInstanceStatus())){
            wrapper.eq("pa1.status", info.getFirstInstanceStatus());
        }
        //二审状态
        if (StringUtils.isNotBlank(info.getSecondInstanceStatus())){
            wrapper.eq("pa2.status", info.getSecondInstanceStatus());
        }
        //三审状态
        if (StringUtils.isNotBlank(info.getThirdInstanceStatus())){
            wrapper.eq("pa3.status", info.getThirdInstanceStatus());
        }
        //项目编号
        if (StringUtils.isNotBlank(info.getNo())){
            wrapper.like("a.no",info.getNo());
        }
        //项目名称
        if (StringUtils.isNotBlank(info.getName())){
            wrapper.like("a.name",info.getName());
        }
        //项目负责人
        if (StringUtils.isNotBlank(info.getProjectLeader())){
            wrapper.like("a.project_leader",info.getProjectLeader());
        }
        //合同名称
        if (StringUtils.isNotBlank(info.getContractName())){
            wrapper.like("a.contract_name",info.getContractName());
        }
        //委托方
        if (StringUtils.isNotBlank(info.getPrimaryLinkman())){
            wrapper.like("wci.primary_linkman",info.getPrimaryLinkman());
        }
        //登记状态
        if (StringUtils.isNotBlank(info.getStatus())){
            wrapper.eq("a.status", info.getStatus());
        }
        //报告签发
        if (StringUtils.isNotBlank(info.getReportIssuance())){
            wrapper.eq("a.report_issuance", info.getReportIssuance());
        }
        //外审
        if (StringUtils.isNotBlank(info.getOutInstance())){
            wrapper.eq("po.status", info.getOutInstance());
        }
        //报批签发
        if (StringUtils.isNotBlank(info.getReportsSubmit())){
            wrapper.eq("a.reports_submit", info.getReportsSubmit());
        }
        //归档状态
        if (StringUtils.isNotBlank(info.getReviewStatus())){
            wrapper.eq("par.status", info.getReviewStatus());
        }
        IPage<JyProject> pageList = jyProjectMapper.findPageList(page, wrapper);
        pageList.getRecords().stream().forEach(item ->{
            // 项目登记
            if (StringUtils.isNotBlank(item.getTaskId()) && StringUtils.isNotBlank(item.getStatus())) {
                if ("2".equals(item.getStatus())) { // “审核中”的数据要获取数据审核人
                    item.setAuditUserIds(flowTaskService.getTaskAuditUsers(item.getTaskId()));  // 获取数据审核人
                }
            }
            //合同名称及委托方联系人
            if (StringUtils.isEmpty(item.getContractName())){
                item.setContractName("暂无合同");
            }
            //获取用印id
            List<Signet> signets = signetMapper.selectList(new QueryWrapper<Signet>());
            for (Signet signet : signets) {
                if (StringUtils.isNotBlank(signet.getProjectId())){
                    String[] split = signet.getProjectId().split(",");
                    for (String s : split) {
                        if (StringUtils.isNotBlank(s)){
                            if (s.equals(item.getId())){
                                item.setSealId(signet.getId());
                            }
                        }
                    }
                }
            }
            //获取报批签发信息
            List<JyProjectReportsSubmit> jyProjectReportsSubmits = jyProjectReportsSubmitMapper.selectList(new QueryWrapper<JyProjectReportsSubmit>());
            for (JyProjectReportsSubmit jyProjectReportsSubmit : jyProjectReportsSubmits) {
                if (StringUtils.isNotBlank(jyProjectReportsSubmit.getProjectId())){
                    String[] split = jyProjectReportsSubmit.getProjectId().split(",");
                    for (String s : split) {
                        if (StringUtils.isNotBlank(s)){
                            if (s.equals(item.getId())){
                                item.setReportsSubmitId(jyProjectReportsSubmit.getId());
                                item.setProcInsIdReports(jyProjectReportsSubmit.getProcInsId());
                                item.setReportsSubmit(jyProjectReportsSubmit.getStatus());
                            }
                        }
                    }
                }

            }


        });
        return pageList;
    }

    /**
     * 查询当前登录人所属项目组的项目
     * @return
     */
    public ResponseEntity<List<String>> getHaveProjectIds() {
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        return ResponseEntity.ok(jyProjectMapper.getHaveProjectIds(userDTO));
    }

    public IPage<JyWorkContractInfo> findContractPageList(JyWorkContractInfoDto dto, Page<JyWorkContractInfo> page) {
        IPage<JyWorkContractInfo> contractPageList = jyProjectMapper.findContractPageList(page, dto);
        contractPageList.getRecords().stream().forEach(i->{
        //合同类型查询
        StringBuffer stringBuffer = new StringBuffer();
        String newType="";
        if (StringUtils.isNotEmpty(i.getContractType())){
            if (!i.getContractType().contains(",")){
                String type= jyWorkContractInfoMapper.findType(i.getContractType());
                i.setContractType(type);
            }else {
                String[] split = i.getContractType().split(",");
                for (String s : split) {
                    String type= jyWorkContractInfoMapper.findType2(s);
                    stringBuffer.append(type+",");
                    newType=stringBuffer.substring(0,stringBuffer.lastIndexOf(","));
                    i.setContractType(newType);
                }
            }
        }
        //主委托方联系人信息查询
            List<JyWorkClientLinkman> jyWorkClientLinkmen=jyWorkClientInfoMapper.getByNameInfo(i.getLinkmanId());
            i.setLinkmanList(jyWorkClientLinkmen);
        });

        return contractPageList;
    }

    /**
     * 项目保存或修改
     * @param dto
     * @return
     * @throws Exception
     */
    public String save(JyProjectDTO  dto) throws Exception{
        if (StringUtils.isNotEmpty(dto.getId())) {
            return update(dto);
        }
        return add(dto);
    }


    public String add(JyProjectDTO dto) throws Exception{
        String currentToken = TokenProvider.getCurrentToken();
        JyProject jyProject = new JyProject();
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        // 保存项目管理列表信息
        BeanUtils.copyProperties(dto, jyProject);
        String projectNo = "";

        String id = UUID.randomUUID().toString().replace("-", "");
        JyWorkContractInfo byId = jyWorkContractInfoMapper.findById(dto.getContractId());
        //生成项目编号
        if (dto.getContractStatus().equals("0")){
            projectNo = SpringUtil.getBean ( IWorkAttachmentApi.class ).genSerialNum(userDTO.getCompanyDTO().getId(), JyProjectDTO.BIZ_CODE,currentToken);
        }else {
            Integer projectNum=jyProjectMapper.getProByContractId(dto.getContractId());
            Integer count=0;
            if (projectNum == 0){
                projectNo=byId.getNo()+"-0"+(count+1);
            }else {
                projectNo=byId.getNo()+"-0"+(projectNum+1);
            }
        }
        jyProject.setId(id);//生成主键id
        jyProject.setName(dto.getName());//项目名称
        if (dto.getContractStatus().equals("0")){
            jyProject.setContractName("");//合同名称
        }else {
            jyProject.setContractName(dto.getContractName());//合同名称
        }
        jyProject.setNo(projectNo); //项目编号
        jyProject.setContractId(dto.getContractId()); //合同编号
        List<JyWorkClientLinkman> clientList = dto.getClientList();
        for (JyWorkClientLinkman jyWorkClientLinkman : clientList) {
            jyProject.setClient(jyWorkClientLinkman.getId());//联系人id
            jyProject.setClientName(jyWorkClientLinkman.getClientName());//联系人姓名
        }
        jyProject.setProjectLeader(dto.getProjectLeader());//项目负责人
        jyProject.setProjectPlace(dto.getProjectPlace());//项目所在地
        jyProject.setStatus(dto.getStatus());
        jyProject.setDelFlag(0);
        jyProject.setContractStatus(dto.getContractStatus());//合同情况
        jyProject.setSpecial(dto.getSpecial());//特殊要求
        jyProject.setProjectOverview(dto.getProjectOverview());//工程概况
        jyProject.setBuildPlace(dto.getBuildPlace());//建设地点
        jyProjectMapper.insert(jyProject);
        //保存项目组成员
        List<String> strings = new ArrayList<>();
        strings.add(userDTO.getId());
        strings.add(dto.getProjectLeader());
        for (String string : strings) {
            JyProjectMembers jyProjectMembers = new JyProjectMembers();
            String memberId = UUID.randomUUID().toString().replace("-", "");
            jyProjectMembers.setId(memberId);
            jyProjectMembers.setCreateById(userDTO.getId());
            jyProjectMembers.setCreateTime(new Date());
            jyProjectMembers.setUpdateById(userDTO.getId());
            jyProjectMembers.setUpdateTime(new Date());
            jyProjectMembers.setProjectId(id);
            jyProjectMembers.setUserId(string);
            jyProjectMembers.setStatus("0");
            jyProjectMemberMapper.insert(jyProjectMembers);
        }
        //三级校审添加
        JyProjectAudit jyProjectAudit1 = new JyProjectAudit();
        jyProjectAudit1.setProjectId(jyProject.getId());
        jyProjectAudit1.setStatus("0");
        jyProjectAudit1.setAuditLevel("1");
        jyProjectAudit1.setExceedTimeStatus("0");
        jyProjectAuditMapper.insert(jyProjectAudit1);
        JyProjectAudit jyProjectAudit2 = new JyProjectAudit();
        jyProjectAudit2.setProjectId(jyProject.getId());
        jyProjectAudit2.setStatus("0");
        jyProjectAudit2.setAuditLevel("2");
        jyProjectAuditMapper.insert(jyProjectAudit2);
        JyProjectAudit jyProjectAudit3 = new JyProjectAudit();
        jyProjectAudit3.setProjectId(jyProject.getId());
        jyProjectAudit3.setStatus("0");
        jyProjectAudit3.setAuditLevel("3");
        jyProjectAuditMapper.insert(jyProjectAudit3);

        //外审添加
        JyProjectOutinstance jyProjectOutinstance = new JyProjectOutinstance();
        jyProjectOutinstance.setProjectId(jyProject.getId());
        jyProjectOutinstance.setStatus("0");
        jyProjectOutinstanceMapper.insert(jyProjectOutinstance);

        //归档添加
        JyProjectArchive jyProjectArchive = new JyProjectArchive();
        jyProjectArchive.setProjectId(jyProject.getId());
        jyProjectArchive.setStatus("0");
        jyProjectArchiveMapper.insert(jyProjectArchive);

        //环评项目资质
        JyProjectEiaQualification jyProjectEiaQualification = new JyProjectEiaQualification();
        jyProjectEiaQualification.setProjectId(jyProject.getId());
        jyProjectEiaQualification.setStatus("0");
        jyProjectEiaQualificationMapper.insert(jyProjectEiaQualification);


        // 保存附件信息
        if (CollectionUtils.isNotEmpty(dto.getFiles())) {
            saveFiles(dto.getFiles(), userDTO, id);
        }
        return id;
    }

    public String update(JyProjectDTO dto) throws Exception{
        String currentToken = TokenProvider.getCurrentToken();
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        String projectNo = "";
        //获取合同编号
        JyWorkContractInfo contractInfo = jyWorkContractInfoMapper.findById(dto.getContractId());
        // 修改项目管理列表主表信息
        if (dto.getContractStatus().equals("1")){
            //获取项目编号
            JyProject project = jyProjectMapper.getById(dto.getId());
            Integer projectNum=jyProjectMapper.getProByContractId(dto.getContractId());
            //生成新的项目编号   合同编号+合同下的项目数量+(项目编号)
            Integer count=0;
            //判断当前合同下是否有项目
            if (projectNum==0){
                projectNo=contractInfo.getNo()+"-0"+(count+1)+"("+project.getNo()+")";
            }else {
                projectNo=contractInfo.getNo()+"-0"+(projectNum+1)+"("+project.getNo()+")";
            }
        }else {
            projectNo = SpringUtil.getBean ( IWorkAttachmentApi.class ).genSerialNum(userDTO.getCompanyDTO().getId(), JyProjectDTO.BIZ_CODE,currentToken);
        }
        JyProject jyProject = new JyProject();
        BeanUtils.copyProperties(dto, jyProject);
        List<JyWorkClientLinkman> clientList = dto.getClientList();
        for (JyWorkClientLinkman jyWorkClientLinkman : clientList) {
            jyProject.setClient(jyWorkClientLinkman.getId());//联系人id
            jyProject.setClientName(jyWorkClientLinkman.getClientName());//联系人姓名
        }
        jyProject.setNo(projectNo);
        jyProject.setUpdateById(userDTO.getId());
        jyProject.setUpdateTime(new Date());
        jyProject.setDelFlag(0);
        jyProjectMapper.updateById(jyProject);
        // 修改附件信息
        if (CollectionUtils.isNotEmpty(dto.getFiles())) {
            updateFiles(dto.getFiles(), userDTO, dto.getId());
        }
        return dto.getId();
    }

    /**
     * 修改附件信息
     * @param list 待修改的附件列表
     * @param userDTO 当前登录用户
     * @param id 关联id
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateFiles(List<WorkAttachmentInfo> list, UserDTO userDTO, String id) {
        int j = 1;
        String names = new String();
        //表中存在，但是传过来不存在，说明已删除，表中数据也要删除
        for (WorkAttachmentInfo dto : list) {
            names = names + "," +dto.getUrl();
        }
        //查询保存的附件信息
        List<WorkAttachmentInfo> infoList = jyProjectMapper.findFileList(id);
        if (CollectionUtil.isNotEmpty(infoList)) {
            for (WorkAttachmentInfo i : infoList) {
                if (!names.contains(i.getUrl())) {
                    SpringUtil.getBean ( IWorkAttachmentApi.class ).deleteById(i.getId());
                }
            }
        }
        //保存信息
        for (WorkAttachmentInfo dto : list) {
            //判断是否存在
            Integer isExit = jyProjectMapper.findIsExit(id, dto.getName());
            if (isExit == 0) {
                WorkAttachmentInfo i = new WorkAttachmentInfo();
                //包含了url、size、name
                i.setId(UUID.randomUUID().toString().replace("-", ""));
//                i.getCreateBy().setId(userDTO.getId());
                i.setCreateTime(new Date());
//                i.getUpdateBy().setId(userDTO.getId());
                i.setUpdateTime(new Date());
                i.setDelFlag(0);
                i.setUrl(dto.getUrl());
                //文件类型处理
                List<String> strings = Arrays.asList(dto.getName().split("\\."));
                if (CollectionUtil.isNotEmpty(strings)) {
                    i.setType(strings.get(1));
                }
                i.setAttachmentId(id);
                i.setAttachmentName(dto.getName());
                i.setAttachmentFlag("jyProject");
                i.setFileSize(dto.getSize());
                i.setSort(j);
                Map<String,String> map = new HashMap<>();
                String workAttachment = JSON.toJSONString((i));
                String userDTOInfo = JSON.toJSONString((userDTO));
                map.put("workAttachment",workAttachment);
                map.put("userDTO",userDTOInfo);
                SpringUtil.getBean ( IWorkAttachmentApi.class ).insertWorkAttachment(map);
                j++;
            }
        }
    }

    /**
     * 保存附件信息
     * @param list 待保存的附件列表
     * @param userDTO 当前登录用户
     * @param id 关联id
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveFiles(List<WorkAttachmentInfo> list, UserDTO userDTO, String id) {
        int j = 1;
        for (WorkAttachmentInfo dto : list) {
            WorkAttachmentInfo i = new WorkAttachmentInfo();
            //包含了url、size、name
            i.setId(UUID.randomUUID().toString().replace("-", ""));
//            i.getCreateBy().setId(userDTO.getId());
            i.setCreateTime(new Date());
//            i.getUpdateBy().setId(userDTO.getId());
            i.setUpdateTime(new Date());
            i.setDelFlag(0);
            i.setUrl(dto.getUrl());
            //文件类型处理
            List<String> strings = Arrays.asList(dto.getName().split("\\."));
            if (CollectionUtil.isNotEmpty(strings)) {
                i.setType(strings.get(1));
            }
            i.setAttachmentId(id);
            i.setAttachmentName(dto.getName());
            i.setAttachmentFlag("jyProject");
            i.setFileSize(dto.getSize());
            i.setSort(j);
            Map<String,String> map = new HashMap<>();
            String workAttachment = JSON.toJSONString((i));
            String userDTOInfo = JSON.toJSONString((userDTO));
            map.put("workAttachment",workAttachment);
            map.put("userDTO",userDTOInfo);
            SpringUtil.getBean ( IWorkAttachmentApi.class ).insertWorkAttachment(map);
            j++;
        }
    }

    public JyProjectDTO findById(String id) {
        JyProjectDTO jyProjectDTO = new JyProjectDTO();
        // 查询主表信息
        JyProject info = jyProjectMapper.getById(id);
        //保存项目信息
        if (ObjectUtil.isNotEmpty(info)){
            BeanUtils.copyProperties(info, jyProjectDTO);
        }
        // 查询联系人列表
        ArrayList<JyWorkClientLinkman> jyWorkClientLinkmen = new ArrayList<>();
        JyWorkClientLinkman jyWorkClientLinkman = jyWorkClientLinkmanMapper.selectByClientId(info.getClient());
        if (ObjectUtil.isNotEmpty(jyWorkClientLinkman)){
            jyWorkClientLinkmen.add(jyWorkClientLinkman);
            jyProjectDTO.setClientList(jyWorkClientLinkmen);
        }else {
            JyWorkClientLinkman jyWorkClientLinkman1=jyWorkClientLinkmanMapper.selectByClientName(info.getClientName());
            if (ObjectUtil.isNotEmpty(jyWorkClientLinkman1)){
                jyWorkClientLinkmen.add(jyWorkClientLinkman1);
                jyProjectDTO.setClientList(jyWorkClientLinkmen);
            }
        }
        // 查询成员列表
        LambdaQueryWrapper<JyProjectMembers> memberWrapper = new LambdaQueryWrapper<>();
        memberWrapper.eq(JyProjectMembers::getProjectId, id);
        memberWrapper.eq(JyProjectMembers::getDelFlag, 0);
        List<JyProjectMembers> members = jyProjectMemberMapper.selectList(memberWrapper);
        if (CollectionUtils.isNotEmpty(members)) {
            members.stream().forEach(member -> {
                if (StringUtils.isNotBlank(member.getUserId())) {
                    UserDTO byId = SpringUtil.getBean ( IUserApi.class ).getById(member.getUserId());
                    if (ObjectUtil.isNotEmpty(byId)) {
                        member.setName(byId.getName());
//                        member.setOfficeName(byId.getOfficeDTO().getName());
//                        member.setRoleName(byId.getRoleDTO().getName());
//                        member.setMobile(byId.getMobile());
                        member.setStatus("0");
                    }
                }
            });
            jyProjectDTO.setProjectMembers(members);
        }
        //合同类型查询
        if (StringUtils.isNotBlank(info.getContractId())){
            String type="";
            if (!info.getContractType().contains(",")){
                jyProjectDTO.setContractType(info.getContractType());
            }else {
                String[] split = info.getContractType().split(",");
                for (String s : split) {
                    String contractType = SpringUtil.getBean(IDictApi.class).getDictLabel(s, "jy_contract_type", "");
                    type=contractType+",";
                    String newType=type.substring(0,type.lastIndexOf(","));
                    jyProjectDTO.setContractType(newType);
                }
            }
        }


        // 查询附件信息
        List<WorkAttachmentInfo> files = jyWorkContractInfoMapper.findDtos(id);
        if (CollectionUtils.isNotEmpty(files)) {
            for (WorkAttachmentInfo i : files) {
                i.setCreateBy(SpringUtil.getBean ( IUserApi.class ).getById(i.getBy()));
            }
            jyProjectDTO.setFiles(files);
        }
        return jyProjectDTO;
    }


    /**
     * 根据id修改status
     */
    public void updateStatusById(JyProjectDTO jyProjectDTO) {
        jyProjectMapper.updateStatusById(jyProjectDTO.getId(), jyProjectDTO.getStatus());
        //获取到项目登记通过的信息
        JyProject byId = jyProjectMapper.getById(jyProjectDTO.getId());
        if (byId.getStatus().equals("5")){
            //获取到项目校审一级的信息
           JyProjectAudit jyProjectAudit= jyProjectAuditMapper.getIdByProId(byId.getId());
           //设置项目登记通过的时间
            jyProjectAudit.setProjectAgreeTime(byId.getUpdateTime());
            jyProjectAuditMapper.updateById(jyProjectAudit);
        }
    }


    public String deleteById(String id) {
        jyProjectMapper.deleteById(id);
        jyProjectMemberMapper.deleteByInfoId(id);
        // 附件删除
//        LambdaQueryWrapper<WorkAttachmentInfo> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(WorkAttachmentInfo::getAttachmentId, id);
        SpringUtil.getBean ( IWorkAttachmentApi.class ).deleteByAttachmentId(id);
        return "删除项目成功";
    }

    /**
     * 根据当前登录人获取项目信息
     * @param page
     * @param info
     * @return
     */
    public IPage<JyProject> getByCreate(Page<JyProject> page, JyProject info) throws Exception {
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        QueryWrapper<JyProject> wrapper = QueryWrapperGenerator.buildQueryCondition(info, JyProject.class);
        wrapper.eq("a.create_by_id",userDTO.getId());
        wrapper.eq("pa.status","5");
        //创建时间
        if (info.getCreateDates() != null && info.getCreateDates().length > 0) {
            wrapper.between("a.create_time", info.getCreateDates()[0], info.getCreateDates()[1]);
        }
        //项目编号
        if (StringUtils.isNotBlank(info.getNo())){
            wrapper.like("a.no",info.getNo());
        }
        //项目名称
        if (StringUtils.isNotBlank(info.getName())){
            wrapper.like("a.name",info.getName());
        }
        IPage<JyProject> projectIPage=jyProjectMapper.getByCreate(userDTO.getId(),page,wrapper);
        return projectIPage;
    }

    /**
     * 获取当前登录人外审通过的项目
     * @param page
     * @param info
     * @return
     */
    public IPage<JyProject> getByCreateOut(Page<JyProject> page, JyProject info) throws Exception {
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        QueryWrapper<JyProject> wrapper = QueryWrapperGenerator.buildQueryCondition(info, JyProject.class);
        wrapper.eq("a.create_by_id",userDTO.getId());
        wrapper.eq("po.status","5");
        //创建时间
        if (info.getCreateDates() != null && info.getCreateDates().length > 0) {
            wrapper.between("a.create_time", info.getCreateDates()[0], info.getCreateDates()[1]);
        }
        //项目编号
        if (StringUtils.isNotBlank(info.getNo())){
            wrapper.like("a.no",info.getNo());
        }
        //项目名称
        if (StringUtils.isNotBlank(info.getName())){
            wrapper.like("a.name",info.getName());
        }
        IPage<JyProject> projectIPage=jyProjectMapper.getByCreateOut(userDTO.getId(),page,wrapper);
        return projectIPage;
    }



    /**
     * 获取三级校审信息
     * @param id
     * @return
     */
    public JyProjectAuditDTO findByIdAudit(String id) {
        //根据id获取校审信息
        JyProjectAuditDTO jyProjectAuditDTO=jyProjectAuditMapper.findByIdAudit(id);
        // 查询附件信息
        List<WorkAttachmentInfo> files = jyWorkContractInfoMapper.findDtos(id);
        if (CollectionUtils.isNotEmpty(files)) {
            for (WorkAttachmentInfo i : files) {
                i.setCreateBy(SpringUtil.getBean ( IUserApi.class ).getById(i.getBy()));
            }
            jyProjectAuditDTO.setWorkAttachmentDtoList(files);
        }
        //获取项目信息
        JyProjectDTO byId = findById(jyProjectAuditDTO.getProjectId());
        jyProjectAuditDTO.setJyProjectList(byId);


        //根据项目的id去查校审的id
        List<String> ids = jyProjectAuditMapper.getIDsByProId(jyProjectAuditDTO.getProjectId());
        if (ids.size() >= 1) {
            jyProjectAuditDTO.setId1(ids.get(0));
        }
        if (ids.size() >= 2) {
            jyProjectAuditDTO.setId2(ids.get(1));
        }
        if (ids.size() >= 3) {
            jyProjectAuditDTO.setId3(ids.get(2));
        }
        return jyProjectAuditDTO;

    }

    /**
     * 三级校审新增或修改
     * @param jyProjectAuditDTO
     * @return
     * @throws Exception
     */
    public String saveFormThree(JyProjectAuditDTO jyProjectAuditDTO) throws Exception {
        //代码转化,将dto中的对象赋到实体类中
        JyProjectAudit jyProjectAudit = JyProjectAuditWrapper.INSTANCE.toEntity(jyProjectAuditDTO);
        //获取审核信息
        JyProjectAuditDTO byIdAudit = jyProjectAuditMapper.findByIdAudit(jyProjectAuditDTO.getId());
        if (ObjectUtil.isNotEmpty(byIdAudit)){
            String audit="";
            //对审核意见进行赋值
            if (StringUtils.isNotBlank(byIdAudit.getInitialAuditOpinion())){
                if (StringUtils.isNotBlank(jyProjectAuditDTO.getAuditOpinion())){
                    audit=byIdAudit.getInitialAuditOpinion()+"<br/>"+jyProjectAuditDTO.getAuditOpinion();
                }else {
                    audit=byIdAudit.getInitialAuditOpinion();
                }
            }else {
                audit=jyProjectAuditDTO.getAuditOpinion();
            }
            jyProjectAudit.setInitialAuditOpinion(audit);
            jyProjectAudit.setAuditOpinion("");
            //对修改意见赋值
            String edit="";
            if (StringUtils.isNotBlank(byIdAudit.getInitialEditOpinion())){
                if (StringUtils.isNotBlank(jyProjectAuditDTO.getEditOpinion())){
                    edit=byIdAudit.getInitialEditOpinion()+"<br/>"+jyProjectAuditDTO.getEditOpinion();
                }else {
                    edit=byIdAudit.getInitialEditOpinion();
                }
            }else {
                edit=jyProjectAuditDTO.getEditOpinion();
            }
            jyProjectAudit.setInitialEditOpinion(edit);
            jyProjectAudit.setEditOpinion("");
        }
        jyProjectAudit.setAuditPeople(jyProjectAuditDTO.getAuditPeople());
        if (StringUtils.isNotBlank(jyProjectAudit.getId())){
            jyProjectAuditMapper.updateById(jyProjectAudit);
        }else{
            jyProjectAuditMapper.insert(jyProjectAudit);
        }
        if (ObjectUtil.isNotEmpty(jyProjectAudit)) {
            //附件
            Map<String,String> map = new HashMap<>();
            String fileList = JSON.toJSONString((jyProjectAudit.getWorkAttachmentDtoList()));
            String attachmentId = jyProjectAudit.getId();
            String attachmentFlag = "jyProjectAudit";
            map.put("fileList",fileList);
            map.put("attachmentId",attachmentId);
            map.put("attachmentFlag",attachmentFlag);
            map.put("currentToken", TokenProvider.getCurrentToken ( ));
            SpringUtil.getBean ( IWorkAttachmentApi.class ).saveOrUpdateFileList(map);

        }
        return jyProjectAudit.getId();
    }

    /**
     * 根据id修改审核状态
     * @param dto
     * @return
     */
    public String updateStatusByAuditId(JyProjectAuditDTO dto) {
        JyProjectAudit jyProjectAudit = new JyProjectAudit();
        jyProjectAudit.setId(dto.getId());
        jyProjectAudit.setStatus(dto.getStatus());
        jyProjectAuditMapper.update(jyProjectAudit,new QueryWrapper<JyProjectAudit>().lambda().eq(JyProjectAudit::getId,jyProjectAudit.getId()));
        //修改逾期完成的校审状态
        JyProjectAuditDTO byIdAudit = jyProjectAuditMapper.findByIdAudit(dto.getId());
        if (byIdAudit.getAuditLevel().equals("1")){
            if (byIdAudit.getExceedTimeStatus().equals("1") && dto.getStatus().equals("5")){
                jyProjectAuditMapper.updateStatus2(byIdAudit.getId());
            }
        }
        return "操作成功";
    }

    /**
     * 查询项目一级校审中逾期未发起的项目
     * @return
     */
    public List<JyProjectAudit> selectAudit() {
        //获取一个月前的时间
        //创建时间格式化对象
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间
        Date currentDate = new Date();
        //将当前时间转化为字符串
        String format = simpleDateFormat.format(currentDate);
        //创建calendar对象
        Calendar instance = Calendar.getInstance();
        //将日期设置为当前时间
        instance.setTime(currentDate);
        //将日期往前推一个月
        instance.add(Calendar.MONTH,-1);
        //获取推送后的日期
        Date newDate = instance.getTime();
        List<JyProjectAudit> list=jyProjectAuditMapper.selectAudit(newDate);
        return list;
    }

    /**
     * 查询项目一级校审中逾期未发起的项目
     * @return
     */
    public List<JyProjectAudit> selectAuditInTime() {
        //获取一个月前的时间
        //创建时间格式化对象
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间
        Date currentDate = new Date();
        //将当前时间转化为字符串
        String format = simpleDateFormat.format(currentDate);
        //创建calendar对象
        Calendar instance = Calendar.getInstance();
        //将日期设置为当前时间
        instance.setTime(currentDate);
        //将日期往前推一个月
        instance.add(Calendar.MONTH,-1);
        //获取推送后的日期
        Date newDate = instance.getTime();
        List<JyProjectAudit> list=jyProjectAuditMapper.selectAuditInTime(newDate);
        return list;
    }

    /**
     * 修改一级校审逾期状态
     * @param
     */
    public void updateStatus(String id,String status,String exceedTimeStatus) {
        jyProjectAuditMapper.updateStatus(id,status,exceedTimeStatus);

    }

    /**
     * 查询校审数据信息并导出文档
     * @param info
     * @return
     */
    public Map<String,Object> getAuditInfoMap(JyProject info){
        Map data = new HashMap();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

        //查询项目信息
        JyProjectDTO projectDTO = findById(info.getProjectId());

        //项目名称
        if(StringUtils.isNotBlank(projectDTO.getName())){
            data.put("projectName",projectDTO.getName());
        }else{
            data.put("projectName","");
        }
        //项目地点
        if (StringUtils.isNotBlank(projectDTO.getBuildPlace())){
            data.put("buildPlace",projectDTO.getBuildPlace());
        }else {
            data.put("buildPlace","");
        }
        //建设单位
        if (StringUtils.isNotBlank(projectDTO.getClientName())){
            data.put("clientName",projectDTO.getClientName());
        }else {
            data.put("clientName","");
        }
        //审批部门
        if (StringUtils.isNotBlank(projectDTO.getAuditOffice())){
            data.put("auditOffice",projectDTO.getAuditOffice());
        }else {
            data.put("auditOffice","");
        }
        //项目负责人
        if(StringUtils.isBlank(projectDTO.getLeader())){
            data.put("projectLeader","");
        }else{
            data.put("projectLeader",projectDTO.getLeader());
        }
        //其他参与人员
            data.put("otherPeople","/");
        //根据项目id获取到所有的校审信息
        List<JyProjectAuditDTO> jyProjectAuditDTOS=jyProjectAuditMapper.getByProjectId(info.getProjectId());
        //如果存在审核意见则进行处理
        if(null != jyProjectAuditDTOS){
            if (jyProjectAuditDTOS.size()>=1){
                //审核意见
                ArrayList<String> strings = new ArrayList<>();
                if (StringUtils.isNotBlank(jyProjectAuditDTOS.get(0).getInitialAuditOpinion())){
                    if (jyProjectAuditDTOS.get(0).getInitialAuditOpinion().contains("<br/>")){
                        String[] split = jyProjectAuditDTOS.get(0).getInitialAuditOpinion().split("<br/>");
                        //将审核意见放入集合中
                        for (String s : split) {
                            strings.add(s);
                        }
                    }else {
                        strings.add(jyProjectAuditDTOS.get(0).getInitialAuditOpinion());
                    }
                    data.put("firstAuditList",strings);
                }else {
                   strings.add("");
                    data.put("firstAuditList",strings);
                }
                //审核人
                if (StringUtils.isNotBlank(jyProjectAuditDTOS.get(0).getAuditPeople())){
                    data.put("firstAuditPeople",SpringUtil.getBean(IUserApi.class).getById(jyProjectAuditDTOS.get(0).getAuditPeople()).getName());
                }else {
                    data.put("firstAuditPeople","");

                }
                //日期
                String format = sdf.format(jyProjectAuditDTOS.get(0).getUpdateTime());
                if (null == jyProjectAuditDTOS.get(0).getUpdateTime()){
                    data.put("firstAuditDate","");
                    data.put("firstDate","");
                }else {
                    data.put("firstAuditDate",format);
                    data.put("firstDate",format);
                }
                //修改意见
                ArrayList<String> strings1 = new ArrayList<>();
                if (StringUtils.isNotBlank(jyProjectAuditDTOS.get(0).getInitialEditOpinion())){

                    if (jyProjectAuditDTOS.get(0).getInitialEditOpinion().contains("<br/>")){
                        String[] split = jyProjectAuditDTOS.get(0).getInitialEditOpinion().split("<br/>");
                        for (String s : split) {
                            strings1.add(s);
                        }
                    }else {
                        strings1.add(jyProjectAuditDTOS.get(0).getInitialEditOpinion());
                    }
                    data.put("firstEditList",strings1);
                }else {
                    strings1.add("");
                    data.put("firstEditList",strings1);
                }
                //签名
                if (StringUtils.isNotBlank(jyProjectAuditDTOS.get(0).getCreateById())){
                    data.put("firstName",SpringUtil.getBean(IUserApi.class).getById(jyProjectAuditDTOS.get(0).getCreateById()).getName());
                }else {
                    data.put("firstName","");
                }
                //校核修改结果认可意见及签名和日期
                data.put("firstAgreeOpinion","");
                data.put("firstAgreeName","");
                data.put("firstAgreeDate","");

            }
            if (jyProjectAuditDTOS.size()>=2){
                //审核意见
                ArrayList<String> strings = new ArrayList<>();
                if (StringUtils.isNotBlank(jyProjectAuditDTOS.get(1).getInitialAuditOpinion())){

                    if (jyProjectAuditDTOS.get(1).getInitialAuditOpinion().contains("<br/>")){
                        String[] split = jyProjectAuditDTOS.get(1).getInitialAuditOpinion().split("<br/>");
                        //将审核意见放入集合中
                        for (String s : split) {
                            strings.add(s);
                        }
                    }else {
                        strings.add(jyProjectAuditDTOS.get(1).getInitialAuditOpinion());
                    }
                    data.put("secondAuditList",strings);
                }else {
                    strings.add("");
                    data.put("secondAuditList",strings);

                }
                //审核人
                if (StringUtils.isNotBlank(jyProjectAuditDTOS.get(1).getAuditPeople())){
                    data.put("secondAuditPeople",SpringUtil.getBean(IUserApi.class).getById(jyProjectAuditDTOS.get(1).getAuditPeople()).getName());
                }else {
                    data.put("secondAuditPeople","");
                }
                //日期
                String format = sdf.format(jyProjectAuditDTOS.get(1).getUpdateTime());
                if (null == jyProjectAuditDTOS.get(1).getUpdateTime()){
                    data.put("secondAuditDate","");
                    data.put("secondDate","");
                }else {
                    data.put("secondAuditDate",format);
                    data.put("secondDate",format);
                }
                //修改意见
                ArrayList<String> strings1 = new ArrayList<>();
                if (StringUtils.isNotBlank(jyProjectAuditDTOS.get(1).getInitialEditOpinion())){
                    if (jyProjectAuditDTOS.get(1).getInitialEditOpinion().contains("<br/>")){
                        String[] split = jyProjectAuditDTOS.get(1).getInitialEditOpinion().split("<br/>");
                        for (String s : split) {
                            strings1.add(s);
                        }
                    }else {
                        strings1.add(jyProjectAuditDTOS.get(1).getInitialEditOpinion());
                    }
                    data.put("secondEditList",strings1);
                }else {
                    strings1.add("");
                    data.put("secondEditList",strings1);
                }
                //签名及日期
                if (StringUtils.isNotBlank(jyProjectAuditDTOS.get(1).getCreateById())){
                    data.put("secondName",SpringUtil.getBean(IUserApi.class).getById(jyProjectAuditDTOS.get(1).getCreateById()).getName());
                }else {
                    data.put("secondName","");
                }
                //校核修改结果认可意见
                data.put("secondAgreeOpinion","");
                data.put("secondAgreeName","");
                data.put("secondAgreeDate","");


            }
            if (jyProjectAuditDTOS.size()>=3){
                //审核意见
                ArrayList<String> strings = new ArrayList<>();
                if (StringUtils.isNotBlank(jyProjectAuditDTOS.get(2).getInitialAuditOpinion())){
                    if (jyProjectAuditDTOS.get(2).getInitialAuditOpinion().contains("<br/>")){
                        String[] split = jyProjectAuditDTOS.get(2).getInitialAuditOpinion().split("<br/>");
                        //将审核意见放入集合中
                        for (String s : split) {
                            strings.add(s);
                        }
                    }else {
                        strings.add(jyProjectAuditDTOS.get(2).getInitialAuditOpinion());
                    }
                    data.put("thirdAuditList",strings);
                }else {
                    strings.add("");
                    data.put("thirdAuditList",strings);
                }
                //审核人
                if (StringUtils.isNotBlank(jyProjectAuditDTOS.get(2).getAuditPeople())){
                    data.put("thirdAuditPeople",SpringUtil.getBean(IUserApi.class).getById(jyProjectAuditDTOS.get(2).getAuditPeople()).getName());
                }else {
                    data.put("thirdAuditPeople","");
                }
                //日期
                String format = sdf.format(jyProjectAuditDTOS.get(2).getUpdateTime());
                if (null == jyProjectAuditDTOS.get(2).getUpdateTime()){
                    data.put("thirdAuditDate","");
                    data.put("thirdDate","");
                }
                data.put("thirdAuditDate",format);
                data.put("thirdDate",format);
                //修改意见
                ArrayList<String> strings1 = Lists.newArrayList();
                if (StringUtils.isNotBlank(jyProjectAuditDTOS.get(2).getInitialEditOpinion())){
//                    ArrayList<String> strings = new ArrayList<>();
                    if (jyProjectAuditDTOS.get(2).getInitialEditOpinion().contains("<br/>")){
                        String[] split = jyProjectAuditDTOS.get(2).getInitialEditOpinion().split("<br/>");
                        for (String s : split) {
                            strings1.add(s);
                        }
                    }else {
                        strings1.add(jyProjectAuditDTOS.get(2).getInitialEditOpinion());
                    }
                    data.put("thirdEditList",strings1);
                }else {
                    strings.add("");
                    data.put("thirdEditList",strings1);
                }
                //签名
                if (StringUtils.isNotBlank(jyProjectAuditDTOS.get(2).getCreateById())){
                    data.put("thirdName",SpringUtil.getBean(IUserApi.class).getById(jyProjectAuditDTOS.get(2).getCreateById()).getName());
                }else {
                    data.put("thirdName","");
                }
                //校核修改结果认可意见
                data.put("thirdAgreeOpinion","");
                data.put("thirdAgreeName","");
                data.put("thirdAgreeDate","");

            }
            //签发人
            data.put("submitPeople","");
            //签发日期
            data.put("submitDate","");
        }else{
            //一级复核意见
            data.put("firstAuditList","");
            //一级认可意见
            data.put("firstAgreeOpinion","");
            //一级审核人
            data.put("firstAuditPeople","");
            //一级认可签名
            data.put("firstAgreeName","");
            //一级审核日期
            data.put("firstAuditDate","");
            //一级认可日期
            data.put("firstAgreeDate","");
            //一级修改时间
            data.put("firstDate","");
            //一级修改意见
            data.put("firstEditList","");
            //一级修改人
            data.put("firstName","");
            //二级审核意见
            data.put("secondAuditList","");
            //二级认可意见
            data.put("secondAgreeOpinion","");
            //二级审核人
            data.put("secondAuditPeople","");
            //二级认可人
            data.put("secondAgreeName","");
            //二级审核时间
            data.put("secondAuditDate","");
            //二级认可时间
            data.put("secondAgreeDate","");
            //二级修改时间
            data.put("secondDate","");
            //二级修改意见
            data.put("secondEditList","");
            //二级修改人
            data.put("secondName","");
            //三级审核意见
            data.put("thirdAuditList","");
            //三级认可意见
            data.put("thirdAgreeOpinion","");
            //三级审核人
            data.put("thirdAuditPeople","");
            //三级认可人
            data.put("thirdAgreeName","");
            //三级审核日期
            data.put("thirdAuditDate","");
            //三级认可时间
            data.put("thirdAgreeDate","");
            //三级修改时间
            data.put("thirdDate","");
            //三级修改意见
            data.put("thirdEditList","");
            //三级修改人
            data.put("thirdName","");
            //签发人
            data.put("submitPeople","");
            //签发日期
            data.put("submitDate","");

        }
        return data;
    }

    /**
     * 根据id查询外审信息
     * @param id
     * @return
     */
    public JyProjectOutinstanceDTO findByIdOut(String id) {
        //根据id获取外审信息
        JyProjectOutinstanceDTO jyProjectOutinstanceDTO=jyProjectOutinstanceMapper.findById(id);

        if (null != jyProjectOutinstanceDTO){
            List<WorkAttachmentInfoDTO> fileList =Lists.newArrayList();
            // 查询附件信息
            List<WorkAttachmentInfoDTO> files = jyProjectOutinstanceMapper.findDtos(id);
            if (CollectionUtils.isNotEmpty(files)) {
                for (WorkAttachmentInfoDTO i : files) {
                    i.setCreateBy(SpringUtil.getBean ( IUserApi.class ).getById(i.getBy()));
                }
            }
            fileList.addAll(files);
            jyProjectOutinstanceDTO.setFileList(fileList);
            //获取项目信息
            JyProjectDTO byId = findById(jyProjectOutinstanceDTO.getProjectId());
            jyProjectOutinstanceDTO.setJyProjectList(byId);
        }


        return jyProjectOutinstanceDTO;


    }

    /**
     * 新增或修改外审信息
     * @param jyProjectOutinstanceDTO
     * @return
     */
    public String saveFormOut(JyProjectOutinstanceDTO jyProjectOutinstanceDTO) {
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        //代码转化,将dto中的对象赋到实体类中
        JyProjectOutinstance jyProjectOutinstance = JyProjectOutInstanceWrapper.INSTANCE.toEntity(jyProjectOutinstanceDTO);
        //获取审核信息
        JyProjectOutinstanceDTO byId = jyProjectOutinstanceMapper.findById(jyProjectOutinstance.getId());
        if (ObjectUtil.isNotEmpty(byId)){
            if (StringUtils.isNotBlank(jyProjectOutinstanceDTO.getRemarks())){
                jyProjectOutinstance.setRemarks(jyProjectOutinstanceDTO.getRemarks());
            }
        }
        if (StringUtils.isNotBlank(jyProjectOutinstance.getId())){
            jyProjectOutinstanceMapper.updateById(jyProjectOutinstance);
        }else{
            jyProjectOutinstanceMapper.insert(jyProjectOutinstance);
        }
        JyProject jyProject = new JyProject();
        jyProject.setId(jyProjectOutinstance.getProjectId());
        jyProject.setOutInstance(jyProjectOutinstanceDTO.getStatus());
        jyProjectMapper.updateById(jyProject);
        //附件
        if (ObjectUtil.isNotEmpty(jyProjectOutinstanceDTO)) {
            SpringUtil.getBean ( IWorkAttachmentApi.class ).deleteByAttachmentId(jyProjectOutinstance.getId());
            AtomicInteger sort = new AtomicInteger(1);
            jyProjectOutinstanceDTO.getFileList().stream().forEach(item->{
                //保存附件信息
                WorkAttachmentInfo workAttachmentDto = new WorkAttachmentInfo();
                workAttachmentDto.setName(item.getName());
                workAttachmentDto.setSize(item.getSize());
                workAttachmentDto.setUrl(item.getUrl());
                Map<String,String> map = new HashMap<>();
                String workAttachmentDtoInfo = JSON.toJSONString(workAttachmentDto);
                String userDTOInfo = JSON.toJSONString(userDTO);
                String attachmentId = jyProjectOutinstance.getId();
                String attachmentFlag = "jyProjectOut";
                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();

                //保存附件类型
                JyProjectOutInstanceFile jyProjectOutInstanceFile = new JyProjectOutInstanceFile();
                jyProjectOutInstanceFile.setFileType(item.getJyFileType());
                jyProjectOutInstanceFile.setWorkAttachmentId(fileId);
                jyProjectOutInstanceFileMapper.insert(jyProjectOutInstanceFile);
            });
        }
        return jyProjectOutinstance.getId();
    }

    /**
     * 根据id修改外审状态值status
     * @param dto
     * @return
     */
    public String updateStatusByOutId(JyProjectOutinstanceDTO dto) {
        JyProjectOutinstance jyProjectOutinstance = new JyProjectOutinstance();
        jyProjectOutinstance.setId(dto.getId());
        jyProjectOutinstance.setStatus(dto.getStatus());
        jyProjectOutinstanceMapper.update(jyProjectOutinstance,new QueryWrapper<JyProjectOutinstance>().lambda().eq(JyProjectOutinstance::getId,jyProjectOutinstance.getId()) );
        //修改项目外审状态
        JyProject jyProject = new JyProject();
        jyProject.setId(jyProjectOutinstance.getProjectId());
        jyProject.setOutInstance(dto.getStatus());
        jyProjectMapper.updateById(jyProject);
        return "操作成功";
    }

    public JyProjectReportsSubmitDTO queryReportsById(String id) {
        JyProjectReportsSubmitDTO jyProjectReportsSubmitDTO = new JyProjectReportsSubmitDTO();
        JyProjectReportsSubmitDTO dto = jyProjectReportsSubmitMapper.findById(id);
        BeanUtils.copyProperties(dto, jyProjectReportsSubmitDTO);
        //查询项目信息
        if (StringUtils.isNotBlank(dto.getProjectId())){
            String[] split = dto.getProjectId().split(",");
            ArrayList<JyProject> signetDTOS = new ArrayList<>();
            for (String s : split) {
                JyProject jyProject=jyProjectMapper.getById(s);
                signetDTOS.add(jyProject);
            }
            jyProjectReportsSubmitDTO.setProjectList(signetDTOS);
        }
        List<String> stringList = new ArrayList<>();
        if (com.jeeplus.utils.StringUtils.isNotBlank(dto.getType())){
            stringList.add(dto.getFirstType());
            stringList.add(dto.getType());
        }else {
            stringList.add(dto.getFirstType());
        }
        jyProjectReportsSubmitDTO.setTypes(stringList);
        jyProjectReportsSubmitDTO.setCreateById(dto.getCreateById());
        // 查询附件信息
        List<WorkAttachmentInfo> files = jyWorkContractInfoMapper.findDtos(id);
        if (CollectionUtils.isNotEmpty(files)) {
            for (WorkAttachmentInfo i : files) {
                i.setCreateBy(SpringUtil.getBean ( IUserApi.class ).getById(i.getBy()));
            }
            jyProjectReportsSubmitDTO.setFiles(files);
        }
        return  jyProjectReportsSubmitDTO;
    }

    /**
     * 保存或修改报批签发
     * @param dto
     * @return
     */
    public String saveReportsSubmit(JyProjectReportsSubmitDTO dto) throws Exception {
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken (TokenProvider.getCurrentToken ( ));
        if (com.jeeplus.utils.StringUtils.isNotEmpty(dto.getId())) {
            return updateReportsSubmit(dto, userDTO);
        } else {
            return addReportsSubmit(dto, userDTO);
        }
    }

    public synchronized String addReportsSubmit(JyProjectReportsSubmitDTO dto, UserDTO userDTO) throws Exception{
        // 生成id
        String id = UUID.randomUUID().toString().replace("-", "");
        // 保存基础信息表信息
        JyProjectReportsSubmit jyProjectReportsSubmit = new JyProjectReportsSubmit();
        BeanUtils.copyProperties(dto, jyProjectReportsSubmit);
        jyProjectReportsSubmit.setId(id);
        jyProjectReportsSubmit.setCreateById(userDTO.getId());
        jyProjectReportsSubmit.setCreateTime(new Date());
        jyProjectReportsSubmit.setUpdateById(userDTO.getId());
        jyProjectReportsSubmit.setUpdateTime(new Date());
        jyProjectReportsSubmit.setDelFlag(0);
        //保存关联项目信息
        String projectId="";
        if (ObjectUtil.isNotEmpty(dto.getProjectList())){
            for (JyProject jyProject : dto.getProjectList()) {

                projectId+=jyProject.getProjectId()+",";
            }
        }
        jyProjectReportsSubmit.setProjectId(projectId);
        //保存用印类型
        if (null != dto.getTypes()&&dto.getTypes().size()>0){
            if (dto.getTypes().size()==1){
                jyProjectReportsSubmit.setFirstType(dto.getTypes().get(0));
            }else {
                jyProjectReportsSubmit.setFirstType(dto.getTypes().get(0));
                jyProjectReportsSubmit.setType(dto.getTypes().get(1));
            }
        }

        jyProjectReportsSubmit.setRemarks(dto.getRemarks());//说明
        jyProjectReportsSubmit.setStatus(dto.getStatus());//状态
        jyProjectReportsSubmit.setNumber(dto.getNumber()); //份数
        jyProjectReportsSubmit.setFirstSubmitTime(new Date()); //初次发起时间
        jyProjectReportsSubmitMapper.insert(jyProjectReportsSubmit);
        // 保存附件列表信息
        if (CollectionUtils.isNotEmpty(dto.getFiles())) {
            saveReportsFiles(dto.getFiles(), userDTO, id);
        }
        //修改项目中的报批签发状态
        if (com.jeeplus.utils.StringUtils.isNotBlank(projectId)){
            String[] split = projectId.split(",");
            for (String s : split) {
                if (com.jeeplus.utils.StringUtils.isNotBlank(s)){
                    jyProjectMapper.updateReportsSubmit(s,jyProjectReportsSubmit.getStatus());
                }
            }
        }
        return id;
    }

    public String updateReportsSubmit(JyProjectReportsSubmitDTO dto, UserDTO userDTO) {
        JyProjectReportsSubmit jyProjectReportsSubmit = new JyProjectReportsSubmit();
        BeanUtils.copyProperties(dto, jyProjectReportsSubmit);
        jyProjectReportsSubmit.setUpdateTime(new Date());
        //保存关联项目信息
        String projectId="";
        if (ObjectUtil.isNotEmpty(dto.getProjectList())){
            for (JyProject jyProject : dto.getProjectList()) {
                projectId=jyProject.getId()+",";
            }
        }
        jyProjectReportsSubmit.setProjectId(projectId);
        //保存用印类型
        if (ObjectUtil.isNotEmpty(dto.getTypes())){
            if (StringUtils.isNotBlank(dto.getTypes().get(1))){
                jyProjectReportsSubmit.setType(dto.getTypes().get(1));
            }
            if (StringUtils.isNotBlank(dto.getTypes().get(0))){
                jyProjectReportsSubmit.setFirstType(dto.getTypes().get(0));
            }
        }
        jyProjectReportsSubmit.setRemarks(dto.getRemarks());//说明
        jyProjectReportsSubmit.setStatus(dto.getStatus());//状态
        jyProjectReportsSubmit.setNumber(dto.getNumber()); //份数
        jyProjectReportsSubmitMapper.updateById(jyProjectReportsSubmit);
        // 修改附件信息
        if (CollectionUtils.isNotEmpty(dto.getFiles())) {
            updateReportsFiles(dto.getFiles(), userDTO, dto.getId());
        }
        //修改项目中的报批签发状态
        if (com.jeeplus.utils.StringUtils.isNotBlank(projectId)){
            String[] split = projectId.split(",");
            for (String s : split) {
                if (com.jeeplus.utils.StringUtils.isNotBlank(s)){
                    jyProjectMapper.updateReportsSubmit(s,jyProjectReportsSubmit.getStatus());
                }
            }
        }
        return dto.getId();
    }

    /**
     * 保存附件信息
     * @param list 待保存的附件列表
     * @param userDTO 当前登录用户
     * @param id 关联id
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveReportsFiles(List<WorkAttachmentInfo> list, UserDTO userDTO, String id) {
        int j = 1;
        for (WorkAttachmentInfo dto : list) {
            WorkAttachmentInfo i = new WorkAttachmentInfo();
            //包含了url、size、name
            i.setId(UUID.randomUUID().toString().replace("-", ""));
//            i.getCreateBy().setId(userDTO.getId());
            i.setCreateTime(new Date());
//            i.getUpdateBy().setId(userDTO.getId());
            i.setUpdateTime(new Date());
            i.setDelFlag(0);
            i.setUrl(dto.getUrl());
            //文件类型处理
            List<String> strings = Arrays.asList(dto.getName().split("\\."));
            if (org.flowable.editor.language.json.converter.util.CollectionUtils.isNotEmpty(strings)) {
                i.setType(strings.get(1));
            }
            i.setAttachmentId(id);
            i.setAttachmentName(dto.getName());
            i.setAttachmentFlag("jyReportsSubmit");
            i.setFileSize(dto.getSize());
            i.setSort(j);
            Map<String,String> map = new HashMap<>();
            String workAttachment = JSON.toJSONString((i));
            String userDTOInfo = JSON.toJSONString((userDTO));
            map.put("workAttachment",workAttachment);
            map.put("userDTO",userDTOInfo);
            SpringUtil.getBean ( IWorkAttachmentApi.class ).insertWorkAttachment(map);
//            ossServiceMapper.insertWorkAttachment(i, userDTO);
            j++;
        }
    }

    /**
     * 修改附件信息
     * @param list 待修改的附件列表
     * @param userDTO 当前登录用户
     * @param id 关联id
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateReportsFiles(List<WorkAttachmentInfo> list, UserDTO userDTO, String id) {
        int j = 1;
        String names = new String();
        //表中存在，但是传过来不存在，说明已删除，表中数据也要删除
        for (WorkAttachmentInfo dto : list) {
            names = names + "," +dto.getUrl();
        }
        //查询保存的附件信息
        List<WorkAttachmentInfo> infoList = signetMapper.findFileList(id);
        if (org.flowable.editor.language.json.converter.util.CollectionUtils.isNotEmpty(infoList)) {
            for (WorkAttachmentInfo i : infoList) {
                if (!names.contains(i.getUrl())) {
//                    ossServiceMapper.deleteById(i.getId());
                    SpringUtil.getBean ( IWorkAttachmentApi.class ).deleteById(i.getId());
                }
            }
        }
        //保存信息
        for (WorkAttachmentInfo dto : list) {
            //判断是否存在
            Integer isExit = signetMapper.findIsExit(id, dto.getName());
            if (isExit == 0) {
                WorkAttachmentInfo i = new WorkAttachmentInfo();
                //包含了url、size、name
                i.setId(UUID.randomUUID().toString().replace("-", ""));
//                i.getCreateBy().setId(userDTO.getId());
                i.setCreateTime(new Date());
//                i.getUpdateBy().setId(userDTO.getId());
                i.setUpdateTime(new Date());
                i.setDelFlag(0);
                i.setUrl(dto.getUrl());
                //文件类型处理
                List<String> strings = Arrays.asList(dto.getName().split("\\."));
                if (org.flowable.editor.language.json.converter.util.CollectionUtils.isNotEmpty(strings)) {
                    i.setType(strings.get(1));
                }
                i.setAttachmentId(id);
                i.setAttachmentName(dto.getName());
                i.setAttachmentFlag("jyReportsSubmit");
                i.setFileSize(dto.getSize());
                i.setSort(j);
                Map<String,String> map = new HashMap<>();
                String workAttachment = JSON.toJSONString((i));
                String userDTOInfo = JSON.toJSONString((userDTO));
                map.put("workAttachment",workAttachment);
                map.put("userDTO",userDTOInfo);
                SpringUtil.getBean ( IWorkAttachmentApi.class ).insertWorkAttachment(map);
//                ossServiceMapper.insertWorkAttachment(i, userDTO);
                j++;
            }
        }
    }

    /**
     * 根据id修改状态
     * @param id
     * @param status
     */
    public void updateReportsSubmitById(String id, String status) {
        JyProjectReportsSubmitDTO byId = jyProjectReportsSubmitMapper.findById(id);
        String submitStatus="";
        //判断报批签发是否逾期
        if (StringUtils.isNotBlank(byId.getExceedTimeStatus())){
            if (byId.getExceedTimeStatus().equals("1") && status.equals("5")){
                submitStatus="7";
            }else {
                submitStatus = status;
            }
        }else {
            submitStatus = status;
        }
        jyProjectReportsSubmitMapper.updateReportsSubmitById(id,submitStatus);

        if (ObjectUtil.isNotEmpty(byId)){
            String[] split = byId.getProjectId().split(",");
            for (String s : split) {
                if (StringUtils.isNotBlank(s)){
                    jyProjectMapper.updateReportsSubmit(s,status);
                }
            }
        }
    }

    public List<JyProjectReportsSubmit> findReportsSubmitListExccedTime() {
        //创建时间格式化对象
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间
        Date currentDate = new Date();
        //将当前时间转化为字符串
        String format = simpleDateFormat.format(currentDate);
        //创建calendar对象
        Calendar instance = Calendar.getInstance();
        //将日期设置为当前时间
        instance.setTime(currentDate);
        //将日期往前推两个月
        instance.add(Calendar.MONTH,-2);
        //获取推送后的日期
        Date newDate = instance.getTime();
        //找出初次发起报批签发时间超过两个月的数据
        List<JyProjectReportsSubmit> jyProjectReportsSubmits=jyProjectReportsSubmitMapper.findReportsSubmitListExccedTime(newDate);
        return jyProjectReportsSubmits;
    }


    public void updateExceedTimeStatus(String id, String status, String exceedTimeStatus) {
        jyProjectReportsSubmitMapper.updateExceedTimeStatus(id,status,exceedTimeStatus);
    }

    public List<JyProjectReportsSubmit> findReportsSubmitListInTime() {
        //创建时间格式化对象
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间
        Date currentDate = new Date();
        //将当前时间转化为字符串
        String format = simpleDateFormat.format(currentDate);
        //创建calendar对象
        Calendar instance = Calendar.getInstance();
        //将日期设置为当前时间
        instance.setTime(currentDate);
        //将日期往前推两个月
        instance.add(Calendar.MONTH,-2);
        //获取推送后的日期
        Date newDate = instance.getTime();
        List<JyProjectReportsSubmit> jyProjectReportsSubmits=jyProjectReportsSubmitMapper.findReportsSubmitListInTime(newDate);
        jyProjectReportsSubmits.stream().forEach(item->{
                String[] split = item.getProjectId().split(",");
                String pName="";
                for (String s : split) {
                    if (StringUtils.isNotBlank(s)){
                        JyProject byId = jyProjectMapper.getById(s);
                        pName+=byId.getName();
                    }
                }
                item.setProjectName(pName);
        });
        return jyProjectReportsSubmits;


    }

    /**
     * 根据id查询归档信息
     * @param id
     * @return
     */
    public JyProjectArchiveDTO findByIdArchive(String id) {
        //根据id获取外审信息
        JyProjectArchiveDTO jyProjectArchiveDTO=jyProjectArchiveMapper.findById(id);

        if (null != jyProjectArchiveDTO){
            List<WorkAttachmentInfoDTO> fileList =Lists.newArrayList();
            // 查询附件信息
            List<WorkAttachmentInfoDTO> files = jyProjectArchiveMapper.findDtos(id);
            if (CollectionUtils.isNotEmpty(files)) {
                for (WorkAttachmentInfoDTO i : files) {
                    i.setCreateBy(SpringUtil.getBean ( IUserApi.class ).getById(i.getBy()));
                }
            }
            fileList.addAll(files);
            jyProjectArchiveDTO.setFileList(fileList);
            //获取项目信息
            JyProjectDTO byId = findById(jyProjectArchiveDTO.getProjectId());
            jyProjectArchiveDTO.setJyProjectList(byId);
        }
        return jyProjectArchiveDTO;
    }

    /**
     * 新增/修改归档
     * @param jyProjectArchiveDTO
     * @return
     */
    public String saveFormArchive(JyProjectArchiveDTO jyProjectArchiveDTO) {
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        //代码转化,将dto中的对象赋到实体类中
        JyProjectArchive jyProjectArchive = JyProjectArchiveWrapper.INSTANCE.toEntity(jyProjectArchiveDTO);
        //获取审核信息
        JyProjectOutinstanceDTO byId = jyProjectOutinstanceMapper.findById(jyProjectArchive.getId());
        if (ObjectUtil.isNotEmpty(byId)){
            if (StringUtils.isNotBlank(jyProjectArchive.getRemarks())){
                jyProjectArchive.setRemarks(jyProjectArchiveDTO.getRemarks());
            }
        }
        if (StringUtils.isNotBlank(jyProjectArchive.getId())){
            jyProjectArchiveMapper.updateById(jyProjectArchive);
        }else{
            jyProjectArchiveMapper.insert(jyProjectArchive);
        }
        JyProject jyProject = new JyProject();
        jyProject.setId(jyProjectArchive.getProjectId());
        jyProject.setOutInstance(jyProjectArchive.getStatus());
        jyProjectMapper.updateById(jyProject);
        //附件
        if (ObjectUtil.isNotEmpty(jyProjectArchiveDTO)) {
            SpringUtil.getBean ( IWorkAttachmentApi.class ).deleteByAttachmentId(jyProjectArchiveDTO.getId());
            AtomicInteger sort = new AtomicInteger(1);
            jyProjectArchiveDTO.getFileList().stream().forEach(item->{
                //保存附件信息
                WorkAttachmentInfo workAttachmentDto = new WorkAttachmentInfo();
                workAttachmentDto.setName(item.getName());
                workAttachmentDto.setSize(item.getSize());
                workAttachmentDto.setUrl(item.getUrl());
                Map<String,String> map = new HashMap<>();
                String workAttachmentDtoInfo = JSON.toJSONString(workAttachmentDto);
                String userDTOInfo = JSON.toJSONString(userDTO);
                String attachmentId = jyProjectArchiveDTO.getId();
                String attachmentFlag = "jyProjectArchive";
                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();

                //保存附件类型
                JyProjectArchiveFile jyProjectArchiveFile = new JyProjectArchiveFile();
                jyProjectArchiveFile.setFileType(item.getJyFileType());
                jyProjectArchiveFile.setWorkAttachmentId(fileId);
                jyProjectArchiveFileMapper.insert(jyProjectArchiveFile);
            });
        }
        return jyProjectArchive.getId();
    }

    /**
     * 根据id修改归档状态
     * @param dto
     * @return
     */
    public String updateStatusByArchiveId(JyProjectArchiveDTO dto) {
        JyProjectArchive jyProjectArchive = new JyProjectArchive();
        jyProjectArchive.setId(dto.getId());
        jyProjectArchive.setStatus(dto.getStatus());
        jyProjectArchiveMapper.update(jyProjectArchive,new QueryWrapper<JyProjectArchive>().lambda().eq(JyProjectArchive::getId,jyProjectArchive.getId()) );
        return "操作成功";
    }

    /**
     * 查询环评资质信息
     * @param id
     * @return
     */
    public JyProjectEiaQualificationDTO findByIdEia(String id) {
        //根据id获取环评资质信息
        JyProjectEiaQualificationDTO jyProjectEiaQualificationDTO=jyProjectEiaQualificationMapper.findById(id);

        if (null != jyProjectEiaQualificationDTO){
            List<WorkAttachmentInfoDTO> fileList =Lists.newArrayList();
            // 查询附件信息
            List<WorkAttachmentInfoDTO> files = jyProjectEiaQualificationMapper.findDtos(id);
            if (CollectionUtils.isNotEmpty(files)) {
                for (WorkAttachmentInfoDTO i : files) {
                    i.setCreateBy(SpringUtil.getBean ( IUserApi.class ).getById(i.getBy()));
                }
            }
            fileList.addAll(files);
            jyProjectEiaQualificationDTO.setFileList(fileList);
            //获取项目信息
            JyProjectDTO byId = findById(jyProjectEiaQualificationDTO.getProjectId());
            jyProjectEiaQualificationDTO.setJyProjectList(byId);
        }
        return jyProjectEiaQualificationDTO;
    }

    /*
    新增/修改环评资质
     */
    public String saveFormEia(JyProjectEiaQualificationDTO jyProjectEiaQualificationDTO) {
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        //代码转化,将dto中的对象赋到实体类中
        JyProjectEiaQualification jyProjectEiaQualification = JyProjectEiaQualificationWrapper.INSTANCE.toEntity(jyProjectEiaQualificationDTO);
        //获取审核信息
        JyProjectOutinstanceDTO byId = jyProjectOutinstanceMapper.findById(jyProjectEiaQualification.getId());
        if (ObjectUtil.isNotEmpty(byId)){
            if (StringUtils.isNotBlank(jyProjectEiaQualification.getRemarks())){
                jyProjectEiaQualification.setRemarks(jyProjectEiaQualificationDTO.getRemarks());
            }
        }
        if (StringUtils.isNotBlank(jyProjectEiaQualification.getId())){
            jyProjectEiaQualificationMapper.updateById(jyProjectEiaQualification);
        }else{
            jyProjectEiaQualificationMapper.insert(jyProjectEiaQualification);
        }
        //附件
        if (ObjectUtil.isNotEmpty(jyProjectEiaQualificationDTO)) {
            SpringUtil.getBean ( IWorkAttachmentApi.class ).deleteByAttachmentId(jyProjectEiaQualificationDTO.getId());
            AtomicInteger sort = new AtomicInteger(1);
            jyProjectEiaQualificationDTO.getFileList().stream().forEach(item->{
                //保存附件信息
                WorkAttachmentInfo workAttachmentDto = new WorkAttachmentInfo();
                workAttachmentDto.setName(item.getName());
                workAttachmentDto.setSize(item.getSize());
                workAttachmentDto.setUrl(item.getUrl());
                Map<String,String> map = new HashMap<>();
                String workAttachmentDtoInfo = JSON.toJSONString(workAttachmentDto);
                String userDTOInfo = JSON.toJSONString(userDTO);
                String attachmentId = jyProjectEiaQualificationDTO.getId();
                String attachmentFlag = "jyProjectEia";
                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();

                //保存附件类型
                JyProjectEiaQualificationFile jyProjectEiaQualificationFile = new JyProjectEiaQualificationFile();
                jyProjectEiaQualificationFile.setFileType(item.getJyFileType());
                jyProjectEiaQualificationFile.setWorkAttachmentId(fileId);
                jyProjectEiaQualificationFileMapper.insert(jyProjectEiaQualificationFile);
            });
        }
        return jyProjectEiaQualification.getId();
    }

    /**
     * 根据id修改环评资质状态
     * @param dto
     * @return
     */
    public String updateStatusByEiaId(JyProjectEiaQualificationDTO dto) {
        JyProjectEiaQualification jyProjectEiaQualification = new JyProjectEiaQualification();
        jyProjectEiaQualification.setId(dto.getId());
        jyProjectEiaQualification.setStatus(dto.getStatus());
        jyProjectEiaQualificationMapper.update(jyProjectEiaQualification,new QueryWrapper<JyProjectEiaQualification>().lambda().eq(JyProjectEiaQualification::getId,jyProjectEiaQualification.getId()) );
        return "操作成功";
    }
}
