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.QuestionBankAddBo;
import com.zhongzheng.modules.bank.bo.QuestionBankEditBo;
import com.zhongzheng.modules.bank.bo.QuestionBankQueryBo;
import com.zhongzheng.modules.bank.domain.QuestionBank;
import com.zhongzheng.modules.bank.mapper.QuestionBankMapper;
import com.zhongzheng.modules.bank.service.IQuestionBankService;
import com.zhongzheng.modules.bank.vo.QuestionBankVo;
import com.zhongzheng.modules.course.bo.CourseQueryBo;
import com.zhongzheng.modules.course.domain.Course;
import com.zhongzheng.modules.course.mapper.CourseMapper;
import com.zhongzheng.modules.course.service.IMajorCategoryService;
import com.zhongzheng.modules.course.vo.CourseVo;
import com.zhongzheng.modules.course.vo.MajorCategoryVo;
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-05-21
 */
@Service
public class QuestionBankServiceImpl extends ServiceImpl<QuestionBankMapper, QuestionBank> implements IQuestionBankService {

    @Autowired
    private QuestionBankMapper questionBankMapper;

    @Autowired
    private IMajorCategoryService iMajorCategoryService;

    @Override
    public QuestionBankVo queryById(Long bankId){
        QuestionBank db = this.baseMapper.selectById(bankId);
        if(db.getCategoryId()!=null){
            MajorCategoryVo majorCategoryVo = iMajorCategoryService.queryById(db.getCategoryId());
            db.setCategoryName(majorCategoryVo.getCategoryName());
        }
        return BeanUtil.toBean(db, QuestionBankVo.class);
    }

    @Override
    public List<QuestionBankVo> queryList(QuestionBankQueryBo bo) {
        LambdaQueryWrapper<QuestionBank> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCategoryId() != null, QuestionBank::getCategoryId, bo.getCategoryId());
        lqw.like(StrUtil.isNotBlank(bo.getBankName()), QuestionBank::getBankName, bo.getBankName());
        lqw.eq(bo.getPrice() != null, QuestionBank::getPrice, bo.getPrice());
        lqw.eq(bo.getStartTime() != null, QuestionBank::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, QuestionBank::getEndTime, bo.getEndTime());
        lqw.eq(StrUtil.isNotBlank(bo.getCoverUrl()), QuestionBank::getCoverUrl, bo.getCoverUrl());
        lqw.eq(StrUtil.isNotBlank(bo.getIntroduction()), QuestionBank::getIntroduction, bo.getIntroduction());
        lqw.in(bo.getStatus() != null, QuestionBank::getStatus, bo.getStatus());
        lqw.eq(bo.getTypeId() != null, QuestionBank::getTypeId, bo.getTypeId());
        lqw.eq(StrUtil.isNotBlank(bo.getTeacherIds()), QuestionBank::getTeacherIds, bo.getTeacherIds());
        lqw.orderByDesc(QuestionBank::getSort);
        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<QuestionBankVo> selectBankList(QuestionBankQueryBo bo) {
        return questionBankMapper.selectBankList(bo);
    }

    @Override
    public List<QuestionBankVo> queryRecommendList(QuestionBankQueryBo bo) {
        return questionBankMapper.queryRecommendList(bo);
    }

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

    @Override
    public Long insertByAddBo(QuestionBankAddBo bo) {
        QuestionBank add = BeanUtil.toBean(bo, QuestionBank.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        return this.save(add)?add.getBankId():0;
    }

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

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

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