using System;
using System.Data;
using System.Text;
using Business.System.User.Entity;
using Business.System.User.Vo;
using Common;
using LogisticsSystem.Control;
using LogisticsSystem.Control.Helper;

namespace LogisticsSystem.SysMain.Setting
{
    public partial class UserSrv : SrvBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var type = Request.QueryString["type"];

            if (!CheckUserLogin()) return;

            if (!CheckUserDo("用户执行--用户管理", type))
            {
                DataRow[] row = InitHelper.Ops.Select("code='" + type + "'");
                if (row.Length <= 0)
                {
                    Response.Write("用户没有权限操作");
                    return;
                }
                Response.Write("用户没有权限操作:" + row[0]["name"]);
                return;
            }

            //根据类型 执行相关操作
            switch (type)
            {
                case "SysUserSave":      //新增用户信息
                    SysUserSave();
                    break;
                case "GetAllUser":       //获取全部用户信息
                    GetAllUser();
                    break;
                case "SysUserUpdate":    //更新用户信息
                    SysUserUpdate();
                    break;
                case "SysUserDel":      //删除用户信息
                    SysUserDel();
                    break;
                case "Upwd":            //用户修改密码
                    Upwd();
                    break;
                case "GetLmtUser":      //获取全部用户权限信息
                     GetAllUser();
                    break;
                case "SelectUser":      //获取用户选择框
                    SelectUser();
                    break;
                default:
                    Response.Write("");
                    break;
            }
        }

        /// <summary>
        /// 获取用户表头
        /// </summary>
        /// <returns>用户表头</returns>
        private StringBuilder UserHead()
        {
            var tableHead = new StringBuilder();
            tableHead.Append("<table class=\"standerTable\" id=\"" + Request.QueryString["tName"] + "\"><thead><td></td>");
            tableHead.Append("<td>姓名</td>");
            tableHead.Append("<td>手机号码</td>");
            tableHead.Append("<td>用户账号</td>");
            tableHead.Append("<td>添加时间</td>");
            tableHead.Append("<td>添加人</td></thead>");
            return tableHead;
        }


        private void GetAllUser()
        {
            //获取表头
            StringBuilder table = UserHead();
            //获取用户数据
            DataTable data = new UserVo().GetUserByDepid(Request.QueryString["sT"], Request.QueryString["depid"]);
            //整合数据表
            StanderTable(table, data, "userid");
            //返回数据
            Response.Write(table.ToString());
        }

        /// <summary>
        /// 新增用户信息
        /// </summary>
        private void SysUserSave()
        {
            var result = new UserVo().InsertUser(GetUserEty());
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        private void SysUserUpdate()
        {
            var result = new UserVo().UpdateUser(GetUserEty());
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        private void Upwd()
        {
            var result = new UserVo().Upwd(Session["Userid"].ToString(), Request.QueryString["oPwd"], Request.QueryString["nPwd"]);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        private void SysUserDel()
        {
            string id = "'";
            string[] ids = Request.QueryString["id"].Split(',');
            id += string.Join("','", ids) + "'";
            var result = new UserVo().DelUser(id);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }


        /// <summary>
        /// 实体类UserEty 赋值
        /// </summary>
        /// <returns>UserEty实体类</returns>
        private UserEty GetUserEty()
        {
            return new UserEty
            {
                Username = Request.QueryString["name"],
                Mobile = Request.QueryString["mobile"],
                Userid = Request.QueryString["id"] == "" ? Com.NewGuid() : Request.QueryString["id"],
                Adduserid = Session["Userid"].ToString(),
                Addtime = DateTime.Now,
                Depid = Request.QueryString["depid"],
                Useraccount = Request.QueryString["useracc"],
                Userpassword = InitHelper.DefaultPwd,
                Delflag = false
            };
        }

        /// <summary>
        /// 获取用户选择框
        /// </summary>
        private void SelectUser()
        {
            var options = new StringBuilder();

            //获取部门数据
            DataTable data = new UserVo().GetUserByDepid("", Request.QueryString["depid"]);

            //打包成option
            foreach (DataRow row in data.Rows)
            {
                options.Append("<option value=\"" + row["userid"] + "\">" + row["username"] + "</option>");
            }
            if (options.ToString() == "")
            {
                Response.Write("");
                return;
            }

            Response.Write(options.ToString());
        }
    }
}