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

import cn.exploring.engine.server.db.dao.SpecialSqlMapper;
import cn.exploring.engine.server.db.dao.StudentPointInfoMapper;
import cn.exploring.engine.server.db.domain.StudentPointInfo;
import cn.exploring.engine.server.db.domain.StudentPointInfoExample;
import org.springframework.stereotype.Service;

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

@Service
public class StudentPointService {

    @Resource
    StudentPointInfoMapper studentPointInfoMapper;

    @Resource
    SpecialSqlMapper specialSqlMapper;

    public List<Map> selectPointByWeb( String studentName,  String studentUniqueId, Integer classId, String startTime,  String endTime, Integer page, Integer limit,  String sort,  String order) {
        return specialSqlMapper.selectPoint(studentName, studentUniqueId, classId, startTime, endTime, page, limit, sort, order);
    }

    /**
     * id 检索
     * @param idList
     * @return
     */
    public List<StudentPointInfo> selectPointInfo(List<Integer> idList) {
        StudentPointInfoExample example = new StudentPointInfoExample();
        StudentPointInfoExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (idList != null && idList.size() > 0) {
            criteria.andIdIn(idList);
        }
        return studentPointInfoMapper.selectByExample(example);

    }

    public String addPoint(StudentPointInfo studentPoint) {
        LocalDateTime now = LocalDateTime.now();
        studentPoint.setRecordTime(now);
        studentPoint.setUpdateTime(now);
        studentPoint.setAddTime(now);
        studentPoint.setDeleted(false);
        studentPointInfoMapper.insertSelective(studentPoint);
        return "add OK";
    }

    public Object updatePoint(StudentPointInfo studentPoint) {
        LocalDateTime now = LocalDateTime.now();
        studentPoint.setUpdateTime(now);
        studentPointInfoMapper.updateByPrimaryKey(studentPoint);
        return "update OK";
    }

    public Object batchDeleted(List<Integer> idList) {
        List<StudentPointInfo> studentPointInfoList = selectPointInfo(idList);
        studentPointInfoList.forEach(x -> {
            x.setDeleted(null);
            updatePoint(x);
        });
        return "batch deleted OK";
    }

}
