package com.fs.company.controller.common;

import com.alibaba.fastjson.JSON;
import com.fs.ad.enums.AdUploadType;
import com.fs.common.annotation.DataSource;
import com.fs.common.enums.DataSourceType;
import com.fs.qw.vo.AdUploadVo;
import com.fs.sop.service.IQwSopTempContentService;
import com.fs.sop.service.IQwSopTempDayService;
import com.fs.sop.service.IQwSopTempRulesService;
import com.fs.sop.service.IQwSopTempService;
import lombok.AllArgsConstructor;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@AllArgsConstructor
public class Test {

    private final IQwSopTempService qwSopTempService;
    private final IQwSopTempRulesService qwSopTempRulesService;
    private final IQwSopTempDayService qwSopTempDayService;
    private final IQwSopTempContentService qwSopTempContentService;
    private final RocketMQTemplate rocketMQTemplate;

//    @GetMapping("test")
//    public void fileDownload(){
////        List<QwSopTempRules> rulesList = qwSopTempRulesService.listByTempId("3922c166-a539-4a64-a535-d4feafc096c3");
//        List<QwSopTemp> tempList = qwSopTempService.listTemp().stream().filter(e -> StringUtils.isNotEmpty(e.getSetting())).collect(Collectors.toList());
//        Map<String, QwSopTemp> tempMap = PubFun.listToMapByGroupObject(tempList, QwSopTemp::getId);
//        Map<String, List<QwSopTempSetting2>> collect = tempList.stream().collect(Collectors.toMap(QwSopTemp::getId, e -> JSONArray.parseArray(e.getSetting(), QwSopTempSetting2.class)));
//        collect.forEach((k, rules) -> {
//            QwSopTemp qwSopTemp = tempMap.get(k);
//            for (int i = 0; i < rules.size(); i++) {
//                QwSopTempSetting2 e = rules.get(i);
//                QwSopTempDay qwSopTempDay = new QwSopTempDay();
//                qwSopTempDay.setSorts(i);
//                qwSopTempDay.setName(e.getName());
//                qwSopTempDay.setDayNum((qwSopTempDay.getSorts() * qwSopTemp.getGap()) + 1);
//                qwSopTempDay.setTempId(k);
//                qwSopTemp.getList().add(qwSopTempDay);
//                for (int i1 = 0; i1 < e.getContent().size(); i1++) {
//                    QwSopTempRules qwSopTempRules = new QwSopTempRules();
//                    qwSopTempRules.setTempId(k);
//                    qwSopTempRules.setName(e.getName());
//                    qwSopTempRules.setSorts(i1);
//                    QwSopTempSetting2.Content vo = e.getContent().get(i1);
//                    qwSopTempRules.setTime(vo.getTime());
//                    qwSopTempRules.setType(vo.getType());
//                    qwSopTempRules.setContentType(StringUtils.isNotEmpty(vo.getContentType()) ? Integer.parseInt(vo.getContentType()) : null);
//                    qwSopTempRules.setCourseType(vo.getCourseType());
//                    qwSopTempRules.setCourseId(vo.getCourseId());
//                    qwSopTempRules.setVideoId(vo.getVideoId());
//                    qwSopTempRules.setAiTouch(vo.getAiTouch());
//                    qwSopTempDay.getList().add(qwSopTempRules);
//                    for (int i2 = 0; i2 < vo.getSetting().size(); i2++) {
//                        QwSopTempSetting2.Content.Setting setting = vo.getSetting().get(i2);
//                        QwSopTempContent qwSopTempContent = new QwSopTempContent();
//                        qwSopTempContent.setTempId(k);
//                        qwSopTempContent.setContentType(StringUtils.isNotEmpty(setting.getContentType()) ? Integer.parseInt(setting.getContentType()) : null);
//                        qwSopTempContent.setContent(JSON.toJSONString(setting));
//                        qwSopTempContent.setIsBindUrl(setting.getIsBindUrl());
//                        qwSopTempContent.setExpiresDays(setting.getExpiresDays());
//                        qwSopTempRules.getList().add(qwSopTempContent);
//                    }
//                }
//            }
//        });
////        System.out.println(1);
//        tempList.parallelStream().forEach(qwSopTemp -> {
////            new Thread(() -> {
//                qwSopTempDayService.saveList(qwSopTemp.getList());
//                List<QwSopTempRules> ruleList = qwSopTemp.getList().stream().flatMap(e -> e.getList().stream().peek(s -> s.setDayId(e.getId()))).collect(Collectors.toList());
//                qwSopTempRulesService.saveList(ruleList);
//                List<QwSopTempContent> contentList = ruleList.stream().flatMap(s -> s.getList().stream().peek(c -> c.setRulesId(s.getId()))).collect(Collectors.toList());
//                qwSopTempContentService.saveList(contentList);
////            }).start();
//        });
//    }
//    @GetMapping("test")
//    @DataSource(DataSourceType.SOP)
//    public void fileDownload(){
//        List<QwSopTempContent> contentList = qwSopTempContentService.list(new QueryWrapper<QwSopTempContent>().isNull("day_id"));
//        List<Long> longs = PubFun.listToNewList(contentList, QwSopTempContent::getRulesId);
//        List<QwSopTempRules> ruleList = qwSopTempRulesService.list(new QueryWrapper<QwSopTempRules>().in("id", longs));
//        Map<Long, QwSopTempRules> rulesMap = PubFun.listToMapByGroupObject(ruleList, QwSopTempRules::getId);
//        contentList.stream().filter(e ->rulesMap.containsKey(e.getRulesId())).forEach(qwSopTempContent -> {
//            qwSopTempContent.setDayId(rulesMap.get(qwSopTempContent.getRulesId()).getDayId());
//        });
//        contentList.forEach(qwSopTempContentService::updateDay);
//    }
    @GetMapping("test")
    @DataSource(DataSourceType.SOP)
    public void fileDownload(){
        rocketMQTemplate.syncSend("ad-upload", JSON.toJSONString(AdUploadVo.builder().state("测试").type(AdUploadType.ADD_WX).build()));
    }
}
