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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.modules.course.bo.*;
import com.zhongzheng.modules.course.domain.CourseEducationType;
import com.zhongzheng.modules.course.domain.CourseSubjectProject;
import com.zhongzheng.modules.course.service.ICourseSubjectProjectService;
import com.zhongzheng.modules.course.vo.CourseProjectTypeVo;
import com.zhongzheng.modules.user.bo.SchoolProjectAddBo;
import com.zhongzheng.modules.user.domain.SchoolProject;
import com.zhongzheng.modules.user.vo.SchoolVo;
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.domain.CourseSubject;
import com.zhongzheng.modules.course.mapper.CourseSubjectMapper;
import com.zhongzheng.modules.course.vo.CourseSubjectVo;
import com.zhongzheng.modules.course.service.ICourseSubjectService;

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-10-09
 */
@Service
public class CourseSubjectServiceImpl extends ServiceImpl<CourseSubjectMapper, CourseSubject> implements ICourseSubjectService {

    @Autowired
    private ICourseSubjectProjectService courseSubjectProjectService;

    @Override
    public CourseSubjectVo queryById(Long id){
        CourseSubject db = this.baseMapper.selectById(id);
        CourseSubjectVo courseSubjectVo = BeanUtil.toBean(db, CourseSubjectVo.class);
        CourseProjectTypeQueryBo courseProjectTypeQueryBo = new CourseProjectTypeQueryBo();
        courseProjectTypeQueryBo.setId(courseSubjectVo.getId());
        List<Integer> status = new ArrayList<>();
        status.add(1);
        courseProjectTypeQueryBo.setStatus(status);
        List<CourseProjectTypeVo> courseProjectTypeVos = baseMapper.selectProject(courseProjectTypeQueryBo);
        courseSubjectVo.setCourseProjectTypes(courseProjectTypeVos);
        return courseSubjectVo;
    }

    @Override
    public List<CourseSubjectVo> queryList(CourseSubjectQueryBo bo) {
        List<CourseSubjectVo> courseSubjectVos = entity2Vo(baseMapper.queryList(bo));
        for (CourseSubjectVo courseSubjectVo : courseSubjectVos) {
            courseSubjectVo.setCourseProjectTypes(baseMapper.queryProject(courseSubjectVo.getId()));
        }
        return courseSubjectVos;
    }

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

    @Override
    public Boolean insertByAddBo(CourseSubjectAddBo bo) {
        CourseSubjectQueryBo courseSubjectQueryBo = new CourseSubjectQueryBo();
        courseSubjectQueryBo.setSubjectName(bo.getSubjectName());
        List<Integer> status = new ArrayList<>();
        status.add(1);
        courseSubjectQueryBo.setStatus(status);
        Collection<CourseSubjectVo> courseSubjectVos = baseMapper.queryList(courseSubjectQueryBo);
        if (CollectionUtils.isNotEmpty(courseSubjectVos)){
            throw new RuntimeException("重复科目名称");
        }
        CourseSubject add = BeanUtil.toBean(bo, CourseSubject.class);
        validEntityBeforeSave(add);
        add.setEncoder(ServletUtils.getEncoded("KM"));
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        boolean save = this.save(add);
        for (Long projectId : bo.getCourseSubjectProjects()) {
            CourseSubjectProjectAddBo courseSubjectProjectAddBo = new CourseSubjectProjectAddBo();
            courseSubjectProjectAddBo.setProjectId(projectId);
            courseSubjectProjectAddBo.setSubjectId(add.getId());
            courseSubjectProjectService.insertByAddBo(courseSubjectProjectAddBo);
        }
        return save;
    }

    @Override
    public Boolean updateByEditBo(CourseSubjectEditBo bo) {
        CourseSubjectQueryBo courseSubjectQueryBo = new CourseSubjectQueryBo();
        courseSubjectQueryBo.setSubjectName(bo.getSubjectName());
        courseSubjectQueryBo.setId(bo.getId());
        Collection<CourseSubjectVo> courseSubjectVos = baseMapper.queryList(courseSubjectQueryBo);
        if (CollectionUtils.isEmpty(courseSubjectVos)){
            CourseSubjectQueryBo courseSubjectQuery = new CourseSubjectQueryBo();
            courseSubjectQuery.setSubjectName(bo.getSubjectName());
            List<Integer> status = new ArrayList<>();
            status.add(1);
            courseSubjectQuery.setStatus(status);
            Collection<CourseSubjectVo> courseSubjectVo = baseMapper.queryList(courseSubjectQuery);
            if (CollectionUtils.isNotEmpty(courseSubjectVo)){
                throw new RuntimeException("重复科目名称");
            }
        }
        CourseSubject update = BeanUtil.toBean(bo, CourseSubject.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        if (bo.getCourseSubjectProjects() != null) {
            LambdaQueryWrapper<CourseSubjectProject> lqw = Wrappers.lambdaQuery();
            lqw.eq(bo.getId() != null, CourseSubjectProject::getSubjectId, bo.getId());
            courseSubjectProjectService.remove(lqw);
            for (Long projectId : bo.getCourseSubjectProjects()) {
                CourseSubjectProjectAddBo courseSubjectProjectAddBo = new CourseSubjectProjectAddBo();
                courseSubjectProjectAddBo.setProjectId(projectId);
                courseSubjectProjectAddBo.setSubjectId(update.getId());
                courseSubjectProjectService.insertByAddBo(courseSubjectProjectAddBo);
            }
        }
        return this.updateById(update);
    }

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

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

    @Override
    public List<CourseSubjectVo> queryListAll(CourseSubjectQueryBo bo) {
        Collection<CourseSubjectVo> courseSubjectVos = baseMapper.queryList(bo);
        if (bo.getEducationId() != null || bo.getBusinessId() != null || bo.getProjectId() !=null){
            CourseSubjectQueryBo courseSubjectQueryBo = new CourseSubjectQueryBo();
            courseSubjectQueryBo.setWrong(1L);
            courseSubjectVos.addAll(baseMapper.queryList(courseSubjectQueryBo));
        }
        return entity2Vo(courseSubjectVos);
    }
}
