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

namespace LogisticsSystem.SysMain.Setting
{
    public partial class DepSrv : 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 "SysDepSave":        //新增部门信息
                    SysDepSave();
                    break;
                case "GetAllDep":          //获取全部部门信息
                    GetAllDep();
                    break;
                case "SysDepUpdate":     //更新部门信息
                    SysDepUpdate();
                    break;
                case "SysDepDel":        //删除部门信息
                    SysDepDel();
                    break;
                case "SelectDep":        //获取部门选择框
                    SelectDep();
                    break;
                default:
                    Response.Write("");
                    break;
            }
        }


        /// <summary>
        /// 获取部门表头
        /// </summary>
        /// <returns>部门表头</returns>
        private StringBuilder DepHead()
        {
            var tableHead = new StringBuilder();
            tableHead.Append("<table class=\"standerTable\" id=\"depSTD\"><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 GetAllDep()
        {
            //获取表头
            StringBuilder table = DepHead();
            //获取部门数据
            DataTable data = new DepVo().AllSysDep(Request.QueryString["sT"]);
            //整合数据表
            StanderTable(table, data,"depid");
            //返回数据
            Response.Write(table.ToString());
        }

        /// <summary>
        /// 新增部门信息
        /// </summary>
        private void SysDepSave()
        {
            var result = new DepVo().InsertSysDep(GetDepEty());
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

        /// <summary>
        /// 修改部门信息
        /// </summary>
        private void SysDepUpdate()
        {
            var result = new DepVo().UpdateSysDep(GetDepEty());
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

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


        /// <summary>
        /// 实体类DepEty 赋值
        /// </summary>
        /// <returns>DepEty实体类</returns>
        private DepEty GetDepEty()
        {
            return new DepEty
            {
                Name = Request.QueryString["name"],
                Remark = Request.QueryString["remark"],
                Id = Request.QueryString["id"] == "" ? Com.NewGuid() : Request.QueryString["id"],
                AddUserId = Session["Userid"].ToString(),
                AddTime = DateTime.Now,
                UpdateUserId = Session["Userid"].ToString(),
                LastTime = DateTime.Now,
                DelFlag = false
            };
        }

        /// <summary>
        /// 获取部门选择框
        /// </summary>
        private void SelectDep()
        {
            var options = new StringBuilder();

            //获取部门数据
            DataTable data = new DepVo().AllSysDep("");

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

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