InsPlyIncomeServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package com.ydtech.modules.fnc.service.impl;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.fasterxml.jackson.core.JsonProcessingException;
  5. import com.fasterxml.jackson.databind.JsonNode;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import com.github.pagehelper.PageHelper;
  8. import com.github.pagehelper.PageInfo;
  9. import com.ydtech.core.page.HttpResult;
  10. import com.ydtech.modules.admin.model.po.SysAmountAuditingEntity;
  11. import com.ydtech.modules.admin.model.po.SysUserAccountHistory;
  12. import com.ydtech.modules.fnc.entity.InsPlyIncome;
  13. import com.ydtech.modules.fnc.dao.InsPlyIncomeMapper;
  14. import com.ydtech.modules.fnc.entity.SettlementDocumentaryLogEntity;
  15. import com.ydtech.modules.fnc.service.InsPlyIncomeService;
  16. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  17. import com.ydtech.modules.fnc.service.SettlementDocumentaryFeesService;
  18. import com.ydtech.modules.fnc.service.SettlementDocumentaryLogService;
  19. import com.ydtech.modules.fnc.vo.InsPlyIncomeVo;
  20. import com.ydtech.modules.fnc.vo.SettlementDocumentaryFeesVo;
  21. import com.ydtech.modules.order.entity.BasePageResult;
  22. import com.ydtech.modules.order.entity.InsAreaCompany;
  23. import com.ydtech.modules.order.entity.InsOrders;
  24. import com.ydtech.modules.order.service.InsAreaCompanyService;
  25. import com.ydtech.modules.order.service.InsOrdersService;
  26. import com.ydtech.modules.protocols.entity.po.InsFeeOrderNew;
  27. import com.ydtech.modules.protocols.service.InsFeeOrderNewService;
  28. import org.apache.commons.lang3.ObjectUtils;
  29. import org.apache.commons.lang3.StringUtils;
  30. import org.springframework.beans.BeanUtils;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.stereotype.Service;
  33. import org.springframework.transaction.annotation.Transactional;
  34. import java.math.BigDecimal;
  35. import java.time.LocalDateTime;
  36. import java.util.Date;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. /**
  40. * <p>
  41. * 保司结算手续费 服务实现类
  42. * </p>
  43. *
  44. * @author gws
  45. * @since 2024-01-05
  46. */
  47. @Service
  48. public class InsPlyIncomeServiceImpl extends ServiceImpl<InsPlyIncomeMapper, InsPlyIncome> implements InsPlyIncomeService {
  49. @Autowired
  50. private InsOrdersService insOrdersService;
  51. @Autowired
  52. private InsAreaCompanyService insAreaCompanyService;
  53. @Autowired
  54. private InsFeeOrderNewService insFeeOrderNewService;
  55. @Autowired
  56. private SettlementDocumentaryFeesService settlementDocumentaryFeesService;
  57. @Autowired
  58. private SettlementDocumentaryLogService settlementDocumentaryLogService;
  59. public HttpResult<BasePageResult<InsPlyIncome>> queryplyIncome(InsPlyIncomeVo insPlyIncomeVo){
  60. try{
  61. Page<InsOrders> page = new Page<>(insPlyIncomeVo.getPageNum(), insPlyIncomeVo.getPageSize());
  62. PageInfo<List<InsPlyIncome>> pageHelper = null;
  63. PageHelper.startPage(insPlyIncomeVo.getPageNum(),insPlyIncomeVo.getPageSize());
  64. List<InsPlyIncome> list= this.baseMapper.selectByCondition(insPlyIncomeVo);
  65. pageHelper=new PageInfo(list);
  66. BasePageResult<InsPlyIncome> pageResult=new BasePageResult<InsPlyIncome>(insPlyIncomeVo.getPageNum(), insPlyIncomeVo.getPageSize(), pageHelper.getTotal(), pageHelper.getPages(), list);
  67. return HttpResult.ok(pageResult);
  68. }catch (Exception e){
  69. e.printStackTrace();
  70. return HttpResult.error("查询异常");
  71. }
  72. }
  73. @Override
  74. public void saveBalance(String companyId, String orderno) {
  75. InsOrders insOrders = insOrdersService.getById(orderno);
  76. InsAreaCompany insAreaCompany = insAreaCompanyService.getById(companyId);
  77. InsFeeOrderNew insFeeOrderNew = insFeeOrderNewService.getInsFeeOrderNewByCompanyId(companyId);
  78. this.saveBalanc(insFeeOrderNew, insOrders, insAreaCompany);
  79. }
  80. private void saveBalanc(InsFeeOrderNew insFeeOrderNew, InsOrders insOrders, InsAreaCompany insAreaCompany) {
  81. InsPlyIncome insPlyIncome = new InsPlyIncome();
  82. insPlyIncome.setOrderno(insFeeOrderNew.getId()); // 子订单号
  83. // 存储保单号
  84. try {
  85. this.setPolicyno(insPlyIncome,insAreaCompany);
  86. } catch (JsonProcessingException e) {
  87. throw new RuntimeException(e);
  88. }
  89. insPlyIncome.setDeptId(insFeeOrderNew.getDeptId());
  90. insPlyIncome.setRiskcode(insOrders.getProductid());
  91. insPlyIncome.setPayDate(insAreaCompany.getPayDate());
  92. insPlyIncome.setSignDate(insAreaCompany.getSigningTime());
  93. insPlyIncome.setCompanyId(insAreaCompany.getCompanyId());
  94. insPlyIncome.setAgreementId(insFeeOrderNew.getAgreementId());
  95. insPlyIncome.setLicenseno(insOrders.getLicenseno());
  96. insPlyIncome.setTaxamount(insAreaCompany.getTaxamount());
  97. BigDecimal bigDecimal = this.sumAmount(insFeeOrderNew.getJqNoTaxPremium(), insFeeOrderNew.getSyNoTaxPremium(), insFeeOrderNew.getNoNoTaxPremium());
  98. // 不含保费
  99. insPlyIncome.setNoTaxPremium(bigDecimal);
  100. // 手续费比例
  101. BigDecimal CostsProportion = this.sumAmount(insFeeOrderNew.getJqSuperviseCostsProportion(), insFeeOrderNew.getSySuperviseCostsProportion(), insFeeOrderNew.getNoSuperviseCostsProportion());
  102. insPlyIncome.setCommissionFeerate(CostsProportion);
  103. // 其他费用比例
  104. BigDecimal OtherCostsProportion = this.sumAmount(insFeeOrderNew.getJqOtherCostsProportion(), insFeeOrderNew.getSyOtherCostsProportion(), insFeeOrderNew.getNoOtherCostsProportion());
  105. insPlyIncome.setOtherFeerate(OtherCostsProportion);
  106. // 手续费金额
  107. BigDecimal SuperviseCostsPremiums = this.sumAmount(insFeeOrderNew.getJqSuperviseCostsPremiums(), insFeeOrderNew.getSySuperviseCostsPremiums(), insFeeOrderNew.getNoSuperviseCostsPremiums());
  108. insPlyIncome.setCommissionFeevalue(SuperviseCostsPremiums);
  109. // 其他费用金额
  110. BigDecimal OtherCostsPremiums = this.sumAmount(insFeeOrderNew.getJqOtherCostsPremiums(), insFeeOrderNew.getSyOtherCostsPremiums(), insFeeOrderNew.getNoOtherCostsPremiums());
  111. insPlyIncome.setOtherFeevalue(OtherCostsPremiums);
  112. insPlyIncome.setAllFeeValue(SuperviseCostsPremiums.add(OtherCostsPremiums));
  113. insPlyIncome.setCreateTime(LocalDateTime.now());
  114. insPlyIncome.setDocumentaryFeesStatus("0");
  115. insPlyIncome.setHandlingFeesStatus("0");
  116. baseMapper.insert(insPlyIncome);
  117. }
  118. private void setPolicyno(InsPlyIncome insPlyIncome, InsAreaCompany insAreaCompany) throws JsonProcessingException {
  119. ObjectMapper objJson = new ObjectMapper();
  120. String jqpolicyno = insAreaCompany.getJqpolicyno(); // 交强保单号
  121. String sypolicyno = insAreaCompany.getSypolicyno(); // 商业保单号
  122. JSONArray crossInsurance = insAreaCompany.getCrossInsurance(); // 非车保单号
  123. if(ObjectUtils.isNotEmpty(jqpolicyno)){
  124. insPlyIncome.setPolicyno(jqpolicyno);
  125. }
  126. if(ObjectUtils.isNotEmpty(sypolicyno)){
  127. insPlyIncome.setSyPolicyno(sypolicyno);
  128. }
  129. if(ObjectUtils.isNotEmpty(crossInsurance)){
  130. JsonNode jsonArryNode= objJson.readTree(crossInsurance.toJSONString());
  131. if (jsonArryNode.isArray()) {
  132. for (JsonNode jsonNode : jsonArryNode) {
  133. JsonNode jsonNode1 = jsonNode.get("policyNumber");//非车保单号
  134. if(ObjectUtils.isNotEmpty(jsonNode1)){
  135. insPlyIncome.setNoPolicyno(jsonNode1.asText());
  136. }else {
  137. String nopolicy = jsonNode.get("policy").asText(); //非车保单号
  138. insPlyIncome.setNoPolicyno(nopolicy);
  139. }
  140. }
  141. }
  142. }
  143. }
  144. private BigDecimal sumAmount(BigDecimal amount1, BigDecimal amount2, BigDecimal amount3) {
  145. BigDecimal jqNoTaxPremium = (amount1 != null) ? amount1 : BigDecimal.ZERO;
  146. BigDecimal syNoTaxPremium = (amount2 != null) ? amount2 : BigDecimal.ZERO;
  147. BigDecimal noNoTaxPremium = (amount3 != null) ? amount3 : BigDecimal.ZERO;
  148. return jqNoTaxPremium.add(syNoTaxPremium).add(noNoTaxPremium);
  149. }
  150. public HttpResult<BasePageResult<InsPlyIncome>> queryNew(InsPlyIncomeVo insPlyIncomeVo){
  151. try{
  152. HashMap<String, Object> params = new HashMap<>();
  153. PageInfo<List<InsPlyIncome>> pageHelper = null;
  154. PageHelper.startPage(insPlyIncomeVo.getPageNum(),insPlyIncomeVo.getPageSize());
  155. params.put("pageNum",insPlyIncomeVo.getPageNum());
  156. params.put("pageSize",insPlyIncomeVo.getPageSize());
  157. if(ObjectUtils.isNotEmpty(insPlyIncomeVo.getStartDate())){
  158. params.put("startDate",insPlyIncomeVo.getStartDate().toString());
  159. }
  160. if(ObjectUtils.isNotEmpty(insPlyIncomeVo.getEndDate())){
  161. params.put("endDate",insPlyIncomeVo.getEndDate().toString());
  162. }
  163. if(StringUtils.isNotEmpty(insPlyIncomeVo.getOrderno())){
  164. params.put("orderno",insPlyIncomeVo.getOrderno());
  165. }
  166. if(StringUtils.isNotEmpty(insPlyIncomeVo.getPolicyno())){
  167. params.put("policyno",insPlyIncomeVo.getPolicyno());
  168. }
  169. List<InsPlyIncome> list= this.baseMapper.queryNew(params);
  170. pageHelper=new PageInfo(list);
  171. BasePageResult<InsPlyIncome> pageResult=new BasePageResult<InsPlyIncome>(insPlyIncomeVo.getPageNum(), insPlyIncomeVo.getPageSize(), pageHelper.getTotal(), pageHelper.getPages(), list);
  172. return HttpResult.ok(pageResult);
  173. }catch (Exception e){
  174. e.printStackTrace();
  175. return HttpResult.error("查询异常");
  176. }
  177. }
  178. @Override
  179. @Transactional
  180. public void uodateBalance(InsPlyIncomeVo insPlyIncomeVo) {
  181. InsPlyIncome insPlyIncome = new InsPlyIncome();
  182. insPlyIncomeVo.setHandlingFeesStatus("1");
  183. BeanUtils.copyProperties(insPlyIncomeVo, insPlyIncome);
  184. baseMapper.uopdateByOrderno(insPlyIncome);
  185. // 生成记录
  186. SettlementDocumentaryLogEntity settlementDocumentaryLogEntity = new SettlementDocumentaryLogEntity();
  187. if(StringUtils.isNotEmpty(insPlyIncomeVo.getAgreementType()) && "1".equals(insPlyIncomeVo.getAgreementType())){
  188. // 只有平台协议 才会跟单费一笔结算
  189. settlementDocumentaryLogEntity.setAmount(insPlyIncomeVo.getOtherFeevalue()); // 跟单费金额
  190. }
  191. settlementDocumentaryLogEntity.setHandingAmount(insPlyIncomeVo.getCommissionFeevalue()); // 手续费金额
  192. settlementDocumentaryLogEntity.setSettlementDocumentaryId(insPlyIncomeVo.getOrderno()); // 关联id
  193. settlementDocumentaryLogEntity.setAmountType("1"); //手续费结算
  194. settlementDocumentaryLogEntity.setCreateTime(new Date());
  195. settlementDocumentaryLogService.save(settlementDocumentaryLogEntity);
  196. }
  197. @Override
  198. @Transactional
  199. public HttpResult<BasePageResult<InsPlyIncome>> totalQuery(InsPlyIncomeVo insPlyIncomeVo) {
  200. HashMap<String, Object> params = new HashMap<>();
  201. PageInfo<List<InsPlyIncome>> pageHelper = null;
  202. params.put("startDate",insPlyIncomeVo.getStartDate().toString());
  203. params.put("endDate",insPlyIncomeVo.getEndDate().toString());
  204. // 判断非跟单结算日期是否已经存在
  205. String resultDate = this.JudgingDate(params);
  206. if(StringUtils.isNotEmpty(resultDate)){
  207. return HttpResult.error("该时间段已近有单据做过结算,请结算");
  208. }
  209. params.put("pageNum",insPlyIncomeVo.getPageNum());
  210. params.put("pageSize",insPlyIncomeVo.getPageSize());
  211. List<InsPlyIncome> list= this.baseMapper.totalQuery(params);
  212. pageHelper=new PageInfo(list);
  213. return HttpResult.ok(new BasePageResult<>(insPlyIncomeVo.getPageNum(), insPlyIncomeVo.getPageSize(), pageHelper.getTotal(), pageHelper.getPages(), list));
  214. }
  215. private String JudgingDate(HashMap<String, Object> params) {
  216. if(ObjectUtils.isNotEmpty(params.get("startDate")) && ObjectUtils.isNotEmpty(params.get("endDate")) ){
  217. String checkResult = settlementDocumentaryFeesService.checkDate(params);
  218. if(StringUtils.isNotEmpty(checkResult)){
  219. return checkResult;
  220. }
  221. }
  222. return null;
  223. }
  224. @Override
  225. @Transactional
  226. public HttpResult batchBalance(InsPlyIncomeVo insPlyIncomeVo) {
  227. // 生成新数据
  228. SettlementDocumentaryFeesVo settlementDocumentaryFeesVo = new SettlementDocumentaryFeesVo();
  229. settlementDocumentaryFeesVo.setTotalAmount(insPlyIncomeVo.getTotAmount());
  230. settlementDocumentaryFeesVo.setTotalOrders(insPlyIncomeVo.getTotalorders());
  231. settlementDocumentaryFeesVo.setTimeFrame(insPlyIncomeVo.getTimeFrame());
  232. settlementDocumentaryFeesVo.setCreateTime(LocalDateTime.now());
  233. settlementDocumentaryFeesVo.setStartTime(insPlyIncomeVo.getStartDate().atStartOfDay());
  234. settlementDocumentaryFeesVo.setEndTime(insPlyIncomeVo.getEndDate().atStartOfDay());
  235. settlementDocumentaryFeesVo.setSettlementAmount(insPlyIncomeVo.getSettlementAmount());// 结算金额
  236. settlementDocumentaryFeesVo.setUnsettledAmount(insPlyIncomeVo.getTotAmount().subtract(insPlyIncomeVo.getSettlementAmount())); //未结算金额
  237. return settlementDocumentaryFeesService.inertSettle(settlementDocumentaryFeesVo);
  238. //
  239. }
  240. }