using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Common;
using System.Web.SessionState;
using System.Text.RegularExpressions;
using System.ComponentModel;
using WebMessageBox;
using hqgl.dept;
using System.Xml;
///5/&1*a*s*p*x**首*发*
namespace hqgl.user
{
    [DataObject(true)]
    public class userchuli
    {

        public void user_add(userinfo uinfo)
        {
            string sql = "insert into reguser(#lst#) values(#vlst#)";
            sql = sql.Replace("#lst#", uinfo.sql);
            sql = sql.Replace("@", "");
            sql = sql.Replace("#vlst#", uinfo.sql);
            Regex rex = new Regex("@.*?((?=,@)|(?=$)|(?=\\)))", RegexOptions.IgnoreCase);
            int i = 0;
            foreach (Match sitem in rex.Matches(sql))
            {
                string at = uinfo[i].ToString();
                i++;
                sql = sql.Replace(sitem.Value, at);
            }
            DbHelper db = new DbHelper();
            db.ExecuteSqlNonQuery(sql);
        }
        public userinfo user_getDetail(string id)
        {
            string sql = "select u_id,u_name,user_priv,user_no,post_priv,not_login,not_view_user,not_view_table,duty_type,sex,pwd,birthday,dept_id from [reguser] where u_id='#uid#'";
            sql = sql.Replace("#uid#", id);
            DbHelper dbh = new DbHelper();
            DataTable dt = dbh.ExecuteSql(sql);
            if (dt.Rows.Count <= 0)
                return null;
            DataRow dr = dt.Rows[0];
            userinfo uinfo = new userinfo();
            uinfo.u_id = dr["u_id"].ToString();
            uinfo.u_name = dr["U_NAME"].ToString();
            uinfo.user_priv = dr["USER_PRIV"].ToString();
            uinfo.user_no = dr["user_no"].ToString();
            uinfo.post_priv = dr["POST_PRIV"].ToString();
            uinfo.not_login = dr["NOT_LOGIN"].ToString();
            uinfo.not_view_user = dr["NOT_VIEW_USER"].ToString();
            uinfo.not_view_table = dr["NOT_VIEW_TABLE"].ToString();
            uinfo.duty_type = dr["DUTY_TYPE"].ToString();
            uinfo.password = dr["pwd"].ToString();
            uinfo.sex = dr["SEX"].ToString();
            uinfo.birthday = dr["BIRTHDAY"].ToString();
            uinfo.dept_id = dr["dept_id"].ToString();
            return uinfo;
        }
        public void user_edit(userinfo uinfo)
        {
            string sql = "delete from reguser where u_id='#u_id#'";
            sql = sql.Replace("#u_id#", uinfo.u_id);
            DbHelper db = new DbHelper();
            db.ExecuteSqlNonQuery(sql);
            user_add(uinfo);
        }
        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public void user_delete(string u_id)
        {
            string sql = "delete from reguser where u_id='#u_id#'";
            sql = sql.Replace("#u_id#", u_id);
            DbHelper db = new DbHelper();
            db.ExecuteSqlNonQuery(sql);
        }
        public int user_login(string uname, string upwd)
        {
            string sql = "select u_id from reguser where u_id='#uid#' and  pwd='#pwd#'";
            sql = sql.Replace("#uid#", uname);
            sql = sql.Replace("#pwd#", upwd);
            DbHelper db = new DbHelper();
            object count = db.ExecuteScalar(sql);
            if (count != null)
            {
                HttpContext.Current.Session["u_id"] = count.ToString();
                HttpContext.Current.Session.Timeout = 5;
                return 1;
            }
            else
                return -1;
        }
        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public DataSet User_GetList(string pid)
        {
            string sql;
            if (string.IsNullOrEmpty(pid))
                sql = "select u_name,POST_PRIV,u_id,sex,duty_type,priv_name,dept_name from reguser,user_priv,department where user_priv.user_priv=reguser.user_priv and reguser.dept_id=department.dept_id";
            else
            {
                sql = "select u_name,POST_PRIV,u_id,sex,duty_type,priv_name,dept_name from reguser,user_priv,department where user_priv.user_priv=reguser.user_priv and reguser.dept_id=department.dept_id and reguser.dept_id='#dept_id#'  ";
                sql = sql.Replace("#dept_id#", pid);
            }
            DbHelper db = new DbHelper();
            DataSet ds = db.ExecuteDataSet(sql);
            return ds;
        }
        public string user_loginname()
        {
            if (HttpContext.Current.Session["u_id"] != null)
                return HttpContext.Current.Session["u_id"].ToString();
            else
                return null;
        }
        public string user_loginid()
        {
            if (HttpContext.Current.Session["u_id"] != null)
                return HttpContext.Current.Session["u_id"].ToString();
            else
                return null;
        }
        public void user_logout()
        {
            HttpSessionState mm = HttpContext.Current.Session;
            mm.Timeout = 0;
        }
        public void user_yanzheng()
        {
            HttpSessionState mm = HttpContext.Current.Session;
            object uname = mm["u_id"];
            if (uname == null)
            {
                string url = HttpContext.Current.Request.Url.Authority;
                string appurl = HttpContext.Current.Request.ApplicationPath;
                url = "http://" + url + appurl + "/" + "login.aspx";
                string script = @"<script language='javascript' type='text/javascript'>
                                    var par=window.top;
                                    par.navigate('###');                         
                                    </script> ";
                script = script.Replace("###", url);
                HttpContext.Current.Response.Write(script);
                HttpContext.Current.Response.End();
            }
            else
            {
                string id = user_getdeptid();
                deptchuli dch = new deptchuli();
                string func = dch.dept_getfunc(id);
                func = ",1000," + func + ",";
                string nowindex = getindex();
                if (!CheckCan(nowindex, func))
                    MessageBox.Show("您无权查看此页,本页即将关闭", true);
            }
        }
        public bool CheckCan(string nowindex, string func)
        {
            string[] eve = nowindex.Split('.');
            string tmp = "";
            for (int i = 0; i < eve.Length; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    if (j > 0)
                        tmp += ".";
                    tmp += eve[j];
                }
                tmp = "," + tmp + ",";
                if (func.Contains(tmp))
                    return true;
                tmp = "";
            }
            return false;
        }
        public string getindex()
        {
            string nowpath = HttpContext.Current.Request.Url.AbsolutePath;
            nowpath = HttpContext.Current.Server.MapPath(nowpath);
            string tree = HttpContext.Current.Server.MapPath("~/tree.aspx");
            string top = HttpContext.Current.Server.MapPath("~/top.aspx");

            if (top.Contains(nowpath) || tree.Contains(nowpath))
                return "1000";
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(HttpContext.Current.Server.MapPath("~/web.sitemap"));
            XmlNodeList xnl = xdoc.GetElementsByTagName("siteMapNode");
            foreach (XmlNode xe in xnl)
            {
                if (xe.Attributes["index"] != null)
                {
                    string mm = HttpContext.Current.Server.MapPath("~/" + xe.Attributes["url"].Value);
                    if (mm.Contains(nowpath))
                        return xe.Attributes["index"].Value;
                }
            }
            return null;
        }
        public string user_getdeptid()
        {
            HttpSessionState mm = HttpContext.Current.Session;
            string uid = mm["u_id"].ToString();
            string sql = "select user_priv from reguser where u_id='#uid#'";
            sql = sql.Replace("#uid#", uid);
            DbHelper dbh = new DbHelper();
            object o = dbh.ExecuteScalar(sql);
            if (o == null)
                throw new Exception("用户信息出错");
            return o.ToString();


        }
    }

    public class userinfo
    {

        public string u_id = "";
        public string u_name = "";
        public string user_priv = "";
        public string dept_id = "";
        public string user_no = "";
        public string post_priv = "";
        public string not_login = "";
        public string not_view_user = "";
        public string not_view_table = "";
        public string duty_type = "";
        public string email_capacity = "";
        public string byname = "";
        public string password = "";
        public string sex = "";
        public string birthday = "";
        public string mobil_no = "";
        public string sql
        {
            get
            {
                return "@u_id,@u_name,@user_priv,@dept_id,@user_no,@post_priv,@not_login,@not_view_user,@not_view_table,@duty_type,@email_capacity,@byname,@pwd,@sex,@birthday,@mobil_no";
            }
        }
        public object this[int index]
        {
            get
            {
                string res = "'res'";
                switch (index)
                {
                    case 0:
                        res = res.Replace("res", u_id);
                        break;
                    case 1:
                        res = res.Replace("res", u_name);
                        break;
                    case 2:
                        res = res.Replace("res", user_priv);
                        break;
                    case 3:
                        res = res.Replace("res", dept_id);
                        break;
                    case 4:
                        res = res.Replace("res", user_no);
                        break;
                    case 5:
                        res = res.Replace("res", post_priv);
                        break;
                    case 6:
                        if (not_login != null)
                            res = res.Replace("res", not_login);
                        else
                            res = res.Replace("res", "off");
                        break;
                    case 7:
                        if (not_view_user != null)
                            res = res.Replace("res", not_view_user);
                        else
                            res = res.Replace("res", "off");
                        break;
                    case 8:
                        if (not_view_table != null)
                            res = res.Replace("res", not_view_table);
                        else
                            res = res.Replace("res", "off");
                        break;
                    case 9:
                        res = res.Replace("res", duty_type);
                        break;
                    case 10:
                        res = res.Replace("res", email_capacity);
                        break;
                    case 11:
                        res = res.Replace("res", byname);
                        break;
                    case 12:
                        res = res.Replace("res", password);
                        break;
                    case 13:
                        res = res.Replace("res", sex);
                        break;
                    case 14:
                        res = res.Replace("'res'", birthday);
                        break;
                    case 15:
                        res = res.Replace("res", mobil_no);
                        break;
                    default:
                        res = "";
                        break;
                }
                return res;
            }
        }
    }
}
