puzhibing
2026-09-28 0f7f9ebdf8c7eb42c74a994b602f33c0d38aafb7
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/TGroupCompanyServiceImpl.java
@@ -1,10 +1,23 @@
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.TFleet;
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.TFleetMapper;
import com.ruoyi.other.mapper.TGroupCompanyMapper;
import com.ruoyi.other.service.TGroupCompanyService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
/**
 * <p>
@@ -17,4 +30,90 @@
@Service
public class TGroupCompanyServiceImpl extends ServiceImpl<TGroupCompanyMapper, TGroupCompany> implements TGroupCompanyService {
    /** 金额保留小数位(与 t_company_recharge_record.arrival_amount 一致) */
    private static final int AMOUNT_SCALE = 2;
    /**
     * 车队 Mapper。
     * 此处注入 Mapper 而非 TFleetService:TFleetServiceImpl 已依赖 TGroupCompanyService,
     * 反向注入 Service 会形成循环引用(Spring Boot 2.7 默认禁止)。
     */
    @Resource
    private TFleetMapper fleetMapper;
    /** 公司充值记录 Mapper */
    @Resource
    private TCompanyRechargeRecordMapper companyRechargeRecordMapper;
    @Override
    public TGroupCompany getDetail(Integer id) {
        if (id == null) {
            return null;
        }
        TGroupCompany company = getById(id);
        if (company == null) {
            return null;
        }
        // 车队数量:t_fleet 逻辑删除由 @TableLogic 自动过滤,统计含停用车队
        company.setFleetCount(countFleet(id));
        // 月充值金额:本月到账金额合计,仅覆盖响应对象,不落库
        company.setMonthRechargeAmount(sumCurrentMonthRechargeAmount(id));
        // 累计充值到账金额:作为公司总账户余额展示(额度分配弹窗使用),仅覆盖响应对象,不落库
        company.setTotalRechargeAmount(sumRechargeAmount(id));
        return company;
    }
    /**
     * 统计公司未删除的车队数量
     */
    private Integer countFleet(Integer companyId) {
        Long count = fleetMapper.selectCount(Wrappers.<TFleet>lambdaQuery().eq(TFleet::getCompanyId, companyId));
        return count == null ? 0 : count.intValue();
    }
    /**
     * 统计公司本月(自然月,按充值时间 [本月1日 00:00:00, 次月1日 00:00:00))到账金额合计
     */
    private BigDecimal sumCurrentMonthRechargeAmount(Integer companyId) {
        LocalDateTime start = monthStart(LocalDate.now());
        LocalDateTime end = start.plusMonths(1);
        List<TCompanyRechargeRecord> records = companyRechargeRecordMapper.selectList(
                Wrappers.<TCompanyRechargeRecord>lambdaQuery()
                        .eq(TCompanyRechargeRecord::getCompanyId, companyId)
                        .ge(TCompanyRechargeRecord::getRechargeTime, start)
                        .lt(TCompanyRechargeRecord::getRechargeTime, end));
        return sumArrivalAmount(records);
    }
    /**
     * 统计公司累计充值到账金额(全部有效充值记录,逻辑删除由 @TableLogic 自动过滤)
     */
    private BigDecimal sumRechargeAmount(Integer companyId) {
        List<TCompanyRechargeRecord> records = companyRechargeRecordMapper.selectList(
                Wrappers.<TCompanyRechargeRecord>lambdaQuery()
                        .eq(TCompanyRechargeRecord::getCompanyId, companyId));
        return sumArrivalAmount(records);
    }
    /**
     * 当月起始时刻(本月1日 00:00:00)
     */
    static LocalDateTime monthStart(LocalDate today) {
        return today.withDayOfMonth(1).atStartOfDay();
    }
    /**
     * 汇总到账金额(空集合返回 0.00,忽略空金额)
     */
    static BigDecimal sumArrivalAmount(List<TCompanyRechargeRecord> records) {
        if (records == null || records.isEmpty()) {
            return BigDecimal.ZERO.setScale(AMOUNT_SCALE, RoundingMode.HALF_UP);
        }
        return records.stream()
                .map(TCompanyRechargeRecord::getArrivalAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add)
                .setScale(AMOUNT_SCALE, RoundingMode.HALF_UP);
    }
}