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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.exception.CustomException;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.bank.mapper.QuestionBankMapper;
import com.zhongzheng.modules.collect.bo.CollectCourseAddBo;
import com.zhongzheng.modules.collect.bo.CollectCourseEditBo;
import com.zhongzheng.modules.collect.bo.CollectCourseQueryBo;
import com.zhongzheng.modules.collect.domain.CollectCourse;
import com.zhongzheng.modules.collect.mapper.CollectCourseMapper;
import com.zhongzheng.modules.collect.service.ICollectCourseService;
import com.zhongzheng.modules.collect.vo.CollectCourseVo;
import com.zhongzheng.modules.course.domain.MajorCategory;
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-06-22
 */
@Service
public class CollectCourseServiceImpl extends ServiceImpl<CollectCourseMapper, CollectCourse> implements ICollectCourseService {

    @Autowired
    private CollectCourseMapper collectCourseMapper;

    @Override
    public CollectCourseVo queryById(Long collectCourseId){
        CollectCourse db = this.baseMapper.selectById(collectCourseId);
        return BeanUtil.toBean(db, CollectCourseVo.class);
    }

    @Override
    public CollectCourse queryByCourseId(Long userID, Long courseId) {
        CollectCourse info = getOne(new LambdaQueryWrapper<CollectCourse>()
                .eq(CollectCourse::getCourseId,courseId)
                .eq(CollectCourse::getUserId,userID)
                .last("limit 1"));
        return info;
    }

    @Override
    public List<CollectCourseVo> queryList(CollectCourseQueryBo bo) {
        LambdaQueryWrapper<CollectCourse> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCourseId() != null, CollectCourse::getCourseId, bo.getCourseId());
        lqw.eq(bo.getUserId() != null, CollectCourse::getUserId, bo.getUserId());
        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<CollectCourseVo> selectList(CollectCourseQueryBo bo) {
        return collectCourseMapper.selectList(bo);
    }

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

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

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

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CollectCourse entity){
        //TODO 做一些数据校验,如唯一约束
        CollectCourse info = getOne(new LambdaQueryWrapper<CollectCourse>()
                .eq(CollectCourse::getCourseId,entity.getCourseId())
                .eq(CollectCourse::getUserId,entity.getUserId())
                .last("limit 1"));
        if (Validator.isNotNull(info)) {
            throw new CustomException("你已收藏了");
        }
    }

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