package com.fs.company.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.DateUtils;
import com.fs.company.domain.CompanyDetectionPhoneDailyStatistics;
import com.fs.company.mapper.CompanyDetectionPhoneDailyStatisticsMapper;
import com.fs.company.service.ICompanyDetectionPhoneDailyStatisticsService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 机型检测日统计Service业务层处理
 * 
 * @author fs
 * @date 2025-12-17
 */
@Service
public class CompanyDetectionPhoneDailyStatisticsServiceImpl extends ServiceImpl<CompanyDetectionPhoneDailyStatisticsMapper, CompanyDetectionPhoneDailyStatistics> implements ICompanyDetectionPhoneDailyStatisticsService {

    /**
     * 查询机型检测日统计
     * 
     * @param id 机型检测日统计主键
     * @return 机型检测日统计
     */
    @Override
    public CompanyDetectionPhoneDailyStatistics selectCompanyDetectionPhoneDailyStatisticsById(Long id)
    {
        return baseMapper.selectCompanyDetectionPhoneDailyStatisticsById(id);
    }

    /**
     * 查询机型检测日统计列表
     * 
     * @param companyDetectionPhoneDailyStatistics 机型检测日统计
     * @return 机型检测日统计
     */
    @Override
    public List<CompanyDetectionPhoneDailyStatistics> selectCompanyDetectionPhoneDailyStatisticsList(CompanyDetectionPhoneDailyStatistics companyDetectionPhoneDailyStatistics)
    {
        return baseMapper.selectCompanyDetectionPhoneDailyStatisticsList(companyDetectionPhoneDailyStatistics);
    }

    /**
     * 新增机型检测日统计
     * 
     * @param companyDetectionPhoneDailyStatistics 机型检测日统计
     * @return 结果
     */
    @Override
    public int insertCompanyDetectionPhoneDailyStatistics(CompanyDetectionPhoneDailyStatistics companyDetectionPhoneDailyStatistics)
    {
        companyDetectionPhoneDailyStatistics.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertCompanyDetectionPhoneDailyStatistics(companyDetectionPhoneDailyStatistics);
    }

    /**
     * 修改机型检测日统计
     * 
     * @param companyDetectionPhoneDailyStatistics 机型检测日统计
     * @return 结果
     */
    @Override
    public int updateCompanyDetectionPhoneDailyStatistics(CompanyDetectionPhoneDailyStatistics companyDetectionPhoneDailyStatistics)
    {
        return baseMapper.updateCompanyDetectionPhoneDailyStatistics(companyDetectionPhoneDailyStatistics);
    }

    /**
     * 批量删除机型检测日统计
     * 
     * @param ids 需要删除的机型检测日统计主键
     * @return 结果
     */
    @Override
    public int deleteCompanyDetectionPhoneDailyStatisticsByIds(Long[] ids)
    {
        return baseMapper.deleteCompanyDetectionPhoneDailyStatisticsByIds(ids);
    }

    /**
     * 删除机型检测日统计信息
     * 
     * @param id 机型检测日统计主键
     * @return 结果
     */
    @Override
    public int deleteCompanyDetectionPhoneDailyStatisticsById(Long id)
    {
        return baseMapper.deleteCompanyDetectionPhoneDailyStatisticsById(id);
    }

    /**
     * 重新计算统计数据
     * @param detectionSystemType 1苹果 2安卓 3其他
     */
    @Override
    public void recalculateStatistics(Long companyId, String companyName, Long companyUserId, String companyUserName, Date detectionTime, int detectionSystemType) {
        // 1. 查询当天是否已有统计记录
        String dateStr = DateUtils.parseDateToStr("yyyy-MM-dd", detectionTime);
        Date toDay = DateUtils.dateTime(DateUtils.YYYY_MM_DD, dateStr);
        CompanyDetectionPhoneDailyStatistics existingStatictics = baseMapper.selectByCompanyUserAndDate(
                companyUserId, toDay);
        if(existingStatictics != null){
            //更新操作
            existingStatictics.setCompanyId(companyId);
            existingStatictics.setCompanyName(companyName);
            existingStatictics.setCompanyUserName(companyUserName);
            updateExistingStatistics(existingStatictics, detectionSystemType);
            baseMapper.updateCompanyDetectionPhoneDailyStatistics(existingStatictics);
        }else{
            //新增
            CompanyDetectionPhoneDailyStatistics newStats = createNewStatistics(
                    companyId, companyName, companyUserId, companyUserName, detectionTime, detectionSystemType);
            baseMapper.insertCompanyDetectionPhoneDailyStatistics(newStats);

        }
    }

    /**
     * 创建新的统计记录
     */
    private CompanyDetectionPhoneDailyStatistics createNewStatistics(Long companyId, String companyName,
                                                                     Long companyUserId, String companyUserName,
                                                                     Date detectionTime, int detectionSystemType) {
        CompanyDetectionPhoneDailyStatistics statistics = new CompanyDetectionPhoneDailyStatistics();
        statistics.setCompanyId(companyId);
        statistics.setCompanyName(companyName);
        statistics.setCompanyUserId(companyUserId);
        statistics.setCompanyUserName(companyUserName);
        statistics.setDetectionTime(detectionTime);

        // 根据检测类型初始化计数
        int azCount = 0, iosCount = 0, unknownCount = 0;
        switch (detectionSystemType) {
            case 1: // 苹果
                iosCount = 1;
                break;
            case 2: // 安卓
                azCount = 1;
                break;
            default: // 其他
                unknownCount = 1;
                break;
        }
        statistics.setAzCount(azCount);
        statistics.setIosCount(iosCount);
        statistics.setUnknownCount(unknownCount);
        statistics.setTotalCount(azCount + iosCount + unknownCount);
        statistics.setCreateTime(new Date());
        return statistics;
    }


    /**
     * 更新现有统计记录
     */
    private void updateExistingStatistics(CompanyDetectionPhoneDailyStatistics statistics, int detectionSystemType) {
        // 根据检测类型增加对应计数
        switch (detectionSystemType) {
            case 1: // 苹果
                statistics.setIosCount(statistics.getIosCount() + 1);
                break;
            case 2: // 安卓
                statistics.setAzCount(statistics.getAzCount() + 1);
                break;
            default: // 其他
                statistics.setUnknownCount(statistics.getUnknownCount() + 1);
                break;
        }
        // 重新计算总数，保证 az_count + ios_count + unknown_count = total_count
        int newTotal = statistics.getAzCount() + statistics.getIosCount() + statistics.getUnknownCount();
        statistics.setTotalCount(newTotal);

        // 更新最后检测时间
        statistics.setDetectionTime(new Date());
    }
}
