package com.ruoyi.demo.utils.statistics;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.demo.entity.AddrCategory;
import com.ruoyi.demo.entity.BrandCityTier;
import com.ruoyi.demo.mapper.AddrCategoryDao;
import com.ruoyi.demo.mapper.BrandCityTierMapper;
import com.ruoyi.demo.mapper.BrandDao;
import com.ruoyi.demo.mapper.BrandZoneDao;
import com.ruoyi.demo.utils.CategoryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class BrandCityTierStat {
    @Autowired
    CategoryUtil categoryUtil;
    @Autowired
    AddrCategoryDao addrCategoryDao;
    @Autowired
    BrandDao brandDao;
    @Autowired
    BrandZoneDao brandZoneDao;
    @Autowired
    BrandCityTierMapper tierMapper;

    public void start() {
        // 查询所有有效品牌
        List<String> brandIds = brandDao.selectAllId();
        int totalBrand = brandIds.size();
        // 查询城市等级类别
        List<String> tierList = addrCategoryDao.selectCityTierList(); // 目前实际上就5个
        Map<String, List<AddrCategory>> map = new LinkedHashMap<>();
        for (String tier : tierList) {
            // 根据类别查询地区信息
            QueryWrapper<AddrCategory> addrCategoryQueryWrapper = new QueryWrapper<>();
            addrCategoryQueryWrapper.eq("city_tier", tier)
                .eq("type","zone");
            map.put(tier, addrCategoryDao.selectList(addrCategoryQueryWrapper));
        }
        int i = 1;
        for (String brandId : brandIds) {
            // 遍历城市等级类别
            for (String tier : tierList) {
                // 根据每个类别的地区，统计其数量
                int total = 0;
                for (AddrCategory addr : map.get(tier))
                    total += brandZoneDao.selectTierCountByBrandIdAndCode(brandId, categoryUtil.whatis(addr.getAddrCode()));
                tierMapper.insert(new BrandCityTier(brandId, tier, total));
            }
            System.out.println(totalBrand + "/" + i);
            i++;
        }
    }

    public void continueInsert() {
        // 查询已完成统计的品牌
        List<String> notIns = tierMapper.selectAllId();
        System.out.println("已完成：" + notIns.size());
        // 查询未完成的有效品牌
        List<String> brandIds = brandDao.selectAllByNotIn(notIns);
        System.out.println("未完成：" + brandIds.size());
        int totalBrand = brandIds.size();
        // 查询城市等级类别
        List<String> tierList = addrCategoryDao.selectCityTierList(); // 目前实际上就5个
        Map<String, List<AddrCategory>> map = new LinkedHashMap<>();
        for (String tier : tierList) {
            // 根据类别查询地区信息
            QueryWrapper<AddrCategory> addrCategoryQueryWrapper = new QueryWrapper<>();
            map.put(tier, addrCategoryDao.selectList(addrCategoryQueryWrapper.eq("city_tier", tier)));
        }
        int i = 1;
        for (String brandId : brandIds) {
            // 遍历城市等级类别
            for (String tier : tierList) {
                // 根据每个类别的地区，统计其数量
                int total = 0;
                for (AddrCategory addr : map.get(tier))
                    total += brandZoneDao.selectTierCountByBrandIdAndCode(brandId, categoryUtil.whatis(addr.getAddrCode()));
                tierMapper.insert(new BrandCityTier(brandId, tier, total));
            }
            System.out.println(totalBrand + "/" + i);
            i++;
        }
    }

    public void newStart(){
        // 查询城市等级类别
        List<String> tierList = addrCategoryDao.selectCityTierList(); // 目前实际上就5个
        Map<String, List<String>> map = new LinkedHashMap<>();
        for (String tier : tierList) {
            // 根据类别查询地区信息
            map.put(tier, addrCategoryDao.selectCodeListByCityTier(tier));
        }
        // 查询未完成统计或者因出错而统计到一半的品牌
        List<String> noUse = tierMapper.selectNoUse(tierList.size());
        // 删除这些无效统计信息
        if (!noUse.isEmpty())
            tierMapper.deleteAllById(noUse);
        System.out.println("删除无效数据：" + noUse.size() + "条");
        // 查询已完成统计的品牌
        List<String> notIns = tierMapper.selectAllId();
        System.out.println("已完成：" + notIns.size());
        // 查询未完成的有效品牌
        List<String> brandIds = brandDao.selectAllByNotIn(notIns);
        System.out.println("未完成：" + brandIds.size());
        int totalBrand = brandIds.size();

        int i = 1;
        for (String brandId : brandIds) {
            // 遍历城市等级类别
            for (String tier : tierList) {
                // 根据每个类别的地区，统计其数量
                int total = 0;
                total += brandZoneDao.selectTierCountByBrandIdAndCodes(brandId, map.get(tier));
                tierMapper.insert(new BrandCityTier(brandId, tier, total));
            }
            System.out.println(totalBrand + "/" + i);
            i++;
        }
    }
}
