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

namespace LogisticsSystem.SysMain.Supply
{
    public partial class SupplySrv : 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 "SupplySave":           //新增供应信息
                    SupplySave();
                    break;
                case "GetAllSupply":         //获取全部供应信息
                    GetAllSupply();
                    break;
                case "SupplyUpdate":         //更新供应信息
                    SupplyUpdate();
                    break;
                case "SupplyDel":            //删除供应信息
                    SupplyDel();
                    break;
                default:
                    Response.Write("");
                    break;
            }
        }

        /// <summary>
        /// 获取供应表头
        /// </summary>
        /// <returns>供应表头</returns>
        private StringBuilder SupplyHead()
        {
            var tableHead = new StringBuilder();
            tableHead.Append("<table class=\"standerTable\" id=\"supplySTD\"><thead><td></td>");
            tableHead.Append("<td>名称</td>");
            tableHead.Append("<td>供给部门</td>");
            tableHead.Append("<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 GetAllSupply()
        {
            //获取表头
            StringBuilder table = SupplyHead();
            //获取供应数据
            DataTable data = new SupplyVo().AllSupply(Request.QueryString["sT"]);

            //带属性td的哈希表
            var hash = new Hashtable();
            hash["depname"] = "depid";

            //整合数据表
            StanderTable(table, data, "id", hash);
            //返回数据
            Response.Write(table.ToString());
        }

        /// <summary>
        /// 新增供应信息
        /// </summary>
        private void SupplySave()
        {
            var supply = new SupplyEty();
            if (!GetSupplyEty(supply))
            {
                Response.Write("请输入正确的时间");
                return;
            }
            var result = new SupplyVo().InsertSupply(supply);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

        /// <summary>
        /// 修改供应信息
        /// </summary>
        private void SupplyUpdate()
        {
            var supply = new SupplyEty();
            if (!GetSupplyEty(supply))
            {
                Response.Write("请输入正确的时间");
                return;
            }
            var result = new SupplyVo().UpdateSupply(supply);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

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


        /// <summary>
        /// 实体类SupplyEty 赋值
        /// </summary>
        /// <returns>SupplyEty实体类</returns>
        private bool GetSupplyEty(SupplyEty supply)
        {
            supply.Name = Request.QueryString["name"];
            supply.DepId = Request.QueryString["depid"];
            supply.Quantity = Convert.ToDecimal(Request.QueryString["quantity"]);
            supply.Remark = Request.QueryString["remark"];
            supply.Id = Request.QueryString["id"] == "" ? Com.NewGuid() : Request.QueryString["id"];
            supply.AddUserId = Session["Userid"].ToString();
            supply.AddTime = DateTime.Now;
            supply.UpdateUserId = Session["Userid"].ToString();
            supply.LastTime = DateTime.Now;
            supply.DelFlag = false;
            try
            {
                supply.Time = Convert.ToDateTime(Request.QueryString["time"]);
                return true;
            }
            catch
            {
                return false;
            }

        }
    }
}