package com.xhkjedu.sstudy.service.recite;

import com.xhkjedu.sstudy.mapper.recite.ReciteGroupMapper;
import com.xhkjedu.sstudy.mapper.recite.ReciteStudentMapper;
import com.xhkjedu.sstudy.model.recite.TReciteGroup;
import com.xhkjedu.sstudy.model.recite.TReciteStudent;
import com.xhkjedu.sstudy.vo.recite.ReciteStudentVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @ClassName ReciteStudentService
 * Description TODO
 * Author WN
 * Date 2020/6/10 14:38
 **/
@Service
public class ReciteStudentService {
    @Autowired
    private ReciteStudentMapper reciteStudentMapper;
    @Autowired
    private ReciteGroupMapper reciteGroupMapper;

    /**
     *功能描述 列表--学生
     * @author WN
     * @date 2020/6/10
     * @param  * @param rs
     * @return java.util.List<com.xhkjedu.vo.recite.ReciteStudentVo>
     */
    public List<ReciteStudentVo> listStuReceites(TReciteStudent rs){
        return reciteStudentMapper.listStuReceites(rs);
    }

    /**
     *功能描述 保存--学生保存背诵内容
     * @author WN
     * @date 2020/6/10
     * @param  * @param rs
     * @return java.lang.Integer
     */
    public Integer updateStudRecite(TReciteStudent rs){
        TReciteGroup rg = reciteGroupMapper.findByRsId(rs.getReciteid());
        Integer nowtime = N_Utils.getSecondTimestamp();
        if(rg.getStoptime()>=nowtime){
            rs.setRsstate(1);//准时提交
        }else{
            rs.setRsstate(2);//超时提交
        }

        return reciteStudentMapper.updateStudRecite(rs);
    }

    /**
     * 功能描述 详情--根据学生背诵id获取背诵内容
     *
     * @param *        @param rsid
     * @param reciteid
     * @return com.xhkjedu.vo.recite.ReciteStudentVo
     * @author WN
     * @date 2020/6/10
     */
    public ReciteStudentVo getReciteStuByRsid(Integer studentid, Integer reciteid) {
        return reciteStudentMapper.getReciteStuByRsid(studentid, reciteid);
    }

    public List<Map> listRecitesClassForAnalyze(Integer reciteid, Integer classid) {
        List<Map> list = new ArrayList<>();
        List<Map> students = reciteStudentMapper.listRecitesClassForAnalyze(reciteid, classid);
        if (N_Utils.isListEmpty(students)) return list;
        Map<Object, List<Map>> collect = students.stream().collect(Collectors.groupingBy(m -> m.get("classid")
                , LinkedHashMap::new, Collectors.toList()));
        for (Map.Entry<Object, List<Map>> entry : collect.entrySet()) {
            Map cmap = new HashMap();//班级统计
            List<Map> stulist = new ArrayList<>();//学生列表
            students = entry.getValue();
            for (int i = 0; i < students.size(); i++) {
                Map s = students.get(i);
                if (i == 0) {
                    cmap.put("classid", s.get("classid"));
                    cmap.put("classname", s.get("classname"));
                    cmap.put("gradeid", s.get("gradeid"));
                }
                s.remove("classid");
                s.remove("classname");
                s.remove("gradeid");
                stulist.add(s);
            }
            cmap.put("stunum", students.size());//学生人数
            List<Map> commitStus = students.stream().filter(s -> (int) s.get("rsstate") > 0).collect(Collectors.toList());
            cmap.put("commitnum", commitStus.size());//已提交人数
            double avgscore = commitStus.stream().mapToInt(s -> (int) s.get("userscore")).summaryStatistics().getAverage();
            cmap.put("avgscore", avgscore);//平均分
            cmap.put("stulist", stulist);//学生列表
            list.add(cmap);
        }
        return list;
    }

    /**
     * 背诵学生--网页--列表
     *
     * @return java.util.List<com.xhkjedu.vo.recite.ReciteStudentVo>
     * @Param [student]
     * @Author ywx
     * @Date 2020/9/4 9:08
     **/
    public List<ReciteStudentVo> listReciteStuWeb(TReciteStudent student) {
        return reciteStudentMapper.listReciteForStuWeb(student);
    }
}
