using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using V5Shop.Data;
using System.Text;
using System.IO;

namespace InfoCenter
{
    public partial class V5Shop_ProductApi : System.Web.UI.Page
    {
        private string code = string.Empty;
        private string apiUrl = string.Empty;
        private string verifyCode = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            string action = GetRequest("action");
            code = GetRequest("code");
            apiUrl = GetRequest("apiurl");
            switch (action)
            {
                case "mUpdateStock":
                    UpdateStock();
                    break;
                case "mNewGoods":
                    NewGoods();
                    break;
                case "mNewGoodsSpec":
                    NewGoodsSpec();
                    break;
                case "mDelGoods":
                    DelGoods();
                    break;
                case "DelGoodsSpec":
                    DelGoods();
                    break;
            }
        }

        /// <summary>
        /// 更新商品库存
        /// </summary>
        private void UpdateStock()
        {
            int result = 0;
            StringBuilder sql = new StringBuilder();
            sql.Append(" UPDATE Web_TrueProduct SET Store = ")
                .Append(GetRequest("Stock"))
                .Append(",LastModify = getdate() WHERE Barcode = '")
                .Append(GetRequest("GoodsNO"))
                .Append("'");
            result = DbHelper.ExecuteNonQuery(sql.ToString());
        }

        /// <summary>
        /// 增加商品规格
        /// </summary>
        private void NewGoodsSpec()
        {
            int result = 0;
            StringBuilder sql = new StringBuilder();
            sql.Append("  ")
                .Append(" ");
            result = DbHelper.ExecuteNonQuery(sql.ToString());
        }

        /// <summary>
        /// 创建新商品
        /// </summary>
        private void NewGoods()
        {
            int result = 0;
            StringBuilder sql = new StringBuilder();
            sql.Append(" INSERT INTO [Web_TrueProduct]")
                .Append("(")
                .Append(" [Barcode]")
                .Append(",[Title]")
                .Append(",[Price]")
                .Append(",[MarketPrice]")
                .Append(",[Unit]")
                .Append(",[Store]")
                .Append(",[LastModify])")
                .Append("VALUES")
                .Append("(")
                .Append(" '" + GetRequest("GoodsNO") + "'")
                .Append(",'" + GetRequest("GoodsName") + "'")
                .Append("," + GetRequest("dPriceDetail") + "")
                .Append("," + GetRequest("dPriceDetail") + "")
                .Append(",'" + GetRequest("GoodsUnit") + "'")
                .Append("," + GetRequest("Stock") + "")
                .Append(",getdate()")
                .Append(")");
            result = DbHelper.ExecuteNonQuery(sql.ToString());
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        private void DelGoods()
        {
            int result = 0;
            StringBuilder sql = new StringBuilder();
            sql.Append("  DELETE FROM Web_TrueProduct WHERE Barcode = '" + GetRequest("GoodsNO") + "'");
            result = DbHelper.ExecuteNonQuery(sql.ToString());
        }

        /// <summary>
        /// 删除商品规格
        /// </summary>
        private void DelGoodsSpec()
        {
            int result = 0;
            StringBuilder sql = new StringBuilder();
            sql.Append("  DELETE FROM Web_TrueProduct WHERE Barcode = '" + GetRequest("GoodsSpec") + "'");
            result = DbHelper.ExecuteNonQuery(sql.ToString());
        }

        /// <summary>
        /// 获取传递过来的参数
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private string GetRequest(string key)
        {
            return Request.QueryString[key] == null ? "" : Request.QueryString[key].ToString();
        }
    }

}
