StatementsServiceImpl.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package com.kym.service.admin.impl;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.yulichang.base.MPJBaseServiceImpl;
  4. import com.github.yulichang.toolkit.JoinWrappers;
  5. import com.github.yulichang.wrapper.MPJLambdaWrapper;
  6. import com.kym.common.exception.BusinessException;
  7. import com.kym.common.utils.CommUtil;
  8. import com.kym.entity.admin.*;
  9. import com.kym.entity.admin.queryParams.StatementsQueryParam;
  10. import com.kym.entity.admin.vo.StatementsVo;
  11. import com.kym.entity.common.PageBean;
  12. import com.kym.mapper.admin.StatementsMapper;
  13. import com.kym.service.admin.AdminUserRoleService;
  14. import com.kym.service.admin.InvestorInfoService;
  15. import com.kym.service.admin.StatementsService;
  16. import com.kym.service.admin.StationStatMonthService;
  17. import com.kym.service.cache.KymCache;
  18. import org.springframework.beans.BeanUtils;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Transactional;
  21. /**
  22. * <p>
  23. * 客户对账单 服务实现类
  24. * </p>
  25. *
  26. * @author skyline
  27. * @since 2023-12-27
  28. */
  29. @Service
  30. public class StatementsServiceImpl extends MPJBaseServiceImpl<StatementsMapper, Statements> implements StatementsService {
  31. private final InvestorInfoService investorInfoService;
  32. private final StationStatMonthService stationStatMonthService;
  33. private final AdminUserRoleService adminUserRoleService;
  34. public StatementsServiceImpl(InvestorInfoService investorInfoService, StationStatMonthService stationStatMonthService, AdminUserRoleService adminUserRoleService) {
  35. this.investorInfoService = investorInfoService;
  36. this.stationStatMonthService = stationStatMonthService;
  37. this.adminUserRoleService = adminUserRoleService;
  38. }
  39. @Override
  40. @Transactional
  41. public void createStatements(String statMonthId) {
  42. // 站点统计信息
  43. var statMonthInfo = stationStatMonthService.lambdaQuery()
  44. .eq(StationStatMonth::getId, statMonthId)
  45. .one();
  46. // 校验站点月统计信息已填写实际抄表电量和实际抄表电费金额
  47. if (statMonthInfo.getActualPower() == null || statMonthInfo.getActualElecMoney() == null) {
  48. throw new BusinessException("请先完善月统计实际抄表电量和实际抄表电费金额");
  49. }
  50. // 站点关联客户和物业信息
  51. var investorInfoList = investorInfoService.lambdaQuery()
  52. .eq(InvestorInfo::getStationId, statMonthInfo.getStationId())
  53. .eq(InvestorInfo::getStatus, InvestorInfo.STATUS_有效)
  54. .list();
  55. if (CommUtil.isEmptyOrNull(investorInfoList)) {
  56. throw new BusinessException("请先完善站点相关投资者/物业信息");
  57. }
  58. // 创建客户对账单
  59. var res = investorInfoList.stream().map(investorInfo -> {
  60. // 实际参与分成的服务费=总服务费-优惠金额-电损电费
  61. var actualServiceMoney = statMonthInfo.getServiceMoney() - statMonthInfo.getDiscountAmount();
  62. // 总电损电费
  63. var elecLossMoney = statMonthInfo.getActualElecMoney() - statMonthInfo.getElecMoney();
  64. var elecLossAmount = (int) (elecLossMoney * investorInfo.getElecLossProportion());
  65. // 分成金额 = 实际参与分成的服务费 * 分成比例 - 电损金额 * 电损承担比例
  66. var splittingAmount = (int) (actualServiceMoney * investorInfo.getSplittingProportion()) - elecLossAmount;
  67. return new Statements()
  68. .setAdminUserId(investorInfo.getAdminUserId())
  69. .setAdminUserName(investorInfo.getAdminUserName())
  70. .setStationId(investorInfo.getStationId())
  71. .setStationName(KymCache.INSTANCE.getStationNameById(investorInfo.getStationId()))
  72. .setStatMonth(statMonthInfo.getStatMonth())
  73. .setTotalPower(statMonthInfo.getTotalPower())
  74. .setActualPower(statMonthInfo.getActualPower()) // 实际抄表电量
  75. .setElecLossPower(statMonthInfo.getActualPower() - statMonthInfo.getTotalPower()) // 电损电量
  76. .setTotalMoney(statMonthInfo.getTotalMoney())
  77. .setElecMoney(statMonthInfo.getElecMoney())
  78. .setActualElecMoney(statMonthInfo.getActualElecMoney())// 实际抄表电费
  79. .setElecLossMoney(elecLossMoney) // 电损电费(分)
  80. .setServiceMoney(statMonthInfo.getServiceMoney())
  81. .setDiscountAmount(statMonthInfo.getDiscountAmount())
  82. .setServiceMoneyDiscount(statMonthInfo.getServiceMoneyDiscount())
  83. .setActualServiceMoney(actualServiceMoney) // 实际参与分成的服务费(分)
  84. .setSplittingProportion(investorInfo.getSplittingProportion()) // 分成比例 0.45表示45%
  85. .setSplittingAmount(splittingAmount) // 分成金额(分)
  86. .setElecLossProportion(investorInfo.getElecLossProportion()) // 电损承担比例
  87. .setElecLossAmount((int) (elecLossMoney * investorInfo.getElecLossProportion())) // 电损承担金额
  88. .setVatRate(investorInfo.getVatRate()) // 增值税率 0.06表示6%
  89. .setVatAmount((int) (splittingAmount / (1 + investorInfo.getVatRate()) * investorInfo.getVatRate() * 1.12)) // 增值税额(分)
  90. .setActualSplittingAmount(splittingAmount - (int) (splittingAmount / (1 + investorInfo.getVatRate()) * investorInfo.getVatRate() * 1.12)); // 实际分成金额(分)
  91. }).toList();
  92. // 删除之前生成的对账单(投资者/物业 - 站点 - 月份)
  93. var oldData = lambdaQuery()
  94. .eq(Statements::getStatMonth, statMonthInfo.getStatMonth())
  95. .eq(Statements::getStationId, statMonthInfo.getStationId())
  96. .in(Statements::getAdminUserId, investorInfoList.stream().map(InvestorInfo::getAdminUserId).toList())
  97. .list().stream().map(Statements::getId).toList();
  98. removeBatchByIds(oldData);
  99. saveBatch(res);
  100. }
  101. @Override
  102. public PageBean<Statements> listStatements(StatementsQueryParam params) {
  103. PageHelper.startPage(params.getPageNum(), params.getPageSize());
  104. var res = lambdaQuery()
  105. .eq(!CommUtil.isEmptyOrNull(params.getStationId()), Statements::getStationId, params.getStationId())
  106. .eq(!CommUtil.isEmptyOrNull(params.getStatMonth()), Statements::getStatMonth, params.getStatMonth())
  107. .like(!CommUtil.isEmptyOrNull(params.getAdminUserName()), Statements::getAdminUserName, params.getAdminUserName())
  108. .orderByDesc(Statements::getCreateTime)
  109. .list();
  110. return new PageBean<>(res);
  111. }
  112. @Override
  113. public StatementsVo preview(String statId) {
  114. var statements = getById(statId);
  115. var investorInfo = investorInfoService.lambdaQuery()
  116. .eq(InvestorInfo::getAdminUserId, statements.getAdminUserId())
  117. .eq(InvestorInfo::getStationId, statements.getStationId())
  118. .one();
  119. // 查询角色
  120. MPJLambdaWrapper<AdminUserRole> wrapper = JoinWrappers.lambda(AdminUserRole.class)
  121. .select(Role::getRoleName, Role::getRoleDesc)
  122. .leftJoin(Role.class, Role::getId, AdminUserRole::getRoleId)
  123. .eq(AdminUserRole::getAdminUserId, statements.getAdminUserId());
  124. var res = adminUserRoleService.selectJoinMap(wrapper);
  125. if (CommUtil.isEmptyOrNull(res)) {
  126. throw new BusinessException("请先完善站点相关投资者/物业客户账号信息关联之后重新生成账单");
  127. }
  128. var statementsVo = new StatementsVo();
  129. statementsVo.setRoleName(res.get("role_name").toString());
  130. statementsVo.setRoleDesc(res.get("role_desc").toString());
  131. BeanUtils.copyProperties(statements, statementsVo);
  132. BeanUtils.copyProperties(investorInfo, statementsVo);
  133. return statementsVo;
  134. }
  135. }