package com.zhongshu.card.server.core.service.quest

import com.github.microservice.net.ResultContent
import com.zhongshu.card.client.model.quest.QuestInfoSearch
import com.zhongshu.card.client.model.quest.QuestTitleModel
import com.zhongshu.card.client.utils.DateUtils
import com.zhongshu.card.server.core.dao.quest.QuestInfoDao
import com.zhongshu.card.server.core.dao.quest.QuestionCleanDao
import com.zhongshu.card.server.core.domain.quest.QuestInfo
import com.zhongshu.card.server.core.domain.quest.QuestionClean
import com.zhongshu.card.server.core.util.BeanUtils
import com.zhongshu.card.server.core.util.ExcelUtils
import com.zhongshu.card.server.core.util.excel.CommonExeclParam
import com.zhongshu.card.server.core.util.excel.CommonExeclTd
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.apache.commons.lang3.StringUtils
import org.bson.types.ObjectId
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Sort
import org.springframework.data.redis.connection.SortParameters
import org.springframework.stereotype.Service
import org.springframework.util.Assert

import java.util.stream.Collectors

@Service
class DataClean {

    @Autowired
    QuestInfoDao questInfoDao;

    @Autowired
    QuestionCleanDao questionCleanDao;

    public Object clean(){

        double probabilityOne = 0.7; // 80%的概率生成1
        double probabilityTwo = 0.5; // 50%的概率生成2
        double probabilitySeven = 0.6;

        Map<Integer, String> school = new HashMap<>();
        school.put(7, "体育学院");
        school.put(8, "外国语学院");
        school.put(9, "马克思主义学院");
        school.put(10, "电子与信息工程学院")
        school.put(11, "化学化工学院")
        school.put(12, "农学院")
        school.put(13, "数理学院")
        school.put(14, "资源与环境工程学院")
        school.putAll(Map.of(1,"教育科学学院", 2,"经济与管理学院", 3,"人文学院", 4,"旅游学院", 5, "艺术学院", 6,"政法学院"))

        List<QuestInfo> list = questInfoDao.findAll();
        Long size = 2500/list.size();

        List<QuestInfo> newList = new ArrayList<>();

        List<QuestInfo> cleanList = list.stream().map {it->   {
            Object data = it.getData();
            List<LinkedHashMap> linkedHashMaps = (List<LinkedHashMap>) data;
            for (LinkedHashMap linkedHashMap : linkedHashMaps){
                String questionId = linkedHashMap["questionId"].toString()

                if (questionId.equals("q1")){
                    Random random = new Random();
                    Integer number = random.nextInt(1, 14);
                    linkedHashMap.put("answer", Map.of("value", school.getOrDefault(number, "数理学院")))
                }

                if (questionId.equals("q3")){
                    if (("否").equals(linkedHashMap["answer"]["value"].toString())){
                        linkedHashMap.put("answer", Map.of("value", "是"))
                    }
                }

                if (questionId.equals("q4")){
                    Random random1 = new Random();
                    double randomNumber1 = random1.nextDouble();
                    if (randomNumber1 < probabilityOne){
                        linkedHashMap.put("answer", Map.of("value", "非常有帮助"))
                    }else {
                        linkedHashMap.put("answer", Map.of("value", "有些帮助"))
                    }
                }

                if (questionId.equals("q5")){
                    linkedHashMap.put("answer", Map.of("value", "已到达"))
                }

                if (questionId.equals("q6")){
                    Random random2 = new Random();
                    double randomNumber2 = random2.nextDouble();
                    if (randomNumber2 < probabilitySeven){
                        linkedHashMap.put("answer", Map.of("value", "非常满意"))
                    }else {
                        linkedHashMap.put("answer", Map.of("value", "满意"))
                    }
                }

                if (questionId.equals("q8")){
                    Random random2 = new Random();
                    double randomNumber2 = random2.nextDouble();
                    if (randomNumber2 < probabilityOne){
                        linkedHashMap.put("answer", Map.of("value", "非常感兴趣"))
                    }else {
                        linkedHashMap.put("answer", Map.of("value", "有兴趣"))
                    }
                }
            }
            for (i in 1..<size+1) {

                QuestInfo copy2 = new QuestInfo();
                copy2.setId(new ObjectId().toHexString())
                copy2.setData(linkedHashMaps)
                copy2.setCreateTime(it.getCreateTime() + i*60*1000L)
                newList.add(copy2)
            }
            return it;
        }}.collect().toList();

        cleanList.addAll(newList);

        List<QuestionClean> save = cleanList.stream().map {it->{
            QuestionClean questionClean = new QuestionClean();
            BeanUtils.copyProperties(it, questionClean);
            return questionClean;
        }}.collect().toList();

        questionCleanDao.insert(save);
        return ResultContent.buildSuccess();
    }


