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 PurchaseSrv : 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 "purchaseSave":        //新增采购信息
                    PurchaseSave();
                    break;
                case "GetAllPurchase":      //获取全部采购信息
                    GetAllPurchase();
                    break;
                case "purchaseUpdate":       //更新采购信息
                    PurchaseUpdate();
                    break;
                case "PurchaseDel":         //删除采购信息
                    PurchaseDel();
                    break;
                default :
                    Response.Write("");
                    break;
            }
        }

        /// <summary>
        /// 获取采购表头
        /// </summary>
        /// <returns>采购表头</returns>
        private StringBuilder PurchaseHead()
        {
            var tableHead = new StringBuilder();
            tableHead.Append("<table class=\"standerTable\" id=\"purchaseSTD\"><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 GetAllPurchase()
        {
            //获取表头
            StringBuilder table = PurchaseHead();
            //获取采购数据
            DataTable data = new PurchaseVo().AllPurchase(Request.QueryString["sT"]);
            //整合数据表
            StanderTable(table, data,"id");
            //返回数据
            Response.Write(table.ToString());
        }

        /// <summary>
        /// 新增采购信息
        /// </summary>
        private void PurchaseSave()
        {
            var purchase = new PurchaseEty();
            if (!GetPurchaseEty(purchase))
            {
                Response.Write("请输入正确的时间");
                return;
            }
            var result = new PurchaseVo().InsertPurchase(purchase);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

        /// <summary>
        /// 修改采购信息
        /// </summary>
        private void PurchaseUpdate()
        {
            var purchase = new PurchaseEty();
            if (!GetPurchaseEty(purchase))
            {
                Response.Write("请输入正确的时间");
                return;
            }
            var result = new PurchaseVo().UpdatePurchase(purchase);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

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


        /// <summary>
        /// 实体类PurchaseEty 赋值
        /// </summary>
        /// <returns>bool</returns>
        private bool GetPurchaseEty(PurchaseEty purchase)
        {

            purchase.Name = Request.QueryString["name"];
            purchase.Price = Convert.ToDecimal(Request.QueryString["price"]);
            purchase.Quantity = Convert.ToDecimal(Request.QueryString["quantity"]);
            purchase.Remark = Request.QueryString["remark"];
            purchase.Id = Request.QueryString["id"] == "" ? Com.NewGuid() : Request.QueryString["id"];
            purchase.AddUserId = Session["Userid"].ToString();
            purchase.AddTime = DateTime.Now;
            purchase.UpdateUserId = Session["Userid"].ToString();
            purchase.LastTime = DateTime.Now;
            purchase.DelFlag = false;
            try
            {
                purchase.Time = Convert.ToDateTime(Request.QueryString["time"]);
                return true;
            }
            catch
            {
                return false;
            }

        }
    }
}