StatPrimaryService.java 8.32 KB
package com.gsc4cloud.gsc.multidata.service.primary;

import com.gsc4cloud.gsc.multidata.dto.BusinessCoverageDTO;
import com.gsc4cloud.gsc.multidata.dto.LongCourseCompletionDTO;
import com.gsc4cloud.gsc.multidata.dto.ShortCourseCompletionDTO;
import com.gsc4cloud.gsc.multidata.dto.TabletCountDTO;
import com.gsc4cloud.gsc.multidata.mapper.primary.InspectionBadgeRecordingsMapper;
import com.gsc4cloud.gsc.multidata.utils.DatetimeScopeUtil;
import com.gsc4cloud.gsc.multidata.utils.UsageRateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class StatPrimaryService {

    @Value("${zhi.merchant.id:44206988}")
    private Integer zhMerchantId;

    @Autowired
    private InspectionBadgeRecordingsMapper inspectionBadgeRecordingsMapper;


    public Double getTabletUsageRate() {
        String startTime = DatetimeScopeUtil.getLastMonthStart();
        String endTime = DatetimeScopeUtil.getLastMonthEnd();
        // 平板月使用率(实际登录人数/发平板的总人数)
        // 1. 获取实际登录人数
        int actualLoginCount = inspectionBadgeRecordingsMapper.countDistinctBadgeIdsBetweenTimes(startTime, endTime);
        // 2. 获取发平板的总人数
        int totalAssetManagement = inspectionBadgeRecordingsMapper.countTotalAssetManagement();
        log.info("实际登录人数:{},发平板的总人数:{}", actualLoginCount, totalAssetManagement);
        
        // 使用专门的工具类计算使用率
        return UsageRateUtil.calculateUsageRate(actualLoginCount, totalAssetManagement);
    }

    public Double getTabletCoverageRate() {
        if (zhMerchantId==null) {
            log.error("商户ID为空,无法获取商户信息");
            return 0.0;
        }
        // 平板月覆盖率(发平板的总人数/直营驻店总人数)
        // 1. 获取发平板的总人数
        int totalAssetManagement = inspectionBadgeRecordingsMapper.countTotalAssetManagement();
        // 2. 获取直营驻店总人数
        int directStoreEmployeeCount = inspectionBadgeRecordingsMapper.countDistinctDirectStoreEmployees(zhMerchantId);
        log.info("发平板的总人数:{},直营驻店总人数:{}", totalAssetManagement, directStoreEmployeeCount);
        
        // 使用专门的工具类计算覆盖率
        return UsageRateUtil.calculateUsageRate(totalAssetManagement, directStoreEmployeeCount);
    }

    public Double getCoverageRate() {
        String startTime = DatetimeScopeUtil.getLastMonthStart();
        String endTime = DatetimeScopeUtil.getLastMonthEnd();
        // 提单月覆盖率(提单人数/轻改业务且有发平板的总人数)
        // 1. 获取 提单人数
        int actualOrderCount = inspectionBadgeRecordingsMapper.countDistinctWorkOrderNumbersBetweenTimes(startTime, endTime);
        // 2. 获取 轻改业务且有发平板的总人数
        TabletCountDTO totalAssetManagement = inspectionBadgeRecordingsMapper.countAllBizTotalAssetManagement();
        log.info("提单人数:{},轻改业务且有发平板的总人数:{}", actualOrderCount, totalAssetManagement);
        return UsageRateUtil.calculateUsageRate(actualOrderCount, totalAssetManagement.getLightUpgradeTabletCount());
    }

    public Map<String, Double> getBusinessWeekUsageRate() {
        String startTime = DatetimeScopeUtil.getLastWeekStart();
        String endTime = DatetimeScopeUtil.getLastWeekEnd();
        
        // 获取各业务类型的平板使用人数
        List<BusinessCoverageDTO> businessCoverageList = inspectionBadgeRecordingsMapper.getBusinessCoverageList(startTime, endTime);
        
        // 获取各种类型的平板总数
        TabletCountDTO tabletCountDTO = inspectionBadgeRecordingsMapper.countAllBizTotalAssetManagement();
        
        // 计算各业务的覆盖率
        Map<String, Double> coverageRates = new HashMap<>();
        // 初始化三个业务的覆盖率为0.0
        coverageRates.put("light_upgrade", 0.0);
        coverageRates.put("warranty", 0.0);
        coverageRates.put("equity", 0.0);
        
        for (BusinessCoverageDTO coverage : businessCoverageList) {
            String businessType = coverage.getCheckTypeLabel();
            int userCount = coverage.getTabletUserCount();
            
            // 根据业务类型获取对应的总平板数
            int totalTablets = 0;
            switch (businessType) {
                case "light_upgrade":
                    totalTablets = tabletCountDTO.getLightUpgradeTabletCount() != null ? tabletCountDTO.getLightUpgradeTabletCount() : 0;
                    break;
                case "warranty":
                    totalTablets = tabletCountDTO.getWarrantyTabletCount() != null ? tabletCountDTO.getWarrantyTabletCount() : 0;
                    break;
                case "equity":
                    totalTablets = tabletCountDTO.getEquityTabletCount() != null ? tabletCountDTO.getEquityTabletCount() : 0;
                    break;
                default:
                    // 对于未知业务类型,暂时跳过
                    continue;
            }
            
            // 计算覆盖率
            double coverageRate = UsageRateUtil.calculateUsageRate(userCount, totalTablets);
            coverageRates.put(businessType, coverageRate);
        }
        
        return coverageRates;
    }
    
    public Double getLongCourseCompletionRate() {
        String startTime = DatetimeScopeUtil.getLastMonthStart();
        String endTime = DatetimeScopeUtil.getLastMonthEnd();

        // 长课程月学习完成率(已完成观看长课程数量/下发长课程数量)
        LongCourseCompletionDTO completionDTO = inspectionBadgeRecordingsMapper.getLongCourseCompletion(startTime, endTime);
        
        if (completionDTO == null) {
            return 0.0;
        }
        
        log.info("长课程总数:{},已完成长课程数:{}", completionDTO.getTotalCourseCount(), completionDTO.getCompletedCourseCount());
        return UsageRateUtil.calculateUsageRate(
            completionDTO.getCompletedCourseCount() != null ? completionDTO.getCompletedCourseCount() : 0,
            completionDTO.getTotalCourseCount() != null ? completionDTO.getTotalCourseCount() : 0
        );
    }
    
    public Double getShortCourseCompletionRate() {
        String startTime = DatetimeScopeUtil.getLastMonthStart();
        String endTime = DatetimeScopeUtil.getLastMonthEnd();

        // 短课程月学习完成率(已完成观看短课程数量/下发短课程数量)
        ShortCourseCompletionDTO completionDTO = inspectionBadgeRecordingsMapper.getShortCourseCompletion(startTime, endTime);
        
        if (completionDTO == null) {
            return 0.0;
        }
        
        log.info("短课程总数:{},已完成短课程数:{}", completionDTO.getTotalCourseCount(), completionDTO.getCompletedCourseCount());
        return UsageRateUtil.calculateUsageRate(
            completionDTO.getCompletedCourseCount() != null ? completionDTO.getCompletedCourseCount() : 0,
            completionDTO.getTotalCourseCount() != null ? completionDTO.getTotalCourseCount() : 0
        );
    }

    public Double getExhibitionCoverageRate() {
        // 展业门店覆盖率(展业门店数/总门店数)
        // 1. 获取展业门店数(scene=7且subMerchantType=7)
        Integer exhibitionMerchantCount = inspectionBadgeRecordingsMapper.countDistinctExhibitionMerchants();
        // 2. 获取总门店数(subMerchantType=7)
        Integer totalMerchantCount = inspectionBadgeRecordingsMapper.countDistinctTotalMerchants();
        log.info("展业门店数:{},总门店数:{}", exhibitionMerchantCount, totalMerchantCount);
        
        // 使用专门的工具类计算覆盖率
        return UsageRateUtil.calculateUsageRate(
            exhibitionMerchantCount != null ? exhibitionMerchantCount : 0,
            totalMerchantCount != null ? totalMerchantCount : 0
        );
    }

    /**
     * 获取各业务下平板总数
     * @return 平板总数DTO
     */
    public TabletCountDTO getAllBizTabletCount() {
        return inspectionBadgeRecordingsMapper.countAllBizTotalAssetManagement();
    }
}