package com.persagy.dmp.dic.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.persagy.dmp.common.constant.ValidEnum;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.dic.dao.DictTypeMapper;
import com.persagy.dmp.dic.entity.DictType;
import com.persagy.dmp.dic.service.DictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 字典类型
 * @author:linhuili
 * @date:2021/6/10
 */
@Service
public class DictTypeServiceImpl implements DictTypeService {

    @Autowired
    private DictTypeMapper dao;

    /**
     * 按条件计数
     * @param queryWrapper 查询条件
     * @return
     */
    @Override
    public Integer queryCount(Wrapper<DictType> queryWrapper) {
        return dao.selectCount(queryWrapper);
    }

    /**
     * 分页查询
     * @param page 分页条件
     * @param queryWrapper 查询条件
     * @return
     */
    @Override
    public Page queryByCondition(Page page, Wrapper<DictType> queryWrapper) {
        return dao.selectPage(page, queryWrapper);
    }

    /**
     * 查询
     * @param queryWrapper 查询条件
     * @return
     */
    @Override
    public List<DictType> queryByCondition(Wrapper<DictType> queryWrapper) {
        return dao.selectList(queryWrapper);
    }

    /**
     * 新增
     * @param vo 待新增数据
     * @return
     */
    @Override
    public DictType insert(DictType vo) {
        //重复校验
        validateRepeat(vo);
        vo.setValid(ValidEnum.TRUE.getType());
        dao.insert(vo);
        return vo;
    }

    /**
     * 修改
     * @param vo 待修改数据
     * @return
     */
    @Override
    public DictType update(DictType vo) {
        DictType dictType = dao.selectById(vo.getId());
        if(dictType == null || 0 == dictType.getValid()){
            throw new BusinessException("数据不存在！");
        }

        //type重复校验
        if(!dictType.getType().equals(vo.getType())){
            validateRepeat(vo);
        }
        dao.updateById(vo);
        return vo;
    }

    /**
     * 删除
     * @param vo 主键
     */
    @Override
    public void delete(DictType vo) {
        DictType dictType = dao.selectById(vo);
        if(dictType == null || 0 == dictType.getValid()){
            throw new BusinessException("数据不存在！");
        }
        dictType.setValid(ValidEnum.FALSE.getType());
        dao.updateById(dictType);
    }

    /**
     * 重复校验
     * @param vo
     */
    private void validateRepeat(DictType vo){
        QueryWrapper<DictType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",vo.getType());
        queryWrapper.eq("valid",ValidEnum.TRUE.getType());
        List<DictType> dictTypeList = queryByCondition(queryWrapper);
        if(dictTypeList != null && dictTypeList.size() > 0){
            throw new BusinessException("type 不能重复！");
        }
    }
}
