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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.course.vo.CourseVo;
import com.zhongzheng.modules.goods.vo.GoodsVo;
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.UserPlanAddBo;
import com.zhongzheng.modules.user.bo.UserPlanQueryBo;
import com.zhongzheng.modules.user.bo.UserPlanEditBo;
import com.zhongzheng.modules.user.domain.UserPlan;
import com.zhongzheng.modules.user.mapper.UserPlanMapper;
import com.zhongzheng.modules.user.vo.UserPlanVo;
import com.zhongzheng.modules.user.service.IUserPlanService;

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

/**
 * 学习计划Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-08
 */
@Service
public class UserPlanServiceImpl extends ServiceImpl<UserPlanMapper, UserPlan> implements IUserPlanService {

    @Override
    public UserPlanVo queryById(Long planId){
        UserPlan db = this.baseMapper.selectById(planId);
        return BeanUtil.toBean(db, UserPlanVo.class);
    }

    @Override
    public List<UserPlanVo> queryList(UserPlanQueryBo bo) {
        LambdaQueryWrapper<UserPlan> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getSchedule()), UserPlan::getSchedule, bo.getSchedule());
        lqw.eq(bo.getUserId() != null, UserPlan::getUserId, bo.getUserId());
        lqw.eq(bo.getExamDate() != null, UserPlan::getExamDate, bo.getExamDate());
        lqw.eq(StrUtil.isNotBlank(bo.getReminderTime()), UserPlan::getReminderTime, bo.getReminderTime());
        lqw.eq(StrUtil.isNotBlank(bo.getStudyCount()), UserPlan::getStudyCount, bo.getStudyCount());
        lqw.eq(bo.getStudyDay() != null, UserPlan::getStudyDay, bo.getStudyDay());
        lqw.eq(bo.getEndTime() != null, UserPlan::getEndTime, bo.getEndTime());
        lqw.eq(bo.getStartTime() != null, UserPlan::getStartTime, bo.getStartTime());
        lqw.eq(bo.getStatus() != null, UserPlan::getStatus, bo.getStatus());
        lqw.eq(bo.getPitchNum() != null, UserPlan::getPitchNum, bo.getPitchNum());
        lqw.eq(bo.getStudyNum() != null, UserPlan::getStudyNum, bo.getStudyNum());
        return entity2Vo(this.list(lqw));
    }

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

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

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

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

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

    @Override
    public UserPlanVo listPlan(UserPlanEditBo bo) {
        validEntityPlanBeforeSave(bo);
        return null;
    }

    @Override
    public List<GoodsVo> listGoods(UserPlanQueryBo bo) {
        return baseMapper.listGoods(bo);
    }


    /**
     * 保存前的数据校验
     *
     * @param bo 实体类数据
     */
    private void validEntityPlanBeforeSave(UserPlanEditBo bo){
        //判断是否商品已被生成过课程
        Integer rank = baseMapper.selectCountGoods(bo);
        if (rank > 0){
            throw new IllegalArgumentException("此网课学习计划正在进行中，若想再次制定请将计划完成或删除");
        }

    }
}
