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.common.utils.ServletUtils;
import com.zhongzheng.modules.course.bo.CourseHandoutsBusinessAddBo;
import com.zhongzheng.modules.course.domain.CourseEducationTier;
import com.zhongzheng.modules.course.domain.CourseHandoutsBusiness;
import com.zhongzheng.modules.course.service.ICourseHandoutsBusinessService;
import com.zhongzheng.modules.course.vo.CourseHandoutsBusinessVo;
import org.apache.commons.math3.distribution.AbstractMultivariateRealDistribution;
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.course.bo.CourseHandoutsAddBo;
import com.zhongzheng.modules.course.bo.CourseHandoutsQueryBo;
import com.zhongzheng.modules.course.bo.CourseHandoutsEditBo;
import com.zhongzheng.modules.course.domain.CourseHandouts;
import com.zhongzheng.modules.course.mapper.CourseHandoutsMapper;
import com.zhongzheng.modules.course.vo.CourseHandoutsVo;
import com.zhongzheng.modules.course.service.ICourseHandoutsService;
import org.springframework.util.CollectionUtils;

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

/**
 * 讲义列Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-02
 */
@Service
public class CourseHandoutsServiceImpl extends ServiceImpl<CourseHandoutsMapper, CourseHandouts> implements ICourseHandoutsService {

    @Autowired
    private ICourseHandoutsBusinessService courseHandoutsBusinessService;

    @Override
    public CourseHandoutsVo queryById(Long handoutsId){
        CourseHandoutsQueryBo courseHandoutsQueryBo = new CourseHandoutsQueryBo();
        courseHandoutsQueryBo.setId(handoutsId);
        List<CourseHandoutsVo> courseHandoutsVos = entity2Vo(baseMapper.queryList(courseHandoutsQueryBo));
        if (CollectionUtils.isEmpty(courseHandoutsVos)){
            return null;
        }
        CourseHandoutsVo courseHandoutsVo = courseHandoutsVos.get(0);
        List<CourseHandoutsBusinessVo> courseHandoutsBusinessVos = baseMapper.selectEntity(courseHandoutsVo.getHandoutsId());
        courseHandoutsVo.setCourseHandoutsBusinessVo(courseHandoutsBusinessVos);
        return courseHandoutsVo;
    }

    @Override
    public List<CourseHandoutsVo> queryList(CourseHandoutsQueryBo bo) {

        return entity2Vo(baseMapper.queryList(bo));
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<CourseHandoutsVo> entity2Vo(Collection<CourseHandouts> collection) {
        List<CourseHandoutsVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, CourseHandoutsVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<CourseHandouts> page = (Page<CourseHandouts>)collection;
            Page<CourseHandoutsVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        for (CourseHandoutsVo courseHandoutsVo : voList) {
            List<CourseHandoutsBusinessVo> courseHandoutsBusinessVos = baseMapper.selectEntity(courseHandoutsVo.getHandoutsId());
            courseHandoutsVo.setCourseHandoutsBusinessVo(courseHandoutsBusinessVos);
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(CourseHandoutsAddBo bo) {
        CourseHandouts add = BeanUtil.toBean(bo, CourseHandouts.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        add.setEncoder(ServletUtils.getEncoded("JYLB"));
        boolean save = this.save(add);
        for (CourseHandoutsBusinessAddBo courseHandoutsBusinessAddBo : bo.getCourseHandoutsBusinessAddBos()) {
            CourseHandoutsBusiness addBusiness = BeanUtil.toBean(courseHandoutsBusinessAddBo, CourseHandoutsBusiness.class);
            addBusiness.setHandoutsId(add.getHandoutsId());
            addBusiness.setCreateTime(DateUtils.getNowTime());
            addBusiness.setUpdateTime(DateUtils.getNowTime());
            courseHandoutsBusinessService.save(addBusiness);
        }
        return save;
    }

    @Override
    public Boolean updateByEditBo(CourseHandoutsEditBo bo) {
        CourseHandouts update = BeanUtil.toBean(bo, CourseHandouts.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        LambdaQueryWrapper<CourseHandoutsBusiness> lqw = Wrappers.lambdaQuery();
        lqw.eq(CourseHandoutsBusiness::getHandoutsId,bo.getHandoutsId());
        courseHandoutsBusinessService.remove(lqw);
        if (!CollectionUtils.isEmpty(bo.getCourseHandoutsBusinessAddBos())) {
            for (CourseHandoutsBusinessAddBo courseHandoutsBusinessAddBo : bo.getCourseHandoutsBusinessAddBos()) {
                CourseHandoutsBusiness addBusiness = BeanUtil.toBean(courseHandoutsBusinessAddBo, CourseHandoutsBusiness.class);
                addBusiness.setHandoutsId(bo.getHandoutsId());
                addBusiness.setCreateTime(DateUtils.getNowTime());
                addBusiness.setUpdateTime(DateUtils.getNowTime());
                courseHandoutsBusinessService.save(addBusiness);
            }
        }
        return this.updateById(update);
    }



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

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