package com.xhkjedu.sapi.service.analyze;

import com.xhkjedu.sapi.mapper.analyze.SchoolRoomMapper;
import com.xhkjedu.sapi.mapper.system.SchoolMapper;
import com.xhkjedu.sapi.vo.analyze.*;
import com.xhkjedu.sapi.vo.analyze.room.SchoolRoomVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName SchoolRoomService
 * Description TODO 课堂概况
 * Author WN
 * Date 2020/8/10 17:40
 **/
@Service
public class SchoolRoomService {
    @Autowired
    private SchoolRoomMapper schoolRoomMapper;
    @Resource
    private SchoolMapper schoolMapper;

    //课堂概况--各学科课堂时间
    public List<SchoolRoomSubjectVo> listSchoolRoomSubject(SchoolParams params){
        setTime(params);
        List<SchoolRoomSubjectVo> lst = schoolRoomMapper.listSchoolRoomSubject(params);
        for(SchoolRoomSubjectVo rs:lst){
            int sumtime = 0;
            List<SchoolRoomGradeRoomVo> roomlist = rs.getRoomlist();

            Integer totaltime = roomlist.stream().mapToInt(SchoolRoomGradeRoomVo::getRoomtime).sum();
            sumtime = sumtime + totaltime;
            rs.setTotaltime(sumtime);
            rs.setTotalnum(roomlist.size());
            rs.setRoomlist(null);
        }
        return lst;
    }

    //课堂概况--科目详情
    public List<SchoolRoomGradeUserVo> listSchoolRoomDetailBySubject(SchoolParams sparams){
        setTime(sparams);
        List<SchoolRoomGradeUserVo> userlist = schoolRoomMapper.listSchoolRoomDetailBySubject(sparams);
        for(SchoolRoomGradeUserVo user:userlist){
            List<SchoolRoomGradeUserDetailVo> udlist = user.getUdlist();

            int totaltime = udlist.stream().mapToInt(SchoolRoomGradeUserDetailVo::getTotaltime).sum();
            int quesitonnum = udlist.stream().mapToInt(SchoolRoomGradeUserDetailVo::getQuestionnum).sum();
            int examnum = udlist.stream().mapToInt(SchoolRoomGradeUserDetailVo::getExamnum).sum();
            int asknum = udlist.stream().mapToInt(SchoolRoomGradeUserDetailVo::getAsknum).sum();
            int callnum = udlist.stream().mapToInt(SchoolRoomGradeUserDetailVo::getCallnum).sum();
            int picnum = udlist.stream().mapToInt(SchoolRoomGradeUserDetailVo::getPicnum).sum();
            int classnum = udlist.stream().mapToInt(SchoolRoomGradeUserDetailVo::getClassnum).sum();
            int realstunum = udlist.stream().mapToInt(SchoolRoomGradeUserDetailVo::getRealstunum).sum();
            int votenum = udlist.stream().mapToInt(SchoolRoomGradeUserDetailVo::getVotenum).sum();

            user.setRoomnum(udlist.size());
            user.setQuestionnum(quesitonnum);
            user.setExamnum(examnum);
            user.setAsknum(asknum);
            user.setCallnum(callnum);
            user.setPicnum(picnum);
            user.setClassnum(classnum);
            user.setRealstunum(realstunum);
            user.setVotenum(votenum);

            user.setTotaltime(totaltime);

            user.setUdlist(null);

        }

        return userlist;
    }

    private void setTime(SchoolParams sparams) {
        //Integer year = schoolMapper.getYearById(sparams.getSchoolid());
        //sparams.setYear(year);
        if (!N_Utils.isEmptyInteger(sparams.getType())) {
            Long endtime = N_Utils.getNowtimestamp();
            Long starttime = N_Utils.getLongGoTime(sparams.getType());
            sparams.setStarttime(starttime.intValue());
            sparams.setStoptime(endtime.intValue());
        }
    }

    //课堂统计--总使用情况
    public SchoolRoomVo getSchoolRoomTotal(SchoolParams sparams){
        SchoolRoomVo schoolRoomVo = new SchoolRoomVo();
        List<SchoolRoomGradeUserVo> userlist = listSchoolRoomDetailBySubject(sparams);
        if(userlist!=null && userlist.size()>0){
            int quesitonnum = userlist.stream().mapToInt(SchoolRoomGradeUserVo::getQuestionnum).sum();
            int examnum = userlist.stream().mapToInt(SchoolRoomGradeUserVo::getExamnum).sum();
            int asknum = userlist.stream().mapToInt(SchoolRoomGradeUserVo::getAsknum).sum();
            int callnum = userlist.stream().mapToInt(SchoolRoomGradeUserVo::getCallnum).sum();
            int classnum = userlist.stream().mapToInt(SchoolRoomGradeUserVo::getClassnum).sum();
            int realstunum = userlist.stream().mapToInt(SchoolRoomGradeUserVo::getRealstunum).sum();
            int votenum = userlist.stream().mapToInt(SchoolRoomGradeUserVo::getVotenum).sum();
            int totalnum = userlist.stream().mapToInt(SchoolRoomGradeUserVo::getRoomnum).sum();

            schoolRoomVo.setTotalnum(totalnum);
            schoolRoomVo.setTotalcallnum(callnum);
            schoolRoomVo.setTotalasknum(asknum);
            schoolRoomVo.setTotalquenum(quesitonnum);
            schoolRoomVo.setTotalexamnum(examnum);
            schoolRoomVo.setTotalvotenum(votenum);
            if(classnum>0){
                schoolRoomVo.setKqrate(N_Utils.getIntegerDivideAndMulitiply(realstunum,classnum));
            }else{
                schoolRoomVo.setKqrate(0);
            }

            //集合排序
            userlist.sort(Comparator.comparing(SchoolRoomGradeUserVo::getRoomnum).reversed());
            schoolRoomVo.setUserlist(userlist.stream().limit(5).collect(Collectors.toList()));
        }

        return schoolRoomVo;
    }

}
