package cn.kk.spring_simple_operation;

import cn.kk.spring_simple_operation.entity.*;
import cn.kk.spring_simple_operation.mapper.*;
import cn.kk.spring_simple_operation.model.vo.BrandRecordStatisticsVo;
import cn.kk.spring_simple_operation.model.vo.SkuAPlusImageAndVideoVO;
import cn.kk.spring_simple_operation.utils.DateUtils;
import cn.kk.spring_simple_operation.utils.ExcelUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.File;
import java.nio.file.Files;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author kk
 * @date 2026/4/27
 */
@SpringBootTest
public class MyTest2 {

	private static final Logger log = LoggerFactory.getLogger(MyTest2.class);
	@Resource
	private APlusProductMapper aPlusProductMapper;

	@Resource
	private VisualCommunicationDesignSecondMapper visualCommunicationDesignSecondMapper;

	@Resource
	private VideoProductMapper videoProductMapper;

	@Resource
	private VisualVideoPlanMapper visualVideoPlanMapper;

	@Resource
	private VisualAPlusPlanMapper visualAPlusPlanMapper;

	@Resource
	private VisualTaskWeightMapper visualTaskWeightMapper;


	@Test
	public void dealSimpleAPlusPlanTime() {
		ArrayList<String> skuList = Lists.newArrayList("STY0089", "IFS2835", "DF1061", "IF2216", "VEA0926", "BV4469", "STY1329", "TAY0257", "LOO2703", "XEU1036", "LOM0105", "XUL0975", "UR1221", "ISI0782", "LIC9145", "HOD5141", "LEM6389", "DF4494", "WAR1487", "PIR5107", "QUN2115", "ANB2765", "JY1066", "RY3111", "SIG0377", "TEA2739", "XEU1563", "YOG1732", "NEV2980", "IG2159", "FLO3335", "ZHI0065", "MZ2724", "JUU0439", "HOD2404", "DF2869", "MM0881", "DUN3755", "HAI6132", "JYY2734", "GUR0168", "HUM1645", "GUU2335", "QUQ1018", "GUR3956", "CAS3083", "GOR2056", "QUN3648", "XIN2813", "FS2247", "HUI2255", "YUN2250", "XAO3558", "YOF1784", "JIA0971", "GUI0229", "GOO2940");
		List<VisualAPlusPlan> visualAPlusPlans = visualAPlusPlanMapper.selectList(Wrappers.<VisualAPlusPlan>lambdaQuery().in(VisualAPlusPlan::getSku, skuList).select(VisualAPlusPlan::getId, VisualAPlusPlan::getSku));
		List<String> weightIds = new ArrayList<>();
		List<String> planIds = new ArrayList<>();

		visualAPlusPlans.forEach(item -> {
			VisualTaskWeight weight = visualTaskWeightMapper.selectOne(Wrappers.<VisualTaskWeight>lambdaQuery().eq(VisualTaskWeight::getTaskId, item.getId()).eq(VisualTaskWeight::getTaskType, 1).orderByAsc(VisualTaskWeight::getCreateTime).last("limit 1"));
			if (Objects.isNull(weight)) return;
			planIds.add(String.valueOf(item.getId()));
			weightIds.add(String.valueOf(weight.getId()));
		});

		log.info("update visual_a_plus_plan set finish_time = 1774101255 where id in ({});", String.join(",", planIds));
		log.info("update visual_task_weight set weight = 1.5, task_time = 1774101255 where id in ({});", String.join(",", weightIds));

	}

