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.collect.bo.CollectNoteAddBo;
import com.zhongzheng.modules.collect.bo.CollectNoteEditBo;
import com.zhongzheng.modules.collect.bo.CollectNoteQueryBo;
import com.zhongzheng.modules.collect.domain.CollectCourse;
import com.zhongzheng.modules.collect.domain.CollectNote;
import com.zhongzheng.modules.collect.mapper.CollectBankMapper;
import com.zhongzheng.modules.collect.mapper.CollectNoteMapper;
import com.zhongzheng.modules.collect.service.ICollectNoteService;
import com.zhongzheng.modules.collect.vo.CollectNoteVo;
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 CollectNoteServiceImpl extends ServiceImpl<CollectNoteMapper, CollectNote> implements ICollectNoteService {

    @Autowired
    private CollectNoteMapper collectNoteMapper;

    @Override
    public CollectNoteVo queryById(Long collectNoteId){
        CollectNote db = this.baseMapper.selectById(collectNoteId);
        return BeanUtil.toBean(db, CollectNoteVo.class);
    }

    @Override
    public List<CollectNoteVo> selectList(CollectNoteQueryBo bo) {
        return collectNoteMapper.selectList(bo);
    }

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

    @Override
    public List<CollectNoteVo> queryList(CollectNoteQueryBo bo) {
        LambdaQueryWrapper<CollectNote> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getFileId() != null, CollectNote::getFileId, bo.getFileId());
        lqw.eq(bo.getUserId() != null, CollectNote::getUserId, bo.getUserId());
        return entity2Vo(this.list(lqw));
    }

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

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

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

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CollectNote entity){
        //TODO 做一些数据校验,如唯一约束
        CollectNote info = getOne(new LambdaQueryWrapper<CollectNote>()
                .eq(CollectNote::getFileId,entity.getFileId())
                .eq(CollectNote::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);
    }
}
