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.CollectBankAddBo;
import com.zhongzheng.modules.collect.bo.CollectBankEditBo;
import com.zhongzheng.modules.collect.bo.CollectBankQueryBo;
import com.zhongzheng.modules.collect.domain.CollectBank;
import com.zhongzheng.modules.collect.domain.CollectCourse;
import com.zhongzheng.modules.collect.mapper.CollectBankMapper;
import com.zhongzheng.modules.collect.mapper.CollectCourseMapper;
import com.zhongzheng.modules.collect.service.ICollectBankService;
import com.zhongzheng.modules.collect.vo.CollectBankVo;
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-23
 */
@Service
public class CollectBankServiceImpl extends ServiceImpl<CollectBankMapper, CollectBank> implements ICollectBankService {


    @Autowired
    private CollectBankMapper collectBankMapper;

    @Override
    public CollectBankVo queryById(Long collectBankId){
        CollectBank db = this.baseMapper.selectById(collectBankId);
        return BeanUtil.toBean(db, CollectBankVo.class);
    }

    @Override
    public CollectBank queryByBankId(Long userID, Long bankId) {
        CollectBank info = getOne(new LambdaQueryWrapper<CollectBank>()
                .eq(CollectBank::getBankId,bankId)
                .eq(CollectBank::getUserId,userID)
                .last("limit 1"));
        return info;
    }

    @Override
    public List<CollectBankVo> queryList(CollectBankQueryBo bo) {
        LambdaQueryWrapper<CollectBank> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getBankId() != null, CollectBank::getBankId, bo.getBankId());
        lqw.eq(bo.getUserId() != null, CollectBank::getUserId, bo.getUserId());
        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<CollectBankVo> selectList(CollectBankQueryBo bo) {
        return collectBankMapper.selectList(bo);
    }

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

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

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

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