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 cn.exploring.engine.server.db.util.Util;
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.function.Function;
import java.util.stream.Collectors;

@Service
public class StudentPointService {

    @Resource
    StudentPointInfoMapper studentPointInfoMapper;

    @Resource
    SpecialSqlMapper specialSqlMapper;

    public Map selectPointByWeb( String studentName,  String studentUniqueId, Integer classId, String startTime,  String endTime, Integer page, Integer limit,  String sort,  String order) {
        List<Map> dataList =  specialSqlMapper.selectPoint(studentName, studentUniqueId, classId, startTime, endTime, page, limit, sort, order);
        Map<String, String> countInfoData = specialSqlMapper.selectPointCount(studentName, studentUniqueId, classId, startTime, endTime);
        Long countInfo = StringUtils.isEmpty(countInfoData.get("count_info")) ? 0L: Long.parseLong(countInfoData.get("count_info").toString());
        List<Map> targetDataList =  dataList.stream().map((Function<Map, Map>) Util::exchangeMapKeyName).collect(Collectors.toList());
        return new HashMap() {{
            put("items", targetDataList);
            put("total", countInfo);
        }};
    }

    /**
     * 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";
    }

}
