package cn.exploring.engine.server.db.service;

import cn.exploring.engine.server.db.dao.SpecialSqlMapper;
import cn.exploring.engine.server.db.dao.StudentInfoMapper;
import cn.exploring.engine.server.db.domain.StudentInfo;
import cn.exploring.engine.server.db.domain.StudentInfoExample;
import cn.exploring.engine.server.db.domain.vo.StudentVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StudentInfoService {

    @Resource
    StudentInfoMapper studentInfoMapper;

    @Resource
    ClassService classService;

    @Resource
    SpecialSqlMapper specialSqlMapper;



    /**
     * 添加
     * @param studentInfo
     * @return
     */
    public String addStudentInfo(StudentInfo studentInfo) {
        if (selectStudentInfoByUniqueId(studentInfo.getStudentUniqueId()) != null) {
            return "Fail 学号重复！ 请重新输入学号";
        }
        LocalDateTime now = LocalDateTime.now();
        studentInfo.setAddTime(now);
        studentInfo.setUpdateTime(now);
        studentInfoMapper.insertSelective(studentInfo);
        return "OK 添加成功";
    }

    /**
     * 随机选取一个 学生数据
     * @param classId
     * @return
     */
    public Map selectStudentInfo(Integer classId) {
        return specialSqlMapper.selectRandomStudentInfo(classId);
    }

    /**
     * 更新数据
     * @param studentInfo
     * @return
     */
    public Object updateStudentInfo(StudentInfo studentInfo) {
        studentInfo.setUpdateTime(LocalDateTime.now());
        studentInfoMapper.updateByPrimaryKey(studentInfo);
        return "OK 修改成功";
    }

    /**
     * 批量删除数据
     * @param idList
     * @return
     */
    public Object batchDeleted(List<Integer> idList) {
        List<StudentInfo> studentInfoList = selectStudentInfo(null, null, idList);
        studentInfoList.forEach(x -> {
            x.setDeleted(null);
            updateStudentInfo(x);
        });
        return "batch deleted  OK";
    }

    /**
     * 根据标记学号进行查询
     * @param uniqueId
     * @return
     */
    public StudentInfo selectStudentInfoByUniqueId(String uniqueId) {
        StudentInfoExample example = new StudentInfoExample();
        StudentInfoExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false).andStudentUniqueIdEqualTo(uniqueId);
        return studentInfoMapper.selectOneByExample(example);

    }

    /**
     * 检索学生相关的信息
     * @param studentName
     * @param studentId
     * @return
     */
    public List<StudentInfo> selectStudentInfo(String studentName, String studentId, List<Integer> idList) {
        StudentInfoExample example = new StudentInfoExample();
        StudentInfoExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(studentName)) {
            criteria.andNameLike("%" + studentName + "%");
        }
        if (!StringUtils.isEmpty(studentId)) {
            criteria.andStudentUniqueIdEqualTo(studentId);
        }
        if (idList != null && idList.size() > 0) {
            criteria.andIdIn(idList);
        }
        return studentInfoMapper.selectByExample(example);
    }

    /**
     * 检索学生相关的信息
     * @param studentName
     * @param studentId
     * @return
     */
    public Map selectStudentInfoByWeb(String studentName, String studentId, List<Integer> idList, Integer classId, Integer page, Integer limit, String sort, String order) {
        StudentInfoExample example = new StudentInfoExample();
        StudentInfoExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(studentName)) {
            criteria.andNameLike("%" + studentName + "%");
        }
        if (!StringUtils.isEmpty(studentId)) {
            criteria.andStudentUniqueIdLike("%" + studentId + "%");
        }
        if (idList != null && idList.size() > 0) {
            criteria.andIdIn(idList);
        }
        if (classId != null) {
            criteria.andClassIdEqualTo(classId);
        }
        PageHelper.startPage(page, limit);
        example.setOrderByClause(sort + " " + order);
        List<StudentInfo> studentInfoList =  studentInfoMapper.selectByExample(example);
        Map<Integer, String> classMap = classService.getClassNameMap();
        List<StudentVo> dataList = studentInfoList.stream().map(x -> {
            StudentVo voData = new StudentVo();
            BeanUtils.copyProperties(x, voData);
            voData.setClassName(classMap.get(x.getClassId()));
            return voData;
        }).collect(Collectors.toList());
        Long total = PageInfo.of(studentInfoList).getTotal();
        return new HashMap(){{
            put("total", total);
            put("items", dataList);
        }};
    }



}
