package com.liang.service;

import com.liang.dao.BaseDictDao;
import com.liang.entity.DictInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author
 * @date 2023-05-29-21:50
 **/
@Service
public class BaseDictService {
    @Autowired
    private BaseDictDao baseDictDao;

    /**
     * 获取全部枚举列表
     *
     * @param map
     * @return
     */
    public List<DictInfo> getAllBaseDict(Map map) {
        return baseDictDao.getAllBaseDict(map);
    }


    /**
     * 检查是否已存在
     *
     * @param idCode
     * @return
     */
    public List<DictInfo> checkExistByDictCode(String idCode) {
        return baseDictDao.checkExistByDictCode(idCode);
    }

    /**
     * 枚举修改，获取一条枚举记录
     *
     * @param dictCode
     * @return
     */
    public List<DictInfo> getDictByDictCode(String dictCode) {
        return baseDictDao.getDictByDictCode(dictCode);
    }

    @Transactional
    public int addDict(List<DictInfo> insertDataList) {
        int nums = 0;
        for (DictInfo dictInfo : insertDataList) {
            int num = baseDictDao.addOneDict(dictInfo);
            nums = nums + num;
        }
        return nums;
    }


    @Transactional
    public int updateDict(String dictCode, List<DictInfo> insertDataList) {
        // 执行先删后插策略
        baseDictDao.realDeleteDict(dictCode);
        int nums = 0;
        for (DictInfo dictInfo : insertDataList) {
            int num = baseDictDao.addOneDict(dictInfo);
            nums = nums + num;
        }
        return nums;
    }

    public int deleteDict(String dictCode) {
        return baseDictDao.deleteDict(dictCode);
    }

    public int forbidDict(String dictCode) {
        return baseDictDao.forbidDict(dictCode);
    }

    public int enableDict(String dictCode) {
        return baseDictDao.enableDict(dictCode);
    }
}
