package com.zhongzheng.modules.collect.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.mapper.QuestionBankExamMapper;
import com.zhongzheng.modules.bank.vo.QuestionVo;
import com.zhongzheng.modules.collect.bo.CollectQuestionAddBo;
import com.zhongzheng.modules.collect.bo.CollectQuestionEditBo;
import com.zhongzheng.modules.collect.bo.CollectQuestionQueryBo;
import com.zhongzheng.modules.collect.domain.CollectNote;
import com.zhongzheng.modules.collect.domain.CollectQuestion;
import com.zhongzheng.modules.collect.mapper.CollectQuestionMapper;
import com.zhongzheng.modules.collect.service.ICollectQuestionService;
import com.zhongzheng.modules.collect.vo.CollectQuestionVo;
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-24
 */
@Service
public class CollectQuestionServiceImpl extends ServiceImpl<CollectQuestionMapper, CollectQuestion> implements ICollectQuestionService {

    @Autowired
    private CollectQuestionMapper collectQuestionMapper;

    @Override
    public CollectQuestionVo queryById(Long collectQuestionId){
        CollectQuestion db = this.baseMapper.selectById(collectQuestionId);
        return BeanUtil.toBean(db, CollectQuestionVo.class);
    }

    @Override
    public CollectQuestion queryQuestionById(Long userID, Long questionId) {
        CollectQuestion info = getOne(new LambdaQueryWrapper<CollectQuestion>()
                .eq(CollectQuestion::getQuestionId,questionId)
                .eq(CollectQuestion::getUserId,userID)
                .last("limit 1"));
        return info;
    }

    @Override
    public List<CollectQuestionVo> queryList(CollectQuestionQueryBo bo) {
        LambdaQueryWrapper<CollectQuestion> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getQuestionId() != null, CollectQuestion::getQuestionId, bo.getQuestionId());
        lqw.eq(bo.getUserId() != null, CollectQuestion::getUserId, bo.getUserId());
        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<QuestionVo> selectList(CollectQuestionQueryBo bo) {
        return collectQuestionMapper.selectList(bo);
    }


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

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

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

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

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