InsPlySettleServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. List<String> plyNoList= generateSettleVo.getPlyNoList();
  72. String companyId=generateSettleVo.getCompanyId();
  73. String deptId =generateSettleVo.getDeptId();
  74. String settleMonth=generateSettleVo.getSettleMonth();
  75. if(StringUtils.isNullOrEmpty(companyId)
  76. ||StringUtils.isNullOrEmpty(deptId)
  77. ||StringUtils.isNullOrEmpty(settleMonth)){
  78. return HttpResult.error("保险公司、机构、结算月份不能为空");
  79. }
  80. String settleno = "";
  81. BigDecimal commissionFeevalue=new BigDecimal(0);
  82. BigDecimal otherFeevalue=new BigDecimal(0);
  83. BigDecimal allFeeValue=new BigDecimal(0);
  84. if(plyNoList==null || plyNoList.size()==0){
  85. return HttpResult.error("结算清单不能传空");
  86. }
  87. // 生成结算单号
  88. // InsPlyIncome insPlyIncome_t=insPlyIncomeMapper.selectByPlyno(plyNoList.get(0));
  89. settleno = companyId+ DateUtil.yyyyMMddHHmmss(new Date());
  90. for(String plyNo:plyNoList){
  91. InsPlyIncome insPlyIncome=insPlyIncomeMapper.selectByPlyno(plyNo);
  92. InsPlySettleDetail insPlySettleDetail =new InsPlySettleDetail();
  93. insPlySettleDetail.setSettleno(settleno);
  94. insPlySettleDetail.setOrderno(insPlyIncome.getOrderno());
  95. insPlySettleDetail.setPolicyno(insPlyIncome.getPolicyno());
  96. insPlySettleDetail.setDeptId(insPlyIncome.getDeptId());
  97. insPlySettleDetail.setCommissionFeevalue(insPlyIncome.getCommissionFeevalue());
  98. insPlySettleDetail.setOtherFeevalue(insPlyIncome.getOtherFeevalue());
  99. insPlySettleDetail.setAllFeeValue(insPlyIncome.getAllFeeValue());
  100. insPlySettleDetail.setCreateTime(LocalDateTime.now());
  101. insPlySettleDetailMapper.insert(insPlySettleDetail);
  102. //修改ins_ply_income
  103. insPlyIncome.setFinalFeeValue(insPlyIncome.getAllFeeValue());
  104. LambdaQueryWrapper<InsPlyIncome> queryWrapper=new LambdaQueryWrapper();
  105. queryWrapper.eq(InsPlyIncome::getPolicyno,plyNo);
  106. insPlyIncomeMapper.update(insPlyIncome,queryWrapper);
  107. commissionFeevalue=commissionFeevalue.add(insPlyIncome.getCommissionFeevalue());
  108. otherFeevalue=otherFeevalue.add(insPlyIncome.getOtherFeevalue());
  109. allFeeValue=allFeeValue.add(insPlyIncome.getAllFeeValue());
  110. }
  111. InsPlySettle insPlySettle =new InsPlySettle();
  112. insPlySettle.setSettleno(settleno);
  113. insPlySettle.setCompanyId(companyId);
  114. insPlySettle.setDeptId(deptId);
  115. insPlySettle.setSettleMonth(settleMonth);
  116. String settleBatch =this.baseMapper.getMaxBatch(insPlySettle);
  117. if(StringUtils.isNullOrEmpty(settleBatch)){
  118. settleBatch="1";
  119. }else{
  120. settleBatch=String.valueOf(Integer.parseInt(settleBatch)+1);
  121. }
  122. insPlySettle.setSettleBatch(settleBatch);
  123. insPlySettle.setCommissionFeevalue(commissionFeevalue);
  124. insPlySettle.setOtherFeevalue(otherFeevalue);
  125. insPlySettle.setAllFeeValue(allFeeValue);
  126. insPlySettle.setOperator(StpUtil.getLoginId().toString());
  127. insPlySettle.setCreateTime(LocalDateTime.now());
  128. insPlySettle.setStatus("0");
  129. this.baseMapper.insert(insPlySettle);
  130. return HttpResult.ok(settleno);
  131. }
  132. @Override
  133. public HttpResult<BasePageResult<InsPlySettle>> selectSettle(InsPlySettleVo insPlySettleVo) {
  134. try{
  135. Page<InsOrders> page = new Page<>(insPlySettleVo.getPageNum(), insPlySettleVo.getPageSize());
  136. PageInfo<List<InsPlyIncome>> pageHelper = null;
  137. PageHelper.startPage(insPlySettleVo.getPageNum(),insPlySettleVo.getPageSize());
  138. List<InsPlySettle> list= this.baseMapper.selectByCondition(insPlySettleVo);
  139. pageHelper=new PageInfo(list);
  140. BasePageResult<InsPlySettle> pageResult=new BasePageResult<InsPlySettle>(insPlySettleVo.getPageNum(), insPlySettleVo.getPageSize(), pageHelper.getTotal(), pageHelper.getPages(), list);
  141. return HttpResult.ok(pageResult);
  142. }catch (Exception e){
  143. e.printStackTrace();
  144. return HttpResult.error("查询异常");
  145. }
  146. }
  147. @Override
  148. public HttpResult<BasePageResult<InsPlyIncome>> selectSettleDetail(InsPlySettleVo insPlySettleVo) {
  149. try{
  150. Page<InsOrders> page = new Page<>(insPlySettleVo.getPageNum(), insPlySettleVo.getPageSize());
  151. PageInfo<List<InsPlyIncome>> pageHelper = null;
  152. PageHelper.startPage(insPlySettleVo.getPageNum(),insPlySettleVo.getPageSize());
  153. List<InsPlyIncome> list= insPlyIncomeMapper.selectSettleByCondition(insPlySettleVo);
  154. pageHelper=new PageInfo(list);
  155. BasePageResult<InsPlyIncome> pageResult=new BasePageResult(insPlySettleVo.getPageNum(), insPlySettleVo.getPageSize(), pageHelper.getTotal(), pageHelper.getPages(), list);
  156. return HttpResult.ok(pageResult);
  157. }catch (Exception e){
  158. e.printStackTrace();
  159. return HttpResult.error("查询异常");
  160. }
  161. }
  162. @Override
  163. public HttpResult hxSettle(InsPlySettleHx insPlySettleHx) {
  164. //必填校验
  165. if(StringUtils.isNullOrEmpty(insPlySettleHx.getSettleno())
  166. ||StringUtils.isNullOrEmpty(insPlySettleHx.getFeeType())
  167. ||StringUtils.isNullOrEmpty(insPlySettleHx.getCheckno())
  168. ||StringUtils.isNullOrEmpty(insPlySettleHx.getAmount())){
  169. return HttpResult.error("结算单号、费用类型、核销金额、发票号不能为空");
  170. }
  171. //核销金额不能超总费用
  172. InsPlySettle insPlySettle=this.baseMapper.selectBySettleno(insPlySettleHx.getSettleno());
  173. insPlySettle.setHxamount(insPlySettle.getHxamount() ==null ? BigDecimal.ZERO:insPlySettle.getHxamount());
  174. insPlySettle.setCommissionHxamount(insPlySettle.getCommissionHxamount() ==null ? BigDecimal.ZERO:insPlySettle.getCommissionHxamount());
  175. insPlySettle.setOtherHxamount(insPlySettle.getOtherHxamount() ==null ? BigDecimal.ZERO:insPlySettle.getOtherHxamount());
  176. if(insPlySettleHx.getFeeType().equals("S")
  177. &&insPlySettle.getCommissionHxamount().add(insPlySettleHx.getAmount()).compareTo(insPlySettle.getCommissionFeevalue())>0){
  178. return HttpResult.error("核销手续费金额已超过结算单手续费,请修改");
  179. }
  180. if(insPlySettleHx.getFeeType().equals("O")
  181. &&insPlySettle.getOtherHxamount().add(insPlySettleHx.getAmount()).compareTo(insPlySettle.getOtherFeevalue())>0){
  182. return HttpResult.error("核销其他费用金额已超过结算单其他费用,请修改");
  183. }
  184. //1.插入核销记录表
  185. insPlySettleHx.setOperator(StpUtil.getLoginId().toString());
  186. insPlySettleHx.setCreateTime(LocalDateTime.now());
  187. insPlySettleHxMapper.insert(insPlySettleHx);
  188. //2.修改核销状态 状态(0-未核销 1-部分核销 2-已核销)
  189. InsPlySettle insPlySettle_status=new InsPlySettle();
  190. if(insPlySettle.getHxamount().add(insPlySettleHx.getAmount()).compareTo(insPlySettle.getAllFeeValue())==0){
  191. insPlySettle_status.setStatus("2");
  192. }else{
  193. insPlySettle_status.setStatus("1");
  194. }
  195. this.baseMapper.update(insPlySettle_status,new QueryWrapper<InsPlySettle>().eq("settleno",insPlySettleHx.getSettleno()));
  196. return HttpResult.ok("核销成功");
  197. }
  198. @Override
  199. public HttpResult queryHxDetail(String settleno) {
  200. LambdaQueryWrapper<InsPlySettleHx> queryWrapper=new LambdaQueryWrapper();
  201. queryWrapper.eq(InsPlySettleHx::getSettleno,settleno);
  202. List<InsPlySettleHx> list=insPlySettleHxMapper.selectList(queryWrapper);
  203. return HttpResult.ok(list);
  204. }
  205. @Override
  206. public BasePageResult<InsPlySettleReportResVo> getInsPlySettleReport( InsPlySettleReportReqVo insPlySettleReportReqVo) {
  207. Page<InsOrders> page = new Page<>(insPlySettleReportReqVo.getPageNum(), insPlySettleReportReqVo.getPageSize());
  208. page.setSearchCount(false).setOptimizeCountSql(false);//关闭count查询
  209. if(StringUtils.isNotEmpty(insPlySettleReportReqVo.getIsReturnBack())&&"1".equals(insPlySettleReportReqVo.getIsReturnBack())){
  210. //如果是返回上级时,找上级的上级往下找
  211. LambdaQueryWrapper<EsmInsCompany> lqw1 = new LambdaQueryWrapper<>();
  212. lqw1.eq(EsmInsCompany::getId, insPlySettleReportReqVo.getCompanyId());
  213. EsmInsCompany esmInsCompany1=esmInsCompanyService.getOne(lqw1);
  214. if(esmInsCompany1==null){
  215. throw new SystemException("已经是最顶层机构,无法继续返回!");
  216. }
  217. LambdaQueryWrapper<EsmInsCompany> lqw2 = new LambdaQueryWrapper<>();
  218. lqw2.eq(EsmInsCompany::getId, esmInsCompany1.getParentId());
  219. EsmInsCompany esmInsCompany2=esmInsCompanyService.getOne(lqw2);
  220. if(esmInsCompany2==null){
  221. insPlySettleReportReqVo.setCompanyId(esmInsCompany1.getParentId());
  222. }else{
  223. insPlySettleReportReqVo.setCompanyId(esmInsCompany2.getParentId());
  224. }
  225. }
  226. //正常查询,下钻查询时调整
  227. List<String> companyArr=esmInsCompanyService.getInsCompanyNextLevel(insPlySettleReportReqVo.getCompanyId());
  228. if(!(companyArr!=null&&companyArr.size()>0)){
  229. throw new SystemException("已经是最底层机构,无法继续下钻!");
  230. }
  231. insPlySettleReportReqVo.setGroupInsCompanyArr(companyArr);
  232. Page<InsPlySettleReportResVo> policyInsuranceReportResVoPage = baseMapper.getInsPlySettleReport(page,
  233. insPlySettleReportReqVo);
  234. if(policyInsuranceReportResVoPage.getRecords().size()==0){
  235. if(StringUtils.isNotEmpty(insPlySettleReportReqVo.getIsDrilling())&&"1".equals(insPlySettleReportReqVo.getIsDrilling())){
  236. throw new SystemException("已经是最底层机构,无法继续下钻!");
  237. }
  238. }
  239. policyInsuranceReportResVoPage.getRecords().stream().forEach(item->{
  240. EsmInsCompany esmInsCompany =esmInsCompanyService.getById(item.getCompanyId());
  241. item.setCompanyName(esmInsCompany.getName());
  242. if(StringUtils.isEmpty(item.getStatus())){
  243. item.setStatus("全部");
  244. }else{
  245. if("0".equals(item.getStatus())){
  246. item.setStatus("未核销");
  247. }else if("1".equals(item.getStatus())){
  248. item.setStatus("部分核销");
  249. }else if ("2".equals(item.getStatus())){
  250. item.setStatus("已核销");
  251. }
  252. }
  253. });
  254. Long total = baseMapper.getInsPlySettleReportCount(insPlySettleReportReqVo);
  255. policyInsuranceReportResVoPage.setTotal(total);
  256. return new BasePageResult<>(insPlySettleReportReqVo.getPageNum(), insPlySettleReportReqVo.getPageSize(),
  257. policyInsuranceReportResVoPage.getTotal(), policyInsuranceReportResVoPage.getPages()
  258. , policyInsuranceReportResVoPage.getRecords());
  259. }
  260. @Override
  261. public HttpResult<String> exportInsPlySettleReport(InsPlySettleReportReqVo insPlySettleReportReqVo) {
  262. if(StringUtils.isNotEmpty(insPlySettleReportReqVo.getIsReturnBack())&&"1".equals(insPlySettleReportReqVo.getIsReturnBack())){
  263. //如果是返回上级时,找上级的上级往下找
  264. LambdaQueryWrapper<EsmInsCompany> lqw1 = new LambdaQueryWrapper<>();
  265. lqw1.eq(EsmInsCompany::getId, insPlySettleReportReqVo.getCompanyId());
  266. EsmInsCompany esmInsCompany1=esmInsCompanyService.getOne(lqw1);
  267. if(esmInsCompany1==null){
  268. throw new SystemException("已经是最顶层机构,无法继续返回!");
  269. }
  270. LambdaQueryWrapper<EsmInsCompany> lqw2 = new LambdaQueryWrapper<>();
  271. lqw2.eq(EsmInsCompany::getId, esmInsCompany1.getParentId());
  272. EsmInsCompany esmInsCompany2=esmInsCompanyService.getOne(lqw2);
  273. if(esmInsCompany2==null){
  274. insPlySettleReportReqVo.setCompanyId(esmInsCompany1.getParentId());
  275. }else{
  276. insPlySettleReportReqVo.setCompanyId(esmInsCompany2.getParentId());
  277. }
  278. }
  279. //正常查询,下钻查询时调整
  280. List<String> companyArr=esmInsCompanyService.getInsCompanyNextLevel(insPlySettleReportReqVo.getCompanyId());
  281. if(!(companyArr!=null&&companyArr.size()>0)){
  282. throw new SystemException("已经是最底层机构,无法继续下钻!");
  283. }
  284. insPlySettleReportReqVo.setGroupInsCompanyArr(companyArr);
  285. List<InsPlySettleReportResVo> insPlySettleReportResVoList = baseMapper.getInsPlySettleReport(insPlySettleReportReqVo);
  286. if(insPlySettleReportResVoList.size()==0){
  287. if(StringUtils.isNotEmpty(insPlySettleReportReqVo.getIsDrilling())&&"1".equals(insPlySettleReportReqVo.getIsDrilling())){
  288. throw new SystemException("已经是最底层机构,无法继续下钻!");
  289. }
  290. }
  291. insPlySettleReportResVoList.stream().forEach(item->{
  292. EsmInsCompany esmInsCompany =esmInsCompanyService.getById(item.getCompanyId());
  293. item.setCompanyName(esmInsCompany.getName());
  294. if(StringUtils.isEmpty(item.getStatus())){
  295. item.setStatus("全部");
  296. }else{
  297. if("0".equals(item.getStatus())){
  298. item.setStatus("未核销");
  299. }else if("1".equals(item.getStatus())){
  300. item.setStatus("部分核销");
  301. }else if ("2".equals(item.getStatus())){
  302. item.setStatus("已核销");
  303. }
  304. }
  305. });
  306. String s = EasyExcelUtils.downExcel(uploadFilePath, InsPlySettleReportResVo.class, insPlySettleReportResVoList);
  307. return HttpResult.ok("", s);
  308. }
  309. @Override
  310. public HttpResult<String> exportInsPlySettleReportList(InsPlySettleReportReqVo insPlySettleReportReqVo) {
  311. List<InsPlySettleStatisticsListResVo> insPlySettleReportList = baseMapper.getInsPlySettleReportList(insPlySettleReportReqVo);
  312. insPlySettleReportList.stream().forEach(item->{
  313. if("0".equals(item.getStatus())){
  314. item.setStatus("未核销");
  315. }else if("1".equals(item.getStatus())){
  316. item.setStatus("部分核销");
  317. }else if ("2".equals(item.getStatus())){
  318. item.setStatus("已核销");
  319. }
  320. });
  321. String s = EasyExcelUtils.downExcel(uploadFilePath, InsPlySettleStatisticsListResVo.class, insPlySettleReportList);
  322. return HttpResult.ok("", s);
  323. }
  324. }