package com.xhkjedu.sapi.service.assess;

import com.xhkjedu.sapi.mapper.assess.AssessFileMapper;
import com.xhkjedu.sapi.mapper.assess.AssessQuotaMapper;
import com.xhkjedu.sapi.mapper.assess.AssessUserMapper;
import com.xhkjedu.sapi.mapper.gradeclass.ClassMapper;
import com.xhkjedu.sapi.mapper.subjectbook.SubjectMapper;
import com.xhkjedu.sapi.mapper.system.SchoolMapper;
import com.xhkjedu.sapi.mapper.system.UserMapper;
import com.xhkjedu.sapi.model.assess.TAssessFile;
import com.xhkjedu.sapi.model.assess.TAssessUser;
import com.xhkjedu.sapi.model.subjectbook.TSubject;
import com.xhkjedu.sapi.model.system.TSchool;
import com.xhkjedu.sapi.vo.subjectbook.SubjectVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author ywx
 * @classname AssessUserService
 * @description
 * @date 2021/11/10 14:25
 **/
@Service
public class AssessUserService {
    @Resource
    private AssessUserMapper assessUserMapper;
    @Resource
    private AssessFileMapper assessFileMapper;
    @Resource
    private SchoolMapper schoolMapper;
    @Resource
    private SubjectMapper subjectMapper;
    @Resource
    private ClassMapper classMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private AssessQuotaMapper assessQuotaMapper;

    //添加
    @Transactional(rollbackFor = Exception.class)
    public TAssessUser save(TAssessUser model) {
        model.setCreatetime(N_Utils.getSecondTimestamp());
        assessUserMapper.insertUseGeneratedKeys(model);
        Integer auid = model.getId();
        model.setAuid(auid);
        List<TAssessFile> files = model.getFiles();
        files.forEach(f -> {
            f.setAuid(auid);
            f.setCreatetime(N_Utils.getSecondTimestamp());
        });
        if (N_Utils.isListNotEmpty(files)) {
            assessFileMapper.insertList(files);
        }
        return model;
    }

    //删除
    public Integer deleteById(Integer id) {
        return assessUserMapper.deleteByPrimaryKey(id);
    }

    //获取评价用户详情
    public List<Map> listByAssess(TAssessUser user) {
        return assessUserMapper.listByAssess(user);
    }

    //获取评价用户附件列表
    public List<Map> listFile(Integer auid) {
        return assessUserMapper.listFile(auid);
    }

    /**
     * 评价用户批量添加
     *
     * @return void
     * @Param [users]
     * @Author ywx
     * @Date 2021/12/7 10:48
     **/
    public void batchSaveAssessUser(List<TAssessUser> users) {
        assessUserMapper.insertList(users);
    }

    //获取学校信息
    public TSchool getSchoolById(Integer schoolid) {
        return schoolMapper.selectByPrimaryKey(schoolid);
    }

    //获取阶段下所有科目
    public List<SubjectVo> listSubject(Integer schoolid, Integer schoollevel) {
        TSubject subject = new TSubject();
        subject.setSchoolid(schoolid);
        subject.setLevel(schoollevel);
        return subjectMapper.listSubjectsForSel(subject);
    }

    //根据班级名称获取班级id
    public Integer getClassIdByName(String classname, Integer schoolid, Integer gradeid, Integer year) {
        return classMapper.getClassIdByName(classname, schoolid, gradeid, year);
    }

    //根据班级id用户名获取学生id和数量
    public Map getUserIdByName(Integer schoolid, Integer classid, String username) {
        return userMapper.getUserIdByName(schoolid, classid, username);
    }

    //根据指标名称或编码获取指标id
    public Integer getQuotaIdByName(Integer schoolid, String quotaname) {
        return assessQuotaMapper.getQuotaIdByName(schoolid, quotaname);
    }

    //根据班级科目id用户名获取教师id和数量
    public Map getUserIdByName2(Integer schoolid, Integer classid, String username) {
        return userMapper.getUserIdByName2(schoolid, classid, username);
    }

    /**
     * 获取模板下的子指标
     *
     * @return java.util.List<java.util.Map>
     * @Param [templateid]
     * @Author ywx
     * @Date 2022/6/16 10:23
     **/
    public List<Map> listChildQuotaByTemplateId(Integer templateid) {
        return assessQuotaMapper.listChildQuotaByTemplateId(templateid);
    }
}