	@Test
	public void exportSkuImage() throws Exception {
		ExcelUtil<SkuAPlusImageAndVideoVO> util = new ExcelUtil<>(SkuAPlusImageAndVideoVO.class);
		List<SkuAPlusImageAndVideoVO> list = util.importExcel(Files.newInputStream(new File("21716-珠宝首饰产品前台图片、A＋和视频导出(1).xlsx").toPath()));

		//list = list.stream().filter(t -> "ST2177".equals(t.getSku())).collect(Collectors.toList());

		Lists.partition(list, 1000)
				.forEach(ll -> {
					List<String> skuList = ll.stream().map(SkuAPlusImageAndVideoVO::getSku).collect(Collectors.toList());
					List<APlusProduct> aPlusProducts = aPlusProductMapper.selectList(Wrappers.<APlusProduct>lambdaQuery().in(APlusProduct::getSku, skuList).select(APlusProduct::getSku, APlusProduct::getSite, APlusProduct::getCommunicationDesignId, APlusProduct::getIsUploadBackstage, APlusProduct::getCreateTime));

					Set<Long> cIds = aPlusProducts.stream().map(APlusProduct::getCommunicationDesignId).collect(Collectors.toSet());
					Map<Long, String> imageMap = visualCommunicationDesignSecondMapper.selectList(Wrappers.<VisualCommunicationDesign>lambdaQuery().in(VisualCommunicationDesign::getId, cIds).select(VisualCommunicationDesign::getId, VisualCommunicationDesign::getFileUrl))
							.stream().collect(Collectors.toMap(VisualCommunicationDesign::getId, VisualCommunicationDesign::getFileUrl));

					Map<String, List<APlusProduct>> skuSiteAPlusMap = aPlusProducts.stream().collect(Collectors.groupingBy(t -> t.getSku() + "-" + t.getSite()));

					for (SkuAPlusImageAndVideoVO dto : ll) {
						List<APlusProduct> plusProducts = skuSiteAPlusMap.get(dto.getSku() + "-" + dto.getSite());
						if (CollectionUtils.isEmpty(plusProducts)) continue;
						plusProducts.sort((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime()));
						for (APlusProduct plusProduct : plusProducts) {
							String image = imageMap.get(plusProduct.getCommunicationDesignId());
							if (!StringUtils.isEmpty(image)) {
								dto.setAPlusImage(image);
								break;
							}
						}
					}


					List<VideoProduct> videoProducts = videoProductMapper.selectList(Wrappers.<VideoProduct>lambdaQuery().in(VideoProduct::getSku, skuList).in(VideoProduct::getOptimizeType, 0, 1).select(VideoProduct::getSku, VideoProduct::getSite, VideoProduct::getVideoPlanId, VideoProduct::getVideoIsUploadBackstage, VideoProduct::getCreateTime));

					Set<Long> planIds = videoProducts.stream().map(VideoProduct::getVideoPlanId).collect(Collectors.toSet());
					Map<Long, String> videoMap = visualVideoPlanMapper.selectList(Wrappers.<VisualVideoPlan>lambdaQuery().in(VisualVideoPlan::getId, planIds).select(VisualVideoPlan::getId, VisualVideoPlan::getLongVideoUrl))
							.stream().collect(Collectors.toMap(VisualVideoPlan::getId, VisualVideoPlan::getLongVideoUrl));
					Map<String, List<VideoProduct>> skuSiteVideoMap = videoProducts.stream().collect(Collectors.groupingBy(t -> t.getSku() + "-" + t.getSite()));

					for (SkuAPlusImageAndVideoVO dto : ll) {
						List<VideoProduct> videoProductList = skuSiteVideoMap.get(dto.getSku() + "-" + dto.getSite());
						if (CollectionUtils.isEmpty(videoProductList)) continue;
						videoProductList.sort((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime()));
						for (VideoProduct videoProduct : videoProductList) {
							String video = videoMap.get(videoProduct.getVideoPlanId());
							if (!StringUtils.isEmpty(video)) {
								dto.setVideoUrl(video);
								break;
							}
						}
					}

				});

		util = new ExcelUtil<SkuAPlusImageAndVideoVO>(SkuAPlusImageAndVideoVO.class);
		util.exportExcelFile(list, "21716-珠宝首饰产品前台图片、A＋和视频导出-.xlsx");

	}
}
