package com.yihu.hos.system.service;

import com.yihu.hos.common.Services;
import com.yihu.hos.core.datatype.StringUtil;
import com.yihu.hos.system.dao.BaseDictDao;
import com.yihu.hos.system.model.SystemDict;
import com.yihu.hos.system.model.SystemDictList;
import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.DataGridResult;
import com.yihu.hos.web.framework.model.DictionaryResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by hzp on 2016/1/11.
 */
@Service(Services.BaseDict)
@Transactional
public class BaseDictManager {

    public static final String BEAN_ID = Services.BaseDict;


    @Resource(name = BaseDictDao.BEAN_ID)
    private BaseDictDao baseDictDao;

    /**
     * 获取字典数据(常用)
     * @return
     */
   
    public DictionaryResult getDict(String name,String where,String type) throws Exception
    {
        if(type == "1")
        {
            return baseDictDao.getStdDict(name);
        }
        else if(type == "2")
        {
            return baseDictDao.getStdDict(name);
        }
        else{
            SystemDictList dict = baseDictDao.getSystemDictInfoByName(name);
            if(dict!=null)
            {
                String table = dict.getRelationTable();
                String codeCol = dict.getRelationColCode();
                String valueCol = dict.getRelationColValue();
                String extendCol = dict.getRelationColExtend();
                //表字典查询
                if(!StringUtil.isEmpty(table)&&!StringUtil.isEmpty(codeCol)&&!StringUtil.isEmpty(valueCol))
                {
                    return baseDictDao.getRelationDict(table,codeCol,valueCol,extendCol,where);
                }
                else{
                    return baseDictDao.getSystemDict(name);
                }
            }

            return null;
        }
    }


    /**
     * 获取字典列表
     * @return
     */
   
    public DataGridResult getSystemDictList(String condition,Integer page, Integer pageSize) throws Exception
    {
        return baseDictDao.getSystemDictList(condition,page,pageSize);
    }

    /**
     * 通过ID获取系统字典信息
     * @return
     */
   
    public SystemDictList getSystemDictInfoById(String id) throws Exception
    {
        return baseDictDao.getSystemDictInfoById(id);
    }

    /**
     * 通过dictName获取字典信息
     * @return
     */
   
    public SystemDictList getSystemDictInfoByName(String dictName) throws Exception
    {
        return baseDictDao.getSystemDictInfoByName(dictName);
    }

    /**
     * 新增字典信息
     */
   
    @Transactional
    public ActionResult addDict(SystemDictList obj) throws Exception
    {
        return null;
    }

    /**
     * 修改字典信息
     */
   
    @Transactional
    public ActionResult updateDict(SystemDictList obj) throws Exception
    {
        return null;
    }

    /**
     * 删除字典信息
     */
   
    @Transactional
    public ActionResult deleteDict(SystemDictList obj) throws Exception
    {
        return null;
    }


    /**
     * 新增字典项
     */
   
    @Transactional
    public ActionResult addDictItem(SystemDict obj) throws Exception
    {
        return null;
    }

    /**
     * 修改字典项
     */
   
    @Transactional
    public ActionResult updateDictItem(SystemDict obj) throws Exception
    {
        return null;
    }

    /**
     * 删除字典项
     */
   
    @Transactional
    public ActionResult deleteDictItem(SystemDict obj) throws Exception
    {
        return null;
    }
}


