package com.xhkjedu.sstudy.service.recite;

import com.xhkjedu.sstudy.mapper.gradeclass.ClassMapper;
import com.xhkjedu.sstudy.mapper.recite.ReciteGroupMapper;
import com.xhkjedu.sstudy.mapper.recite.ReciteMapper;
import com.xhkjedu.sstudy.mapper.recite.ReciteStudentMapper;
import com.xhkjedu.sstudy.model.recite.TRecite;
import com.xhkjedu.sstudy.model.recite.TReciteGroup;
import com.xhkjedu.sstudy.model.recite.TReciteStudent;
import com.xhkjedu.sstudy.vo.recite.ReciteClassVo;
import com.xhkjedu.sstudy.vo.recite.ReciteVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

/**
 * @ClassName ReciteService
 * Description TODO
 * Author WN
 * Date 2020/6/10 9:25
 **/
@Slf4j
@Service
public class ReciteService {
    @Autowired
    private ReciteMapper reciteMapper;

    @Autowired
    private ReciteGroupMapper reciteGroupMapper;

    @Autowired
    private ReciteStudentMapper reciteStudentMapper;

    @Autowired
    private ClassMapper classMapper;

    @Transactional(rollbackFor = Exception.class)
    public ResultVo save(TRecite recite){
        try {

            recite.setCreatetime(N_Utils.getSecondTimestamp());
            recite.setRecitestate(22);//背诵作业状态：21未布置 22 已布置
            reciteMapper.insertUseGeneratedKeys(recite);
            //保存背诵班级-分组-学生
            saveReciteGroup(recite);

        }catch (Exception e){
            log.error("发布背诵失败：" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResultVo(1,"保存失败");
        }
        return new ResultVo(0,"保存成功");
    }

    //保存背诵班级-分组-学生
    private void saveReciteGroup(TRecite recite){
        //班级
        List<TReciteGroup> classlst = recite.getRclist().stream().distinct().collect(Collectors.toList());

        List<TReciteGroup> savegroup = new ArrayList<>();//保存班级分组
        List<TReciteStudent> savestu = new ArrayList<>();//保存班级学生

        for(TReciteGroup cl : classlst){

            TReciteGroup rg = new TReciteGroup();
            rg.setCreateid(recite.getCreateid());
            rg.setSchoolid(recite.getSchoolid());
            rg.setCreatetime(N_Utils.getSecondTimestamp());
            rg.setRgstate(22);//已布置
            rg.setReciteid(recite.getId());
            rg.setCreatetime(N_Utils.getSecondTimestamp());
            rg.setClassid(cl.getClassid());
            rg.setStarttime(recite.getStarttime());
            rg.setStoptime(recite.getStoptime());
            savegroup.add(rg);

            String groupids = StringUtils.join(cl.getGroups(),",");

            //获取选中班级分组下所有学生
            List<Integer> stus = classMapper.listStudentidsByGroupids(groupids);
            for(Integer s : stus){
                TReciteStudent rs = new TReciteStudent();
                rs.setReciteid(recite.getId());
                rs.setClassid(cl.getClassid());
                rs.setStudentid(s);
                rs.setCreateid(recite.getCreateid());
                rs.setCreatetime(N_Utils.getSecondTimestamp());
                rs.setSchoolid(recite.getSchoolid());

                savestu.add(rs);
            }
        }

        //批量保存背诵班级分组
        reciteGroupMapper.insertList(savegroup);
        //批量保存背诵班级分组学生
        reciteStudentMapper.saveBatch(savestu);
    }

    /**
     *功能描述 修改
     * @author WN
     * @date 2020/6/10
     * @param  * @param recite
     * @return java.lang.Integer
     */
    public Integer updateRecite(TRecite recite){
        return reciteMapper.updateRecite(recite);
    }

    /**
     *功能描述 删除
     * @author WN
     * @date 2020/6/10
     * @param  * @param reciteid
     * @return java.lang.Integer
     */
    public Integer delRecite(Integer reciteid){
        return reciteMapper.deleteByPrimaryKey(reciteid);
    }

    /**
     * 功能描述 列表--网页
     *
     * @param * @param recite
     * @return java.util.Map
     * @author WN
     * @date 2020/6/10
     */
    public List<Map> listRecites(TRecite recite) {
        List<Map> list = reciteMapper.listRecites(recite);
        return list;
    }

    /**
     * 获取背诵班级
     *
     * @return java.util.List<java.util.Map>
     * @Param [reciteid]
     * @Author ywx
     * @Date 2022/6/21 10:33
     **/
    public List<Map> listClass(Integer reciteid) {
        return reciteMapper.listClass(reciteid);
    }
}
