using System.Collections;
using System.Data;
using System.Text;
using LogisticsSystem.Control.Helper;

namespace LogisticsSystem.Control
{
    public class SrvBase : System.Web.UI.Page
    {
        //判断用户是否登陆
        protected bool CheckUserLogin()
        {
            return (string)Session["Userid"] != "" && Session["Userid"] != null;
        }

        //判断用户是否有权进行操作,并记录操作
        protected bool CheckUserDo(string dowhich, string type)
        {
            if (Session["Userroleid"].ToString() == "Im") return true;
            string lmt = Session["lmt"].ToString();
            if (lmt == "**" || lmt.Contains(type))
            {
                return false;
            }
            DataRow[] row = InitHelper.Ops.Select("code='" + type + "'");
            if (row.Length <= 0) return true;
            LogHelper.Log(dowhich + "--" + row[0]["name"]);
            return true;
        }



        //创建标准数据表
        protected void StanderTable(StringBuilder tableHead, DataTable data, string idName)
        {
            try
            {
                foreach (DataRow row in data.Rows)
                {
                    tableHead.Append("<tr>");
                    tableHead.Append("<td><input type=\"checkbox\" value=\"" + row[idName] + "\"></td>");
                    foreach (DataColumn column in data.Columns)
                    {
                        if (column.ColumnName == idName) continue;
                        tableHead.Append("<td>" + row[column] + "</td>");
                    }
                    tableHead.Append("</tr>");
                }
                tableHead.Append("</table>");
            }
            catch
            {
                tableHead.Clear();
                tableHead.Append("获取数据出错");
            }
        }

        //创建标准数据表  可加属性 cl keys为需要加属性的td列名 , value为属性值的列名
        protected void StanderTable(StringBuilder tableHead, DataTable data, string idName, Hashtable cl)
        {
            try
            {
                foreach (DataRow row in data.Rows)
                {
                    tableHead.Append("<tr>");
                    tableHead.Append("<td><input type=\"checkbox\" value=\"" + row[idName] + "\"></td>");
                    foreach (DataColumn column in data.Columns)
                    {
                        if (column.ColumnName == idName) continue;
                        if (cl.ContainsValue(column.ColumnName)) continue;
                        if (cl.ContainsKey(column.ColumnName))
                        {
                            tableHead.Append("<td " + cl[column.ColumnName] + "=\"" + row[cl[column.ColumnName].ToString()] + "\">" + row[column] + "</td>");
                        }
                        else
                        {
                            tableHead.Append("<td>" + row[column] + "</td>");
                        }

                    }
                    tableHead.Append("</tr>");
                }
                tableHead.Append("</table>");
            }
            catch
            {
                tableHead.Clear();
                tableHead.Append("获取数据出错");
            }
        }

        //创建标准数据表  可加属性 cl keys为需要加属性的td列名 , value为用户需要添加的值 , keyCname为全套哈希表key与数据表关联的列名
        protected void VoStanderTable(StringBuilder tableHead, DataTable data, string idName, Hashtable cl,string keyCname)
        {
            try
            {
                foreach (DataRow row in data.Rows)
                {
                    tableHead.Append("<tr>");
                    tableHead.Append("<td><input type=\"checkbox\" value=\"" + row[idName] + "\"></td>");
                    foreach (DataColumn column in data.Columns)
                    {
                        if (column.ColumnName == idName) continue;
                        if (cl.ContainsKey(column.ColumnName))
                        {
                            tableHead.Append("<td " + column.ColumnName + "=\"" + row[column] + "\">" + ((Hashtable)cl[column.ColumnName])[row[keyCname].ToString()] + "</td>");
                        }
                        else
                        {
                            tableHead.Append("<td>" + row[column] + "</td>");
                        }

                    }
                    tableHead.Append("</tr>");
                }
                tableHead.Append("</table>");
            }
            catch
            {
                tableHead.Clear();
                tableHead.Append("获取数据出错");
            }
        }

        //创建定制数据表  cl keys为需要加属性的td列名 , value为用户需要添加的值 , keyCname为全套哈希表key与数据表关联的列名
        protected void CusTable(StringBuilder tableHead, DataTable data, Hashtable cl, string keyCname)
        {
            try
            {
                foreach (DataRow row in data.Rows)
                {
                    tableHead.Append("<tr>");
                    foreach (DataColumn column in data.Columns)
                    {
                        if (cl.ContainsKey(column.ColumnName))
                        {
                            tableHead.Append("<td>" + ((Hashtable)cl[column.ColumnName])[row[keyCname].ToString()] + "</td>");
                        }
                        else
                        {
                            tableHead.Append("<td>" + row[column] + "</td>");
                        }

                    }
                    tableHead.Append("</tr>");
                }
                tableHead.Append("</table>");
            }
            catch
            {
                tableHead.Clear();
                tableHead.Append("获取数据出错");
            }
        }

       /// <summary>
       /// 超级定制表
       /// </summary>
       /// <param name="tableHead">表头</param>
       /// <param name="data">数据表</param>
        /// <param name="cl">cl keys为需要加属性的td列名 , value为用户需要添加的值</param>
        /// <param name="keyCname"> keyCname为全套哈希表key与数据表关联的列名</param>
        /// <param name="hashtable">可以加属性 ,  cl keys为需要加属性的td列名 , value为属性值的列名</param>
        protected void VoCusTable(StringBuilder tableHead, DataTable data, Hashtable cl, string keyCname,Hashtable hashtable)
        {
            try
            {
                foreach (DataRow row in data.Rows)
                {
                    tableHead.Append("<tr>");
                    foreach (DataColumn column in data.Columns)
                    {
                        if (hashtable.ContainsValue(column.ColumnName))continue;
                        if (hashtable.ContainsKey(column.ColumnName))
                        {
                            if (cl.ContainsKey(column.ColumnName))
                            {
                                tableHead.Append("<td " + hashtable[column.ColumnName] + "=\"" + row[hashtable[column.ColumnName].ToString()] + "\" >" + ((Hashtable)cl[column.ColumnName])[row[keyCname].ToString()] + "</td>");
                            }
                            else
                            {
                                tableHead.Append("<td " + hashtable[column.ColumnName] + "=\"" + row[hashtable[column.ColumnName].ToString()] + "\" >" + row[column] + "</td>");
                            }
                        }
                        else
                        {
                            if (cl.ContainsKey(column.ColumnName))
                            {
                                tableHead.Append("<td>" + ((Hashtable)cl[column.ColumnName])[row[keyCname].ToString()] + "</td>");
                            }
                            else
                            {
                                tableHead.Append("<td>" + row[column] + "</td>");
                            }
                        }
                    }
                    tableHead.Append("</tr>");
                }
                tableHead.Append("</table>");
            }
            catch
            {
                tableHead.Clear();
                tableHead.Append("获取数据出错");
            }
        }


        //创建标准数据表(没有checkbox)
        protected void NoCheckTable(StringBuilder tableHead, DataTable data)
        {
            try
            {
                foreach (DataRow row in data.Rows)
                {
                    tableHead.Append("<tr>");
                    foreach (DataColumn column in data.Columns)
                    {
                        tableHead.Append("<td>" + row[column] + "</td>");
                    }
                    tableHead.Append("</tr>");
                }
                tableHead.Append("</table>");
            }
            catch
            {
                tableHead.Clear();
                tableHead.Append("获取数据出错");
            }
        }
    }
}