package com.gyee.alarm.service.auto.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.alarm.model.auto.ProBasicDataDictionary;
import com.gyee.alarm.mapper.auto.ProBasicDataDictionaryMapper;
import com.gyee.alarm.service.auto.IProBasicDataDictionaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.common.model.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 数据字典表 服务实现类
 * </p>
 *
 * @author shilin
 * @since 2023-08-16
 */
@Service
public class ProBasicDataDictionaryServiceImpl extends ServiceImpl<ProBasicDataDictionaryMapper, ProBasicDataDictionary> implements IProBasicDataDictionaryService {

    @Override
    public List<ProBasicDataDictionary> getAllDatadictionary() {
        List<ProBasicDataDictionary> list = baseMapper.selectList(null);
        return list;
    }

    @Override
    public List<ProBasicDataDictionary> getDatadictionary(String category) {
        QueryWrapper<ProBasicDataDictionary> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(category)) {
            qw.lambda().eq(ProBasicDataDictionary::getCategory, category);
        }
        List <ProBasicDataDictionary> companysPage = baseMapper.selectList(qw);
        return companysPage;
    }

    @Override
    public int saveOrUpdateDatadictionary(ProBasicDataDictionary datadictionary) {
        int result = 0;
        if(datadictionary.getId()<=0){
            QueryWrapper<ProBasicDataDictionary> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("id");
            List<ProBasicDataDictionary> datadictionaries = baseMapper.selectList(wrapper);
            datadictionary.setId(datadictionaries.get(0).getId()+1);
            result = baseMapper.insert(datadictionary);
        }else {
            ProBasicDataDictionary datadictionary1 = baseMapper.selectById(datadictionary.getId());
            if (datadictionary1!=null){
                result = baseMapper.updateById(datadictionary);
            }
            else {
                result = baseMapper.insert(datadictionary);
            }
        }
        return result;
    }

    @Override
    public IPage<ProBasicDataDictionary> list(String category, String name, String pageNum, String pageSize) {
        QueryWrapper<ProBasicDataDictionary> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(category)) {
            qw.lambda().eq(ProBasicDataDictionary::getCategory, category);
        }

        if (StringUtils.isNotEmpty(name)) {
            qw.lambda().like( ProBasicDataDictionary::getName,name);
        }
        Page<ProBasicDataDictionary> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        IPage<ProBasicDataDictionary> companysPage = getBaseMapper().selectPage(page, qw);
        return companysPage;
    }
}
