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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.bank.bo.QuestionChapterExamAddBo;
import com.zhongzheng.modules.bank.bo.QuestionChapterExamEditBo;
import com.zhongzheng.modules.bank.bo.QuestionChapterExamQueryBo;
import com.zhongzheng.modules.bank.domain.QuestionChapterExam;
import com.zhongzheng.modules.bank.mapper.QuestionChapterExamMapper;
import com.zhongzheng.modules.bank.mapper.QuestionModuleChapterMapper;
import com.zhongzheng.modules.bank.service.IQuestionChapterExamService;
import com.zhongzheng.modules.bank.vo.ExamVo;
import com.zhongzheng.modules.bank.vo.QuestionChapterExamVo;
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 ruoyi
 * @date 2021-10-25
 */
@Service
public class QuestionChapterExamServiceImpl extends ServiceImpl<QuestionChapterExamMapper, QuestionChapterExam> implements IQuestionChapterExamService {

    @Autowired
    private QuestionChapterExamMapper questionChapterExamMapper;

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

    @Override
    public List<ExamVo> getList(QuestionChapterExamQueryBo bo) {
        return questionChapterExamMapper.getList(bo);
    }

    @Override
    public List<ExamVo> getDoList(QuestionChapterExamQueryBo bo) {
        return questionChapterExamMapper.getDoList(bo);
    }

    @Override
    public List<QuestionChapterExamVo> queryList(QuestionChapterExamQueryBo bo) {
        LambdaQueryWrapper<QuestionChapterExam> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExamId() != null, QuestionChapterExam::getExamId, bo.getExamId());
        lqw.eq(bo.getChapterExamId() != null, QuestionChapterExam::getChapterExamId, bo.getChapterExamId());
        lqw.eq(bo.getSort() != null, QuestionChapterExam::getSort, bo.getSort());
        return entity2Vo(this.list(lqw));
    }

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

    @Override
    public Boolean insertByAddBo(QuestionChapterExamAddBo bo) {
        QuestionChapterExam add = BeanUtil.toBean(bo, QuestionChapterExam.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(QuestionChapterExamEditBo bo) {
        QuestionChapterExam update = BeanUtil.toBean(bo, QuestionChapterExam.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

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

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