package com.xhkjedu.sapi.service.analyze;

import com.xhkjedu.sapi.mapper.analyze.SchoolTaskMapper;
import com.xhkjedu.sapi.mapper.system.SchoolMapper;
import com.xhkjedu.sapi.vo.analyze.SchoolParams;
import com.xhkjedu.sapi.vo.analyze.work.SchoolWorkSubjectVo;
import com.xhkjedu.sapi.vo.analyze.work.SchoolWorkVo;
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.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName SchoolTaskService
 * Description TODO 作业概况
 * Author WN
 * Date 2020/8/10 14:21
 **/
@Service
public class SchoolTaskService {

    @Autowired
    private SchoolTaskMapper schoolTaskMapper;
    @Resource
    private SchoolMapper schoolMapper;

    //常规作业统计
    public SchoolWorkVo listSchoolTaskWork(SchoolParams params){
        setTime(params);
        SchoolWorkVo schoolWorkVo = new SchoolWorkVo();

        List<SchoolWorkSubjectVo> lst = schoolTaskMapper.listSchoolTaskWork(params);

        if(lst!=null && lst.size()>0){
            for(SchoolWorkSubjectVo subject : lst){
                subject.setCommitrate(N_Utils.getIntegerDivideAndMulitiply(subject.getCommitnum(),subject.getStunum()));
                subject.setCheckrate(N_Utils.getIntegerDivideAndMulitiply(subject.getChecknum(),subject.getCommitnum()));
                subject.setWorklist(new ArrayList<>());
            }

            Integer totalnum = lst.stream().mapToInt(SchoolWorkSubjectVo::getFabunum).sum();
            Integer totalcommit =  lst.stream().mapToInt(SchoolWorkSubjectVo::getCommitnum).sum();
            Integer totalcheck = lst.stream().mapToInt(SchoolWorkSubjectVo::getChecknum).sum();
            Integer totalstunum = lst.stream().mapToInt(SchoolWorkSubjectVo::getStunum).sum();
            schoolWorkVo.setTotalnum(totalnum);
            schoolWorkVo.setCommitrate(N_Utils.getIntegerDivideAndMulitiply(totalcommit,totalstunum));
            if(totalcommit>0){
                schoolWorkVo.setCheckrate(N_Utils.getIntegerDivideAndMulitiply(totalcheck,totalcommit));
            }else{
                schoolWorkVo.setCheckrate(0);
            }
            schoolWorkVo.setSubjectlist(lst);
        }

        return schoolWorkVo;
    }

    //常规作业统计--详情
    public List<SchoolWorkSubjectVo> listSchoolTaskWorkDetail(SchoolParams srparams){
        setTime(srparams);
        List<SchoolWorkSubjectVo> lst = new ArrayList<>();
        List<SchoolParams> users = schoolTaskMapper.listUser(srparams);//获取用户集合
        List<SchoolWorkSubjectVo> subjects = schoolTaskMapper.listSchoolTaskWorkDetail(srparams);
        for (SchoolParams user : users) {
            SchoolWorkSubjectVo subject = subjects.stream().filter(s -> s.getUserid().equals(user.getCreateid())).findFirst().orElse(null);
            if (subject == null) {
                subject = new SchoolWorkSubjectVo();
            } else {
                subject.setCommitrate(N_Utils.getIntegerDivideAndMulitiply(subject.getCommitnum(), subject.getStunum()));
                subject.setCheckrate(N_Utils.getIntegerDivideAndMulitiply(subject.getChecknum(), subject.getCommitnum()));
            }
            subject.setUsername(user.getUsername());
            lst.add(subject);
        }
        return SortedList(lst);
    }

    private List<SchoolWorkSubjectVo> SortedList(List<SchoolWorkSubjectVo> lst) {
        return lst.stream().sorted(Comparator.comparing(SchoolWorkSubjectVo::getFabunum).reversed()
                .thenComparing(SchoolWorkSubjectVo::getUsername)).collect(Collectors.toList());
    }

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

