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);
|
}
|
|
}
|