package com.zhongzheng.modules.user.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.CourseProjectTypeQueryBo;
import com.zhongzheng.modules.course.domain.CourseEducationType;
import com.zhongzheng.modules.course.service.ICourseProjectTypeService;
import com.zhongzheng.modules.course.vo.CourseProjectTypeVo;
import com.zhongzheng.modules.user.bo.*;
import com.zhongzheng.modules.user.domain.SchoolProject;
import com.zhongzheng.modules.user.service.ISchoolProjectService;
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.user.domain.School;
import com.zhongzheng.modules.user.mapper.SchoolMapper;
import com.zhongzheng.modules.user.vo.SchoolVo;
import com.zhongzheng.modules.user.service.ISchoolService;
import org.springframework.util.CollectionUtils;

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 SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements ISchoolService {

    @Autowired
    private ISchoolProjectService schoolProjectService;

    @Autowired
    private ICourseProjectTypeService courseProjectTypeService;

    @Override
    public SchoolVo queryById(Long id){
        SchoolQueryBo schoolQueryBo = new SchoolQueryBo();
        schoolQueryBo.setId(id);
        List<SchoolVo> schoolVos = baseMapper.queryList(schoolQueryBo);
        if (CollectionUtils.isEmpty(schoolVos)) {
            throw new IllegalArgumentException("错误ID");
        }
        return entity2Vo(schoolVos).get(0);
    }

    @Override
    public List<SchoolVo> queryList(SchoolQueryBo bo) {

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

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

    @Override
    public Boolean insertByAddBo(SchoolAddBo bo) {
        School add = BeanUtil.toBean(bo, School.class);
        validEntityBeforeSave(add);
        add.setEncoder(ServletUtils.getEncoded("YX"));
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        boolean save = this.save(add);
        for (Long schoolProject : bo.getSchoolProjects()) {
            SchoolProjectAddBo schoolProjectAddBo = new SchoolProjectAddBo();
            schoolProjectAddBo.setProjectId(schoolProject);
            schoolProjectAddBo.setSchoolId(add.getId());
            schoolProjectService.insertByAddBo(schoolProjectAddBo);
        }
        return save;
    }

    @Override
    public Boolean updateByEditBo(SchoolEditBo bo) {
        School update = BeanUtil.toBean(bo, School.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        if (bo.getSchoolProjects() != null) {
            LambdaQueryWrapper<SchoolProject> lqw = Wrappers.lambdaQuery();
            lqw.eq(bo.getId() != null, SchoolProject::getSchoolId, bo.getId());
            schoolProjectService.remove(lqw);
            for (Long schoolProject : bo.getSchoolProjects()) {
                SchoolProjectAddBo schoolProjectAddBo = new SchoolProjectAddBo();
                schoolProjectAddBo.setProjectId(schoolProject);
                schoolProjectAddBo.setSchoolId(update.getId());
                schoolProjectService.insertByAddBo(schoolProjectAddBo);
            }
        }
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(School entity){
        SchoolQueryBo schoolQueryBo = new SchoolQueryBo();
        schoolQueryBo.setSchoolName(entity.getSchoolName());
        List<SchoolVo> schoolVos = baseMapper.queryList(schoolQueryBo);
        //TODO 做一些数据校验,如唯一约束
    }

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