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.BrandCity;
import com.ruoyi.demo.mapper.BrandCityDao;
import com.ruoyi.demo.mapper.BrandDao;
import com.ruoyi.demo.mapper.BrandZoneDao;
import com.ruoyi.demo.mapper.StoreWdDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class BrandCityStat {
    @Autowired
    BrandDao brandDao;
    @Autowired
    BrandCityDao brandCityDao;
    @Autowired
    BrandZoneDao brandZoneDao;
    @Autowired
    StoreWdDao storeDao;

    public void continueInsert(){
//        List<String> ins = brandCityDao.selectIns();
//        System.out.println("已处理：" + notIns.size() + "个");
//        List<Brand> brands = brandDao.searchNotIns(notIns);
        List<String> ins = brandZoneDao.selectIns();
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("brand_id",ins);
        List<Brand> brands = brandDao.selectList(queryWrapper);
        int index = 1;
        int total = brands.size();
        for (Brand brand : brands){
//            统计本月
            String month = LocalDateTime.now(ZoneId.of("Asia/Shanghai")).format(DateTimeFormatter.ofPattern("yyyy-MM"));
            List<BrandCity> cities = brandCityDao.searchByBrandIdOrderByCity(brand.getBrandId(),month);
            if (cities.isEmpty()){
                System.out.println(index+"/"+total);
                index++;
                continue;
            }
            for (BrandCity city : cities){
                city.setDisId(UUID.randomUUID().toString().replace("-",""));
                city.setBrandId(brand.getBrandId());
                city.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
            }
            brandCityDao.insertAll(cities);
            System.out.println(index+"/"+total);
            index++;
        }
    }
}
