|  |  | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.metadata.IPage; | 
 |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
 |  |  | import com.doumee.service.business.SmsEmailService; | 
 |  |  | import com.doumee.service.business.third.EmailService; | 
 |  |  | import com.doumee.service.business.third.SignService; | 
 |  |  | import com.github.xiaoymin.knife4j.core.util.CollectionUtils; | 
 |  |  | import com.github.yulichang.wrapper.MPJLambdaWrapper; | 
 |  |  | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  | import org.springframework.transaction.annotation.Transactional; | 
 |  |  |  | 
 |  |  | import java.io.File; | 
 |  |  | import java.math.BigDecimal; | 
 |  |  | import java.util.*; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private InsuranceApplyMapper insuranceApplyMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private ApplyChangeMapper applyChangeMapper; | 
 |  |  |     private SmsEmailService smsEmailService; | 
 |  |  |     @Autowired | 
 |  |  |     private ApplyChangeJoinMapper applyChangeMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SystemDictDataBiz systemDictDataBiz; | 
 |  |  | 
 |  |  |                 return tempFileName; | 
 |  |  |             } | 
 |  |  |         }catch (Exception e){ | 
 |  |  |  | 
 |  |  |         } | 
 |  |  |         return  null; | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public Integer uploadSignedFileDo(InsuranceApply model,String fileurl) { | 
 |  |  |     public Multifile   uploadSignedFileDo(InsuranceApply model,String fileurl,String fullUrl) { | 
 |  |  |         InsuranceApply update = new InsuranceApply(); | 
 |  |  |         update.setEditDate(new Date()); | 
 |  |  |         update.setEditor(model.getCreator()); | 
 |  |  | 
 |  |  |         f.setObjType(Constants.MultiFile.BD_SIGNED_PDF.getKey()); | 
 |  |  |         f.setType(Constants.TWO); | 
 |  |  |         f.setFileurl(fileurl); | 
 |  |  |         f.setFileurlFull(fullUrl); | 
 |  |  |         f.setInfo("投保单企业签约合同"); | 
 |  |  |         f.setName("投保单签约合同.pdf"); | 
 |  |  |         multifileMapper.insert(f); | 
 |  |  |         Constants.ApplyLogType applyLogType = Constants.ApplyLogType.SIGNATURE; | 
 |  |  |         ApplyLog log = new ApplyLog(update,applyLogType.getName(), null,update.getId(),applyLogType.getKey(),JSONObject.toJSONString(model), JSONObject.toJSONString(update)); | 
 |  |  |         applyLogMapper.insert(log); | 
 |  |  |         return  1; | 
 |  |  |  | 
 |  |  |         startSendEmail(f, model.getCompanyName(),model.getSolutionsName(),model.getSolutionEmail()); | 
 |  |  |         return  f; | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |     public Integer uploadChangeSignedFileDo(ApplyChange model,String fileurl) { | 
 |  |  |     public Multifile  uploadChangeSignedFileDo(ApplyChange model,String fileurl,String fullurl) { | 
 |  |  |         ApplyChange update = new ApplyChange(); | 
 |  |  |         update.setEditDate(new Date()); | 
 |  |  |         update.setEditor(model.getCreator()); | 
 |  |  | 
 |  |  |         f.setObjType(Constants.equalsInteger(Constants.ZERO,model.getType())?Constants.MultiFile.CA_APPLY_JIAJIAN_SIGN.getKey() : Constants.MultiFile.CA_APPLY_CHANGEUNIT_SIGN.getKey()); | 
 |  |  |         f.setType(Constants.TWO); | 
 |  |  |         f.setFileurl(fileurl); | 
 |  |  |         f.setFileurlFull(fullurl); | 
 |  |  |         f.setInfo("企业申请表签署文件"); | 
 |  |  |         f.setName("签章申请表.pdf"); | 
 |  |  |         multifileMapper.insert(f); | 
 |  |  |         Constants.ApplyLogType applyLogType =Constants.equalsInteger(Constants.ZERO,model.getType())? Constants.ApplyLogType.CA_JIAJIAN_APPLY_SIGN: Constants.ApplyLogType.CA_CHANGUNIT_APPLY_SIGN; | 
 |  |  |         ApplyLog log = new ApplyLog(update,applyLogType.getName(), null,update.getId(),applyLogType.getKey(),JSONObject.toJSONString(model), JSONObject.toJSONString(update)); | 
 |  |  |         applyLogMapper.insert(log); | 
 |  |  |         return  1; | 
 |  |  |  | 
 |  |  |         startSendEmail(f,model.getCompanyName(),model.getSolutionsName(),model.getSolutionEmail()); | 
 |  |  |         return f; | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private void startSendEmail(Multifile f, String companyName, String solutionsName, String solutionEmail) { | 
 |  |  |         if(StringUtils.isBlank(solutionEmail) || f==null|| StringUtils.isBlank(f.getFileurl())){ | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         SmsEmail email = new SmsEmail(); | 
 |  |  |         email.setTitle(f.getInfo()); | 
 |  |  |         email.setEmail(solutionEmail); | 
 |  |  |         email.setContent("来自企业【"+companyName+"】关于保险方案【"+solutionsName+"】的"+f.getInfo()); | 
 |  |  |         File file = Constants.getFileByNetFile(f.getFileurlFull(),f.getName()); | 
 |  |  |         if(file!=null && file.isFile()){ | 
 |  |  |             Map<String,Object> files = new HashMap<>(); | 
 |  |  |             files.put("name",f.getName()); | 
 |  |  |             files.put("file",file); | 
 |  |  |             email.setFileList(new ArrayList<>()); | 
 |  |  |             email.getFileList().add(files); | 
 |  |  |             smsEmailService.sendEmail(email); | 
 |  |  |             file.delete();//发送后删除临时文件 | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public   void dealWaitSignedData(){ | 
 |  |  |         List<InsuranceApply> applyList = insuranceApplyMapper.selectList(new QueryWrapper<InsuranceApply>().lambda() | 
 |  |  |         List<InsuranceApply> applyList = insuranceApplyJoinMapper.selectJoinList(InsuranceApply.class, | 
 |  |  |                 new MPJLambdaWrapper<InsuranceApply>() | 
 |  |  |                 .selectAll(InsuranceApply.class) | 
 |  |  |                 .selectAs(Solutions::getName,InsuranceApply::getSolutionsName) | 
 |  |  |                 .selectAs(Solutions::getEmail,InsuranceApply::getSolutionEmail) | 
 |  |  |                 .selectAs(Company::getName,InsuranceApply::getCompanyName) | 
 |  |  |                 .leftJoin(Solutions.class,Solutions::getId,InsuranceApply::getSolutionId) | 
 |  |  |                 .leftJoin(Company.class,Company::getId,InsuranceApply::getCompanyId) | 
 |  |  |                 .eq(InsuranceApply::getStatus,Constants.InsuranceApplyStatus.WAIT_SIGNATURE.getKey()) | 
 |  |  |                 .isNotNull(InsuranceApply::getSignApplyNo)); | 
 |  |  |         String path = systemDictDataBiz.queryByCode(Constants.OSS,Constants.RESOURCE_PATH).getCode()+ | 
 |  |  |                 systemDictDataBiz.queryByCode(Constants.OSS,Constants.APPLY_FILE).getCode(); | 
 |  |  |         if(applyList !=null && applyList.size()>0 ){ | 
 |  |  |             for(InsuranceApply model : applyList){ | 
 |  |  |                 String status = signService.linkFileStatus(model.getSignApplyNo()); | 
 |  |  | 
 |  |  |                     //如果下载失败,清除合同,下次签章重新生成新合同 | 
 |  |  |                     return; | 
 |  |  |                 } | 
 |  |  |                 uploadSignedFileDo(model,fileUrl); | 
 |  |  |                 uploadSignedFileDo(model,fileUrl,path+fileUrl); | 
 |  |  |                 return; | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         List<ApplyChange> chagneList = applyChangeMapper.selectList(new QueryWrapper<ApplyChange>().lambda() | 
 |  |  |         List<ApplyChange> chagneList = applyChangeMapper.selectJoinList(ApplyChange.class, | 
 |  |  |                 new MPJLambdaWrapper<ApplyChange>() | 
 |  |  |                 .selectAll(ApplyChange.class) | 
 |  |  |                 .selectAs(Solutions::getName,InsuranceApply::getSolutionsName) | 
 |  |  |                 .selectAs(Solutions::getEmail,InsuranceApply::getSolutionEmail) | 
 |  |  |                 .selectAs(Company::getName,InsuranceApply::getCompanyName) | 
 |  |  |                 .leftJoin(InsuranceApply.class,InsuranceApply::getId,ApplyChange::getApplyId) | 
 |  |  |                 .leftJoin(Solutions.class,Solutions::getId,InsuranceApply::getSolutionId) | 
 |  |  |                 .leftJoin(Company.class,Company::getId,InsuranceApply::getCompanyId) | 
 |  |  |                 .eq(ApplyChange::getStatus,Constants.ApplyChangeStatus.UPLOAD.getKey()) | 
 |  |  |                 .isNotNull(ApplyChange::getSignApplyNo)); | 
 |  |  |         if(chagneList !=null && chagneList.size()>0 ){ | 
 |  |  | 
 |  |  |                     //如果下载失败,清除合同,下次签章重新生成新合同 | 
 |  |  |                     return; | 
 |  |  |                 } | 
 |  |  |                 uploadChangeSignedFileDo(model,fileUrl); | 
 |  |  |                 uploadChangeSignedFileDo(model,fileUrl,path+fileUrl); | 
 |  |  |                 return; | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  | 
 |  |  |         } | 
 |  |  |         if(Constants.equalsInteger(data.getSignStatus(), Constants.THREE )){ | 
 |  |  |             //如果已保全,可下载签约附件 | 
 |  |  |             InsuranceApply model = insuranceApplyMapper.selectOne(new QueryWrapper<InsuranceApply>().lambda().eq(InsuranceApply::getSignApplyNo,data.getApplyNo()).last("limit 1" )); | 
 |  |  |             String path = systemDictDataBiz.queryByCode(Constants.OSS,Constants.RESOURCE_PATH).getCode()+ | 
 |  |  |                     systemDictDataBiz.queryByCode(Constants.OSS,Constants.APPLY_FILE).getCode(); | 
 |  |  |             InsuranceApply model = insuranceApplyJoinMapper.selectJoinOne(InsuranceApply.class, | 
 |  |  |                     new MPJLambdaWrapper<InsuranceApply>() | 
 |  |  |                             .selectAll(InsuranceApply.class) | 
 |  |  |                             .selectAs(Solutions::getName,InsuranceApply::getSolutionsName) | 
 |  |  |                             .selectAs(Solutions::getEmail,InsuranceApply::getSolutionEmail) | 
 |  |  |                             .selectAs(Company::getName,InsuranceApply::getCompanyName) | 
 |  |  |                             .leftJoin(Solutions.class,Solutions::getId,InsuranceApply::getSolutionId) | 
 |  |  |                             .leftJoin(Company.class,Company::getId,InsuranceApply::getCompanyId) | 
 |  |  |                             .eq(InsuranceApply::getSignApplyNo,data.getApplyNo()) | 
 |  |  |                             .last("limit 1" )); | 
 |  |  |             if(model != null && Constants.equalsInteger(model.getStatus(),Constants.InsuranceApplyStatus.WAIT_SIGNATURE.getKey())){ | 
 |  |  |                 //完成签署已完成操作 | 
 |  |  |                 String link = signService.linkFile(data.getApplyNo()); | 
 |  |  | 
 |  |  |                     //如果下载失败,清除合同,下次签章重新生成新合同 | 
 |  |  |                     return; | 
 |  |  |                 } | 
 |  |  |                 uploadSignedFileDo(model,fileUrl); | 
 |  |  |                 uploadSignedFileDo(model,fileUrl,path+fileUrl); | 
 |  |  |                 return; | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             ApplyChange applyChange = applyChangeMapper.selectOne(new QueryWrapper<ApplyChange>().lambda().eq(ApplyChange::getSignApplyNo,data.getApplyNo()).last("limit 1" )); | 
 |  |  |             ApplyChange applyChange = applyChangeMapper.selectJoinOne(ApplyChange.class, | 
 |  |  |                     new MPJLambdaWrapper<ApplyChange>() | 
 |  |  |                             .selectAll(ApplyChange.class) | 
 |  |  |                             .selectAs(Solutions::getName,InsuranceApply::getSolutionsName) | 
 |  |  |                             .selectAs(Solutions::getEmail,InsuranceApply::getSolutionEmail) | 
 |  |  |                             .selectAs(Company::getName,InsuranceApply::getCompanyName) | 
 |  |  |                             .leftJoin(InsuranceApply.class,InsuranceApply::getId,ApplyChange::getApplyId) | 
 |  |  |                             .leftJoin(Solutions.class,Solutions::getId,InsuranceApply::getSolutionId) | 
 |  |  |                             .leftJoin(Company.class,Company::getId,InsuranceApply::getCompanyId) | 
 |  |  |                             .eq(ApplyChange::getSignApplyNo,data.getApplyNo()).last("limit 1" )); | 
 |  |  |             if(applyChange != null &&Constants.equalsInteger(applyChange.getStatus(),Constants.ApplyChangeStatus.UPLOAD.getKey())){ | 
 |  |  |                 String link = signService.linkFile(data.getApplyNo()); | 
 |  |  |                 String fileUrl = uploadSignFile(link); | 
 |  |  | 
 |  |  |                     //如果下载失败,清除合同,下次签章重新生成新合同 | 
 |  |  |                     return; | 
 |  |  |                 } | 
 |  |  |                 uploadChangeSignedFileDo(applyChange,fileUrl); | 
 |  |  |                 uploadChangeSignedFileDo(applyChange,fileUrl,path+fileUrl); | 
 |  |  |             } | 
 |  |  |         }else if(Constants.equalsInteger(data.getSignStatus(), Constants.TWO )){ | 
 |  |  |             //如果已拒签,清除合同,下次签章重新生成新合同 | 
 |  |  | 
 |  |  |         info = info.replace("${param}", update.getCheckInfo()); | 
 |  |  |         ApplyLog log = new ApplyLog(update,applyLogType.getName(),info,update.getId(),applyLogType.getKey(),JSONObject.toJSONString(model), JSONObject.toJSONString(update)); | 
 |  |  |         applyLogMapper.insert(log); | 
 |  |  |  | 
 |  |  |         return  1; | 
 |  |  |  | 
 |  |  |     } |