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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.bank.bo.QuestionAddBo;
import com.zhongzheng.modules.bank.bo.QuestionEditBo;
import com.zhongzheng.modules.bank.bo.QuestionQueryBo;
import com.zhongzheng.modules.bank.domain.*;
import com.zhongzheng.modules.bank.mapper.*;
import com.zhongzheng.modules.bank.service.IQuestionBankService;
import com.zhongzheng.modules.bank.service.IQuestionService;
import com.zhongzheng.modules.bank.vo.QuestionVo;
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.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 题库题目Service业务层处理
 *
 * @author hjl
 * @date 2021-05-20
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements IQuestionService {

    @Autowired
    private QuestionBankExamMapper questionBankExamMapper;

    @Autowired
    private QuestionBankMapper questionBankMapper;

    @Autowired
    private IQuestionBankService questionBankService;

    @Autowired
    private QuestionBankSectionMapper questionBankSectionMapper;

    @Autowired
    private QuestionBankChapterMapper questionBankChapterMapper;

    @Override
    public QuestionVo queryById(Long questionId){
        Question db = this.baseMapper.selectById(questionId);
        return BeanUtil.toBean(db, QuestionVo.class);
    }

    @Override
    public List<QuestionVo> queryList(QuestionQueryBo bo) {
        LambdaQueryWrapper<Question> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getContent()), Question::getContent, bo.getContent());
        lqw.eq(StrUtil.isNotBlank(bo.getType()), Question::getType, bo.getType());
        lqw.eq(bo.getAnswerQuestion() != null, Question::getAnswerQuestion, bo.getAnswerQuestion());
        lqw.eq(bo.getBankSectionId() != null, Question::getBankSectionId, bo.getBankSectionId());
        lqw.in(bo.getStatus() != null, Question::getStatus, bo.getStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getAnalysisContent()), Question::getAnalysisContent, bo.getAnalysisContent());
        lqw.eq(bo.getExamId() != null, Question::getExamId, bo.getExamId());
        lqw.eq(bo.getParentType() != null, Question::getParentType, bo.getParentType());
        return entity2Vo(this.list(lqw));
    }

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

    @Override
    public Boolean insertByAddBo(QuestionAddBo bo) {
        Question add = BeanUtil.toBean(bo, Question.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        boolean rs = this.save(add);
        if(rs){
            this.updateQuestionNum(add,1);
        }
        return rs;
    }

    @Override
    public Boolean updateByEditBo(QuestionEditBo bo) {
        Question update = BeanUtil.toBean(bo, Question.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        boolean rs = this.updateById(update);
        if(bo.getStatus()==-1&&rs){
            this.updateQuestionNum(update,-1);
        }
        return rs;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(Question entity){
        //TODO 做一些数据校验,如唯一约束
    }

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

    //更新题库题目数目
    private void updateQuestionNum(Question entity,int num){
        if(entity.getParentType()==1){
            //1普通练习
            QuestionBankSection dbSection = questionBankSectionMapper.selectById(entity.getBankSectionId());
            QuestionBankChapter dbChapter = questionBankChapterMapper.selectById(dbSection.getBankChapterId());
            UpdateWrapper<QuestionBank> updateWrapper = new UpdateWrapper<>();
            updateWrapper.setSql("question_num = question_num +"+num);
            updateWrapper.eq("bank_id", dbChapter.getBankId());
            questionBankService.update(updateWrapper);

        }else{
            // 2试卷练习 3真题练习
            QuestionBankExam db = questionBankExamMapper.selectById(entity.getExamId());
            UpdateWrapper<QuestionBank> updateWrapper = new UpdateWrapper<>();
            updateWrapper.setSql("question_num = question_num +"+num);
            updateWrapper.eq("bank_id", db.getBankId());
            questionBankService.update(updateWrapper);
        }
    }
}
