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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.constant.UserConstants;
import com.zhongzheng.common.core.domain.entity.SysMenu;
import com.zhongzheng.common.exception.CustomException;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.course.bo.MajorCategoryAddBo;
import com.zhongzheng.modules.course.bo.MajorCategoryEditBo;
import com.zhongzheng.modules.course.bo.MajorCategoryQueryBo;
import com.zhongzheng.modules.course.domain.CourseSubjectProject;
import com.zhongzheng.modules.course.domain.MajorCategory;
import com.zhongzheng.modules.course.mapper.MajorCategoryMapper;
import com.zhongzheng.modules.course.service.IMajorCategoryService;
import com.zhongzheng.modules.course.vo.MajorCategoryVo;
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 java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【专业分类】Service业务层处理
 *
 * @author hjl
 * @date 2021-05-13
 */
@Service
public class MajorCategoryServiceImpl extends ServiceImpl<MajorCategoryMapper, MajorCategory> implements IMajorCategoryService {

    @Override
    public MajorCategoryVo queryById(Long categoryId){
        MajorCategory db = this.baseMapper.selectById(categoryId);
        return BeanUtil.toBean(db, MajorCategoryVo.class);
    }

    @Override
    public List<MajorCategoryVo> queryList(MajorCategoryQueryBo bo) {
        LambdaQueryWrapper<MajorCategory> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPId() != null, MajorCategory::getPId, bo.getPId());
        lqw.eq(bo.getSort() != null, MajorCategory::getSort, bo.getSort());
        lqw.in(bo.getStatus() != null, MajorCategory::getStatus, bo.getStatus());
        lqw.like(StrUtil.isNotBlank(bo.getCategoryName()), MajorCategory::getCategoryName, bo.getCategoryName());
        lqw.orderByDesc(MajorCategory::getUpdateTime);
        return entity2Vo(this.list(lqw));
    }

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

    @Override
    public Long insertByAddBo(MajorCategoryAddBo bo) {
        MajorCategory add = BeanUtil.toBean(bo, MajorCategory.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        return this.save(add)?add.getCategoryId():0;
    }

    @Override
    public Boolean updateByEditBo(MajorCategoryEditBo bo) {
        MajorCategory update = BeanUtil.toBean(bo, MajorCategory.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MajorCategory entity){
        //TODO 做一些数据校验,如唯一约束
        if(checkMajorCategoryNameUnique(entity)&&Validator.isEmpty(entity.getCategoryId())){
            throw new CustomException("分类名已存在");
        }
        if(Validator.isNotNull(entity.getPId())&&entity.getPId()>0&&!checkMajorCategoryPid(entity)){
            throw new CustomException("父ID不存在");
        }
    }

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


    private boolean checkMajorCategoryNameUnique(MajorCategory entity) {
        MajorCategory info = getOne(new LambdaQueryWrapper<MajorCategory>()
                .eq(MajorCategory::getCategoryName,entity.getCategoryName())
                .last("limit 1"));
        if (Validator.isNotNull(info)) {
            return true;
        }
        return false;
    }

    private boolean checkMajorCategoryPid(MajorCategory entity) {
        MajorCategory info = getOne(new LambdaQueryWrapper<MajorCategory>()
                .eq(MajorCategory::getCategoryId,entity.getPId())
                .last("limit 1"));
        if (Validator.isNotNull(info)) {
            return true;
        }
        return false;
    }
}
