package com.ruoyi.benyun.utils.statistics;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.benyun.entity.Brand;
import com.ruoyi.benyun.entity.BrandProvince;
import com.ruoyi.benyun.mapper.BrandCityDao;
import com.ruoyi.benyun.mapper.BrandDao;
import com.ruoyi.benyun.mapper.BrandProvinceDao;
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 BrandProvinceStat {
    @Autowired
    BrandDao brandDao;
    @Autowired
    BrandProvinceDao brandProvinceDao;
    @Autowired
    BrandCityDao cityDao;

    public void continueInsert(){
//        List<String> notIns = brandProvinceDao.selectIns();
//        System.out.println("已处理：" + notIns.size() + "个");
//        List<Brand> brands = brandDao.searchNotIns(notIns);
        List<String> ins = cityDao.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<BrandProvince> provinces = brandProvinceDao.searchByBrandIdOrderByProvince(brand.getBrandId(),month);
            if (provinces.isEmpty()){
                System.out.println(index+"/"+total);
                index++;
                continue;
            }
            for (BrandProvince bp : provinces){
                bp.setDisId(UUID.randomUUID().toString().replace("-",""));
                bp.setBrandId(brand.getBrandId());
                bp.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
            }
            brandProvinceDao.insertAll(provinces);
            System.out.println(index+"/"+total);
            index++;
        }
    }
}
