package com.ruoyi.other.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.other.api.domain.TGroupCompany;
import com.ruoyi.other.api.model.TCompanyRechargeRecord;
import com.ruoyi.other.mapper.TCompanyRechargeRecordMapper;
import com.ruoyi.other.mapper.TGroupCompanyMapper;
import com.ruoyi.other.service.TCompanyRechargeRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
*
* 公司充值记录 服务实现类
*
*
* @author mxcd
* @since 2026-09-23
*/
@Service
public class TCompanyRechargeRecordServiceImpl extends ServiceImpl implements TCompanyRechargeRecordService {
@Resource
private TGroupCompanyMapper groupCompanyMapper;
@Override
@Transactional(rollbackFor = Exception.class)
public Integer recharge(TCompanyRechargeRecord record) {
if (record.getCompanyId() == null) {
throw new IllegalArgumentException("公司ID不能为空");
}
if (record.getPayAmount() == null || record.getPayAmount().compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException("实付金额不能为空且必须大于等于0");
}
BigDecimal giftAmount = record.getGiftAmount() == null ? BigDecimal.ZERO : record.getGiftAmount();
if (giftAmount.compareTo(BigDecimal.ZERO) < 0) {
throw new IllegalArgumentException("赠送金额不能小于0");
}
TGroupCompany company = groupCompanyMapper.selectById(record.getCompanyId());
if (company == null) {
throw new IllegalArgumentException("公司不存在");
}
BigDecimal arrival = record.getPayAmount().add(giftAmount);
record.setArrivalAmount(arrival);
record.setId(null);
record.setDelFlag(false);
record.setRechargeTime(LocalDateTime.now());
try {
record.setOperatorId(SecurityUtils.getLoginUser().getUserid());
} catch (Exception ignored) {
}
// 更新公司余额(原子加):拼接 toString 防止 SQL 注入,BigDecimal 是受控数值
groupCompanyMapper.update(null, Wrappers.lambdaUpdate(TGroupCompany.class)
.eq(TGroupCompany::getId, record.getCompanyId())
.setSql("available_balance = IFNULL(available_balance, 0) + " + arrival.toPlainString()));
this.save(record);
return record.getId();
}
@Override
public PageInfo pageList(Integer current, Integer size, Integer companyId, String companyName) {
Page page = new Page<>(current, size);
LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(TCompanyRechargeRecord.class)
.eq(TCompanyRechargeRecord::getDelFlag, false)
.eq(companyId != null, TCompanyRechargeRecord::getCompanyId, companyId)
.orderByDesc(TCompanyRechargeRecord::getId);
Page result = this.page(page, wrapper);
PageInfo pageInfo = new PageInfo<>(result.getCurrent(), result.getSize());
pageInfo.setRecords(result.getRecords());
pageInfo.setTotal(result.getTotal());
return pageInfo;
}
}