RepaymentController.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. package com.loan.system.controller.wechat;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.loan.system.context.BaseContext;
  4. import com.loan.system.domain.dto.*;
  5. import com.loan.system.domain.entity.*;
  6. import com.loan.system.domain.enums.*;
  7. import com.loan.system.domain.pojo.Result;
  8. import com.loan.system.domain.vo.*;
  9. import com.loan.system.exception.DescribeException;
  10. import com.loan.system.repository.ContractSeqRepository;
  11. import com.loan.system.service.*;
  12. import com.loan.system.service.DisbursementRecordService;
  13. import com.loan.system.utils.ResultUtil;
  14. import io.swagger.annotations.Api;
  15. import io.swagger.annotations.ApiOperation;
  16. import net.bytebuddy.dynamic.scaffold.MethodRegistry;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.security.access.prepost.PreAuthorize;
  19. import org.springframework.util.ObjectUtils;
  20. import org.springframework.web.bind.annotation.*;
  21. import java.time.LocalDateTime;
  22. import java.time.format.DateTimeFormatter;
  23. import java.util.*;
  24. import java.util.stream.Collectors;
  25. import static com.loan.system.domain.enums.ExceptionEnum.*;
  26. import static com.loan.system.utils.ValidUtil.isAllFieldsNull;
  27. @RestController
  28. @RequestMapping("/wechat/repayment")
  29. @Api(tags = "还款接口")
  30. public class RepaymentController {
  31. @Autowired
  32. private DisbursementService disbursementService;
  33. @Autowired
  34. private StepService stepService;
  35. @Autowired
  36. private ContractService contractService;
  37. @Autowired
  38. private ContractRepaymentService contractRepaymentService;
  39. @Autowired
  40. private RepaymentService repaymentService;
  41. @Autowired
  42. private RepaymentRecordService repaymentRecordService;
  43. @Autowired
  44. private LoanService loanService;
  45. @GetMapping("/{caseId}/repayDetails")
  46. @ApiOperation("显示回款详情")//与loanContraller的方法一致,但为了规范
  47. @PreAuthorize("@pms.hasAnyRoles('SYSTEM_ADMIN','APPROVER','LEAD_SALES', 'ASSIST_SALES', 'FINANCE', 'BACK_OFFICE')")
  48. public Result findLoanCaseDetails(@PathVariable("caseId")Long caseId){
  49. if(!loanService.existsByIdAndIsDelete(caseId))
  50. throw new DescribeException(ExceptionEnum.PROJECT_NOT_EXIST);
  51. StepVO stepVO = stepService.findByStepNameAndCaseId(StepPropertyEnum.REPAY_START.getLabel(),caseId);
  52. if(ObjectUtils.isEmpty(stepVO) || stepVO.getStatus().equals(StepEnum.UNSTART.getMsg()))
  53. throw new DescribeException(STEP_HAS_NOT_PROCESS);
  54. return ResultUtil.success("success",repaymentService.getDetails(caseId, StepPropertyEnum.REPAY_START.getLabel()));
  55. }
  56. @PostMapping("/{id}")
  57. @ApiOperation(value = "启动回款")
  58. @PreAuthorize("@pms.hasAnyRoles('LEAD_SALES', 'ASSIST_SALES')")
  59. public Result addDisbursement(@RequestBody RepaymentRecordDTO repaymentRecordDTO, @RequestParam Long caseId, @PathVariable("id") Long recordId) {
  60. if(ObjectUtils.isEmpty(repaymentRecordDTO)||isAllFieldsNull(repaymentRecordDTO))
  61. throw new DescribeException(INPUT_ERROR);
  62. System.out.println(repaymentRecordDTO);
  63. if(caseId == null ||!loanService.existsByIdAndIsDelete(caseId))
  64. throw new DescribeException(ExceptionEnum.PROJECT_NOT_EXIST);
  65. StepVO stepVO = stepService.findByStepNameAndCaseId(StepPropertyEnum.REPAY_START.getLabel(),caseId);
  66. if(ObjectUtils.isEmpty(stepVO) || !stepVO.getStatus().equals(StepEnum.PROCESS.getMsg()))
  67. throw new DescribeException(STEP_HAS_NOT_PROCESS);
  68. Repayment repayment = repaymentService.findByCaseIdAndIsDelete(caseId, false);
  69. if(ObjectUtils.isEmpty(repayment)){
  70. Repayment rep = new Repayment();
  71. rep.setCaseId(caseId);
  72. rep.setStartUserId(BaseContext.getCurrentId());
  73. rep.setTotalAmount(disbursementService.getDisbursementByCaseId(caseId).getPlannedAmount());
  74. repayment=repaymentService.addRepayment(rep);
  75. }
  76. RepaymentRecord record =new RepaymentRecord();
  77. //上报计划(-1表示当前为新增)
  78. if(recordId <= 0){
  79. List<RepaymentRecord> repaymentRecords = repaymentRecordService.findByRepaymentIdAndIsDelete(repayment.getId(), false);
  80. if(!repaymentRecords.isEmpty()){
  81. record = repaymentRecords.get(repaymentRecords.size()-1);
  82. if(record.getApprovalDecision()==null || !record.getApprovalDecision().equals(DecisionEnum.PASS.getMsg()))
  83. throw new DescribeException(REPAYMENT_HAS_BEEN_REJECTED);
  84. }
  85. RepaymentRecord repaymentRecord = repaymentRecordService.addRepaymentRecord(repaymentRecordDTO, repayment.getId());
  86. recordId = repaymentRecord.getId();
  87. } else{
  88. //审批意见为通过,无法修改
  89. record = repaymentRecordService.findByIdAndIsDelete(recordId);
  90. if(record.getApprovalDecision()==null || record.getApprovalDecision().equals(DecisionEnum.PASS.getMsg()))
  91. throw new DescribeException(REPAYMENT_HAS_COMPLETED);
  92. repaymentRecordService.updateRepaymentRecordById(repaymentRecordDTO,recordId);
  93. //获取合同回款
  94. List<Long> Ids = contractRepaymentService.findByRepaymentRecordIdAndIsDelete(recordId);
  95. contractRepaymentService.deleteByIds(Ids);
  96. }
  97. //创建新关联关系
  98. Iterator<Map.Entry<Long, Double>> iterator = repaymentRecordDTO.getContractIdAndAmount().entrySet().iterator();
  99. while (iterator.hasNext()) {
  100. Map.Entry<Long, Double> entry = iterator.next();
  101. Long contractId = entry.getKey();
  102. double amount = entry.getValue();
  103. contractRepaymentService.addContractRepayment(recordId,contractId,caseId,amount,record.getRepayTime());
  104. }
  105. //修改状态
  106. if(isCleared(caseId,StepPropertyEnum.REPAY_START.getLabel())){
  107. //若结清,则回款完成
  108. stepService.updateStatusByCaseId( StepEnum.COMPLETED.getMsg(),StepPropertyEnum.REPAY_START.getLabel(), caseId);
  109. }
  110. stepService.tryStartStep(StepPropertyEnum.REPAY_START.getLabel(),caseId);
  111. //TODO:微信推送预审通过消息和通知下一环节
  112. // SysMessage message = new SysMessage();
  113. // message.setMobile(null);
  114. // message.setUserRole("");
  115. // message.setMessageTitle("");
  116. // message.setMessageContent("");
  117. // message.setStepName("");
  118. // message.setRelatedId(loanCase.getId());
  119. // message.setRelatedType("");
  120. // messageService.addMessage(message);
  121. // wxService.sendTemplateMessage(loanCase.getCustomer().getMobile(),new TemplateMessage());
  122. return ResultUtil.success("success");
  123. }
  124. @GetMapping("/{caseId}/approvalDetails")
  125. @ApiOperation("显示审批详情")//与loanContraller的方法一致,但为了规范
  126. @PreAuthorize("@pms.hasAnyRoles('SYSTEM_ADMIN','APPROVER','LEAD_SALES', 'ASSIST_SALES', 'FINANCE', 'BACK_OFFICE')")
  127. public Result findLoanCaseDetails2(@PathVariable("caseId")Long caseId){
  128. if (caseId == null ||!loanService.existsByIdAndIsDelete(caseId))
  129. throw new DescribeException(ExceptionEnum.PROJECT_NOT_EXIST);
  130. return ResultUtil.success("success",repaymentService.getDetails(caseId, StepPropertyEnum.REPAY_APPROVAL.getLabel()));
  131. }
  132. //TODO:在回款记录添加审批记录,且每次审批完之后才能再回款
  133. @PostMapping("/approval/pass")
  134. @ApiOperation(value = "回款审批")
  135. @PreAuthorize("@pms.hasRole('APPROVER')")
  136. public Result repaymentApproval(@RequestBody RepaymentApprovalDTO approvalDTO,@RequestParam Long chargeId) {
  137. if(ObjectUtils.isEmpty(approvalDTO)||approvalDTO.getCaseId() == null||approvalDTO.getRecordId() == null)
  138. throw new DescribeException(INPUT_ERROR);
  139. if(!loanService.existsByIdAndIsDelete(approvalDTO.getCaseId()))
  140. throw new DescribeException(ExceptionEnum.PROJECT_NOT_EXIST);
  141. Long caseId = approvalDTO.getCaseId();
  142. StepVO stepVO = stepService.findByStepNameAndCaseId(StepPropertyEnum.REPAY_APPROVAL.getLabel(),caseId);
  143. if(!stepVO.getStatus().equals(StepEnum.PROCESS.getMsg()))
  144. throw new DescribeException(STEP_HAS_NOT_PROCESS);
  145. //填写审批意见
  146. repaymentRecordService.updateApprovalById(approvalDTO.getComments(),DecisionEnum.PASS.getMsg(),BaseContext.getCurrentId(),approvalDTO.getRecordId());
  147. //TODO:微信推送预审通过消息和通知下一环节
  148. // SysMessage message = new SysMessage();
  149. // message.setMobile(null);
  150. // message.setUserRole("");
  151. // message.setMessageTitle("");
  152. // message.setMessageContent("");
  153. // message.setStepName("");
  154. // message.setRelatedId(loanCase.getId());
  155. // message.setRelatedType("");
  156. // messageService.addMessage(message);
  157. // wxService.sendTemplateMessage(loanCase.getCustomer().getMobile(),new TemplateMessage());
  158. //修改状态
  159. stepService.updateUserByCaseId(StepPropertyEnum.REPAY_APPROVAL.getLabel(), BaseContext.getCurrentId(), caseId);
  160. //TODO:更新结清状态(因为是一审一批,只有通过的时候才结清状态,不通过则不结清,保证了结清时的总金额为审批通过的)
  161. contractIsCleared(repaymentService.findByCaseIdAndIsDelete(caseId,false).getId(),chargeId);
  162. if(isCleared(caseId,StepPropertyEnum.REPAY_APPROVAL.getLabel()))
  163. stepService.updateStatusByCaseId(StepEnum.COMPLETED.getMsg(),StepPropertyEnum.REPAY_APPROVAL.getLabel(), caseId);
  164. stepService.tryStartStep(StepPropertyEnum.REPAY_APPROVAL.getLabel(),caseId);
  165. return ResultUtil.success("success");
  166. }
  167. @PostMapping("/approval/reject")
  168. @ApiOperation(value = "回款审批驳回")
  169. @PreAuthorize("@pms.hasRole('APPROVER')")
  170. public Result repaymentCancel(@RequestBody RepaymentApprovalDTO approvalDTO) {
  171. if(ObjectUtils.isEmpty(approvalDTO)||approvalDTO.getCaseId() == null||approvalDTO.getRecordId() == null)
  172. throw new DescribeException(INPUT_ERROR);
  173. if(!loanService.existsByIdAndIsDelete(approvalDTO.getCaseId()))
  174. throw new DescribeException(ExceptionEnum.PROJECT_NOT_EXIST);
  175. Long caseId = approvalDTO.getCaseId();
  176. StepVO stepVO = stepService.findByStepNameAndCaseId(StepPropertyEnum.REPAY_APPROVAL.getLabel(),caseId);
  177. if(stepVO.getStatus().equals(StepEnum.COMPLETED.getMsg()))
  178. throw new DescribeException(ExceptionEnum.STEP_HAS_COMPLETEED);
  179. //填写驳回意见
  180. repaymentRecordService.updateApprovalById(approvalDTO.getComments(),DecisionEnum.REJECT.getMsg(),BaseContext.getCurrentId(),approvalDTO.getRecordId());
  181. //修改负责人
  182. stepService.updateUserByCaseId(StepPropertyEnum.REPAY_APPROVAL.getLabel(), BaseContext.getCurrentId(), caseId);
  183. stepService.updateStatusByCaseId(StepEnum.PROCESS.getMsg(), StepPropertyEnum.REPAY_START.getLabel(), caseId);
  184. stepService.updateStatusByCaseId(StepEnum.UNSTART.getMsg(), StepPropertyEnum.REPAY_APPROVAL.getLabel(), caseId);
  185. //TODO:微信推送预审通过消息和通知下一环节
  186. // SysMessage message = new SysMessage();
  187. // message.setMobile(null);
  188. // message.setUserRole("");
  189. // message.setMessageTitle("");
  190. // message.setMessageContent("");
  191. // message.setStepName("");
  192. // message.setRelatedId(loanCase.getId());
  193. // message.setRelatedType("");
  194. // messageService.addMessage(message);
  195. // wxService.sendTemplateMessage(loanCase.getCustomer().getMobile(),new TemplateMessage());
  196. return ResultUtil.success("success");
  197. }
  198. //TODO:用于判断总的金额是否结清
  199. private Boolean isCleared(Long caseId,String stepName){
  200. Repayment repayment = repaymentService.findByCaseIdAndIsDelete(caseId, false);
  201. //获取回款记录
  202. List<RepaymentRecord> repaymentRecords = repaymentRecordService.findByRepaymentIdAndIsDelete(repayment.getId(), false);
  203. Double totalAmount = repaymentService.findByCaseIdAndIsDelete(caseId, false).getTotalAmount();
  204. Double currentAmount= 0.0;
  205. for (RepaymentRecord repaymentRecord : repaymentRecords)
  206. if(stepName.equals(StepPropertyEnum.REPAY_APPROVAL.getLabel())){
  207. if(repaymentRecord.getApprovalDecision()!=null && repaymentRecord.getApprovalDecision().equals(DecisionEnum.PASS.getMsg())
  208. && !repaymentRecord.getIsInterest())//审批、财务环节显示审批通过后的总额
  209. currentAmount += repaymentRecord.getAmount();
  210. }else if(stepName.equals(StepPropertyEnum.REPAY_START.getLabel())){
  211. if( !repaymentRecord.getIsInterest())//审批、财务环节显示审批通过后的总额
  212. currentAmount += repaymentRecord.getAmount();
  213. }
  214. return Math.abs(totalAmount-currentAmount)< Double.MIN_VALUE;
  215. }
  216. /*TODO:更新合同结清状态
  217. * 1.根据recordId查合同关联表,
  218. * 2.查出每个合同id对应的回款金额
  219. * 3.若回款金额与借款金额相同,则结清
  220. * */
  221. private void contractIsCleared(Long repaymentId,Long chargeId) {
  222. //1.
  223. List<RepaymentRecord> repaymentRecords = repaymentRecordService.findByRepaymentIdAndIsDelete(repaymentId, false);
  224. List<Long> recordIds= repaymentRecords.stream().map(RepaymentRecord::getId).collect(Collectors.toList());
  225. List<ContractRepayment> contractRepayments = contractRepaymentService.findByRepaymentRecordIdsAndIsDelete(recordIds);
  226. //2.
  227. List<Long> contractIds = contractRepayments.stream().map(ContractRepayment::getContractId).collect(Collectors.toList());
  228. for(Long contractId:contractIds){
  229. double amount = 0.0;
  230. for (ContractRepayment contractRepayment:contractRepayments){
  231. RepaymentRecord repaymentRecord = repaymentRecordService.findByIdAndIsDelete(contractRepayment.getRepaymentRecordId());
  232. if(!repaymentRecord.getIsInterest() && contractRepayment.getContractId().equals(contractId))
  233. amount += contractRepayment.getAmount();
  234. }
  235. System.out.println("amount:"+amount);
  236. //3.
  237. if(Math.abs(amount-contractService.findContractById(contractId).getContractAmount())< Double.MIN_VALUE)
  238. contractService.updateRepayById(true,0.0,chargeId,contractId,false);
  239. }
  240. }
  241. @GetMapping("/financeDetails")
  242. @ApiOperation("显示财务核算详情")//与loanContraller的方法一致,但为了规范
  243. @PreAuthorize("@pms.hasAnyRoles('SYSTEM_ADMIN','APPROVER','LEAD_SALES', 'ASSIST_SALES', 'FINANCE', 'BACK_OFFICE')")
  244. public Result findLoanCaseDetails3(@RequestParam Long caseId){
  245. if (caseId == null ||!loanService.existsByIdAndIsDelete(caseId))
  246. throw new DescribeException(ExceptionEnum.PROJECT_NOT_EXIST);
  247. return ResultUtil.success("success",repaymentService.getDetails(caseId,StepPropertyEnum.FINANCE_CHECK.getLabel()));
  248. }
  249. @PostMapping("/financeCheck/{caseId}")
  250. @ApiOperation(value = "财务核算")
  251. @PreAuthorize("@pms.hasRole('FINANCE')")
  252. public Result financeDisbursement( @PathVariable Long caseId,@RequestParam Double interest,@RequestParam Long contractId) {
  253. if(caseId==null||!loanService.existsByIdAndIsDelete(caseId))
  254. throw new DescribeException(ExceptionEnum.PROJECT_NOT_EXIST);
  255. StepVO stepVO = stepService.findByStepNameAndCaseId(StepPropertyEnum.FINANCE_CHECK.getLabel(),caseId);
  256. if(ObjectUtils.isEmpty(stepVO)||!stepVO.getStatus().equals(StepEnum.PROCESS.getMsg()))
  257. throw new DescribeException(STEP_HAS_NOT_PROCESS);
  258. Contract contract = contractService.findContractById(contractId);
  259. if(!contract.getIsCleared())
  260. throw new DescribeException(FUND_NOT_CLEAR);
  261. if(contract.getFinanceUserId()!=null && !contract.getFinanceUserId().equals(BaseContext.getCurrentId()))
  262. throw new DescribeException(STEP_USER_NOT_EXPECTED);
  263. //修改合同信息
  264. contractService.updateRepayById(true,interest,BaseContext.getCurrentId(),contractId,false);
  265. //修改状态
  266. stepService.updateUserByCaseId(StepPropertyEnum.FINANCE_CHECK.getLabel(), BaseContext.getCurrentId(), caseId);
  267. boolean flag = true;
  268. List<Contract> contractByCaseId = contractService.findContractByCaseId2(caseId);
  269. for (Contract contract2 : contractByCaseId)
  270. if (contract2.getFinanceUserId()== null){
  271. flag = false;
  272. break;
  273. }
  274. if (flag)//如果全部合同都有财务人员,则表示全部核算完成
  275. stepService.updateStatusByCaseId( StepEnum.COMPLETED.getMsg(),StepPropertyEnum.FINANCE_CHECK.getLabel(),caseId);
  276. stepService.tryStartStep(StepPropertyEnum.FINANCE_CHECK.getLabel(),caseId);
  277. //TODO:微信推送预审通过消息和通知下一环节
  278. // SysMessage message = new SysMessage();
  279. // message.setMobile(null);
  280. // message.setUserRole("");
  281. // message.setMessageTitle("");
  282. // message.setMessageContent("");
  283. // message.setStepName("");
  284. // message.setRelatedId(loanCase.getId());
  285. // message.setRelatedType("");
  286. // messageService.addMessage(message);
  287. // wxService.sendTemplateMessage(loanCase.getCustomer().getMobile(),new TemplateMessage());
  288. return ResultUtil.success("success");
  289. }
  290. @GetMapping("/balanceDetails")
  291. @ApiOperation("显示费息回款详情")//与loanContraller的方法一致,但为了规范
  292. @PreAuthorize("@pms.hasAnyRoles('SYSTEM_ADMIN','APPROVER','LEAD_SALES', 'ASSIST_SALES', 'FINANCE', 'BACK_OFFICE')")
  293. public Result findLoanCaseDetails4(@RequestParam Long caseId){
  294. if (caseId == null ||!loanService.existsByIdAndIsDelete(caseId))
  295. throw new DescribeException(ExceptionEnum.PROJECT_NOT_EXIST);
  296. return ResultUtil.success("success",repaymentService.getDetails(caseId,StepPropertyEnum.BALANCE_REPAY.getLabel()));
  297. }
  298. @PostMapping("/balanceRepay")
  299. @ApiOperation(value = "费息回款")
  300. @PreAuthorize("@pms.hasAnyRoles('LEAD_SALES', 'ASSIST_SALES')")
  301. public Result balanceRepay(@RequestParam Long caseId , @RequestBody RepaymentRecordDTO repaymentRecordDTO ) {
  302. if(ObjectUtils.isEmpty(repaymentRecordDTO)||caseId==null)
  303. throw new DescribeException(INPUT_ERROR);
  304. if(!loanService.existsByIdAndIsDelete(caseId))
  305. throw new DescribeException(ExceptionEnum.PROJECT_NOT_EXIST);
  306. StepVO stepVO = stepService.findByStepNameAndCaseId(StepPropertyEnum.BALANCE_REPAY.getLabel(),caseId);
  307. if(ObjectUtils.isEmpty(stepVO)||!stepVO.getStatus().equals(StepEnum.PROCESS.getMsg()))
  308. throw new DescribeException(STEP_HAS_NOT_PROCESS);
  309. // Long currentId = stepVO.getUserId1();
  310. // if(currentId!=null && !BaseContext.getCurrentId().equals(currentId)){
  311. // throw new DescribeException(STEP_USER_NOT_EXPECTED);
  312. // }
  313. //添加回款记录
  314. repaymentRecordDTO.setIsInterest( true);
  315. RepaymentRecord record = repaymentRecordService.addRepaymentRecord(repaymentRecordDTO, repaymentService.findByCaseIdAndIsDelete(caseId, false).getId());
  316. //创建新关联关系
  317. Iterator<Map.Entry<Long, Double>> iterator = repaymentRecordDTO.getContractIdAndAmount().entrySet().iterator();
  318. while (iterator.hasNext()) {
  319. Map.Entry<Long, Double> entry = iterator.next();
  320. Long contractId = entry.getKey();
  321. double amount = entry.getValue();
  322. contractRepaymentService.addContractRepayment(record.getId(),contractId,caseId,amount,record.getRepayTime());
  323. }
  324. //TODO:添加赎当号码
  325. stepService.updateUserId1ByCaseIdAndStepName(StepPropertyEnum.BALANCE_REPAY.getLabel(), BaseContext.getCurrentId(), caseId);
  326. if(interestIsCleared(repaymentService.findByCaseIdAndIsDelete(caseId, false).getId()))//当全部的合同的费息都完成时才结束环节
  327. stepService.updateStatusByCaseId(StepEnum.COMPLETED.getMsg(),StepPropertyEnum.BALANCE_REPAY.getLabel(),caseId);
  328. stepService.tryStartStep(StepPropertyEnum.BALANCE_REPAY.getLabel(),caseId);
  329. //TODO:微信推送预审通过消息和通知下一环节
  330. // SysMessage message = new SysMessage();
  331. // message.setMobile(null);
  332. // message.setUserRole("");
  333. // message.setMessageTitle("");
  334. // message.setMessageContent("");
  335. // message.setStepName("");
  336. // message.setRelatedId(loanCase.getId());
  337. // message.setRelatedType("");
  338. // messageService.addMessage(message);
  339. // wxService.sendTemplateMessage(loanCase.getCustomer().getMobile(),new TemplateMessage());
  340. return ResultUtil.success("success");
  341. }
  342. /*TODO:更新合同费息结清状态
  343. * 1.根据recordId查合同关联表,
  344. * 2.查出每个合同id对应的回款金额
  345. * 3.若全部合同的回款金额与费息相同,则结清
  346. * */
  347. private Boolean interestIsCleared(Long repaymentId) {
  348. //1.
  349. List<RepaymentRecord> repaymentRecords = repaymentRecordService.findByRepaymentIdAndIsInterestAndIsDelete(repaymentId, false);
  350. List<Long> recordIds= repaymentRecords.stream().map(RepaymentRecord::getId).collect(Collectors.toList());
  351. List<ContractRepayment> contractRepayments = contractRepaymentService.findByRepaymentRecordIdsAndIsDelete(recordIds);
  352. //2.
  353. List<Long> contractIds = contractRepayments.stream().map(ContractRepayment::getContractId).collect(Collectors.toList());
  354. for(Long contractId:contractIds){
  355. double amount = 0.0;
  356. for (ContractRepayment contractRepayment:contractRepayments){
  357. RepaymentRecord record = repaymentRecordService.findByIdAndIsDelete(contractRepayment.getRepaymentRecordId());
  358. if(contractRepayment.getContractId().equals(contractId)&&record.getIsInterest())
  359. amount += contractRepayment.getAmount();
  360. }
  361. //3.
  362. if(Math.abs(amount-contractService.findContractById(contractId).getInterestAmount())> Double.MIN_VALUE)
  363. return false;
  364. }
  365. return true;
  366. }
  367. @PostMapping("/financeConfirm")
  368. @ApiOperation(value = "财务确认")
  369. @PreAuthorize("@pms.hasRole('FINANCE')")
  370. public Result financeConfirm(@RequestParam List<Long> recordIds,@RequestParam Long caseId) {//TODO:一下子可以确认多笔
  371. if(caseId==null || !loanService.existsByIdAndIsDelete(caseId))
  372. throw new DescribeException(ExceptionEnum.PROJECT_NOT_EXIST);
  373. StepVO stepVO = stepService.findByStepNameAndCaseId(StepPropertyEnum.FINANCE_CONFIRM.getLabel(),caseId);
  374. if(ObjectUtils.isEmpty(stepVO)||!stepVO.getStatus().equals(StepEnum.PROCESS.getMsg()))
  375. throw new DescribeException(STEP_HAS_NOT_PROCESS);
  376. // Long currentId = stepVO.getUserId1();
  377. // if(currentId!=null && !BaseContext.getCurrentId().equals(currentId)){
  378. // throw new DescribeException(STEP_USER_NOT_EXPECTED);
  379. // }
  380. for(Long recordId:recordIds){
  381. repaymentRecordService.updateApprovalById("",DecisionEnum.PASS.getMsg(),BaseContext.getCurrentId(),recordId);
  382. }
  383. stepService.updateUserId1ByCaseIdAndStepName(StepPropertyEnum.FINANCE_CONFIRM.getLabel(), BaseContext.getCurrentId(), caseId);
  384. if(interestIsCleared(repaymentService.findByCaseIdAndIsDelete(caseId, false).getId())){
  385. stepService.updateStatusByCaseId(StepEnum.COMPLETED.getMsg(),StepPropertyEnum.FINANCE_CONFIRM.getLabel(),caseId);
  386. // loanService.updateIsCompleteByCaseId(DecisionEnum.COMPLETE.getMsg(),caseId);
  387. }
  388. stepService.tryStartStep(StepPropertyEnum.FINANCE_CONFIRM.getLabel(),caseId);
  389. //TODO:微信推送预审通过消息和通知下一环节
  390. // SysMessage message = new SysMessage();
  391. // message.setMobile(null);
  392. // message.setUserRole("");
  393. // message.setMessageTitle("");
  394. // message.setMessageContent("");
  395. // message.setStepName("");
  396. // message.setRelatedId(loanCase.getId());
  397. // message.setRelatedType("");
  398. // messageService.addMessage(message);
  399. // wxService.sendTemplateMessage(loanCase.getCustomer().getMobile(),new TemplateMessage());
  400. return ResultUtil.success("success");
  401. }
  402. @PostMapping("/repayComplete")
  403. @ApiOperation(value = "回款完成")
  404. @PreAuthorize("@pms.hasAnyRoles('APPROVAL')")
  405. public Result repayComplete(@RequestParam Long caseId) {
  406. if(caseId==null || !loanService.existsByIdAndIsDelete(caseId))
  407. throw new DescribeException(ExceptionEnum.PROJECT_NOT_EXIST);
  408. StepVO stepVO = stepService.findByStepNameAndCaseId(StepPropertyEnum.REPAYMENT_COMPLETE.getLabel(),caseId);
  409. if(ObjectUtils.isEmpty(stepVO)||!stepVO.getStatus().equals(StepEnum.PROCESS.getMsg()))
  410. throw new DescribeException(STEP_HAS_NOT_PROCESS);
  411. StepVO stepVO1 = stepService.findByStepNameAndCaseId(StepPropertyEnum.FINANCE_CONFIRM.getLabel(), caseId);
  412. if(ObjectUtils.isEmpty(stepVO1)||!stepVO1.getStatus().equals(StepEnum.COMPLETED.getMsg()))
  413. throw new DescribeException(PRE_STEP_NOT_COMPLETE);
  414. List<StepVO> steps = stepService.getStepByCaseId(caseId);
  415. for(StepVO step:steps){
  416. if(!step.getStatus().equals(StepEnum.COMPLETED.getMsg()))
  417. throw new DescribeException(STEP_EXIST_NOT_COMPLETE);
  418. }
  419. //更新回款单
  420. repaymentService.updateStatusByCaseId(DecisionEnum.REPAYMENT_COMPLETE.getMsg(),caseId);
  421. stepService.updateUserId1ByCaseIdAndStepName(StepPropertyEnum.REPAYMENT_COMPLETE.getLabel(),BaseContext.getCurrentId(), caseId);
  422. //stepService.updateStatusByCaseId(StepEnum.COMPLETED.getMsg(),StepPropertyEnum.REPAY_PARENT.getLabel(),caseId);
  423. stepService.updateStatusByCaseId(StepEnum.COMPLETED.getMsg(),StepPropertyEnum.REPAYMENT_COMPLETE.getLabel(),caseId);
  424. stepService.tryStartStep(StepPropertyEnum.REPAYMENT_COMPLETE.getLabel(),caseId);
  425. return ResultUtil.success("success");
  426. }
  427. @PostMapping("/caseComplete")
  428. @ApiOperation(value = "业务完成")
  429. @PreAuthorize("@pms.hasAnyRoles('BACK_OFFICE')")
  430. public Result loanCaseComplete(@RequestParam Long caseId) {
  431. if(caseId==null || !loanService.existsByIdAndIsDelete(caseId))
  432. throw new DescribeException(ExceptionEnum.PROJECT_NOT_EXIST);
  433. StepVO stepVO = stepService.findByStepNameAndCaseId(StepPropertyEnum.CASE_COMPLETE.getLabel(),caseId);
  434. if(ObjectUtils.isEmpty(stepVO)||!stepVO.getStatus().equals(StepEnum.PROCESS.getMsg()))
  435. throw new DescribeException(STEP_HAS_NOT_PROCESS);
  436. StepVO stepVO1 = stepService.findByStepNameAndCaseId(StepPropertyEnum.FINANCE_CONFIRM.getLabel(), caseId);
  437. if(ObjectUtils.isEmpty(stepVO1)||!stepVO1.getStatus().equals(StepEnum.COMPLETED.getMsg()))
  438. throw new DescribeException(PRE_STEP_NOT_COMPLETE);
  439. //设置业务状态
  440. loanService.updateIsCompleteByCaseId(DecisionEnum.COMPLETE.getMsg(),caseId);
  441. stepService.updateUserId1ByCaseIdAndStepName(StepPropertyEnum.CASE_COMPLETE.getLabel(),BaseContext.getCurrentId(), caseId);
  442. stepService.updateStatusByCaseId(StepEnum.COMPLETED.getMsg(),StepPropertyEnum.REPAY_PARENT.getLabel(),caseId);
  443. stepService.updateStatusByCaseId(StepEnum.COMPLETED.getMsg(),StepPropertyEnum.CASE_COMPLETE.getLabel(),caseId);
  444. stepService.tryStartStep(StepPropertyEnum.CASE_COMPLETE.getLabel(),caseId);
  445. return ResultUtil.success("success");
  446. }
  447. //TODO:因为现在是一审一批,所以仅最后一笔金额可能是未通过
  448. // private Boolean isCleared(Long caseId,String stepName){
  449. // Repayment repayment = repaymentService.findByCaseIdAndIsDelete(caseId, false);
  450. // //获取回款记录
  451. // List<RepaymentRecord> repaymentRecords = repaymentRecordService.findByRepaymentIdAndIsDelete(repayment.getId(), false);
  452. // Double totalAmount = repaymentService.findByCaseIdAndIsDelete(caseId, false).getTotalAmount();
  453. //
  454. // Double currentAmount= 0.0;
  455. // if(stepName.equals(StepPropertyEnum.REPAY_START.getLabel())){
  456. // //不计算最后一次,因为最后一次一定未审批
  457. // totalAmount-= repaymentRecords.get(repaymentRecords.size()-1).getAmount();
  458. // for (int i=0;i<repaymentRecords.size()-1;i++){
  459. // RepaymentRecord repaymentRecord = repaymentRecords.get(i);
  460. // if(repaymentRecord.getApprovalDecision().equals(DecisionEnum.PASS.getMsg())&&!repaymentRecord.getIsInterest())//审批、财务环节显示审批通过后的总额
  461. // currentAmount += repaymentRecord.getAmount();
  462. // }
  463. // }else{
  464. // for (RepaymentRecord repaymentRecord : repaymentRecords)
  465. // if(repaymentRecord.getApprovalDecision().equals(DecisionEnum.PASS.getMsg())&& !repaymentRecord.getIsInterest())//审批、财务环节显示审批通过后的总额
  466. // currentAmount += repaymentRecord.getAmount();
  467. //
  468. // }
  469. //
  470. // return Math.abs(totalAmount-currentAmount)< Double.MIN_VALUE;
  471. // }
  472. }