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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.exception.CustomException;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.user.bo.SchoolTypeAddBo;
import com.zhongzheng.modules.user.bo.SchoolTypeEditBo;
import com.zhongzheng.modules.user.bo.SchoolTypeQueryBo;
import com.zhongzheng.modules.user.domain.SchoolType;
import com.zhongzheng.modules.user.mapper.SchoolTypeMapper;
import com.zhongzheng.modules.user.service.ISchoolTypeService;
import com.zhongzheng.modules.user.vo.SchoolTypeVo;
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.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 高校类型Service业务层处理
 *
 * @author hjl
 * @date 2021-05-18
 */
@Service
public class SchoolTypeServiceImpl extends ServiceImpl<SchoolTypeMapper, SchoolType> implements ISchoolTypeService {

    @Override
    public SchoolTypeVo queryById(Long id){
        SchoolType db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, SchoolTypeVo.class);
    }

    @Override
    public List<SchoolTypeVo> queryList(SchoolTypeQueryBo bo) {
        LambdaQueryWrapper<SchoolType> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getTypeName()), SchoolType::getTypeName, bo.getTypeName());
        lqw.eq(bo.getOrderNum() != null, SchoolType::getOrderNum, bo.getOrderNum());
        lqw.eq(bo.getStatus() != null, SchoolType::getStatus, bo.getStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getIntroduce()), SchoolType::getIntroduce, bo.getIntroduce());
        return entity2Vo(this.list(lqw));
    }

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

    @Override
    public Boolean insertByAddBo(SchoolTypeAddBo bo) {
        SchoolType add = BeanUtil.toBean(bo, SchoolType.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        return this.save(add);
    }

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

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SchoolType entity){
        //TODO 做一些数据校验,如唯一约束
        if(checkNameUnique(entity)){
            throw new CustomException("分类名已存在");
        }
    }

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

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