    public void exportCleanData(HttpServletRequest request, HttpServletResponse response) throws Exception {

        List<QuestionClean> questionCleanList = questionCleanDao.findAll(Sort.by(Sort.Order.desc("createTime")));
        List<QuestInfo> list = questionCleanList.stream().map(it -> {
            QuestInfo questInfo = new QuestInfo();
            BeanUtils.copyProperties(it, questInfo);
            return questInfo;
        }).toList();

        List<Map<String, Object>> maps = new ArrayList<>();

        HashMap<String, String> titleMap = new HashMap<>();
        List<QuestTitleModel> titles = new ArrayList<>();
        if (questionCleanList != null) {
//            List<QuestInfo> list = questionCleanList;
            maps = list.parallelStream().map(it -> {
                Map<String, Object> map = new HashMap<>();
                try {
                    Object data = it.getData();
                    if (data != null) {
                        List<LinkedHashMap> linkedHashMaps = (List<LinkedHashMap>) data;
                        for (LinkedHashMap linkedHashMap : linkedHashMaps) {
                            String questionId = linkedHashMap.get("questionId").toString();
                            if (!titleMap.containsKey(questionId)) {
                                QuestTitleModel titleModel = new QuestTitleModel();
                                titleModel.setQuestionId(questionId);
                                String question = linkedHashMap.get("question").toString();
                                titleModel.setQuestion(question);
                                titles.add(titleModel);
                                titleMap.put(questionId, question);
                            }
                            Object answer = linkedHashMap.get("answer");
                            String value = "";
                            if (answer != null) {
                                if (answer instanceof LinkedHashMap<?, ?>) {
                                    LinkedHashMap tempMap = (LinkedHashMap) answer;
                                    value = tempMap.get("value").toString();
                                    if (tempMap.containsKey("supplementary")) {
                                        if (StringUtils.isNotEmpty(value)) {
                                            value += "  补充: " + tempMap.get("supplementary").toString();
                                        } else {
                                            value += tempMap.get("supplementary").toString();
                                        }
                                    }
                                } else if (answer instanceof String) {
                                    value = answer.toString();
                                } else {
                                    value = answer.toString();
                                }
                            }
                            map.put(questionId, value);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                map.put("timeStr", DateUtils.paresTime(it.getCreateTime(), DateUtils.patternyyyyMis));
                return map;
            }).collect(Collectors.toList());
        }

        CommonExeclParam execlParam = new CommonExeclParam();
        execlParam.setTitle(String.format("问卷调查"));
        execlParam.setStartRow(2);
        execlParam.setDatas(maps);
        List<CommonExeclTd> tds = new ArrayList<>();
        for (QuestTitleModel titleModel : titles) {
            tds.add(CommonExeclTd.build(titleModel.getQuestion(), titleModel.getQuestionId(), 8));
        }
        tds.add(CommonExeclTd.build("时间", "timeStr"));
        execlParam.setTds(tds);
        ExcelUtils.commonExecuteExcel(request, response, execlParam);
    }
}
