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

import cn.exploring.engine.server.db.dao.CourseInitTestMapper;
import cn.exploring.engine.server.db.domain.CourseInitTest;
import cn.exploring.engine.server.db.domain.CourseInitTestExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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;

/**
 * @author Liu_c
 */
@Service
public class InitTestService {
    @Resource
    private CourseInitTestMapper initTestMapper;

    public Map selectCourseInitTest(Integer id, Integer titleRank, String title, String topic, List<Integer> idList, Integer page, Integer limit, String sort, String order){
        CourseInitTestExample example = new CourseInitTestExample();
        CourseInitTestExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (id != null && id > 0) {
            criteria.andIdEqualTo(id);
        }
        if (titleRank != null && titleRank > 0) {
            criteria.andTitleRankEqualTo(titleRank);
        }
        if (!StringUtils.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (!StringUtils.isEmpty(topic)) {
            criteria.andTopicLike("%" + topic + "%");
        }
        if (idList != null && idList.size() != 0) {
            criteria.andIdIn(idList);
        }
        PageHelper.startPage(page, limit);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order + " , add_time asc");
        }
        List<CourseInitTest> initTestList = initTestMapper.selectByExample(example);
        long total = PageInfo.of(initTestList).getTotal();
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("total", total);
        dataMap.put("items", initTestList);
        return dataMap;
    }

    public List<CourseInitTest> selectCourseInitTest(Integer id, Integer titleRank, String title, String topic, List<Integer> idList){
        CourseInitTestExample example = new CourseInitTestExample();
        CourseInitTestExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (id != null && id > 0) {
            criteria.andIdEqualTo(id);
        }
        if (titleRank != null && titleRank > 0) {
            criteria.andTitleRankEqualTo(titleRank);
        }
        if (!StringUtils.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (!StringUtils.isEmpty(topic)) {
            criteria.andTopicLike("%" + topic + "%");
        }
        if (idList != null && idList.size() != 0) {
            criteria.andIdIn(idList);
        }
        return initTestMapper.selectByExample(example);
    }

    public String addInitTest(CourseInitTest courseInitTest) {
        List<CourseInitTest> bothInitTest = selectCourseInitTest(null, courseInitTest.getTitleRank(), null, null, null);
        if (bothInitTest.size() != 0) {
            return "当前有重复的测试题顺序， 请检查输入数据";
        }
        LocalDateTime now = LocalDateTime.now();
        courseInitTest.setAddTime(now);
        courseInitTest.setUpdateTime(now);
        courseInitTest.setDeleted(false);
        initTestMapper.insertSelective(courseInitTest);
        return "insert Ok";
    }

    public Object updateInitTest(CourseInitTest courseInitTest) {
        LocalDateTime now = LocalDateTime.now();
        courseInitTest.setAddTime(now);
        courseInitTest.setUpdateTime(now);
        initTestMapper.updateByPrimaryKey(courseInitTest);
        return "update OK";
    }

    public String batchDeletedInitTest(List<Integer> initTestIdList) {
        List<CourseInitTest> initTestList = selectCourseInitTest(null, null, null, null, initTestIdList);
        LocalDateTime now = LocalDateTime.now();
        initTestList.forEach(x -> {
            x.setUpdateTime(now);
            x.setDeleted(null);
            updateInitTest(x);
        });
        return "batch deleted ok";
    }

    /**
     * 获取初始测试题
     * @param titleRank 题目的顺序  第几题  不传就给所有的题
     * @return
     */
    public List<CourseInitTest> selectInitTest(Integer titleRank) {
        CourseInitTestExample example = new CourseInitTestExample();
        CourseInitTestExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (titleRank != null && titleRank > 0) {
            criteria.andTitleRankEqualTo(titleRank);
        }
        example.orderBy("title_rank asc");
        return initTestMapper.selectByExample(example);
    }


}
