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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
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.DictDataMapMapper;
import com.persagy.dmp.dic.entity.DictDataMap;
import com.persagy.dmp.dic.service.DictDataMapService;
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 DictDataMapServiceImpl implements DictDataMapService {

    @Autowired
    private DictDataMapMapper dao;

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

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

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

    /**
     * 新增
     *
     * @param vo 待新增数据
     * @return
     */
    @Override
    public DictDataMap insert(DictDataMap vo) {
        vo.setValid(ValidEnum.TRUE.getType());
        dao.insert(vo);
        return vo;
    }

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

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

