puzhibing
4 天以前 3650ce7f5d42d1bef58abea9693f20d1f209d3b0
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
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.TCompanyPreferential;
import com.ruoyi.other.api.domain.TGroupCompany;
import com.ruoyi.other.api.dto.CompanyPreferentialDTO;
import com.ruoyi.other.mapper.TCompanyPreferentialMapper;
import com.ruoyi.other.mapper.TGroupCompanyMapper;
import com.ruoyi.other.service.TCompanyPreferentialService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
 
/**
 * <p>
 * 公司优惠设置 服务实现类
 * </p>
 *
 * @author mxcd
 * @since 2026-09-28
 */
@Service
public class TCompanyPreferentialServiceImpl
        extends ServiceImpl<TCompanyPreferentialMapper, TCompanyPreferential>
        implements TCompanyPreferentialService {
 
    /** 公司 Mapper(校验配置所属公司是否存在) */
    @Resource
    private TGroupCompanyMapper groupCompanyMapper;
 
    @Override
    public CompanyPreferentialDTO getPreferential(Integer companyId) {
        if (companyId == null) {
            return null;
        }
        CompanyPreferentialDTO dto = new CompanyPreferentialDTO();
        dto.setCompanyId(companyId);
        // 未配置时优惠字段保持为空,由前端置默认值(不优惠)
        TCompanyPreferential entity = getByCompanyId(companyId);
        if (entity != null) {
            dto.setPreferentialType(entity.getPreferentialType());
            dto.setServiceDiscountRate(entity.getServiceDiscountRate());
            dto.setBillingTemplateId(entity.getBillingTemplateId());
        }
        return dto;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer savePreferential(CompanyPreferentialDTO dto) {
        if (dto == null || dto.getCompanyId() == null) {
            throw new IllegalArgumentException("配置所属公司不能为空");
        }
        TGroupCompany company = groupCompanyMapper.selectById(dto.getCompanyId());
        if (company == null) {
            throw new IllegalArgumentException("公司不存在");
        }
        // 校验并规范化:仅保留当前优惠方式对应的字段,其余落库为 NULL(避免切换方式后残留旧配置)
        int preferentialType = CompanyPreferentialSupport.normalizePreferentialType(dto.getPreferentialType());
        BigDecimal serviceDiscountRate = CompanyPreferentialSupport
                .normalizeDiscountRate(preferentialType, dto.getServiceDiscountRate());
        Integer billingTemplateId = CompanyPreferentialSupport
                .normalizeBillingTemplateId(preferentialType, dto.getBillingTemplateId());
 
        // 一个公司一条配置:存在则覆盖,不存在则新增
        TCompanyPreferential entity = getByCompanyId(dto.getCompanyId());
        if (entity == null) {
            entity = new TCompanyPreferential();
            entity.setCompanyId(dto.getCompanyId());
            entity.setDelFlag(false);
        }
        entity.setPreferentialType(preferentialType);
        entity.setServiceDiscountRate(serviceDiscountRate);
        entity.setBillingTemplateId(billingTemplateId);
        saveOrUpdate(entity);
        return entity.getId();
    }
 
    /**
     * 按公司查询配置(唯一索引保障最多一条,多余数据取首条不抛异常)
     */
    private TCompanyPreferential getByCompanyId(Integer companyId) {
        return getOne(Wrappers.<TCompanyPreferential>lambdaQuery()
                .eq(TCompanyPreferential::getCompanyId, companyId), false);
    }
 
}