package com.ruoyi.benyun.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.benyun.entity.Brand;
import com.ruoyi.benyun.entity.StoreWd;
import com.ruoyi.benyun.mapper.BrandMapper;
import com.ruoyi.benyun.mapper.StoreWdDao;
import com.ruoyi.benyun.mapper.WdInfoMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class ClearJob implements Job {
    @Autowired
    BrandMapper brandMapper;

    @Autowired
    StoreWdDao storeWdDao;

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

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        ArrayList<String> moreBrandName = new ArrayList<>();
        ArrayList<String> existBrandName = new ArrayList<>();
        for (String brandName : brandNames) {
            //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());
                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){
                exception.printStackTrace();
                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);
        }
    }
}
