package com.yihu.hos.system.dao;

import com.yihu.hos.core.datatype.StringUtil;
import com.yihu.hos.system.model.SystemDict;
import com.yihu.hos.system.model.SystemDictList;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.model.DataGridResult;
import com.yihu.hos.web.framework.model.DictItem;
import com.yihu.hos.web.framework.model.DictionaryResult;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hzp on 2016/1/11.
 */
@Repository("BaseDictDao")
public class BaseDictDao extends SQLGeneralDAO {

    public static final String BEAN_ID = "BaseDictDao";


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


        return null;
    }

    /**
     * 通过ID获取系统字典信息
     * @return
     */
   
    public SystemDictList getSystemDictInfoById(String id) throws Exception  {
        String sql = "select * from system_dict_list where id = '"+id+"'";
        return super.queryObjBySql(sql,SystemDictList.class);
    }

    /**
     * 通过dictName获取字典信息
     * @return
     */
   
    public SystemDictList getSystemDictInfoByName(String dictName) throws Exception {
        String sql = "select * from system_dict_list where dict_name = '"+dictName+"'";
        return super.queryObjBySql(sql,SystemDictList.class);
    }



    /**
     * 获取某字典数据
     * @return
     */
   
    public DictionaryResult getSystemDict(String dictName) throws Exception
    {
        String sql = "select * from system_dict where dict_name = '"+dictName+"' order by sort";
        List<SystemDict> list = super.queryListBySql(sql,SystemDict.class);

        DictionaryResult re = new DictionaryResult(dictName);
        if(list!=null&&list.size()>0)
        {
            List<DictItem> dictList = new ArrayList<>();
            for(SystemDict item:list){
                DictItem dict = new DictItem();
                dict.setCode(item.getCode());
                dict.setValue(item.getValue());
                dict.setExtend(item.getPyCode());
                dictList.add(dict);
            }
            re.setDetailModelList(dictList);
        }
        return re;
    }

    /**
     * 获取某字典数据
     * @return
     */
   
    public DictionaryResult getRelationDict(String tableName,String codeCol,String valueCole,String extendCol,String Where) throws Exception
    {
        String sql = "select "+ codeCol+" as code,"+valueCole +" as value";
        if(!StringUtil.isEmpty(extendCol))
        {
            sql +=","+extendCol +" as extend";
        }
        else{
            sql+=",'' as extend";
        }
        sql += " from "+tableName.toLowerCase();
        if(!StringUtil.isEmpty(Where))
        {
            sql +=" where " + Where;
        }

        sql += " order by " + valueCole;

        List<DictItem> list = super.queryListBySql(sql,DictItem.class);

        DictionaryResult re = new DictionaryResult(tableName);
        re.setDetailModelList(list);
        return re;
    }


    /**
     * 获取标准字典数据
     * @return
     */
    public DictionaryResult getStdDict(String code) throws Exception
    {
        return null;
    }



}
