package com.xmomen.module.system.service;

import com.xmomen.framework.mybatis.dao.MybatisDao;
import com.xmomen.framework.mybatis.page.Page;
import com.xmomen.framework.utils.StringUtilsExt;
import com.xmomen.module.system.entity.SysDictionary;
import com.xmomen.module.system.entity.SysDictionaryExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Jeng on 16/4/2.
 */
@Service
public class DictionaryGroupService {

    // 注入MybatisDao
    @Autowired
    MybatisDao mybatisDao;

    // 创建字典
    @Transactional
    public SysDictionary createDictionary(SysDictionary sysDictionary){
        // 调用mybatisDao的insertByModel方法插入字典
        return mybatisDao.insertByModel(sysDictionary);
    }

    // 获取字典列表
    public Page<SysDictionary> getDictionaryList(String keyword, Integer limit, Integer offset){
        // 将关键字前后加上%符号，用于模糊查询
        String keywordP ="%" + StringUtilsExt.trimToEmpty(keyword) + "%";
        // 创建SysDictionaryExample对象
        SysDictionaryExample sysDictionaryExample = new SysDictionaryExample();
        // 添加查询条件，字典编码或字典描述包含关键字
        sysDictionaryExample.createCriteria()
                .andDictionaryCodeLike(keywordP);
        sysDictionaryExample.or().andDictionaryDescLike(keywordP);
        // 调用mybatisDao的selectPageByExample方法查询字典列表
        return mybatisDao.selectPageByExample(sysDictionaryExample, limit, offset);
    }
    // 根据id获取字典

    public SysDictionary getDictionary(Integer id){
    // 更新字典
        return mybatisDao.selectByPrimaryKey(SysDictionary.class, id);
    }

    // 根据id删除字典
    public void updateDictionary(SysDictionary sysDictionary){
        mybatisDao.update(sysDictionary);
    }

    public void deleteDictionary(Integer id){
        mybatisDao.deleteByPrimaryKey(SysDictionary.class, id);
    }


}
