package com.zhongzheng.modules.exam.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import com.zhongzheng.common.exception.CustomException;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.course.mapper.CourseMapper;
import com.zhongzheng.modules.exam.bo.ExamConfigAddBo;
import com.zhongzheng.modules.exam.bo.ExamConfigEditBo;
import com.zhongzheng.modules.exam.bo.ExamConfigQueryBo;
import com.zhongzheng.modules.exam.domain.ExamConfig;
import com.zhongzheng.modules.exam.mapper.ExamConfigMapper;
import com.zhongzheng.modules.exam.service.IExamConfigService;
import com.zhongzheng.modules.exam.vo.ExamConfigVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 考题配置Service业务层处理
 *
 * @author hjl
 * @date 2021-05-21
 */
@Service
public class ExamConfigServiceImpl extends ServiceImpl<ExamConfigMapper, ExamConfig> implements IExamConfigService {


    @Autowired
    private ExamConfigMapper examConfigMapper;


    @Override
    public ExamConfigVo queryById(Long id){
        ExamConfig db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, ExamConfigVo.class);
    }

    @Override
    public List<ExamConfigVo> queryList(ExamConfigQueryBo bo) {
        LambdaQueryWrapper<ExamConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPassingScore() != null, ExamConfig::getPassingScore, bo.getPassingScore());
        lqw.eq(bo.getExamTime() != null, ExamConfig::getExamTime, bo.getExamTime());
        lqw.eq(bo.getSingleChoiceNum() != null, ExamConfig::getSingleChoiceNum, bo.getSingleChoiceNum());
        lqw.eq(bo.getSingleChoiceScore() != null, ExamConfig::getSingleChoiceScore, bo.getSingleChoiceScore());
        lqw.eq(bo.getMultipleChoiceNum() != null, ExamConfig::getMultipleChoiceNum, bo.getMultipleChoiceNum());
        lqw.eq(bo.getMultipleChoiceScore() != null, ExamConfig::getMultipleChoiceScore, bo.getMultipleChoiceScore());
        lqw.eq(bo.getJudgmentNum() != null, ExamConfig::getJudgmentNum, bo.getJudgmentNum());
        lqw.eq(bo.getJudgmentScore() != null, ExamConfig::getJudgmentScore, bo.getJudgmentScore());
        lqw.eq(bo.getCaseNum() != null, ExamConfig::getCaseNum, bo.getCaseNum());
        lqw.eq(bo.getCaseScore() != null, ExamConfig::getCaseScore, bo.getCaseScore());
        lqw.eq(bo.getTotalScore() != null, ExamConfig::getTotalScore, bo.getTotalScore());
        lqw.eq(bo.getStatus() != null, ExamConfig::getStatus, bo.getStatus());
        lqw.eq(bo.getCategoryId() != null, ExamConfig::getCategoryId, bo.getCategoryId());
        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<ExamConfigVo> selectExamConfigList(ExamConfigQueryBo bo) {
        return examConfigMapper.selectExamConfigList(bo);
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<ExamConfigVo> entity2Vo(Collection<ExamConfig> collection) {
        List<ExamConfigVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, ExamConfigVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<ExamConfig> page = (Page<ExamConfig>)collection;
            Page<ExamConfigVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(ExamConfigAddBo bo) {
        ExamConfig add = BeanUtil.toBean(bo, ExamConfig.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(ExamConfigEditBo bo) {
        ExamConfig update = BeanUtil.toBean(bo, ExamConfig.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ExamConfig entity){
        //TODO 做一些数据校验,如唯一约束
        if(Validator.isNotNull(entity.getCategoryId())&&checkMajorCategoryIdUnique(entity)){
            throw new CustomException("该分类ID配置已存在");
        }
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }

    private boolean checkMajorCategoryIdUnique(ExamConfig entity) {
        ExamConfig info = getOne(new LambdaQueryWrapper<ExamConfig>()
                .eq(ExamConfig::getCategoryId,entity.getCategoryId())
                .last("limit 1"));
        if (Validator.isNotNull(info)) {
            return true;
        }
        return false;
    }
}
