using System;
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 StockSrv : 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 "stockSave":        //新增库存信息
                    StockSave();
                    break;
                case "GetAllStock":      //获取全部库存信息
                    GetAllStock();
                    break;
                case "stockUpdate":     //更新库存信息
                    StockUpdate();
                    break;
                case "stockDel":        //删除库存信息
                    StockDel();
                    break;
                default:
                    Response.Write("");
                    break;
            }
        }


        /// <summary>
        /// 获取库存表头
        /// </summary>
        /// <returns>库存表头</returns>
        private StringBuilder StockHead()
        {
            var tableHead = new StringBuilder();
            tableHead.Append("<table class=\"standerTable\" id=\"stockSTD\"><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></thead>");
            return tableHead;
        }


        private void GetAllStock()
        {
            //获取表头
            StringBuilder table = StockHead();
            //获取库存数据
            DataTable data = new StockVo().AllStock(Request.QueryString["sT"]);
            //整合数据表
            StanderTable(table, data, "id");
            //返回数据
            Response.Write(table.ToString());
        }

        /// <summary>
        /// 新增库存信息
        /// </summary>
        private void StockSave()
        {
            var stock = new StockEty();
            if (!GetStockEty(stock))
            {
                Response.Write("请输入正确的时间");
                return;
            }
            var result = new StockVo().InsertStock(stock);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

        /// <summary>
        /// 修改库存信息
        /// </summary>
        private void StockUpdate()
        {
            var stock = new StockEty();
            if (!GetStockEty(stock))
            {
                Response.Write("请输入正确的时间");
                return;
            }
            var result = new StockVo().UpdateStock(stock);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

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


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

        }
    }
}