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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.course.domain.Course;
import com.zhongzheng.modules.course.domain.MajorCategory;
import com.zhongzheng.modules.course.vo.CourseVo;
import com.zhongzheng.modules.exam.domain.ExamNote;
import com.zhongzheng.modules.exam.vo.ExamNoteVo;
import io.swagger.models.auth.In;
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.OrderPossessUserAddBo;
import com.zhongzheng.modules.user.bo.OrderPossessUserQueryBo;
import com.zhongzheng.modules.user.bo.OrderPossessUserEditBo;
import com.zhongzheng.modules.user.domain.OrderPossessUser;
import com.zhongzheng.modules.user.mapper.OrderPossessUserMapper;
import com.zhongzheng.modules.user.vo.OrderPossessUserVo;
import com.zhongzheng.modules.user.service.IOrderPossessUserService;

import java.util.ArrayList;
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 OrderPossessUserServiceImpl extends ServiceImpl<OrderPossessUserMapper, OrderPossessUser> implements IOrderPossessUserService {

    @Override
    public OrderPossessUserVo queryById(Long userId){
        OrderPossessUser db = this.baseMapper.selectById(userId);
        return BeanUtil.toBean(db, OrderPossessUserVo.class);
    }

    @Override
    public OrderPossessUser queryByTypeAndId(Long possessId, Integer typeId, Long userId) {
        OrderPossessUser info = getOne(new LambdaQueryWrapper<OrderPossessUser>()
                .eq(OrderPossessUser::getPossessId,possessId)
                .eq(OrderPossessUser::getTypeId,typeId)
                .eq(OrderPossessUser::getUserId,userId)
                .last("limit 1"));
        if (Validator.isNotNull(info)) {
            return info;
        }
        return null;
    }

    @Override
    public OrderPossessUserVo queryList(OrderPossessUserQueryBo bo) {
        OrderPossessUserVo orderPossessUserVo = BeanUtil.toBean(bo, OrderPossessUserVo.class);
        if (bo.getTypeId() == 1){
            List<Course> courses= baseMapper.selectByCource(bo);
            List<CourseVo> coursesVO = entityCourseVo(courses);
            orderPossessUserVo.setCourseVoList(coursesVO);
       }else if (bo.getTypeId() == 2){

       }else if (bo.getTypeId() == 3){
            List<ExamNote> examNoteVos = baseMapper.selectByEmphasis(bo);
            List<ExamNoteVo> ExamNoteVos = entityExamNoteVo(examNoteVos);
            orderPossessUserVo.setExamNoteList(ExamNoteVos);
       }
        return orderPossessUserVo;
    }

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


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



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

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

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

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

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