    //学生预习统计
    public SchoolWorkVo listSchoolTaskGuide(SchoolParams params){
        setTime(params);
        SchoolWorkVo schoolWorkVo = new SchoolWorkVo();

        List<SchoolWorkSubjectVo> lst = schoolTaskMapper.listSchoolTaskGuide(params);
        if(lst!=null && lst.size()>0){
            for(SchoolWorkSubjectVo subject : lst){
                subject.setCommitrate(N_Utils.getIntegerDivideAndMulitiply(subject.getCommitnum(),subject.getStunum()));

                subject.setWorklist(new ArrayList<>());
            }

            Integer totalnum = lst.stream().mapToInt(SchoolWorkSubjectVo::getFabunum).sum();
            Integer totalstunum = lst.stream().mapToInt(SchoolWorkSubjectVo::getStunum).sum();
            Integer totalcommit = lst.stream().mapToInt(SchoolWorkSubjectVo::getCommitnum).sum();
            schoolWorkVo.setTotalnum(totalnum);
            if(totalcommit>0){
                schoolWorkVo.setCommitrate(N_Utils.getIntegerDivideAndMulitiply(totalcommit,totalstunum));
            }else{
                schoolWorkVo.setCommitrate(0);
            }
            schoolWorkVo.setSubjectlist(lst);
        }

        return schoolWorkVo;
    }


    //生预习统计--详情
    public List<SchoolWorkSubjectVo> listSchoolTaskGuideDetail(SchoolParams sparam){
        setTime(sparam);

        List<SchoolWorkSubjectVo> lst = new ArrayList<>();
        List<SchoolParams> users = schoolTaskMapper.listUser(sparam);//获取用户集合
        List<SchoolWorkSubjectVo> subjects = schoolTaskMapper.listSchoolTaskGuideDetail(sparam);
        for (SchoolParams user : users) {
            SchoolWorkSubjectVo subject = subjects.stream().filter(s -> s.getUserid().equals(user.getCreateid())).findFirst().orElse(null);
            if (subject == null) {
                subject = new SchoolWorkSubjectVo();
            } else {
                subject.setCommitrate(N_Utils.getIntegerDivideAndMulitiply(subject.getCommitnum(), subject.getStunum()));
            }
            subject.setUsername(user.getUsername());
            lst.add(subject);
        }
        return SortedList(lst);
    }


    //背诵统计

    public SchoolWorkVo listSchoolTaskRecite(SchoolParams params){
        setTime(params);
        SchoolWorkVo schoolWorkVo = new SchoolWorkVo();
        List<SchoolWorkSubjectVo> lst = schoolTaskMapper.listSchoolTaskRecite(params);
        if(lst!=null && lst.size()>0){
            for(SchoolWorkSubjectVo subject : lst){
                subject.setCommitrate(N_Utils.getIntegerDivideAndMulitiply(subject.getCommitnum(),subject.getStunum()));

                subject.setWorklist(new ArrayList<>());
            }
            Integer totalnum = lst.stream().mapToInt(SchoolWorkSubjectVo::getFabunum).sum();
            Integer totalstunum = lst.stream().mapToInt(SchoolWorkSubjectVo::getStunum).sum();
            Integer totalcommit = lst.stream().mapToInt(SchoolWorkSubjectVo::getCommitnum).sum();
            if(totalcommit>0){
                schoolWorkVo.setCommitrate(N_Utils.getIntegerDivideAndMulitiply(totalcommit,totalstunum));
            }else{
                schoolWorkVo.setCommitrate(0);
            }
            schoolWorkVo.setTotalnum(totalnum);
            schoolWorkVo.setSubjectlist(lst);

        }

        return schoolWorkVo;
    }

    //背诵统计--详情
    public List<SchoolWorkSubjectVo> listSchoolTaskReciteDetail(SchoolParams sparams){
        setTime(sparams);
        List<SchoolWorkSubjectVo> lst = new ArrayList<>();
        List<SchoolParams> users = schoolTaskMapper.listUser(sparams);//获取用户集合
        List<SchoolWorkSubjectVo> subjects = schoolTaskMapper.listSchoolTaskReciteDetail(sparams);
        for (SchoolParams user : users) {
            SchoolWorkSubjectVo subject = subjects.stream().filter(s -> s.getUserid().equals(user.getCreateid())).findFirst().orElse(null);
            if (subject == null) {
                subject = new SchoolWorkSubjectVo();
            } else {
                subject.setCommitrate(N_Utils.getIntegerDivideAndMulitiply(subject.getCommitnum(), subject.getStunum()));
            }
            subject.setUsername(user.getUsername());
            lst.add(subject);
        }
        return SortedList(lst);
    }
}
