puzhibing
2026-09-28 d4bde58ab669a434bb0c96f391153ec66eedd5b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.ruoyi.other.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.other.api.domain.TChargeAuthorization;
import com.ruoyi.other.api.domain.TGroupCompany;
import com.ruoyi.other.api.dto.ChargeAuthorizationDTO;
import com.ruoyi.other.api.support.ChargeAuthorizationSupport;
import com.ruoyi.other.mapper.TChargeAuthorizationMapper;
import com.ruoyi.other.mapper.TGroupCompanyMapper;
import com.ruoyi.other.service.TChargeAuthorizationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
 
/**
 * <p>
 * 充电授权配置 服务实现类
 * </p>
 *
 * @author mxcd
 * @since 2026-09-26
 */
@Service
public class TChargeAuthorizationServiceImpl extends ServiceImpl<TChargeAuthorizationMapper, TChargeAuthorization>
        implements TChargeAuthorizationService {
 
    /** 公司 Mapper(校验配置所属公司是否存在) */
    @Resource
    private TGroupCompanyMapper groupCompanyMapper;
 
    @Override
    public ChargeAuthorizationDTO getAuthorization(Integer companyId) {
        if (companyId == null) {
            return null;
        }
        TChargeAuthorization entity = getByCompanyId(companyId);
        return entity == null ? null : toDTO(entity);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveAuthorization(ChargeAuthorizationDTO dto) {
        if (dto == null || dto.getCompanyId() == null) {
            throw new IllegalArgumentException("配置所属公司不能为空");
        }
        TGroupCompany company = groupCompanyMapper.selectById(dto.getCompanyId());
        if (company == null) {
            throw new IllegalArgumentException("公司不存在");
        }
        // 校验并规范化各配置项(不合法直接抛出业务提示)
        List<Integer> partnerIds = ChargeAuthorizationSupport.normalizePartnerIds(dto.getPartnerIds());
        Integer timeMode = ChargeAuthorizationSupport.normalizeTimeMode(dto.getTimeMode());
        String timeRanges = ChargeAuthorizationSupport.normalizeTimeRanges(dto.getTimeRanges(), timeMode);
        String priceTypes = ChargeAuthorizationSupport.normalizePriceTypes(dto.getPriceTypes(), timeMode);
        List<Integer> siteIds = ChargeAuthorizationSupport.normalizeSiteIds(dto.getSiteIds());
        BigDecimal dispatchAmount = ChargeAuthorizationSupport.normalizeDispatchAmount(dto.getDispatchAmount());
 
        // 一个公司一条配置:存在则覆盖,不存在则新增
        TChargeAuthorization entity = getByCompanyId(dto.getCompanyId());
        if (entity == null) {
            entity = new TChargeAuthorization();
            entity.setCompanyId(dto.getCompanyId());
            entity.setDelFlag(false);
        }
        entity.setPartnerIds(ChargeAuthorizationSupport.joinPartnerIds(partnerIds));
        entity.setTimeMode(timeMode);
        entity.setTimeRanges(timeRanges);
        entity.setPriceTypes(priceTypes);
        entity.setAutoCharge(Boolean.TRUE.equals(dto.getAutoCharge()));
        entity.setSiteIds(ChargeAuthorizationSupport.joinSiteIds(siteIds));
        entity.setDispatchAmount(dispatchAmount);
        saveOrUpdate(entity);
        return entity.getId();
    }
 
    /**
     * 按公司查询配置(唯一索引保障最多一条,多余数据取首条不抛异常)
     */
    private TChargeAuthorization getByCompanyId(Integer companyId) {
        return getOne(Wrappers.<TChargeAuthorization>lambdaQuery()
                .eq(TChargeAuthorization::getCompanyId, companyId), false);
    }
 
    /**
     * 实体转详情对象(多值字段解析为数组便于前端回显)
     */
    private ChargeAuthorizationDTO toDTO(TChargeAuthorization entity) {
        ChargeAuthorizationDTO dto = new ChargeAuthorizationDTO();
        dto.setCompanyId(entity.getCompanyId());
        dto.setPartnerIds(ChargeAuthorizationSupport.parsePartnerIds(entity.getPartnerIds()));
        dto.setTimeMode(entity.getTimeMode());
        dto.setTimeRanges(ChargeAuthorizationSupport.parseTimeRanges(entity.getTimeRanges()));
        dto.setPriceTypes(ChargeAuthorizationSupport.parsePriceTypes(entity.getPriceTypes()));
        dto.setAutoCharge(Boolean.TRUE.equals(entity.getAutoCharge()));
        dto.setSiteIds(ChargeAuthorizationSupport.parseSiteIds(entity.getSiteIds()));
        dto.setDispatchAmount(entity.getDispatchAmount());
        return dto;
    }
 
}