package com.ruoyi.web.modules.trading.service.impl;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.modules.trading.domain.dto.AppSupplierAmountBucketDto;
import com.ruoyi.web.modules.trading.domain.dto.AppSupplierSettlementSummaryDto;
import com.ruoyi.web.modules.trading.domain.dto.AppSupplierTradeSummaryDto;
import com.ruoyi.web.modules.trading.domain.vo.AppSupplierHomeDashboardVo;
import com.ruoyi.web.modules.trading.domain.vo.AppSupplierYakStatusVo;
import com.ruoyi.web.modules.trading.mapper.AppSupplierHomeMapper;
import com.ruoyi.web.modules.trading.service.IAppSupplierHomeService;
import com.ruoyi.web.modules.trading.support.AppSupplierHomeRules;
import com.ruoyi.web.modules.trading.support.AppSupplierHomeSupport;
import com.ruoyi.web.modules.trading.support.AppSupplierSessionSupport;

/**
 * 供应商小程序首页统计。
 */
@Service
public class AppSupplierHomeServiceImpl implements IAppSupplierHomeService
{
    @Autowired
    private AppSupplierSessionSupport appSupplierSessionSupport;

    @Autowired
    private AppSupplierHomeMapper appSupplierHomeMapper;

    @Override
    public AppSupplierHomeDashboardVo loadDashboard(String range)
    {
        String normalized = AppSupplierHomeSupport.normalizeRange(range);
        Long supplierId = appSupplierSessionSupport.requireSupplierId();
        AppSupplierHomeSupport.DateRange dateRange = AppSupplierHomeSupport.resolveRange(normalized);
        Date start = AppSupplierHomeSupport.toDayStart(dateRange.startDay);
        Date end = AppSupplierHomeSupport.toDayEnd(dateRange.endDay);

        AppSupplierTradeSummaryDto tradeSummary = appSupplierHomeMapper.selectTradeSummary(supplierId, start, end);
        AppSupplierSettlementSummaryDto settlementSummary = appSupplierHomeMapper.selectSettlementSummary(
                supplierId, start, end);

        List<AppSupplierAmountBucketDto> buckets;
        if (AppSupplierHomeRules.TREND_GRANULARITY_HOUR.equals(dateRange.trendGranularity))
        {
            buckets = appSupplierHomeMapper.selectHourlyAmountTrend(supplierId, start, end);
        }
        else
        {
            buckets = appSupplierHomeMapper.selectDailyAmountTrend(supplierId, start, end);
        }

        AppSupplierYakStatusVo yakStatus = loadYakStatus(supplierId);

        return AppSupplierHomeSupport.buildDashboard(
                normalized, tradeSummary, settlementSummary, buckets, dateRange, yakStatus);
    }

    private AppSupplierYakStatusVo loadYakStatus(Long supplierId)
    {
        LocalDate today = LocalDate.now(ZoneId.systemDefault());
        Date dayStart = AppSupplierHomeSupport.toDayStart(today);
        Date dayEnd = AppSupplierHomeSupport.toDayEnd(today);

        Long tradedHeads = appSupplierHomeMapper.selectTodayTradedHeads(supplierId, dayStart, dayEnd);
        Long enteredPenEarTags = appSupplierHomeMapper.countTodayEntryEarTags(supplierId, dayStart, dayEnd);
        Long pendingPayHeads = appSupplierHomeMapper.selectPendingPayHeads(supplierId);
        List<String> activeRows = appSupplierHomeMapper.selectActiveIsolationEarTagRows(supplierId);
        List<String> sickRows = appSupplierHomeMapper.selectSickDeadEarTagRowsBySupplier(supplierId);

        return AppSupplierHomeSupport.buildYakStatus(
                tradedHeads, enteredPenEarTags, pendingPayHeads, activeRows, sickRows);
    }
}
