PolicyFeeServiceImpl.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.ThirdPartyStationInfo;
  11. import com.zsElectric.boot.system.mapper.DictItemMapper;
  12. import com.zsElectric.boot.system.model.entity.DictItem;
  13. import lombok.RequiredArgsConstructor;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import java.math.BigDecimal;
  18. import java.util.List;
  19. /**
  20. * 策略费用服务实现
  21. *
  22. * @author system
  23. * @since 2025-12-15
  24. */
  25. @Slf4j
  26. @Service
  27. @RequiredArgsConstructor
  28. public class PolicyFeeServiceImpl implements PolicyFeeService {
  29. private final PolicyFeeMapper policyFeeMapper;
  30. private final ThirdPartyStationInfoMapper stationInfoMapper;
  31. private final DictItemMapper dictItemMapper;
  32. /**
  33. * 时段标志字典编码
  34. */
  35. private static final String TIME_PERIOD_FLAG_DICT_CODE = "time_period_flag";
  36. @Override
  37. public List<TimePeriodPriceVO> getPolicyFee(long stationId, int salesType, Long firmId, Long thirdPartyId) {
  38. return policyFeeMapper.selectTimePeriodPriceList(stationId, salesType, firmId, thirdPartyId);
  39. }
  40. @Override
  41. public boolean addPolicyFee(AddPolicyFeeDTO addPolicyFeeDTO) {
  42. // 根据站点+时段+销售类型+企业/渠道方查询是否已存在
  43. PolicyFee existPolicyFee = policyFeeMapper.selectOne(Wrappers.<PolicyFee>lambdaQuery()
  44. .eq(PolicyFee::getStationInfoId, addPolicyFeeDTO.getStationInfoId())
  45. .eq(PolicyFee::getStartTime, addPolicyFeeDTO.getTimePeriod())
  46. .eq(PolicyFee::getSalesType, addPolicyFeeDTO.getSalesType())
  47. .eq(addPolicyFeeDTO.getSalesType() == 1, PolicyFee::getFirmId, addPolicyFeeDTO.getFirmId())
  48. .eq(addPolicyFeeDTO.getSalesType() == 2, PolicyFee::getThirdPartyId, addPolicyFeeDTO.getThirdPartyId())
  49. .eq(PolicyFee::getIsDeleted, 0)
  50. .last("limit 1"));
  51. if (existPolicyFee != null) {
  52. // 已存在,执行更新
  53. existPolicyFee.setOpFee(addPolicyFeeDTO.getOperationServiceFee());
  54. // 计算并设置综合销售费
  55. BigDecimal compSalesFee = getCompSalesFeeByPeriodFlag(addPolicyFeeDTO.getPeriodFlag());
  56. existPolicyFee.setCompSalesFee(compSalesFee);
  57. return policyFeeMapper.updateById(existPolicyFee) > 0;
  58. } else {
  59. // 不存在,执行新增
  60. PolicyFee policyFee = new PolicyFee();
  61. policyFee.setStationInfoId(addPolicyFeeDTO.getStationInfoId());
  62. policyFee.setStartTime(addPolicyFeeDTO.getTimePeriod());
  63. policyFee.setOpFee(addPolicyFeeDTO.getOperationServiceFee());
  64. // 计算并设置综合销售费
  65. BigDecimal compSalesFee = getCompSalesFeeByPeriodFlag(addPolicyFeeDTO.getPeriodFlag());
  66. policyFee.setCompSalesFee(compSalesFee);
  67. policyFee.setSalesType(addPolicyFeeDTO.getSalesType());
  68. // 根据销售类型设置对应的ID
  69. if (addPolicyFeeDTO.getSalesType() == 1) {
  70. policyFee.setFirmId(addPolicyFeeDTO.getFirmId());
  71. } else if (addPolicyFeeDTO.getSalesType() == 2) {
  72. policyFee.setThirdPartyId(addPolicyFeeDTO.getThirdPartyId());
  73. }
  74. boolean inserted = policyFeeMapper.insert(policyFee) > 0;
  75. // 新增成功后,更新站点的配置状态为已配置
  76. if (inserted) {
  77. stationInfoMapper.update(null, Wrappers.<ThirdPartyStationInfo>lambdaUpdate()
  78. .eq(ThirdPartyStationInfo::getId, addPolicyFeeDTO.getStationInfoId())
  79. .set(ThirdPartyStationInfo::getPolicyConfigured, 1));
  80. }
  81. return inserted;
  82. }
  83. }
  84. /**
  85. * 根据时段标志获取综合销售费(从字典表查询)
  86. *
  87. * @param periodFlag 时段标志(1-尖 2-峰 3-平 4-谷)
  88. * @return 综合销售费
  89. */
  90. private BigDecimal getCompSalesFeeByPeriodFlag(Integer periodFlag) {
  91. if (periodFlag == null) {
  92. return BigDecimal.ZERO;
  93. }
  94. // 根据时段标志获取对应的label
  95. String label;
  96. switch (periodFlag) {
  97. case 1:
  98. label = "尖";
  99. break;
  100. case 2:
  101. label = "峰";
  102. break;
  103. case 3:
  104. label = "平";
  105. break;
  106. case 4:
  107. label = "谷";
  108. break;
  109. default:
  110. return BigDecimal.ZERO;
  111. }
  112. // 从字典表查询对应的value
  113. DictItem dictItem = dictItemMapper.selectOne(
  114. new LambdaQueryWrapper<DictItem>()
  115. .eq(DictItem::getDictCode, TIME_PERIOD_FLAG_DICT_CODE)
  116. .eq(DictItem::getStatus, 1)
  117. .eq(DictItem::getLabel, label)
  118. .last("LIMIT 1")
  119. );
  120. if (dictItem != null && dictItem.getValue() != null) {
  121. try {
  122. return new BigDecimal(dictItem.getValue());
  123. } catch (NumberFormatException e) {
  124. log.warn("字典值value转换为BigDecimal失败: {}", dictItem.getValue());
  125. return BigDecimal.ZERO;
  126. }
  127. }
  128. return BigDecimal.ZERO;
  129. }
  130. @Override
  131. @Transactional(rollbackFor = Exception.class)
  132. public boolean batchAddPolicyFee(List<AddPolicyFeeDTO> addPolicyFeeDTOList) {
  133. if (addPolicyFeeDTOList == null || addPolicyFeeDTOList.isEmpty()) {
  134. return false;
  135. }
  136. for (AddPolicyFeeDTO dto : addPolicyFeeDTOList) {
  137. addPolicyFee(dto);
  138. }
  139. return true;
  140. }
  141. }