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