InsPlySettleServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. package com.ydtech.modules.fnc.service.impl;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.github.pagehelper.PageHelper;
  7. import com.github.pagehelper.PageInfo;
  8. import com.ydtech.core.page.HttpResult;
  9. import com.ydtech.exception.SystemException;
  10. import com.ydtech.modules.admin.model.SysUser;
  11. import com.ydtech.modules.esm.model.EsmInsCompany;
  12. import com.ydtech.modules.esm.service.EsmInsCompanyService;
  13. import com.ydtech.modules.fnc.dao.InsPlyIncomeMapper;
  14. import com.ydtech.modules.fnc.dao.InsPlySettleDetailMapper;
  15. import com.ydtech.modules.fnc.dao.InsPlySettleHxMapper;
  16. import com.ydtech.modules.fnc.entity.InsPlyIncome;
  17. import com.ydtech.modules.fnc.entity.InsPlySettle;
  18. import com.ydtech.modules.fnc.dao.InsPlySettleMapper;
  19. import com.ydtech.modules.fnc.entity.InsPlySettleDetail;
  20. import com.ydtech.modules.fnc.entity.InsPlySettleHx;
  21. import com.ydtech.modules.fnc.service.InsPlySettleService;
  22. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  23. import com.ydtech.modules.fnc.vo.GenerateSettleVo;
  24. import com.ydtech.modules.fnc.vo.InsPlyIncomeVo;
  25. import com.ydtech.modules.fnc.vo.InsPlySettleVo;
  26. import com.ydtech.modules.inv.utils.EasyExcelUtils;
  27. import com.ydtech.modules.order.entity.BasePageResult;
  28. import com.ydtech.modules.order.entity.InsOrders;
  29. import com.ydtech.modules.order.entity.vo.PolicyInsuranceReportReqVo;
  30. import com.ydtech.modules.order.entity.vo.PolicyInsuranceReportResVo;
  31. import com.ydtech.modules.report.model.vo.InsPlySettleReportReqVo;
  32. import com.ydtech.modules.report.model.vo.InsPlySettleReportResVo;
  33. import com.ydtech.modules.report.model.vo.InsPlySettleStatisticsListResVo;
  34. import com.ydtech.utils.DateUtil;
  35. import com.ydtech.utils.StringUtils;
  36. import org.springframework.beans.factory.annotation.Autowired;
  37. import org.springframework.beans.factory.annotation.Value;
  38. import org.springframework.stereotype.Service;
  39. import org.springframework.transaction.annotation.Transactional;
  40. import java.math.BigDecimal;
  41. import java.sql.Wrapper;
  42. import java.time.LocalDate;
  43. import java.time.LocalDateTime;
  44. import java.time.YearMonth;
  45. import java.util.ArrayList;
  46. import java.util.Date;
  47. import java.util.List;
  48. /**
  49. * <p>
  50. * 月度结算单汇总表 服务实现类
  51. * </p>
  52. *
  53. * @author gws
  54. * @since 2024-01-10
  55. */
  56. @Service
  57. public class InsPlySettleServiceImpl extends ServiceImpl<InsPlySettleMapper, InsPlySettle> implements InsPlySettleService {
  58. @Value("${upload.file.path}")
  59. private String uploadFilePath;
  60. @Autowired
  61. private InsPlySettleDetailMapper insPlySettleDetailMapper;
  62. @Autowired
  63. private InsPlyIncomeMapper insPlyIncomeMapper;
  64. @Autowired
  65. private InsPlySettleHxMapper insPlySettleHxMapper;
  66. @Autowired
  67. private EsmInsCompanyService esmInsCompanyService;
  68. @Override
  69. @Transactional
  70. public HttpResult generateSettleAndDetail(GenerateSettleVo generateSettleVo) {
  71. // 保单号数组
  72. List<String> plyNoList= generateSettleVo.getPlyNoList();
  73. // 保险公司
  74. String companyId=generateSettleVo.getCompanyId();
  75. String deptId =generateSettleVo.getDeptId();
  76. // 结算月份
  77. String settleMonth=generateSettleVo.getSettleMonth();
  78. if(StringUtils.isNullOrEmpty(companyId)
  79. ||StringUtils.isNullOrEmpty(deptId)
  80. ||StringUtils.isNullOrEmpty(settleMonth)){
  81. return HttpResult.error("保险公司、机构、结算月份不能为空");
  82. }
  83. String settleno = "";
  84. BigDecimal commissionFeevalue=new BigDecimal(0);
  85. BigDecimal otherFeevalue=new BigDecimal(0);
  86. BigDecimal allFeeValue=new BigDecimal(0);
  87. if(plyNoList==null || plyNoList.size()==0){
  88. return HttpResult.error("结算清单不能传空");
  89. }
  90. // 生成结算单号
  91. // InsPlyIncome insPlyIncome_t=insPlyIncomeMapper.selectByPlyno(plyNoList.get(0));
  92. settleno = companyId+ DateUtil.yyyyMMddHHmmss(new Date());
  93. for(String plyNo:plyNoList){
  94. InsPlyIncome insPlyIncome=insPlyIncomeMapper.selectByPlyno(plyNo);
  95. InsPlySettleDetail insPlySettleDetail =new InsPlySettleDetail();
  96. insPlySettleDetail.setSettleno(settleno);
  97. // 订单号
  98. insPlySettleDetail.setOrderno(insPlyIncome.getOrderno());
  99. // 保单号
  100. insPlySettleDetail.setPolicyno(insPlyIncome.getPolicyno());
  101. insPlySettleDetail.setDeptId(insPlyIncome.getDeptId());
  102. // 手续费
  103. insPlySettleDetail.setCommissionFeevalue(insPlyIncome.getCommissionFeevalue());
  104. // 其他费用金额
  105. insPlySettleDetail.setOtherFeevalue(insPlyIncome.getOtherFeevalue());
  106. // 总费用
  107. insPlySettleDetail.setAllFeeValue(insPlyIncome.getAllFeeValue());
  108. insPlySettleDetail.setCreateTime(LocalDateTime.now());
  109. insPlySettleDetailMapper.insert(insPlySettleDetail);
  110. //修改ins_ply_income 将总费用转成计算费用
  111. insPlyIncome.setFinalFeeValue(insPlyIncome.getAllFeeValue());
  112. LambdaQueryWrapper<InsPlyIncome> queryWrapper=new LambdaQueryWrapper();
  113. queryWrapper.eq(InsPlyIncome::getPolicyno,plyNo);
  114. insPlyIncomeMapper.update(insPlyIncome,queryWrapper);
  115. // 手续费金额
  116. commissionFeevalue=commissionFeevalue.add(insPlyIncome.getCommissionFeevalue());
  117. // 其他费用金额
  118. otherFeevalue=otherFeevalue.add(insPlyIncome.getOtherFeevalue());
  119. // 总费用
  120. allFeeValue=allFeeValue.add(insPlyIncome.getAllFeeValue());
  121. }
  122. // 月度结算单汇总表
  123. InsPlySettle insPlySettle =new InsPlySettle();
  124. insPlySettle.setSettleno(settleno);
  125. insPlySettle.setCompanyId(companyId);
  126. insPlySettle.setDeptId(deptId);
  127. insPlySettle.setSettleMonth(settleMonth);
  128. String settleBatch =this.baseMapper.getMaxBatch(insPlySettle);
  129. if(StringUtils.isNullOrEmpty(settleBatch)){
  130. settleBatch="1";
  131. }else{
  132. settleBatch=String.valueOf(Integer.parseInt(settleBatch)+1);
  133. }
  134. insPlySettle.setSettleBatch(settleBatch);
  135. insPlySettle.setCommissionFeevalue(commissionFeevalue);
  136. insPlySettle.setOtherFeevalue(otherFeevalue);
  137. insPlySettle.setAllFeeValue(allFeeValue);
  138. insPlySettle.setOperator(StpUtil.getLoginId().toString());
  139. insPlySettle.setCreateTime(LocalDateTime.now());
  140. insPlySettle.setStatus("0");
  141. this.baseMapper.insert(insPlySettle);
  142. return HttpResult.ok(settleno);
  143. }
  144. @Override
  145. public HttpResult<BasePageResult<InsPlySettle>> selectSettle(InsPlySettleVo insPlySettleVo) {
  146. try{
  147. Page<InsOrders> page = new Page<>(insPlySettleVo.getPageNum(), insPlySettleVo.getPageSize());
  148. PageInfo<List<InsPlyIncome>> pageHelper = null;
  149. PageHelper.startPage(insPlySettleVo.getPageNum(),insPlySettleVo.getPageSize());
  150. List<InsPlySettle> list= this.baseMapper.selectByCondition(insPlySettleVo);
  151. pageHelper=new PageInfo(list);
  152. BasePageResult<InsPlySettle> pageResult=new BasePageResult<InsPlySettle>(insPlySettleVo.getPageNum(), insPlySettleVo.getPageSize(), pageHelper.getTotal(), pageHelper.getPages(), list);
  153. return HttpResult.ok(pageResult);
  154. }catch (Exception e){
  155. e.printStackTrace();
  156. return HttpResult.error("查询异常");
  157. }
  158. }
  159. @Override
  160. public HttpResult<BasePageResult<InsPlyIncome>> selectSettleDetail(InsPlySettleVo insPlySettleVo) {
  161. try{
  162. Page<InsOrders> page = new Page<>(insPlySettleVo.getPageNum(), insPlySettleVo.getPageSize());
  163. PageInfo<List<InsPlyIncome>> pageHelper = null;
  164. PageHelper.startPage(insPlySettleVo.getPageNum(),insPlySettleVo.getPageSize());
  165. List<InsPlyIncome> list= insPlyIncomeMapper.selectSettleByCondition(insPlySettleVo);
  166. pageHelper=new PageInfo(list);
  167. BasePageResult<InsPlyIncome> pageResult=new BasePageResult(insPlySettleVo.getPageNum(), insPlySettleVo.getPageSize(), pageHelper.getTotal(), pageHelper.getPages(), list);
  168. return HttpResult.ok(pageResult);
  169. }catch (Exception e){
  170. e.printStackTrace();
  171. return HttpResult.error("查询异常");
  172. }
  173. }
  174. @Override
  175. public HttpResult hxSettle(InsPlySettleHx insPlySettleHx) {
  176. //必填校验
  177. if(StringUtils.isNullOrEmpty(insPlySettleHx.getSettleno())
  178. ||StringUtils.isNullOrEmpty(insPlySettleHx.getFeeType())
  179. ||StringUtils.isNullOrEmpty(insPlySettleHx.getCheckno())
  180. ||StringUtils.isNullOrEmpty(insPlySettleHx.getAmount())){
  181. return HttpResult.error("结算单号、费用类型、核销金额、发票号不能为空");
  182. }
  183. //核销金额不能超总费用
  184. InsPlySettle insPlySettle=this.baseMapper.selectBySettleno(insPlySettleHx.getSettleno());
  185. insPlySettle.setHxamount(insPlySettle.getHxamount() ==null ? BigDecimal.ZERO:insPlySettle.getHxamount());
  186. insPlySettle.setCommissionHxamount(insPlySettle.getCommissionHxamount() ==null ? BigDecimal.ZERO:insPlySettle.getCommissionHxamount());
  187. insPlySettle.setOtherHxamount(insPlySettle.getOtherHxamount() ==null ? BigDecimal.ZERO:insPlySettle.getOtherHxamount());
  188. // 如果等于手续费时 核销金额 比 手续费 >0
  189. if(insPlySettleHx.getFeeType().equals("S")
  190. &&insPlySettle.getCommissionHxamount().add(insPlySettleHx.getAmount()).compareTo(insPlySettle.getCommissionFeevalue())>0){
  191. return HttpResult.error("核销手续费金额已超过结算单手续费,请修改");
  192. }
  193. // 其他费用 核销金额 比 其他费用
  194. if(insPlySettleHx.getFeeType().equals("O")
  195. &&insPlySettle.getOtherHxamount().add(insPlySettleHx.getAmount()).compareTo(insPlySettle.getOtherFeevalue())>0){
  196. return HttpResult.error("核销其他费用金额已超过结算单其他费用,请修改");
  197. }
  198. //1.插入核销记录表
  199. insPlySettleHx.setOperator(StpUtil.getLoginId().toString());
  200. insPlySettleHx.setCreateTime(LocalDateTime.now());
  201. insPlySettleHxMapper.insert(insPlySettleHx);
  202. //2.修改核销状态 状态(0-未核销 1-部分核销 2-已核销)
  203. InsPlySettle insPlySettle_status=new InsPlySettle();
  204. if(insPlySettle.getHxamount().add(insPlySettleHx.getAmount()).compareTo(insPlySettle.getAllFeeValue())==0){
  205. insPlySettle_status.setStatus("2");
  206. }else{
  207. insPlySettle_status.setStatus("1");
  208. }
  209. this.baseMapper.update(insPlySettle_status,new QueryWrapper<InsPlySettle>().eq("settleno",insPlySettleHx.getSettleno()));
  210. return HttpResult.ok("核销成功");
  211. }
  212. @Override
  213. public HttpResult queryHxDetail(String settleno) {
  214. LambdaQueryWrapper<InsPlySettleHx> queryWrapper=new LambdaQueryWrapper();
  215. queryWrapper.eq(InsPlySettleHx::getSettleno,settleno);
  216. List<InsPlySettleHx> list=insPlySettleHxMapper.selectList(queryWrapper);
  217. return HttpResult.ok(list);
  218. }
  219. @Override
  220. public BasePageResult<InsPlySettleReportResVo> getInsPlySettleReport( InsPlySettleReportReqVo insPlySettleReportReqVo) {
  221. Page<InsOrders> page = new Page<>(insPlySettleReportReqVo.getPageNum(), insPlySettleReportReqVo.getPageSize());
  222. page.setSearchCount(false).setOptimizeCountSql(false);//关闭count查询
  223. if(StringUtils.isNotEmpty(insPlySettleReportReqVo.getIsReturnBack())&&"1".equals(insPlySettleReportReqVo.getIsReturnBack())){
  224. //如果是返回上级时,找上级的上级往下找
  225. LambdaQueryWrapper<EsmInsCompany> lqw1 = new LambdaQueryWrapper<>();
  226. lqw1.eq(EsmInsCompany::getId, insPlySettleReportReqVo.getCompanyId());
  227. EsmInsCompany esmInsCompany1=esmInsCompanyService.getOne(lqw1);
  228. if(esmInsCompany1==null){
  229. throw new SystemException("已经是最顶层机构,无法继续返回!");
  230. }
  231. LambdaQueryWrapper<EsmInsCompany> lqw2 = new LambdaQueryWrapper<>();
  232. lqw2.eq(EsmInsCompany::getId, esmInsCompany1.getParentId());
  233. EsmInsCompany esmInsCompany2=esmInsCompanyService.getOne(lqw2);
  234. if(esmInsCompany2==null){
  235. insPlySettleReportReqVo.setCompanyId(esmInsCompany1.getParentId());
  236. }else{
  237. insPlySettleReportReqVo.setCompanyId(esmInsCompany2.getParentId());
  238. }
  239. }
  240. //正常查询,下钻查询时调整
  241. List<String> companyArr=esmInsCompanyService.getInsCompanyNextLevel(insPlySettleReportReqVo.getCompanyId());
  242. if(!(companyArr!=null&&companyArr.size()>0)){
  243. throw new SystemException("已经是最底层机构,无法继续下钻!");
  244. }
  245. insPlySettleReportReqVo.setGroupInsCompanyArr(companyArr);
  246. Page<InsPlySettleReportResVo> policyInsuranceReportResVoPage = baseMapper.getInsPlySettleReport(page,
  247. insPlySettleReportReqVo);
  248. if(policyInsuranceReportResVoPage.getRecords().size()==0){
  249. if(StringUtils.isNotEmpty(insPlySettleReportReqVo.getIsDrilling())&&"1".equals(insPlySettleReportReqVo.getIsDrilling())){
  250. throw new SystemException("已经是最底层机构,无法继续下钻!");
  251. }
  252. }
  253. policyInsuranceReportResVoPage.getRecords().stream().forEach(item->{
  254. EsmInsCompany esmInsCompany =esmInsCompanyService.getById(item.getCompanyId());
  255. item.setCompanyName(esmInsCompany.getName());
  256. if(StringUtils.isEmpty(item.getStatus())){
  257. item.setStatus("全部");
  258. }else{
  259. if("0".equals(item.getStatus())){
  260. item.setStatus("未核销");
  261. }else if("1".equals(item.getStatus())){
  262. item.setStatus("部分核销");
  263. }else if ("2".equals(item.getStatus())){
  264. item.setStatus("已核销");
  265. }
  266. }
  267. });
  268. Long total = baseMapper.getInsPlySettleReportCount(insPlySettleReportReqVo);
  269. policyInsuranceReportResVoPage.setTotal(total);
  270. return new BasePageResult<>(insPlySettleReportReqVo.getPageNum(), insPlySettleReportReqVo.getPageSize(),
  271. policyInsuranceReportResVoPage.getTotal(), policyInsuranceReportResVoPage.getPages()
  272. , policyInsuranceReportResVoPage.getRecords());
  273. }
  274. @Override
  275. public HttpResult<String> exportInsPlySettleReport(InsPlySettleReportReqVo insPlySettleReportReqVo) {
  276. if(StringUtils.isNotEmpty(insPlySettleReportReqVo.getIsReturnBack())&&"1".equals(insPlySettleReportReqVo.getIsReturnBack())){
  277. //如果是返回上级时,找上级的上级往下找
  278. LambdaQueryWrapper<EsmInsCompany> lqw1 = new LambdaQueryWrapper<>();
  279. lqw1.eq(EsmInsCompany::getId, insPlySettleReportReqVo.getCompanyId());
  280. EsmInsCompany esmInsCompany1=esmInsCompanyService.getOne(lqw1);
  281. if(esmInsCompany1==null){
  282. throw new SystemException("已经是最顶层机构,无法继续返回!");
  283. }
  284. LambdaQueryWrapper<EsmInsCompany> lqw2 = new LambdaQueryWrapper<>();
  285. lqw2.eq(EsmInsCompany::getId, esmInsCompany1.getParentId());
  286. EsmInsCompany esmInsCompany2=esmInsCompanyService.getOne(lqw2);
  287. if(esmInsCompany2==null){
  288. insPlySettleReportReqVo.setCompanyId(esmInsCompany1.getParentId());
  289. }else{
  290. insPlySettleReportReqVo.setCompanyId(esmInsCompany2.getParentId());
  291. }
  292. }
  293. //正常查询,下钻查询时调整
  294. List<String> companyArr=esmInsCompanyService.getInsCompanyNextLevel(insPlySettleReportReqVo.getCompanyId());
  295. if(!(companyArr!=null&&companyArr.size()>0)){
  296. throw new SystemException("已经是最底层机构,无法继续下钻!");
  297. }
  298. insPlySettleReportReqVo.setGroupInsCompanyArr(companyArr);
  299. List<InsPlySettleReportResVo> insPlySettleReportResVoList = baseMapper.getInsPlySettleReport(insPlySettleReportReqVo);
  300. if(insPlySettleReportResVoList.size()==0){
  301. if(StringUtils.isNotEmpty(insPlySettleReportReqVo.getIsDrilling())&&"1".equals(insPlySettleReportReqVo.getIsDrilling())){
  302. throw new SystemException("已经是最底层机构,无法继续下钻!");
  303. }
  304. }
  305. insPlySettleReportResVoList.stream().forEach(item->{
  306. EsmInsCompany esmInsCompany =esmInsCompanyService.getById(item.getCompanyId());
  307. item.setCompanyName(esmInsCompany.getName());
  308. if(StringUtils.isEmpty(item.getStatus())){
  309. item.setStatus("全部");
  310. }else{
  311. if("0".equals(item.getStatus())){
  312. item.setStatus("未核销");
  313. }else if("1".equals(item.getStatus())){
  314. item.setStatus("部分核销");
  315. }else if ("2".equals(item.getStatus())){
  316. item.setStatus("已核销");
  317. }
  318. }
  319. });
  320. String s = EasyExcelUtils.downExcel(uploadFilePath, InsPlySettleReportResVo.class, insPlySettleReportResVoList);
  321. return HttpResult.ok("", s);
  322. }
  323. @Override
  324. public HttpResult<String> exportInsPlySettleReportList(InsPlySettleReportReqVo insPlySettleReportReqVo) {
  325. List<InsPlySettleStatisticsListResVo> insPlySettleReportList = baseMapper.getInsPlySettleReportList(insPlySettleReportReqVo);
  326. insPlySettleReportList.stream().forEach(item->{
  327. if("0".equals(item.getStatus())){
  328. item.setStatus("未核销");
  329. }else if("1".equals(item.getStatus())){
  330. item.setStatus("部分核销");
  331. }else if ("2".equals(item.getStatus())){
  332. item.setStatus("已核销");
  333. }
  334. });
  335. String s = EasyExcelUtils.downExcel(uploadFilePath, InsPlySettleStatisticsListResVo.class, insPlySettleReportList);
  336. return HttpResult.ok("", s);
  337. }
  338. }