package cn.iocoder.yudao.module.product.service.statistics;

import cn.iocoder.yudao.module.basic.api.goodsinfo.GoodsInfoApi;
import cn.iocoder.yudao.module.basic.api.goodsinfo.dto.GoodsInfoRespDTO;
import cn.iocoder.yudao.module.product.controller.admin.envdata.vo.EnvDataRespVO;
import cn.iocoder.yudao.module.product.controller.admin.statistics.vo.HarvestInfoDataRespVO;
import cn.iocoder.yudao.module.product.controller.admin.statistics.vo.TopDataRespVO;
import cn.iocoder.yudao.module.product.dal.mysql.envdata.EnvDataMapper;
import cn.iocoder.yudao.module.product.dal.mysql.harvestinfo.HarvestInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * 统计数据 service 实现类
 */
@Service
@Validated
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    private HarvestInfoMapper harvestInfoMapper;

    @Resource
    private EnvDataMapper envDataMapper;

    @Resource
    private GoodsInfoApi goodsInfoApi;

    @Override
    public TopDataRespVO getTopDataStatistics() {
        TopDataRespVO result = new TopDataRespVO();

        List<GoodsInfoRespDTO> goodsInfoList = goodsInfoApi.getAllGoodsList().getData();
        if (goodsInfoList != null) {
            result.setGoodsTypeCount(goodsInfoList.size());
        } else {
            result.setGoodsTypeCount(BigInteger.ZERO.intValue());
        }

        // 查询产量
        BigDecimal nums = harvestInfoMapper.getNumStatisticsOfYear();
        if (nums != null) {
            result.setTotalHarvestCount(nums);
        } else {
            result.setTotalHarvestCount(BigDecimal.ZERO);
        }
        return result;
    }

    @Override
    public HarvestInfoDataRespVO getHarvestInfoDataStatistics() {
        HarvestInfoDataRespVO result = new HarvestInfoDataRespVO();
        // 查询月同比
        result.setMonthYOYDatas(harvestInfoMapper.getMonthYOYDatas());

        // 查询周同比
        result.setWeekYOYDatas(harvestInfoMapper.getWeekYOYDatas());

        // 查询本月采收，按照品种分组
        result.setGoodsMonthStatisticsDatas(harvestInfoMapper.getDatasGroupByGoodsId("month"));

        // 查询本周采收，按照品种分组
        result.setGoodsWeekStatisticsDatas(harvestInfoMapper.getDatasGroupByGoodsId("week"));

        // 查询今日采收数量
        result.setTodayHarvestCount(harvestInfoMapper.getTodayHarvestCount());
        return result;
    }

    @Override
    public List<EnvDataRespVO> getAvgEnvDataStatistics() {
        return envDataMapper.getAVGEnvDatas(null, 7, null);

    }
}
