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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.course.bo.CourseChapterBusinessAddBo;
import com.zhongzheng.modules.course.bo.CourseChapterBusinessEditBo;
import com.zhongzheng.modules.course.bo.CourseChapterBusinessQueryBo;
import com.zhongzheng.modules.course.domain.CourseChapterBusiness;
import com.zhongzheng.modules.course.mapper.CourseChapterBusinessMapper;
import com.zhongzheng.modules.course.mapper.CourseSectionBusinessMapper;
import com.zhongzheng.modules.course.service.ICourseChapterBusinessService;
import com.zhongzheng.modules.course.vo.CourseChapterBusinessVo;
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 hjl
 * @date 2021-10-09
 */
@Service
public class CourseChapterBusinessServiceImpl extends ServiceImpl<CourseChapterBusinessMapper, CourseChapterBusiness> implements ICourseChapterBusinessService {

    @Autowired
    private CourseChapterBusinessMapper courseChapterBusinessMapper;

    @Override
    public CourseChapterBusinessVo queryById(Long id){
        CourseChapterBusiness db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, CourseChapterBusinessVo.class);
    }

    @Override
    public List<CourseChapterBusiness> getListById(Long id) {
        return courseChapterBusinessMapper.getListById(id);
    }

    @Override
    public List<CourseChapterBusinessVo> queryList(CourseChapterBusinessQueryBo bo) {
        LambdaQueryWrapper<CourseChapterBusiness> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getChapterId() != null, CourseChapterBusiness::getChapterId, bo.getChapterId());
        lqw.eq(bo.getEducationTypeId() != null, CourseChapterBusiness::getEducationTypeId, bo.getEducationTypeId());
        lqw.eq(bo.getBusinessId() != null, CourseChapterBusiness::getBusinessId, bo.getBusinessId());
        lqw.eq(bo.getSubjectId() != null, CourseChapterBusiness::getSubjectId, bo.getSubjectId());
        return entity2Vo(this.list(lqw));
    }

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

    @Override
    public Boolean insertByAddBo(CourseChapterBusinessAddBo bo) {
        CourseChapterBusiness add = BeanUtil.toBean(bo, CourseChapterBusiness.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

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

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

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