package com.ssj.service.sys.situation.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.situation.domain.LearningSituation;
import com.ssj.conch.constant.CourseConstant;
import com.ssj.service.sys.situation.bo.DimesionCount;
import com.ssj.service.sys.situation.service.ILearningSituationMain;
import com.ssj.service.sys.situation.service.KnowledgeSituation;
import com.ssj.service.sys.situation.service.LearningSituationService;

@Service(version="1.0")
@Transactional
public class LearningSituationMain implements ILearningSituationMain {
    private static final Logger logger = LoggerFactory.getLogger(LearningSituation.class);

    @Autowired
    private LearningSituationService learningSituationService;

    /**
     * 计算学生的整体学情
     */
    @Override
    public void calTotalSituation (){
        //学生id为空是计算所有学生
        this.calStudentSituation(null);
    }

    /**
     * 计算某个学生的学情
     * @param vipId 学生id, id为空表示全部学生
     */
    @Override
    public void calStudentSituation (String vipId){
        logger.info("======================计算学生id的学情{}======================", vipId);
        List<LearningSituation> result = new ArrayList<>();
        CourseConstant.subjectList.forEach((courseName) -> {
            KnowledgeSituation homework = new HomeworkSituation(vipId, courseName);
            KnowledgeSituation homewokrKnowHow = new HomeworkKnowHowSituation(vipId, courseName);
//            KnowledgeSituation one2one = new One2OneSituation(vipId, courseName);
            KnowledgeSituation proxy = new ProxySituation(vipId, courseName, homework, homewokrKnowHow, null);
            List<DimesionCount> knows =  ((ProxySituation) proxy).getKnowList();
            List<DimesionCount> skills = ((ProxySituation) proxy).getSkillList();
            Date now = new Date();
            knows.stream().filter((dc) -> (dc.getWrongCount() > 0 || dc.getTotalCount() > 0)).forEach((dc) -> {
                LearningSituation ls = new LearningSituation();
                ls.setCourseName(courseName);
                ls.setCreateTime(now);
                if (StringUtils.isEmpty(vipId)){
                    //总体知识维度
                    ls.setType(3);
                } else {
                    //个人知识维度
                    ls.setType(1);
                }
                ls.setStudentId(vipId);
                ls.setRemark(dc.getName());
                ls.setWrongCount(dc.getWrongCount());
                ls.setTotalCount(dc.getTotalCount());
                result.add(ls);
            });
            skills.stream().filter((dc) -> (dc.getWrongCount() > 0 || dc.getTotalCount() > 0)).forEach( (dc) -> {
                LearningSituation ls = new LearningSituation();
                ls.setCourseName(courseName);
                ls.setCreateTime(now);
                if (StringUtils.isEmpty(vipId)){
                    //总体技能维度
                    ls.setType(4);
                } else {
                    //个人技能维度
                    ls.setType(2);
                }
                ls.setStudentId(vipId);
                ls.setRemark(dc.getName());
                ls.setWrongCount(dc.getWrongCount());
                ls.setTotalCount(dc.getTotalCount());
                result.add(ls);
            });
            //各类知识点数量,总体是没有的,只保留有结果的数据
            if (StringUtils.isNotEmpty(vipId)){
                int wrongCount = ((ProxySituation) proxy).getUnderstandWrongCount();
                int totalCount = ((ProxySituation) proxy).getUnderstandTotalCount();
                LearningSituation ls = null;
                if (wrongCount > 0 || totalCount > 0){
                    ls = new LearningSituation();
                    ls.setCourseName(courseName);
                    ls.setCreateTime(now);
                    ls.setType(6);
                    ls.setStudentId(vipId);
                    ls.setRemark("理解类知识点");
                    ls.setWrongCount(wrongCount);
                    ls.setTotalCount(totalCount);
                    result.add(ls);
                }

                wrongCount = ((ProxySituation) proxy).getMemoryWrongCount();
                totalCount = ((ProxySituation) proxy).getMemoryTotalCount();
                if (wrongCount > 0 || totalCount > 0){
                    ls = new LearningSituation();
                    ls.setCourseName(courseName);
                    ls.setCreateTime(now);
                    ls.setType(7);
                    ls.setStudentId(vipId);
                    ls.setRemark("记忆类知识点");
                    ls.setWrongCount(wrongCount);
                    ls.setTotalCount(totalCount);
                    result.add(ls);
                }

                wrongCount = ((ProxySituation) proxy).getApplyWrongCount();
                totalCount = ((ProxySituation) proxy).getApplyTotalCount();
                if (wrongCount >0 || totalCount > 0){
                    ls = new LearningSituation();
                    ls.setCourseName(courseName);
                    ls.setCreateTime(now);
                    ls.setType(5);
                    ls.setStudentId(vipId);
                    ls.setRemark("应用类知识点");
                    ls.setWrongCount(wrongCount);
                    ls.setTotalCount(totalCount);
                    result.add(ls);
                }
            }
        });
        if (result != null && result.size() > 0){
            learningSituationService.save(result);
        }
    }


    /**
     * 查询所有学生的学情并插入到学情表里
     */
    @Override
    public void querySituation () {
        learningSituationService.truncateTable();
        List<String> vipIds = learningSituationService.getServiceStudentIds();
        this.calTotalSituation();
        if (vipIds != null) {
            vipIds.forEach((id) -> {
                if (StringUtils.isNotBlank(id)){
                    this.calStudentSituation(id);
                }
            });
        }
    }
}
