package com.ruoyi.demo.utils.statistics;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.demo.entity.Brand;
import com.ruoyi.demo.entity.BrandBusiness;
import com.ruoyi.demo.entity.BrandStatistics;
import com.ruoyi.demo.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.UUID;

@Component
public class BrandStat {
    @Autowired
    BrandDao brandMapper;
    @Autowired
    BrandZoneDao brandZoneDao;
    @Autowired
    StoreWdDao storeDao;
    @Autowired
    BrandStatisticsDao brandStatisticsDao;
    @Autowired
    BrandCityDao cityDao;
    @Autowired
    BrandProvinceDao provinceDao;
    @Autowired
    BrandBusinessDao businessDao;

    public void continueInsertPlus2(){
//        查询已统计的品牌
//        List<String> notIns = brandStatisticsDao.searchStated();
//        System.out.println("已处理品牌："+notIns.size()+"个");
//        处理剩下的
//        List<Brand> brands = brandMapper.searchNotIns(notIns);
        List<String> ins = brandZoneDao.selectIns();
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("brand_id",ins);
        List<Brand> brands = brandMapper.selectList(queryWrapper);
        int index = 0;
        int total = brands.size();
        for (Brand brand : brands){
            BrandStatistics statistics = new BrandStatistics();
            statistics.setStatId(UUID.randomUUID().toString().replace("-",""));
            statistics.setBrandId(brand.getBrandId());
            statistics.setBrandName(brand.getBrandName());
            statistics.setCoverProvinceCount(provinceDao.selectCountByBrandId(brand.getBrandId()));
            statistics.setCoverCityCount(cityDao.selectCountByBrandId(brand.getBrandId()));
            statistics.setCoverZoneCount(brandZoneDao.searchZoneCountByBrandId(brand.getBrandId()));

            QueryWrapper<BrandBusiness> nomalStatusQueryWrapper = new QueryWrapper<>();
            nomalStatusQueryWrapper.eq("brand_id",brand.getBrandId());
            nomalStatusQueryWrapper.eq("business_status","正常");
            BrandBusiness nomalStatus = businessDao.selectOne(nomalStatusQueryWrapper);
            if (nomalStatus == null)
                statistics.setBusinessCount(0);
            else
                statistics.setBusinessCount(nomalStatus.getCount());
//            statistics.setBusinessCount(storeDao.searchNomalStatusByBrandId(brand.getBrandId()));

            QueryWrapper<BrandBusiness> pauseStatusQueryWrapper = new QueryWrapper<>();
            pauseStatusQueryWrapper.eq("brand_id",brand.getBrandId());
            pauseStatusQueryWrapper.eq("business_status","暂停");
            BrandBusiness pauseStatus = businessDao.selectOne(pauseStatusQueryWrapper);
            if (pauseStatus == null)
                statistics.setPauseBusinessCount(0);
            else
                statistics.setPauseBusinessCount(pauseStatus.getCount());
//            statistics.setPauseBusinessCount(storeDao.searchPauseStatusByBrandId(brand.getBrandId()));

            QueryWrapper<BrandBusiness> noStatusQueryWrapper = new QueryWrapper<>();
            noStatusQueryWrapper.eq("brand_id",brand.getBrandId());
            noStatusQueryWrapper.eq("business_status","尚未营业");
            BrandBusiness noStatus = businessDao.selectOne(noStatusQueryWrapper);
            if (noStatus == null)
                statistics.setNoBusinessCount(0);
            else
                statistics.setNoBusinessCount(noStatus.getCount());
//            statistics.setNoBusinessCount(storeDao.searchNoStatusByBrandId(brand.getBrandId()));

            statistics.setTotal(statistics.getBusinessCount() + statistics.getPauseBusinessCount() + statistics.getNoBusinessCount());
            statistics.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
            index += brandStatisticsDao.insert(statistics);
            System.out.println(index + "/" + total);
        }
    }
}
