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.CourseSectionBusinessAddBo;
import com.zhongzheng.modules.course.bo.CourseSectionBusinessEditBo;
import com.zhongzheng.modules.course.bo.CourseSectionBusinessQueryBo;
import com.zhongzheng.modules.course.domain.CourseSectionBusiness;
import com.zhongzheng.modules.course.mapper.CourseSectionBusinessMapper;
import com.zhongzheng.modules.course.service.ICourseSectionBusinessService;
import com.zhongzheng.modules.course.service.IMajorService;
import com.zhongzheng.modules.course.vo.CourseSectionBusinessVo;
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-11
 */
@Service
public class CourseSectionBusinessServiceImpl extends ServiceImpl<CourseSectionBusinessMapper, CourseSectionBusiness> implements ICourseSectionBusinessService {

    @Autowired
    private CourseSectionBusinessMapper courseSectionBusinessMapper;

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

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

    @Override
    public List<CourseSectionBusinessVo> queryList(CourseSectionBusinessQueryBo bo) {
        LambdaQueryWrapper<CourseSectionBusiness> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSectionId() != null, CourseSectionBusiness::getSectionId, bo.getSectionId());
        lqw.eq(bo.getEducationTypeId() != null, CourseSectionBusiness::getEducationTypeId, bo.getEducationTypeId());
        lqw.eq(bo.getBusinessId() != null, CourseSectionBusiness::getBusinessId, bo.getBusinessId());
        lqw.eq(bo.getSubjectId() != null, CourseSectionBusiness::getSubjectId, bo.getSubjectId());
        return entity2Vo(this.list(lqw));
    }

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

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

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

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

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