package com.ruoyi.benyun;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.benyun.constant.RedisContant;
import com.ruoyi.benyun.constant.RedisInitContant;
import com.ruoyi.benyun.entity.Brand;
import com.ruoyi.benyun.entity.BrandStatistics;
import com.ruoyi.benyun.entity.StoreWd;
import com.ruoyi.benyun.mapper.BrandMapper;
import com.ruoyi.benyun.mapper.BrandStatisticsMapper;
import com.ruoyi.benyun.mapper.StoreWdDao;
import com.ruoyi.benyun.mapper.WdInfoDao;
//import com.ruoyi.benyun.service.WriteService;
import com.ruoyi.benyun.utils.WdRedisStoreage;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@SpringBootTest
class BenyunCoreApplicationTests {
    @Autowired
    WdRedisStoreage wdRedisStoreage;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    WdInfoDao wdInfoDao;

    //@Autowired
    //WriteService writeService;

    @Value("${clear.BrandNames}")
    String[] brandNames;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    BrandStatisticsMapper brandStatisticsMapper;

    @Autowired
    StoreWdDao storeWdDao;

    @Test
    void contextLoads() {
        QueryWrapper<BrandStatistics> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.select("brand_name");
        queryWrapper3.ge("total",30);
        queryWrapper3.orderByDesc("total");
        List<BrandStatistics> brandStatistics = brandStatisticsMapper.selectList(queryWrapper3);
        List<String> collect1 = brandStatistics.stream().map(item -> {
            return item.getBrandName();
        }).collect(Collectors.toList());

        ArrayList<String> moreBrandName = new ArrayList<>();
        ArrayList<String> existBrandName = new ArrayList<>();
        int i = 1;
        for (String brandName : collect1) {
            System.out.println((i++) + "/"+collect1.size());
            //1.找到主品牌
            QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("brand_id","brand_name");
            queryWrapper.eq("brand_name",brandName);
            try {
                Brand brand = brandMapper.selectOne(queryWrapper);
                if (brand == null){
                    existBrandName.add(brandName);
                    continue;
                }


                //2.找到其他类似的品牌名
                QueryWrapper<Brand> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.select("brand_id","brand_name");
                queryWrapper1.like("brand_name",brandName);

                queryWrapper1.and(brandQueryWrapper -> {
                    brandQueryWrapper.ne("brand_id",brand.getBrandId());
                });
                List<Brand> brands = brandMapper.selectList(queryWrapper1);

                //3.修改对应的品牌绑定
                List<Object> collect = brands.stream().map(item -> {
                    return item.getBrandId();
                }).collect(Collectors.toList());
                if (collect.isEmpty())
                    continue;

                UpdateWrapper<StoreWd> updateWrapper = new UpdateWrapper<>();
                updateWrapper.set("brand_id",brand.getBrandId());
                updateWrapper.set("brand_name",brand.getBrandName());
                updateWrapper.in("brand_id",collect);
                storeWdDao.update(null,updateWrapper);

                //4.修改完毕后，删除对应的品牌信息
                QueryWrapper<Brand> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.in("brand_id",collect);
                brandMapper.delete(queryWrapper2);
            }catch (Exception exception){
                System.out.println("多个: "+brandName);
                moreBrandName.add(brandName);
            }
        }

        System.out.println("不存在品牌：");
        for (String s : existBrandName) {
            System.out.println(s);
        }

        System.out.println("出错品牌：");
        for (String s : moreBrandName) {
            System.out.println(s);
        }
    }


}
