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 IngredientsSrv : 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 "IngredientsSave":           //新增用料信息
                    IngredientsSave();
                    break;
                case "GetAllIngredients":         //获取全部用料信息
                    GetAllIngredients();
                    break;
                case "IngredientsUpdate":         //更新用料信息
                    IngredientsUpdate();
                    break;
                case "IngredientsDel":            //删除用料信息
                    IngredientsDel();
                    break;
                default:
                    Response.Write("");
                    break;
            }
        }

        /// <summary>
        /// 获取用料表头
        /// </summary>
        /// <returns>用料表头</returns>
        private StringBuilder IngredientsHead()
        {
            var tableHead = new StringBuilder();
            tableHead.Append("<table class=\"standerTable\" id=\"ingredientsSTD\"><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 GetAllIngredients()
        {
            //获取表头
            StringBuilder table = IngredientsHead();
            //获取用料数据
            DataTable data = new IngredientsVo().AllIngredients(Request.QueryString["sT"]);

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

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

        /// <summary>
        /// 新增用料信息
        /// </summary>
        private void IngredientsSave()
        {
            var ingredients = new IngredientsEty();
            if (!GetIngredientsEty(ingredients))
            {
                Response.Write("请输入正确的时间");
                return;
            }
            var result = new IngredientsVo().InsertIngredients(ingredients);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

        /// <summary>
        /// 修改用料信息
        /// </summary>
        private void IngredientsUpdate()
        {
            var ingredients = new IngredientsEty();
            if (!GetIngredientsEty(ingredients))
            {
                Response.Write("请输入正确的时间");
                return;
            }
            var result = new IngredientsVo().UpdateIngredients(ingredients);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

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


        /// <summary>
        /// 实体类IngredientsEty 赋值
        /// </summary>
        /// <returns>bool</returns>
        private bool GetIngredientsEty(IngredientsEty ingredients)
        {

            ingredients.Name = Request.QueryString["name"];

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