PolicyFeeServiceImpl.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package com.zsElectric.boot.business.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  4. import com.zsElectric.boot.business.mapper.PolicyFeeMapper;
  5. import com.zsElectric.boot.business.mapper.ThirdPartyStationInfoMapper;
  6. import com.zsElectric.boot.business.model.dto.AddPolicyFeeDTO;
  7. import com.zsElectric.boot.business.model.entity.PolicyFee;
  8. import com.zsElectric.boot.business.model.vo.TimePeriodPriceVO;
  9. import com.zsElectric.boot.business.service.PolicyFeeService;
  10. import com.zsElectric.boot.charging.entity.ThirdPartyPolicyInfo;
  11. import com.zsElectric.boot.charging.entity.ThirdPartyStationInfo;
  12. import com.zsElectric.boot.charging.mapper.ThirdPartyPolicyInfoMapper;
  13. import com.zsElectric.boot.system.mapper.DictItemMapper;
  14. import com.zsElectric.boot.system.model.entity.DictItem;
  15. import lombok.RequiredArgsConstructor;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.annotation.Transactional;
  19. import java.math.BigDecimal;
  20. import java.util.List;
  21. /**
  22. * 策略费用服务实现
  23. *
  24. * @author system
  25. * @since 2025-12-15
  26. */
  27. @Slf4j
  28. @Service
  29. @RequiredArgsConstructor
  30. public class PolicyFeeServiceImpl implements PolicyFeeService {
  31. private final PolicyFeeMapper policyFeeMapper;
  32. private final ThirdPartyStationInfoMapper stationInfoMapper;
  33. private final ThirdPartyPolicyInfoMapper policyInfoMapper;
  34. private final DictItemMapper dictItemMapper;
  35. /**
  36. * 时段标志字典编码
  37. */
  38. private static final String TIME_PERIOD_FLAG_DICT_CODE = "time_period_flag";
  39. @Override
  40. public List<TimePeriodPriceVO> getPolicyFee(long stationId, int salesType, Long firmId, Long thirdPartyId) {
  41. return policyFeeMapper.selectTimePeriodPriceList(stationId, salesType, firmId, thirdPartyId);
  42. }
  43. @Override
  44. public boolean addPolicyFee(AddPolicyFeeDTO addPolicyFeeDTO) {
  45. // 根据站点+时段+销售类型+企业/渠道方查询是否已存在
  46. PolicyFee existPolicyFee = policyFeeMapper.selectOne(Wrappers.<PolicyFee>lambdaQuery()
  47. .eq(PolicyFee::getStationInfoId, addPolicyFeeDTO.getStationInfoId())
  48. .eq(PolicyFee::getStartTime, addPolicyFeeDTO.getTimePeriod())
  49. .eq(PolicyFee::getSalesType, addPolicyFeeDTO.getSalesType())
  50. .eq(addPolicyFeeDTO.getSalesType() == 1, PolicyFee::getFirmId, addPolicyFeeDTO.getFirmId())
  51. .eq(addPolicyFeeDTO.getSalesType() == 2, PolicyFee::getThirdPartyId, addPolicyFeeDTO.getThirdPartyId())
  52. .eq(PolicyFee::getIsDeleted, 0)
  53. .last("limit 1"));
  54. if (existPolicyFee != null) {
  55. // 已存在,执行更新
  56. existPolicyFee.setOpFee(addPolicyFeeDTO.getOperationServiceFee());
  57. // 计算并设置综合销售费
  58. BigDecimal compSalesFee = calculateCompSalesFee(
  59. addPolicyFeeDTO.getStationInfoId(),
  60. addPolicyFeeDTO.getTimePeriod(),
  61. addPolicyFeeDTO.getPeriodFlag(),
  62. addPolicyFeeDTO.getOperationServiceFee()
  63. );
  64. existPolicyFee.setCompSalesFee(compSalesFee);
  65. return policyFeeMapper.updateById(existPolicyFee) > 0;
  66. } else {
  67. // 不存在,执行新增
  68. PolicyFee policyFee = new PolicyFee();
  69. policyFee.setStationInfoId(addPolicyFeeDTO.getStationInfoId());
  70. policyFee.setStartTime(addPolicyFeeDTO.getTimePeriod());
  71. policyFee.setOpFee(addPolicyFeeDTO.getOperationServiceFee());
  72. // 计算并设置综合销售费
  73. BigDecimal compSalesFee = calculateCompSalesFee(
  74. addPolicyFeeDTO.getStationInfoId(),
  75. addPolicyFeeDTO.getTimePeriod(),
  76. addPolicyFeeDTO.getPeriodFlag(),
  77. addPolicyFeeDTO.getOperationServiceFee()
  78. );
  79. policyFee.setCompSalesFee(compSalesFee);
  80. policyFee.setSalesType(addPolicyFeeDTO.getSalesType());
  81. // 根据销售类型设置对应的ID
  82. if (addPolicyFeeDTO.getSalesType() == 1) {
  83. policyFee.setFirmId(addPolicyFeeDTO.getFirmId());
  84. } else if (addPolicyFeeDTO.getSalesType() == 2) {
  85. policyFee.setThirdPartyId(addPolicyFeeDTO.getThirdPartyId());
  86. }
  87. boolean inserted = policyFeeMapper.insert(policyFee) > 0;
  88. // 新增成功后,更新站点的配置状态为已配置
  89. if (inserted) {
  90. stationInfoMapper.update(null, Wrappers.<ThirdPartyStationInfo>lambdaUpdate()
  91. .eq(ThirdPartyStationInfo::getId, addPolicyFeeDTO.getStationInfoId())
  92. .set(ThirdPartyStationInfo::getPolicyConfigured, 1));
  93. }
  94. return inserted;
  95. }
  96. }
  97. /**
  98. * 计算综合销售费
  99. * 公式:compSalesFee = elec_price + service_price + 字典表值 + op_fee
  100. *
  101. * @param stationInfoId 站点信息ID
  102. * @param timePeriod 时间段(HHmmss格式)
  103. * @param periodFlag 时段标志
  104. * @param opFee 运营费
  105. * @return 综合销售费
  106. */
  107. private BigDecimal calculateCompSalesFee(Long stationInfoId, String timePeriod, Integer periodFlag, BigDecimal opFee) {
  108. BigDecimal elecPrice = BigDecimal.ZERO;
  109. BigDecimal servicePrice = BigDecimal.ZERO;
  110. // 直接通过stationInfoId和timePeriod查询电价和服务费
  111. ThirdPartyPolicyInfo policyInfo = policyInfoMapper.selectElecAndServicePriceByStation(stationInfoId, timePeriod);
  112. log.info("计算compSalesFee - stationInfoId: {}, timePeriod: {}, policyInfo: {}", stationInfoId, timePeriod, policyInfo);
  113. if (policyInfo != null) {
  114. elecPrice = policyInfo.getElecPrice() != null ? policyInfo.getElecPrice() : BigDecimal.ZERO;
  115. servicePrice = policyInfo.getServicePrice() != null ? policyInfo.getServicePrice() : BigDecimal.ZERO;
  116. }
  117. // 从字典表获取periodFlag对应的值
  118. BigDecimal dictValue = getDictValueByPeriodFlag(periodFlag);
  119. // 计算综合销售费 = elec_price + service_price + 字典值 + op_fee
  120. BigDecimal opFeeValue = opFee != null ? opFee : BigDecimal.ZERO;
  121. BigDecimal compSalesFee = elecPrice.add(servicePrice).add(dictValue).add(opFeeValue);
  122. log.info("计算综合销售费 - stationInfoId: {}, timePeriod: {}, elecPrice: {}, servicePrice: {}, dictValue: {}, opFee: {}, compSalesFee: {}",
  123. stationInfoId, timePeriod, elecPrice, servicePrice, dictValue, opFeeValue, compSalesFee);
  124. return compSalesFee;
  125. }
  126. /**
  127. * 根据时段标志获取字典值
  128. *
  129. * @param periodFlag 时段标志(1-尖 2-峰 3-平 4-谷)
  130. * @return 综合销售费
  131. */
  132. private BigDecimal getDictValueByPeriodFlag(Integer periodFlag) {
  133. if (periodFlag == null) {
  134. return BigDecimal.ZERO;
  135. }
  136. // 根据时段标志获取对应的label
  137. String label;
  138. switch (periodFlag) {
  139. case 1:
  140. label = "尖";
  141. break;
  142. case 2:
  143. label = "峰";
  144. break;
  145. case 3:
  146. label = "平";
  147. break;
  148. case 4:
  149. label = "谷";
  150. break;
  151. default:
  152. return BigDecimal.ZERO;
  153. }
  154. // 从字典表查询对应的value
  155. DictItem dictItem = dictItemMapper.selectOne(
  156. new LambdaQueryWrapper<DictItem>()
  157. .eq(DictItem::getDictCode, TIME_PERIOD_FLAG_DICT_CODE)
  158. .eq(DictItem::getStatus, 1)
  159. .eq(DictItem::getLabel, label)
  160. .last("LIMIT 1")
  161. );
  162. if (dictItem != null && dictItem.getValue() != null) {
  163. try {
  164. return new BigDecimal(dictItem.getValue());
  165. } catch (NumberFormatException e) {
  166. log.warn("字典值value转换为BigDecimal失败: {}", dictItem.getValue());
  167. return BigDecimal.ZERO;
  168. }
  169. }
  170. return BigDecimal.ZERO;
  171. }
  172. @Override
  173. @Transactional(rollbackFor = Exception.class)
  174. public boolean batchAddPolicyFee(List<AddPolicyFeeDTO> addPolicyFeeDTOList) {
  175. if (addPolicyFeeDTOList == null || addPolicyFeeDTOList.isEmpty()) {
  176. return false;
  177. }
  178. for (AddPolicyFeeDTO dto : addPolicyFeeDTOList) {
  179. addPolicyFee(dto);
  180. }
  181. return true;
  182. }
  183. }