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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.user.bo.UserSubscribeQueryBo;
import com.zhongzheng.modules.user.mapper.UserSubscribeMapper;
import com.zhongzheng.modules.user.service.IUserSubscribeService;
import com.zhongzheng.modules.user.vo.UserExamGoodsSupplementVo;
import com.zhongzheng.modules.user.vo.UserSubscribeVo;
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 com.zhongzheng.modules.user.bo.UserExamGoodsAddBo;
import com.zhongzheng.modules.user.bo.UserExamGoodsQueryBo;
import com.zhongzheng.modules.user.bo.UserExamGoodsEditBo;
import com.zhongzheng.modules.user.domain.UserExamGoods;
import com.zhongzheng.modules.user.mapper.UserExamGoodsMapper;
import com.zhongzheng.modules.user.vo.UserExamGoodsVo;
import com.zhongzheng.modules.user.service.IUserExamGoodsService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户下这个商品的考试次数，前培次数Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-07
 */
@Service
public class UserExamGoodsServiceImpl extends ServiceImpl<UserExamGoodsMapper, UserExamGoods> implements IUserExamGoodsService {

    @Autowired
    private UserSubscribeMapper userSubscribeMapper;

    @Override
    public UserExamGoodsVo queryById(Long userExamGoodsId){
        UserExamGoods db = this.baseMapper.selectById(userExamGoodsId);
        return BeanUtil.toBean(db, UserExamGoodsVo.class);
    }

    @Override
    public List<UserExamGoodsVo> queryList(UserExamGoodsQueryBo bo) {
        LambdaQueryWrapper<UserExamGoods> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, UserExamGoods::getUserId, bo.getUserId());
        lqw.eq(bo.getGoodsId() != null, UserExamGoods::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getExamNumber() != null, UserExamGoods::getExamNumber, bo.getExamNumber());
        lqw.eq(bo.getDoNumber() != null, UserExamGoods::getDoNumber, bo.getDoNumber());
        return entity2Vo(this.list(lqw));
    }

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

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

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

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

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

    @Override
    public List<UserExamGoodsVo> listUserExam(UserExamGoodsQueryBo bo) {
        return baseMapper.listUserExam(bo);
    }

    @Override
    public UserExamGoodsVo getUserExamInfo(UserExamGoodsQueryBo bo) {
        List<UserExamGoodsVo> userExamGoodsVo = baseMapper.listUserExam(bo);
        if (!CollectionUtils.isEmpty(userExamGoodsVo)) {
            UserExamGoodsVo userExamGoodsVos = userExamGoodsVo.get(0);
            List<UserExamGoodsSupplementVo> userExamGoodsSupplement = baseMapper.getUserExam(userExamGoodsVos.getUserExamGoodsId(), 1L);
            List<UserExamGoodsSupplementVo> userExamGoodsSupplementVos = baseMapper.getUserExam(userExamGoodsVos.getUserExamGoodsId(), 2L);
            UserSubscribeQueryBo userSubscribeQueryBo = new UserSubscribeQueryBo();
            userSubscribeQueryBo.setUserId(userExamGoodsVos.getUserId());
            userSubscribeQueryBo.setGoodsId(userExamGoodsVos.getGoodsId());
            userSubscribeQueryBo.setExamExpend(1);
            List<UserSubscribeVo> userSubscribe = userSubscribeMapper.listSubscribe(userSubscribeQueryBo);
            userSubscribeQueryBo.setExamExpend(null);
            userSubscribeQueryBo.setBeforeExpend(1);
            List<UserSubscribeVo> userSubscribeVos = userSubscribeMapper.listSubscribe(userSubscribeQueryBo);
            userExamGoodsVos.setUserExamGoodsSupplementVos(userExamGoodsSupplementVos);
            userExamGoodsVos.setUserSubscribe(userSubscribe);
            userExamGoodsVos.setUserSubscribeVos(userSubscribeVos);
            userExamGoodsVos.setUserExamGoodsSupplement(userExamGoodsSupplement);
            return userExamGoodsVos;
        }
        return null;
    }
}
