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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.course.service.IMajorCategoryService;
import com.zhongzheng.modules.course.vo.MajorCategoryVo;
import com.zhongzheng.modules.exam.bo.ExamNoteAddBo;
import com.zhongzheng.modules.exam.bo.ExamNoteEditBo;
import com.zhongzheng.modules.exam.bo.ExamNoteQueryBo;
import com.zhongzheng.modules.exam.domain.ExamNote;
import com.zhongzheng.modules.exam.mapper.ExamNoteMapper;
import com.zhongzheng.modules.exam.service.IExamNoteService;
import com.zhongzheng.modules.exam.vo.ExamNoteVo;
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 ExamNoteServiceImpl extends ServiceImpl<ExamNoteMapper, ExamNote> implements IExamNoteService {

    @Autowired
    private ExamNoteMapper examNoteMapper;

    @Autowired
    private IMajorCategoryService iMajorCategoryService;

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


    @Override
    public List<ExamNoteVo> queryList(ExamNoteQueryBo bo) {
        LambdaQueryWrapper<ExamNote> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCategoryId() != null, ExamNote::getCategoryId, bo.getCategoryId());
        lqw.like(StrUtil.isNotBlank(bo.getName()), ExamNote::getName, bo.getName());
        lqw.eq(StrUtil.isNotBlank(bo.getTeacherIds()), ExamNote::getTeacherIds, bo.getTeacherIds());
        lqw.eq(bo.getPrice() != null, ExamNote::getPrice, bo.getPrice());
        lqw.eq(StrUtil.isNotBlank(bo.getCoverUrl()), ExamNote::getCoverUrl, bo.getCoverUrl());
        lqw.eq(StrUtil.isNotBlank(bo.getIntroduction()), ExamNote::getIntroduction, bo.getIntroduction());
        lqw.in(bo.getStatus() != null, ExamNote::getStatus, bo.getStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getFileUrls()), ExamNote::getFileUrls, bo.getFileUrls());
        lqw.eq(bo.getSort() != null, ExamNote::getSort, bo.getSort());
        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<ExamNoteVo> queryRecommendList(ExamNoteQueryBo bo) {
        return examNoteMapper.queryRecommendList(bo);
    }

    @Override
    public List<ExamNoteVo> selectExamNoteList(ExamNoteQueryBo bo) {
        return examNoteMapper.selectExamNoteList(bo);
    }

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

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

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

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

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