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.*;
import com.zhongzheng.modules.course.domain.CourseEducationType;
import com.zhongzheng.modules.course.domain.MajorProject;
import com.zhongzheng.modules.course.service.IMajorLabelService;
import com.zhongzheng.modules.course.service.IMajorProjectService;
import com.zhongzheng.modules.course.vo.CourseProjectTypeVo;
import com.zhongzheng.modules.course.vo.MajorLabelVo;
import com.zhongzheng.modules.course.vo.MajorProjectVo;
import com.zhongzheng.modules.user.bo.SchoolProjectAddBo;
import com.zhongzheng.modules.user.domain.SchoolProject;
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.Major;
import com.zhongzheng.modules.course.mapper.MajorMapper;
import com.zhongzheng.modules.course.vo.MajorVo;
import com.zhongzheng.modules.course.service.IMajorService;

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 MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements IMajorService {

    @Autowired
    private IMajorProjectService majorProjectService;


    @Autowired
    private IMajorLabelService majorLabelService;

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

    @Override
    public List<MajorVo> queryList(MajorQueryBo bo) {

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

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<MajorVo> entity2Vo(Collection<Major> collection) {
        List<MajorVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, MajorVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<Major> page = (Page<Major>)collection;
            Page<MajorVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        for (MajorVo majorVo : voList) {
            CourseProjectTypeQueryBo courseProjectTypeQueryBo = new CourseProjectTypeQueryBo();
            courseProjectTypeQueryBo.setId(majorVo.getId());
            List<Integer> status = new ArrayList<>();
            status.add(1);
            courseProjectTypeQueryBo.setStatus(status);
            List<CourseProjectTypeVo> courseProjectTypeVos = baseMapper.selectProject(courseProjectTypeQueryBo);
            majorVo.setCourseProjectTypeVo(courseProjectTypeVos);

            MajorLabelVo majorLabelVo = majorLabelService.queryById(majorVo.getLabelId());
            majorVo.setLabelEncoder(majorLabelVo.getEncoder());
            majorVo.setLabelName(majorLabelVo.getLabelName());

        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(MajorAddBo bo) {
        Major add = BeanUtil.toBean(bo, Major.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        add.setEncoder(ServletUtils.getEncoded("ZY"));
        boolean save = this.save(add);
        for (Long schoolProject : bo.getMajorProjects()) {
            MajorProjectAddBo majorProjectAddBo = new MajorProjectAddBo();
            majorProjectAddBo.setProjectId(schoolProject);
            majorProjectAddBo.setMajorId(add.getId());
            majorProjectService.insertByAddBo(majorProjectAddBo);
        }
        return save;
    }

    @Override
    public Boolean updateByEditBo(MajorEditBo bo) {
        Major update = BeanUtil.toBean(bo, Major.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        if (bo.getMajorProjects() != null) {
            LambdaQueryWrapper<MajorProject> lqw = Wrappers.lambdaQuery();
            lqw.eq(bo.getId() != null, MajorProject::getMajorId, bo.getId());
            majorProjectService.remove(lqw);
            for (Long schoolProject : bo.getMajorProjects()) {
                MajorProjectAddBo majorProjectAddBo = new MajorProjectAddBo();
                majorProjectAddBo.setProjectId(schoolProject);
                majorProjectAddBo.setMajorId(update.getId());
                majorProjectService.insertByAddBo(majorProjectAddBo);
            }
        }
        return this.updateById(update);
    }

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

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