| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- package com.zsElectric.boot.business.service.impl;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.zsElectric.boot.business.mapper.PolicyFeeMapper;
- import com.zsElectric.boot.business.mapper.ThirdPartyStationInfoMapper;
- import com.zsElectric.boot.business.model.dto.AddPolicyFeeDTO;
- import com.zsElectric.boot.business.model.entity.PolicyFee;
- import com.zsElectric.boot.business.model.vo.TimePeriodPriceVO;
- import com.zsElectric.boot.business.service.PolicyFeeService;
- import com.zsElectric.boot.charging.entity.ThirdPartyStationInfo;
- import com.zsElectric.boot.system.mapper.DictItemMapper;
- import com.zsElectric.boot.system.model.entity.DictItem;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.math.BigDecimal;
- import java.util.List;
- /**
- * 策略费用服务实现
- *
- * @author system
- * @since 2025-12-15
- */
- @Slf4j
- @Service
- @RequiredArgsConstructor
- public class PolicyFeeServiceImpl implements PolicyFeeService {
- private final PolicyFeeMapper policyFeeMapper;
- private final ThirdPartyStationInfoMapper stationInfoMapper;
- private final DictItemMapper dictItemMapper;
-
- /**
- * 时段标志字典编码
- */
- private static final String TIME_PERIOD_FLAG_DICT_CODE = "time_period_flag";
- @Override
- public List<TimePeriodPriceVO> getPolicyFee(long stationId, int salesType, Long firmId, Long thirdPartyId) {
- return policyFeeMapper.selectTimePeriodPriceList(stationId, salesType, firmId, thirdPartyId);
- }
- @Override
- public boolean addPolicyFee(AddPolicyFeeDTO addPolicyFeeDTO) {
- // 根据站点+时段+销售类型+企业/渠道方查询是否已存在
- PolicyFee existPolicyFee = policyFeeMapper.selectOne(Wrappers.<PolicyFee>lambdaQuery()
- .eq(PolicyFee::getStationInfoId, addPolicyFeeDTO.getStationInfoId())
- .eq(PolicyFee::getStartTime, addPolicyFeeDTO.getTimePeriod())
- .eq(PolicyFee::getSalesType, addPolicyFeeDTO.getSalesType())
- .eq(addPolicyFeeDTO.getSalesType() == 1, PolicyFee::getFirmId, addPolicyFeeDTO.getFirmId())
- .eq(addPolicyFeeDTO.getSalesType() == 2, PolicyFee::getThirdPartyId, addPolicyFeeDTO.getThirdPartyId())
- .eq(PolicyFee::getIsDeleted, 0)
- .last("limit 1"));
-
- if (existPolicyFee != null) {
- // 已存在,执行更新
- existPolicyFee.setOpFee(addPolicyFeeDTO.getOperationServiceFee());
- // 计算并设置综合销售费
- BigDecimal compSalesFee = getCompSalesFeeByPeriodFlag(addPolicyFeeDTO.getPeriodFlag());
- existPolicyFee.setCompSalesFee(compSalesFee);
- return policyFeeMapper.updateById(existPolicyFee) > 0;
- } else {
- // 不存在,执行新增
- PolicyFee policyFee = new PolicyFee();
- policyFee.setStationInfoId(addPolicyFeeDTO.getStationInfoId());
- policyFee.setStartTime(addPolicyFeeDTO.getTimePeriod());
- policyFee.setOpFee(addPolicyFeeDTO.getOperationServiceFee());
- // 计算并设置综合销售费
- BigDecimal compSalesFee = getCompSalesFeeByPeriodFlag(addPolicyFeeDTO.getPeriodFlag());
- policyFee.setCompSalesFee(compSalesFee);
- policyFee.setSalesType(addPolicyFeeDTO.getSalesType());
- // 根据销售类型设置对应的ID
- if (addPolicyFeeDTO.getSalesType() == 1) {
- policyFee.setFirmId(addPolicyFeeDTO.getFirmId());
- } else if (addPolicyFeeDTO.getSalesType() == 2) {
- policyFee.setThirdPartyId(addPolicyFeeDTO.getThirdPartyId());
- }
- boolean inserted = policyFeeMapper.insert(policyFee) > 0;
-
- // 新增成功后,更新站点的配置状态为已配置
- if (inserted) {
- stationInfoMapper.update(null, Wrappers.<ThirdPartyStationInfo>lambdaUpdate()
- .eq(ThirdPartyStationInfo::getId, addPolicyFeeDTO.getStationInfoId())
- .set(ThirdPartyStationInfo::getPolicyConfigured, 1));
- }
- return inserted;
- }
- }
-
- /**
- * 根据时段标志获取综合销售费(从字典表查询)
- *
- * @param periodFlag 时段标志(1-尖 2-峰 3-平 4-谷)
- * @return 综合销售费
- */
- private BigDecimal getCompSalesFeeByPeriodFlag(Integer periodFlag) {
- if (periodFlag == null) {
- return BigDecimal.ZERO;
- }
-
- // 根据时段标志获取对应的label
- String label;
- switch (periodFlag) {
- case 1:
- label = "尖";
- break;
- case 2:
- label = "峰";
- break;
- case 3:
- label = "平";
- break;
- case 4:
- label = "谷";
- break;
- default:
- return BigDecimal.ZERO;
- }
-
- // 从字典表查询对应的value
- DictItem dictItem = dictItemMapper.selectOne(
- new LambdaQueryWrapper<DictItem>()
- .eq(DictItem::getDictCode, TIME_PERIOD_FLAG_DICT_CODE)
- .eq(DictItem::getStatus, 1)
- .eq(DictItem::getLabel, label)
- .last("LIMIT 1")
- );
-
- if (dictItem != null && dictItem.getValue() != null) {
- try {
- return new BigDecimal(dictItem.getValue());
- } catch (NumberFormatException e) {
- log.warn("字典值value转换为BigDecimal失败: {}", dictItem.getValue());
- return BigDecimal.ZERO;
- }
- }
-
- return BigDecimal.ZERO;
- }
- @Override
- @Transactional(rollbackFor = Exception.class)
- public boolean batchAddPolicyFee(List<AddPolicyFeeDTO> addPolicyFeeDTOList) {
- if (addPolicyFeeDTOList == null || addPolicyFeeDTOList.isEmpty()) {
- return false;
- }
- for (AddPolicyFeeDTO dto : addPolicyFeeDTOList) {
- addPolicyFee(dto);
- }
- return true;
- }
- }
|