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 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.CourseProjectTypeAddBo;
import com.zhongzheng.modules.course.bo.CourseProjectTypeQueryBo;
import com.zhongzheng.modules.course.bo.CourseProjectTypeEditBo;
import com.zhongzheng.modules.course.domain.CourseProjectType;
import com.zhongzheng.modules.course.mapper.CourseProjectTypeMapper;
import com.zhongzheng.modules.course.vo.CourseProjectTypeVo;
import com.zhongzheng.modules.course.service.ICourseProjectTypeService;
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-08
 */
@Service
public class CourseProjectTypeServiceImpl extends ServiceImpl<CourseProjectTypeMapper, CourseProjectType> implements ICourseProjectTypeService {

    @Override
    public CourseProjectTypeVo queryById(Long id){
        CourseProjectTypeQueryBo courseProjectTypeQueryBo = new CourseProjectTypeQueryBo();
        courseProjectTypeQueryBo.setId(id);
        List<CourseProjectTypeVo> courseProjectTypeVos = entity2Vo(baseMapper.queryList(courseProjectTypeQueryBo));
        if (CollectionUtils.isEmpty(courseProjectTypeVos)){
            throw new IllegalArgumentException("请输入正确业务ID");
        }
        return BeanUtil.toBean(courseProjectTypeVos.get(0), CourseProjectTypeVo.class);
    }

    @Override
    public List<CourseProjectTypeVo> queryList(CourseProjectTypeQueryBo bo) {
        return entity2Vo(baseMapper.queryList(bo));
    }

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

    @Override
    public Boolean insertByAddBo(CourseProjectTypeAddBo bo) {
        CourseProjectType add = BeanUtil.toBean(bo, CourseProjectType.class);
        validEntityBeforeSave(add);
        add.setEncoder(ServletUtils.getEncoded("XM"));
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(CourseProjectTypeEditBo bo) {
        CourseProjectType update = BeanUtil.toBean(bo, CourseProjectType.class);
        if (bo.getStatus() != -1) {
            CourseProjectTypeQueryBo courseProjectTypeQueryBo = new CourseProjectTypeQueryBo();
            courseProjectTypeQueryBo.setProjectName(bo.getProjectName());
            courseProjectTypeQueryBo.setEducationId(bo.getEducationId());
            courseProjectTypeQueryBo.setId(bo.getId());
            List<Integer> longs = new ArrayList<>();
            longs.add(1);
            courseProjectTypeQueryBo.setStatus(longs);
            Collection<CourseProjectTypeVo> courseProjectTypeVos = baseMapper.queryList(courseProjectTypeQueryBo);
            if (CollectionUtils.isEmpty(courseProjectTypeVos)) {
                validEntityBeforeSave(update);
            }
        }
        update.setUpdateTime(DateUtils.getNowTime());
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CourseProjectType entity){
        CourseProjectTypeQueryBo courseProjectTypeQueryBo = new CourseProjectTypeQueryBo();
        courseProjectTypeQueryBo.setProjectName(entity.getProjectName());
        courseProjectTypeQueryBo.setEducationId(entity.getEducationId());
        List<Integer> longs = new ArrayList<>();
        longs.add(1);
        courseProjectTypeQueryBo.setStatus(longs);
        Collection<CourseProjectTypeVo> courseProjectTypeVos = baseMapper.queryList(courseProjectTypeQueryBo);
        if (!CollectionUtils.isEmpty(courseProjectTypeVos)){
            throw new RuntimeException("项目重复");
        }
        //TODO 做一些数据校验,如唯一约束
    }

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