package com.ssj.service.kmt.kmt.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.kmt.kmt.model.KmtKnowledgeModel;
import com.ssj.dao.kmt.kmt.dao.KmtKnowledgeQueryDao;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.mapper.knowledge.KmtKnowledgeMapper;
import com.ssj.service.kmt.constant.KnowledgeVersion;
import com.ssj.service.kmt.dto.KnowHowDTO;
import com.ssj.service.kmt.dto.KnowHowDetailsDTO;
import com.ssj.service.kmt.dto.TestPointDTO;
import com.ssj.service.kmt.request.KnowledgeRequest;
import com.ssj.service.kmt.request.TestPointRequest;
import com.ssj.service.kmt.service.KmtKnowledgeService;
import com.ssj.service.weixin.zuoyb.service.ZuoybKnowHowDetailsService;
import com.ssj.service.weixin.zuoyb.service.ZuoybKnowHowService;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service(version ="1.0")
public class KmtKnowledgeServiceImpl implements KmtKnowledgeService {

    @Autowired
    private ZuoybKnowHowService zuoybKnowHowService;
    @Autowired
    private ZuoybKnowHowDetailsService zuoybKnowHowDetailsService;
    @Autowired
    private KmtKnowledgeQueryDao kmtKnowledgeQueryDao;
    @Autowired
    private KmtKnowledgeMapper kmtKnowledgeMapper;
    @Autowired
    private KnowledgeVersion knowledgeVersion;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<KnowHowDTO> getKnowledge(KnowledgeRequest request) {
        return zuoybKnowHowService.findListByCondition(request);
    }


    @Override
    public List<KnowHowDTO> findSortKnowledgeByCondition(KnowledgeRequest request) {
        Integer moduleType = null;
        if (request.getModuleType() == 1){
            moduleType = 0;
        } else if (request.getModuleType() == 2){
            moduleType = 1;
        }
        List<KmtKnowledgeModel> mapList = kmtKnowledgeMapper.findKmtKnowledgeByGradeCourseNameTypeSemester(request.getGrade(),
                request.getSubject(), request.getVersionType(), request.getSemester(), moduleType, request.getSearchKey());
        List<KnowHowDTO> knowHowDTOList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(mapList)){
            Map<String, List<KmtKnowledgeModel>> knwHowMap = mapList.stream().collect(Collectors.groupingBy(temp ->
                    String.valueOf(temp.getKnowHowId()), LinkedHashMap::new, Collectors.toList()));
            knwHowMap.forEach((knowHowId, value) -> {
                KnowHowDTO knowHowDTO = new KnowHowDTO();
                knowHowDTO.setId(knowHowId);
                knowHowDTO.setName(String.valueOf(value.get(0).getKnowHowName()));
                LinkedHashMap<String, List<KmtKnowledgeModel>> knowHowDetailsMap = value.stream().collect(
                        Collectors.groupingBy(temp -> String.valueOf(temp.getKnowHowDetailsId()), LinkedHashMap::new,
                                Collectors.toList()));
                List<KnowHowDetailsDTO> knowHowDetailsDTOList = new ArrayList<>(value.size());
                knowHowDetailsMap.forEach((knowHowDetailId, value2) -> {
                    if (knowHowDetailId != null) {
                        KnowHowDetailsDTO knowHowDetailsDTO = new KnowHowDetailsDTO();
                        knowHowDetailsDTO.setId(knowHowDetailId);
                        knowHowDetailsDTO.setName(String.valueOf(value2.get(0).getKnowHowDetailsName()));
                        knowHowDetailsDTO.setGrade(request.getGrade());
                        knowHowDetailsDTO.setSemester(request.getSemester());
                        knowHowDetailsDTO.setVersionName(knowledgeVersion.getVersionNameBySubjectAndVersionNum(request.getSubject(), request.getVersionType()));

                        List<TestPointDTO> testPointDTOList = new ArrayList<>();
                        value2.forEach(temp4kd -> {
                            String howDetailsKdId = temp4kd.getHowDetailsKdId();
                            if (howDetailsKdId != null) {
                                TestPointDTO testPointDTO = new TestPointDTO();
                                testPointDTO.setId(String.valueOf(howDetailsKdId));
                                testPointDTO.setName(String.valueOf(temp4kd.getKnowHowDetailsKdName()));
                                testPointDTOList.add(testPointDTO);
                            }
                        });
                        knowHowDetailsDTO.setPoints(testPointDTOList);
                        knowHowDetailsDTOList.add(knowHowDetailsDTO);
                    }
                });

                knowHowDTO.setDetails(knowHowDetailsDTOList);
                knowHowDTOList.add(knowHowDTO);
            });
        }
        return knowHowDTOList;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<KnowHowDetailsDTO> findTestPoint(TestPointRequest request) {
        return zuoybKnowHowDetailsService.findTestPoint(request.getDetailIds(), request.getVersionType(),
                 request.getGrade(), request.getSubject(), request.getSemester());
    }

    @Override
    public List<KnowHowDetailsDTO> findByQuestionId(String questionId) {
        return kmtKnowledgeQueryDao.findByQuestionId(questionId);
    }

    public List<KnowHowDetailsDTO> findDetailsListWithKd (List<String> detailIds){
        List<KmtKnowledgeModel> mapList = kmtKnowledgeMapper.findDetailsListWithKd(detailIds);
        List<KnowHowDetailsDTO> knowHowDetailsDTOList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(mapList)){
            Map<String, List<KmtKnowledgeModel>> listMap = mapList.stream().collect(Collectors.groupingBy(temp ->
                    String.valueOf(temp.getKnowHowDetailsId()), LinkedHashMap::new, Collectors.toList()));
            listMap.forEach((key, value) -> {
                KnowHowDetailsDTO knowHowDetailsDTO = new KnowHowDetailsDTO();
                knowHowDetailsDTO.setId(key);
                knowHowDetailsDTO.setName(String.valueOf(value.get(0).getKnowHowDetailsName()));
                knowHowDetailsDTO.setGrade(value.get(0).getGrade());
                knowHowDetailsDTO.setSemester(value.get(0).getSemester());
                knowHowDetailsDTO.setVersionName(knowledgeVersion.getVersionNameBySubjectAndVersionNum(value.get(0).getCourseName(), value.get(0).getType()));

                List<TestPointDTO> testPointDTOList = new ArrayList<>(value.size());
                value.forEach(map -> {
                    if (StringUtil.isNotBlank((String)map.getHowDetailsKdId())){
                        TestPointDTO testPointDTO = new TestPointDTO();
                        testPointDTO.setId(String.valueOf(map.getHowDetailsKdId()));
                        testPointDTO.setName(String.valueOf(map.getKnowHowDetailsKdName()));
                        testPointDTOList.add(testPointDTO);
                    }
                });

                knowHowDetailsDTO.setPoints(testPointDTOList);
                knowHowDetailsDTOList.add(knowHowDetailsDTO);
            });
        }
        return knowHowDetailsDTOList;
    }

}
