package com.xhkjedu.suser.service.gradeclass;

import com.xhkjedu.suser.mapper.gradeclass.GradeMapper;
import com.xhkjedu.suser.mapper.gradeclass.SchoolMapper;
import com.xhkjedu.suser.model.gradeclass.TGrade;
import com.xhkjedu.suser.vo.gradeclass.GradeVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

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

/**
 * @author ywx
 * @classname TGradeService
 * @description
 * @date 2020/6/2 14:53
 **/
@Service
public class GradeService {
    @Resource
    private GradeMapper gradeMapper;
    @Resource
    private SchoolMapper schoolMapper;

    // 添加
    public Integer save(TGrade model) {
        model.setCreatetime(N_Utils.getSecondTimestamp());
        gradeMapper.insertUseGeneratedKeys(model);
        return model.getId();
    }

    // 删除
    public Integer deleteById(Integer gradeid) {
        return gradeMapper.deleteByPrimaryKey(gradeid);
    }

    // 修改
    public Integer update(TGrade model) {
        return gradeMapper.updateByPrimaryKeySelective(model);
    }

    // 获取详情
    public TGrade findById(Integer gradeid) {
        return gradeMapper.findById(gradeid);
    }

    // 获取列表
    public List<TGrade> findAll(Integer schoolid) {
        Integer year = schoolMapper.getYearById(schoolid);
        return gradeMapper.findAll(schoolid, year);
    }

    /**
     * 获取通知班级列表
     *
     * @return java.util.List<com.xhkjedu.suser.vo.gradeclass.GradeVo>
     * @Param [schoolid, year]
     * @Author ywx
     * @Date 2020/6/13 15:41
     **/
    public List<GradeVo> listClass(Integer schoolid, Integer classtype) {
        Integer year = schoolMapper.getYearById(schoolid);
        return gradeMapper.listClass(schoolid, year, classtype);
    }

    /**
     * @Description 获取班级学生列表
     * @Date 2023/12/18 14:50
     * @Author YWX
     * @Param [schoolid, classtype]
     * @Return java.util.List<java.util.Map>
     **/
    public List<Map> listClassStu(Integer schoolid, Integer classtype) {
        Integer year = schoolMapper.getYearById(schoolid);
        return gradeMapper.listClassStu(schoolid, year, classtype);
    }

    /**
     * 获取通知教师列表
     *
     * @return java.util.List<com.xhkjedu.suser.vo.gradeclass.GradeVo>
     * @Param [schoolid, year]
     * @Author ywx
     * @Date 2020/6/13 15:42
     **/
    public List<GradeVo> listTeacher(Integer schoolid) {
        Integer year = schoolMapper.getYearById(schoolid);
        return gradeMapper.listTeacher(schoolid, year);
    }

    // 获取行政班和提高班
    public List<GradeVo> listClassForTeacher(Integer schoolid) {
        Integer year = schoolMapper.getYearById(schoolid);
        return gradeMapper.listClassForTeacher(schoolid, year);
    }

    /*
     * @Description 集团校班级列表
     * @Author WN
     * @Date 2023/4/10 15:39
     */
    public List<Map> listJtClasses(Integer schoolid) {
        List<Map> list = gradeMapper.listJtClasses(schoolid);
        if (N_Utils.isListNotEmpty(list)) {
            for (Map map : list) {
                // 学校下年级
                List<Map> classes = (List<Map>) map.get("classes");
                Map<Integer, List<Map>> gradeMap = new LinkedHashMap<>();// 年级集合
                if (N_Utils.isListNotEmpty(classes)) {
                    for (Map classMap : classes) {
                        Integer gradeid = Integer.parseInt(classMap.get("gradeid").toString());
                        if (gradeMap.containsKey(gradeid)) {
                            List<Map> grade_class = gradeMap.get(gradeid);
                            grade_class.add(classMap);
                        } else {
                            List<Map> grade_class = new ArrayList<>();
                            grade_class.add(classMap);
                            gradeMap.put(gradeid,grade_class);
                        }
                    }

                    List<Map> gradelist = new ArrayList<>();
                    for (Map.Entry<Integer, List<Map>> entry : gradeMap.entrySet()) {
                        Map grademap = new LinkedHashMap();
                        grademap.put("gradeid", entry.getKey());
                        grademap.put("gradename", N_Utils.getGradeName(entry.getKey()));
                        grademap.put("classes", entry.getValue());
                        gradelist.add(grademap);
                    }
                    map.put("grades", gradelist);
                    map.remove("classes");
                }
            }
        }
        return list;
    }

    /**
     * @Description 集团校教师列表
     * @Param [schoolid]
     * @Author ywx
     * @Date 2023/7/21 16:17
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.String>>
     **/
    public List<Map<String, String>> listJtTeacher(Integer schoolid) {
        Integer year = schoolMapper.getYearById(schoolid);
        return gradeMapper.listJtTeacher(schoolid, year);
    }
}
