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.QuestionModuleChapterAddBo;
import com.zhongzheng.modules.bank.bo.QuestionModuleChapterEditBo;
import com.zhongzheng.modules.bank.bo.QuestionModuleChapterQueryBo;
import com.zhongzheng.modules.bank.domain.QuestionModuleChapter;
import com.zhongzheng.modules.bank.mapper.QuestionModuleChapterMapper;
import com.zhongzheng.modules.bank.service.IQuestionBusinessService;
import com.zhongzheng.modules.bank.service.IQuestionModuleChapterService;
import com.zhongzheng.modules.bank.vo.QuestionChapterVo;
import com.zhongzheng.modules.bank.vo.QuestionModuleChapterVo;
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-10-26
 */
@Service
public class QuestionModuleChapterServiceImpl extends ServiceImpl<QuestionModuleChapterMapper, QuestionModuleChapter> implements IQuestionModuleChapterService {

    @Autowired
    private QuestionModuleChapterMapper questionModuleChapterMapper;

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

    @Override
    public List<QuestionChapterVo> getList(QuestionModuleChapterQueryBo bo) {
        return questionModuleChapterMapper.getList(bo);
    }

    @Override
    public List<QuestionModuleChapterVo> queryList(QuestionModuleChapterQueryBo bo) {
        LambdaQueryWrapper<QuestionModuleChapter> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getModuleExamId() != null, QuestionModuleChapter::getModuleExamId, bo.getModuleExamId());
        lqw.eq(bo.getChapterExamId() != null, QuestionModuleChapter::getChapterExamId, bo.getChapterExamId());
        lqw.eq(bo.getSort() != null, QuestionModuleChapter::getSort, bo.getSort());
        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<QuestionChapterVo> getDoList(QuestionModuleChapterQueryBo bo) {
        return questionModuleChapterMapper.getDoList(bo);
    }

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

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

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

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

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