package com.xhkjedu.sstudy.service.resource;

import com.xhkjedu.sstudy.mapper.resource.GuideGroupMapper;
import com.xhkjedu.sstudy.mapper.resource.GuideMapper;
import com.xhkjedu.sstudy.mapper.system.UserMapper;
import com.xhkjedu.sstudy.model.resource.TGuide;
import com.xhkjedu.sstudy.model.resource.TGuideGroup;
import com.xhkjedu.sstudy.service.gradeclass.ClassService;
import com.xhkjedu.sstudy.vo.resource.GuideGroupStudentVo;
import com.xhkjedu.sstudy.vo.resource.GuideGroupVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ywx
 * @classname TGuideGroupService
 * @description 
 * @date 2020/6/2 17:04
 **/
@Service
public class GuideGroupService {
    @Resource
    private GuideGroupMapper guideGroupMapper;
    @Resource
    private GuideMapper guideMapper;
    @Resource
    private ClassService classService;
    @Resource
    private UserMapper userMapper;

    //添加
    public Integer batchSave(List<TGuideGroup> list) {
        return guideGroupMapper.insertList(list);
    }

    //导学分析
    public List <GuideGroupVo> findAll(TGuideGroup group) {
        List<GuideGroupVo> list = guideGroupMapper.findAll(group);
        setGroups(list);
        return list;
    }

    //预习统计兼容集团校
    public List findAll2(TGuide guide) {
        Integer createid = guide.getCreateid();
        Integer usertype = userMapper.getUsertypeByUserid(createid);
        if (N_Utils.isEmptyInteger(guide.getClassid()) && usertype.equals(1)) {
            //获取全部
            TGuide tGuide = guideMapper.selectByPrimaryKey(guide.getGuideid());
            Integer hashb = tGuide.getHashb();
            Integer schoolid = tGuide.getSchoolid();
            if (createid.equals(tGuide.getCreateid()) || usertype.equals(0)) {//自己发布的或管理员看所有班级
                guide.setClassids(null);
            } else {
                Map<String, String> cmap = classService.getQxClassidsByTeacherid(createid, tGuide.getYear());
                if (hashb == 1 && guide.getSchoolid().equals(schoolid)) {//有伙伴学校并且是本校发的
                    guide.setClassids(cmap.get("classids"));
                } else {
                    guide.setClassids(cmap.get("bxclassids"));
                }
            }
        }

        List<Map> list = guideGroupMapper.findAll2(guide);
        for (Map map : list) {
            List<GuideGroupVo> groups = (List<GuideGroupVo>) map.get("groups");
            setGroups(groups);
            map.put("groups", groups);
        }
        return list;
    }

    //处理预习人数、比例
    private void setGroups(List<GuideGroupVo> groups) {
        for (GuideGroupVo guideGroupVo : groups) {
            List<GuideGroupStudentVo> students = guideGroupVo.getStudents();
            Integer groupnum = students.size();//分组人数
            guideGroupVo.setGroupnum(groupnum);
            Integer yyxnum = 0;//已预习人数
            for (GuideGroupStudentVo student : students) {
                if (student.getCosttime() > 0) {
                    yyxnum++;
                }
            }
            guideGroupVo.setYyxnum(yyxnum);
            guideGroupVo.setWyxnum(groupnum - yyxnum);//未预习人数
            guideGroupVo.setScale(N_Utils.getIntegerDivideAndMulitiply(yyxnum, groupnum));//预习比例向下取整
        }
    }
}
