|  |  | 
 |  |  | package com.doumee.service.business.impl; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSONObject; | 
 |  |  | import com.doumee.biz.system.SystemDictDataBiz; | 
 |  |  | import com.doumee.core.annotation.excel.ExcelExporter; | 
 |  |  | import com.doumee.core.constants.ResponseStatus; | 
 |  |  | import com.doumee.core.exception.BusinessException; | 
 |  |  | import com.doumee.core.model.LoginUserInfo; | 
 |  |  | 
 |  |  | 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.third.SignService; | 
 |  |  | import com.github.xiaoymin.knife4j.core.util.CollectionUtils; | 
 |  |  | import com.github.yulichang.wrapper.MPJLambdaWrapper; | 
 |  |  | import org.apache.commons.lang3.StringUtils; | 
 |  |  | 
 |  |  |     private InsuranceApplyMapper insuranceApplyMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private CompanySolutionMapper companySolutionMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private ApplyChangeJoinMapper applyChangeJoinMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SystemDictDataBiz systemDictDataBiz; | 
 |  |  |     @Autowired | 
 |  |  |     private SignService signService; | 
 |  |  |     @Autowired | 
 |  |  |     private ApplyChagneDetailJoinMapper applyChagneDetailJoinMapper; | 
 |  |  |  | 
 |  |  | 
 |  |  |         multifileMapper.insert(param.getPidanFile()); | 
 |  |  |  | 
 |  |  |         update.setApplyId(model.getApplyId()); | 
 |  |  |         long times =  param.getApplyStartTime().getTime() - model.getValidTime().getTime(); | 
 |  |  |         //处理加减保明细数据 | 
 |  |  |         dealDetailsValidTime(update,times); | 
 |  |  |         if(Constants.equalsInteger(model.getType(),Constants.ZERO)){ | 
 |  |  |             //如果是加减保申请 处理加减保明细数据 | 
 |  |  |             dealDetailsValidTime(update); | 
 |  |  |         }else{ | 
 |  |  |             //如果是换厂申请 处理明细数据 | 
 |  |  |             dealDetailsDUdata(update); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         Constants.ApplyLogType applyLogType = Constants.ApplyLogType.CA_PLATFORM_APPROVE; | 
 |  |  | 
 |  |  |     /** | 
 |  |  |      * 处理加减保明细数据 | 
 |  |  |      * @param update | 
 |  |  |      * @param times | 
 |  |  |      */ | 
 |  |  |     private void dealDetailsValidTime(ApplyChange update,long times) { | 
 |  |  |     private void dealDetailsDUdata(ApplyChange update ) { | 
 |  |  |         List<ApplyChagneDetail> detailList = applyChagneDetailJoinMapper.selectJoinList(ApplyChagneDetail.class, | 
 |  |  |                 new MPJLambdaWrapper<ApplyChagneDetail>() | 
 |  |  |                         .selectAll(ApplyChagneDetail.class) | 
 |  |  |                         .selectAs(Solutions::getTimeUnit,ApplyChagneDetail::getSolutionTimeUnit) | 
 |  |  |                         .selectAs(Solutions::getPrice,ApplyChagneDetail::getSolutionPrice) | 
 |  |  |                   .leftJoin(ApplyChange.class, ApplyChange::getId, ApplyChagneDetail::getApplyChangeId) | 
 |  |  |                   .leftJoin(Solutions.class, Solutions::getId, ApplyChange::getSolutionsId) | 
 |  |  |                   .eq(ApplyChagneDetail::getApplyChangeId,update.getId()) | 
 |  |  |                   .eq(ApplyChagneDetail::getIsdeleted,Constants.ZERO) ); | 
 |  |  |         if(detailList ==null || detailList.size()==0){ | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         for(ApplyChagneDetail detail : detailList){ | 
 |  |  |             //查询员工是在主单下 是否存在生效中的数据 | 
 |  |  |             ApplyDetail oldModel = applyDetailJoinMapper.selectOne(new QueryWrapper<ApplyDetail>().lambda() | 
 |  |  |                     .eq(ApplyDetail::getApplyId, update.getApplyId()) | 
 |  |  |                     .eq(ApplyDetail::getMemberId, detail.getMemberId()) | 
 |  |  |                     .orderByDesc(ApplyDetail::getCreateDate) | 
 |  |  |                     .last("limit 1")); | 
 |  |  |             if(oldModel == null  ){ | 
 |  |  |                 throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,用户【"+detail.getMemberName()+"】原保单信息有误,当前申请不支持减保处理!"); | 
 |  |  |             } | 
 |  |  |             applyDetailJoinMapper.update(null, new UpdateWrapper<ApplyDetail>().lambda() | 
 |  |  |                     .set(ApplyDetail::getEditor,update.getEditor()) | 
 |  |  |                     .set(ApplyDetail::getWorktypeId,detail.getWorktypeId()) | 
 |  |  |                     .set(ApplyDetail::getDuId,detail.getDuId()) | 
 |  |  |                     .set(ApplyDetail::getEditDate,update.getEditDate()) | 
 |  |  |                     .eq(ApplyDetail::getId,oldModel.getApplyId()) | 
 |  |  |             ); | 
 |  |  |  | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |     /** | 
 |  |  |      * 处理加减保明细数据 | 
 |  |  |      * @param update | 
 |  |  |      */ | 
 |  |  |     private void dealDetailsValidTime(ApplyChange update ) { | 
 |  |  |         List<ApplyChagneDetail> detailList = applyChagneDetailJoinMapper.selectJoinList(ApplyChagneDetail.class, | 
 |  |  |                 new MPJLambdaWrapper<ApplyChagneDetail>() | 
 |  |  |                         .selectAll(ApplyChagneDetail.class) | 
 |  |  | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         if(addList!=null && addList.size()>0){ | 
 |  |  |             applyChagneDetailJoinMapper.insertBatchSomeColumn(addList); | 
 |  |  |             applyDetailJoinMapper.insertBatchSomeColumn(addList); | 
 |  |  |         } | 
 |  |  |         if(totalFee.compareTo(new BigDecimal(0)) != 0){ | 
 |  |  |             //如果保单金额发生编码,更新总保单金额 | 
 |  |  | 
 |  |  |         if(Objects.isNull(solutions)){ | 
 |  |  |             throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到保险方案"); | 
 |  |  |         } | 
 |  |  |         CompanySolution companySolution = companySolutionMapper.selectOne(new QueryWrapper<CompanySolution>().lambda() | 
 |  |  |                 .eq(CompanySolution::getCompanyId,loginUserInfo.getCompanyId()) | 
 |  |  |                 .eq(CompanySolution::getSolutionBaseId,solutions.getId()) | 
 |  |  |                 .eq(CompanySolution::getIsdeleted,Constants.ZERO) | 
 |  |  |                 .last(" limit 1 ") | 
 |  |  |         ); | 
 |  |  |         if(Objects.isNull(companySolution)){ | 
 |  |  |             throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到企业分配保险方案信息"); | 
 |  |  |         } | 
 |  |  |         //申请时间必须处于保单的时间范围内 | 
 |  |  |         if (!(DateUtil.compareDate(applyChange.getValidTime(), insuranceApply.getStartTime()) >= Constants.ZERO | 
 |  |  |                 && DateUtil.compareDate(insuranceApply.getEndTime(), applyChange.getValidTime()) >= Constants.ZERO)) { | 
 |  |  | 
 |  |  |         //加保数据 | 
 |  |  |         List<ApplyChagneDetail> addDetailList = applyChange.getAddDetailList(); | 
 |  |  |         if (CollectionUtils.isNotEmpty(addDetailList)) { | 
 |  |  |            if(companySolution.getCanAdd().equals(Constants.ZERO)){ | 
 |  |  |                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "该企业分配保险方案无法进行加保"); | 
 |  |  |            } | 
 |  |  |             this.addChangeDetail(applyChange,addDetailList,duWorktypeList,duSolutionList,insuranceApply,solutions,loginUserInfo); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         //减保数据 | 
 |  |  |         List<ApplyChagneDetail> delDetailList = applyChange.getDelDetailList(); | 
 |  |  |         if (CollectionUtils.isNotEmpty(delDetailList)) { | 
 |  |  |             if(companySolution.getCanReduce().equals(Constants.ZERO)){ | 
 |  |  |                 throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "该企业分配保险方案无法进行加保"); | 
 |  |  |             } | 
 |  |  |             this.delChangeDetail(applyChange,delDetailList,insuranceApply,solutions,loginUserInfo); | 
 |  |  |         } | 
 |  |  |         //换厂业务 | 
 |  |  | 
 |  |  |         ApplyChange applyChange =  applyChangeJoinMapper.selectOne(queryWrapper); | 
 |  |  |         return applyChange; | 
 |  |  |     } | 
 |  |  |     /** | 
 |  |  |      * 导出换厂申请表 | 
 |  |  |      * @param param | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  |     public  ApplyChange  exportChangeUnitExcel(ApplyChange param){ | 
 |  |  |         ApplyChange model = findJoinDetail(param); | 
 |  |  |         if(Objects.isNull(model) ||! Constants.equalsInteger(Constants.ZERO,model.getType())){ | 
 |  |  |             throw new BusinessException(ResponseStatus.DATA_EMPTY); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         //查询明细 | 
 |  |  |         List<ApplyChagneDetail> detailList =findJoinChangeDetailList(model); | 
 |  |  |         model.setChangeDetailList(detailList!=null?detailList:new ArrayList<>()); | 
 |  |  |         model.setChangeNum(model.getChangeDetailList().size()); | 
 |  |  |         return model; | 
 |  |  |     } | 
 |  |  |     /** | 
 |  |  |      * 导出换厂申请表 | 
 |  |  |      * @param param | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  |     public  String  getSignLinkJiajiabao(ApplyChange param){ | 
 |  |  |         ApplyChange model = exportJiajianBaoExcel(param); | 
 |  |  |         return  getOnlineSignLink(model); | 
 |  |  |     } | 
 |  |  |     /** | 
 |  |  |      * 导出换厂申请表 | 
 |  |  |      * @param param | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  |     public  String  getSignLinkChangeUnit(ApplyChange param){ | 
 |  |  |         ApplyChange model = exportChangeUnitExcel(param); | 
 |  |  |         return  getOnlineSignLink(model); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private String getOnlineSignLink(ApplyChange model) { | 
 |  |  |         if(Objects.isNull(model) ||! Constants.equalsInteger(Constants.ZERO,model.getType())){ | 
 |  |  |             throw new BusinessException(ResponseStatus.DATA_EMPTY); | 
 |  |  |         } | 
 |  |  |         if(!Constants.equalsInteger(Constants.ZERO,model.getStatus())){ | 
 |  |  |             throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"对不起,该申请状态已流转,当前不支持签章操作!"); | 
 |  |  |         } | 
 |  |  |         LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
 |  |  |         Company company = user.getCompany(); | 
 |  |  |         if(company== null || StringUtils.isBlank( company.getEmail()) || !Constants.equalsInteger(company.getSignStatus(),Constants.ONE)){ | 
 |  |  |             throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,企业尚未具备在线签章条件,请联系平台管理员确认~"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         String fileUrl = ExcelExporter.build(ApplyChange.class).exportChangeUnitExcelToPdf(model,"缓存申请表"); | 
 |  |  |         String notifyUrl = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.SIGN_DONE_NOTIFY_URL).getCode(); | 
 |  |  |         notifyUrl = notifyUrl.replace("${type}","0").replace("${id}",model.getId().toString()); | 
 |  |  |         String applyNo = signService.applySignLocalFile(company.getName(),fileUrl,company.getName(),company.getCode(),company.getEmail(),null,company.getSignId(),notifyUrl); | 
 |  |  |         if(StringUtils.isBlank(applyNo) ){ | 
 |  |  |             throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,获取在线签章地址失败,请稍后重试!"); | 
 |  |  |         } | 
 |  |  |         String link = signService.signLink(applyNo,company.getName(),company.getCode()); | 
 |  |  |         if(StringUtils.isBlank(link) ){ | 
 |  |  |             throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,获取在线签章地址失败,请稍后重试!"); | 
 |  |  |         } | 
 |  |  |         ApplyChange update= new ApplyChange(); | 
 |  |  |         update.setId(model.getId()); | 
 |  |  |         update.setEditor(user.getId()); | 
 |  |  |         update.setEditDate(new Date()); | 
 |  |  |         update.setSignApplyNo(applyNo); | 
 |  |  |         applyChangeMapper.updateById(update); | 
 |  |  |         return  link; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 导出加减保申请表 | 
 |  |  |      * @param param | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  |     public  ApplyChange  exportJiajianBaoExcel(ApplyChange param){ | 
 |  |  |  | 
 |  |  |         ApplyChange model = findJoinDetail(param); | 
 |  |  |         if(Objects.isNull(model) ||! Constants.equalsInteger(Constants.ZERO,model.getType())){ | 
 |  |  |             throw new BusinessException(ResponseStatus.DATA_EMPTY); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         //查询明细 | 
 |  |  |         List<ApplyChagneDetail> detailList =findJoinDetailList(model); | 
 |  |  |         model.setAddDetailList(new ArrayList<>()); | 
 |  |  |         model.setDelDetailList(new ArrayList<>()); | 
 |  |  |         if(detailList!=null){ | 
 |  |  |             for(ApplyChagneDetail ad :detailList){ | 
 |  |  |                 if(Constants.equalsInteger(ad.getType(),Constants.ZERO)){ | 
 |  |  |                     model.getAddDetailList().add(ad); | 
 |  |  |                 }else{ | 
 |  |  |                     model.getDelDetailList().add(ad); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         model.setDelNum(model.getDelDetailList().size()); | 
 |  |  |         model.setAddNum(model.getAddDetailList().size()); | 
 |  |  |         return model; | 
 |  |  |     } | 
 |  |  |     private ApplyChange findJoinDetail(ApplyChange param) { | 
 |  |  |         MPJLambdaWrapper wrapper=  new MPJLambdaWrapper<ApplyChange>() | 
 |  |  |                 .selectAll(ApplyChange.class) | 
 |  |  |                 .selectAs(Solutions::getName,ApplyChange::getSolutionsName) | 
 |  |  |                 .selectAs(Company::getName,ApplyChange::getCompanyName) | 
 |  |  |                 .selectAs(InsuranceApply::getCode,ApplyChange::getApplyCode) | 
 |  |  |                 .selectAs(InsuranceApply::getStartTime,ApplyChange::getStartTime) | 
 |  |  |                 .selectAs(InsuranceApply::getStartTime,ApplyChange::getEndTime) | 
 |  |  |                 .leftJoin(InsuranceApply.class,InsuranceApply::getId,ApplyChange::getApplyId) | 
 |  |  |                 .leftJoin(Solutions.class,Solutions::getId,InsuranceApply::getSolutionId) | 
 |  |  |                 .leftJoin(Company.class,Company::getId,ApplyChange::getCompanyId) | 
 |  |  |                 .eq(ApplyChange::getId,param.getId()) | 
 |  |  |                 .last("limit 1"); | 
 |  |  |  | 
 |  |  |         ApplyChange model = applyChangeJoinMapper.selectJoinOne(ApplyChange.class,wrapper); | 
 |  |  |         return  model; | 
 |  |  |     } | 
 |  |  |     private List<ApplyChagneDetail> findJoinDetailList(ApplyChange model) { | 
 |  |  |         MPJLambdaWrapper wrapper1=  new MPJLambdaWrapper<ApplyChagneDetail>() | 
 |  |  |                 .selectAll(ApplyChagneDetail.class) | 
 |  |  |                 .selectAs(Member::getName,ApplyChagneDetail::getMemberName) | 
 |  |  |                 .selectAs(Member::getSex,ApplyChagneDetail::getSex) | 
 |  |  |                 .selectAs(Member::getIdcardNo,ApplyChagneDetail::getIdcardNo) | 
 |  |  |                 .selectAs(Worktype::getName,ApplyChagneDetail::getWorkTypeName) | 
 |  |  |                 .selectAs(DispatchUnit::getName,ApplyChagneDetail::getDuName) | 
 |  |  |                 .leftJoin(Worktype.class,Worktype::getId,ApplyChagneDetail::getWorktypeId) | 
 |  |  |                 .leftJoin(DispatchUnit.class,DispatchUnit::getId,ApplyChagneDetail::getDuId) | 
 |  |  |                 .leftJoin(Member.class,Member::getId,ApplyChagneDetail::getMemberId) | 
 |  |  |                 .eq(ApplyChagneDetail::getIsdeleted,Constants.ZERO) | 
 |  |  |                 .eq(ApplyChagneDetail::getApplyChangeId,model.getId()); | 
 |  |  |         //查询明细 | 
 |  |  |         List<ApplyChagneDetail> detailList =applyChagneDetailJoinMapper.selectJoinList(ApplyChagneDetail.class,wrapper1); | 
 |  |  |         return  detailList; | 
 |  |  |     } | 
 |  |  |     private List<ApplyChagneDetail> findJoinChangeDetailList(ApplyChange model) { | 
 |  |  |         MPJLambdaWrapper wrapper1=  new MPJLambdaWrapper<ApplyChagneDetail>() | 
 |  |  |                 .selectAll(ApplyChagneDetail.class) | 
 |  |  |                 .selectAs(Member::getName,ApplyChagneDetail::getMemberName) | 
 |  |  |                 .selectAs(Member::getSex,ApplyChagneDetail::getSex) | 
 |  |  |                 .select("t1.name as worktypeName") | 
 |  |  |                 .select("t2.name as duName") | 
 |  |  |                 .select("t3.name as oldWorktypeName") | 
 |  |  |                 .select("t4.name as oldDuName") | 
 |  |  |                 .selectAs(Member::getSex,ApplyChagneDetail::getSex) | 
 |  |  |                 .selectAs(Member::getIdcardNo,ApplyChagneDetail::getIdcardNo) | 
 |  |  |                 .leftJoin(Worktype.class,Worktype::getId,ApplyChagneDetail::getWorktypeId) | 
 |  |  |                 .leftJoin(DispatchUnit.class,DispatchUnit::getId,ApplyChagneDetail::getDuId) | 
 |  |  |                 .leftJoin(Worktype.class,Worktype::getId,ApplyChagneDetail::getOldWorktypeId) | 
 |  |  |                 .leftJoin(DispatchUnit.class,DispatchUnit::getId,ApplyChagneDetail::getOldDuId) | 
 |  |  |                 .leftJoin(Member.class,Member::getId,ApplyChagneDetail::getMemberId) | 
 |  |  |                 .eq(ApplyChagneDetail::getIsdeleted,Constants.ZERO) | 
 |  |  |                 .eq(ApplyChagneDetail::getApplyChangeId,model.getId()); | 
 |  |  |         //查询明细 | 
 |  |  |         List<ApplyChagneDetail> detailList =applyChagneDetailJoinMapper.selectJoinList(ApplyChagneDetail.class,wrapper1); | 
 |  |  |         return  detailList; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  | } |