using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace PMSUI
{
    public class CFormColumnManager
    {
        /// <summary>
        /// 根据列显示设置，获取DataGridview的数据
        /// </summary>
        /// <param name="formName">界面名字，要和数据库事先输入的一致</param>
        /// <param name="SqlTableName">实际数据从哪个表获取</param>
        /// <param name="strOrderby">如果需要排序，格式例：order by CreateTime desc</param>
        /// <param name="strwhere">查询条件，格式例：Age < 30</param>
        /// <returns></returns>
        public static DataTable GetDGVDataSource(string formName, string SqlTableName, string strOrderby = "", string strwhere = "")
        {
            string temp = "FormName ='" + formName + "' and IsShow = 1 order by Sort";
            List<PmsCoreDB.Model.PMS_sys_FormColumnSetting> columnModelList = PmsCoreDB.DBHost.Instance.ColumnSetting.GetModelList(temp);
            
            string SqlQueryString = "Select ";

            for (int i = 0; i < columnModelList.Count; i++)
            {
                if (i == 0)
                {
                    SqlQueryString += columnModelList[i].DBColumnName + " as [" + CLanguageManager.LanguageChange(columnModelList[i].ShowColumnName) + "]";
                    //SqlQueryString += columnModelList[i].DBColumnName + " as " + columnModelList[i].ShowColumnName;
                }
                else
                {
                    SqlQueryString += ("," + columnModelList[i].DBColumnName + " as [" + CLanguageManager.LanguageChange(columnModelList[i].ShowColumnName) + "]");
                    //SqlQueryString += ("," + columnModelList[i].DBColumnName + " as " + columnModelList[i].ShowColumnName);
                }
            }

            SqlQueryString += " from " + SqlTableName;
            if (strwhere != "")
            {
                SqlQueryString += " where " + strwhere;
            }

            if (strOrderby != "")
            {
                SqlQueryString +=  "  "+ strOrderby;
            }

            return PmsCoreDB.DBHost.Instance.ColumnSetting.GetDataTableByString(SqlQueryString);
        }


        /// <summary>
        /// 获取List<string> 用于初始化界面控件是否显示
        /// </summary>
        /// <param name="formName">界面名字，要和数据库事先输入的一致</param>
        /// <returns></returns>
        public static List<string> GetNeedShowDBList(string formName)
        {
            List<string> returnStringList = new List<string>();

            string strWhere = "FormName ='" + formName + "' and IsShow = 1 order by Sort";
            List<PmsCoreDB.Model.PMS_sys_FormColumnSetting> columnModelList = PmsCoreDB.DBHost.Instance.ColumnSetting.GetModelList(strWhere);

            foreach (PmsCoreDB.Model.PMS_sys_FormColumnSetting model in columnModelList)
            {
                returnStringList.Add(model.DBColumnName);
            }

            return returnStringList;
        }

        /// <summary>
        /// 获取字典 Key：数据库字段名，Value：显示名，用于在各界面初始化获取，用于datagridview CellMouseClick事件
        /// </summary>
        /// <param name="formName">界面名字，要和数据库事先输入的一致</param>
        /// <returns></returns>
        public static bool GetDBNameToShowNameDic(string formName,out Dictionary<string, string> returnDic)
        {
            returnDic = new Dictionary<string, string>();
            bool ret = false;

            string strWhere = "FormName ='" + formName + "' and IsShow = 1 order by Sort";
            List<PmsCoreDB.Model.PMS_sys_FormColumnSetting> columnModelList = PmsCoreDB.DBHost.Instance.ColumnSetting.GetModelList(strWhere);

            foreach (PmsCoreDB.Model.PMS_sys_FormColumnSetting model in columnModelList)
            {
                returnDic.Add(model.DBColumnName, CLanguageManager.LanguageChange(model.ShowColumnName));
                //returnDic.Add(model.DBColumnName, model.ShowColumnName);
                ret = true;
            }
            return ret;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="formName">界面名字，要和数据库事先输入的一致</param>
        /// <returns></returns>
        public static bool GetAllFormNameToShowNameDic(out Dictionary<string, string> returnDic)
        {
            returnDic = new Dictionary<string, string>();
            bool ret = false;

            List<PmsCoreDB.Model.PMS_sys_MenuSetting> columnModelList = PmsCoreDB.DBHost.Instance.MenuSetting.GetModelList("");

            foreach (PmsCoreDB.Model.PMS_sys_MenuSetting model in columnModelList)
            {
                returnDic.Add(model.MenuName, CLanguageManager.LanguageChange(model.DisplayMenuName));
                ret = true;
            }
            return ret;
        }
    }
}
