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;
using System.Net;

public partial class member_Api_V5ShopApiOrder : 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)
    {
        //生成API验证码
        string app = "V5Shop";
        string code = MD5("v5shopv5mall");
        //同步获取订单数据
        string url = GetTrueUrl().Replace("member/api/", "") + "member/InfoCenter/InfoCenterApi.aspx?ic_apitype=out&ic_key=GetOrder&ic_code=" + code + "&action=getorder";
        Response.Write(GetUrlHtml(url));
    }

    /// <summary>
    /// 获取真实URL地址
    /// </summary>
    /// <returns></returns>
    public static string GetTrueUrl()
    {
        string TrueUrl = string.Empty;
        string TheUrl = HttpContext.Current.Request.Url.ToString();
        string[] UrlArray = TheUrl.Split('?')[0].Split('/');

        TrueUrl = TheUrl.Split('?')[0].Replace(UrlArray[UrlArray.Length - 1], "");

        return TrueUrl;
    }

    /// <summary> 
    /// MD5 加密函数 
    /// </summary> 
    /// <param name="str"></param> 
    /// <param name="code"></param> 
    /// <returns></returns> 
    public static string MD5(string str)
    {
        return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
    }

    /// <summary>
    /// 读取页面生成静态页面
    /// </summary>
    /// <param name="url">地址</param>
    /// <param name="fna"></param>
    /// <returns></returns>
    private string GetUrlHtml(string url)
    {
        bool ok;
        //准备生成
        string strHtml;
        StreamReader sr = null; //用来读取流
        StreamWriter sw = null; //用来写文件
        Encoding code = Encoding.GetEncoding("utf-8"); //定义编码 
        //构造web请求，发送请求，获取响应
        WebRequest HttpWebRequest = null;
        WebResponse HttpWebResponse = null;
        HttpWebRequest = WebRequest.Create(url);
        try
        {
            HttpWebResponse = HttpWebRequest.GetResponse();
            //获得流
            sr = new StreamReader(HttpWebResponse.GetResponseStream(), code);
            strHtml = sr.ReadToEnd();
        }
        catch (Exception ex)
        {
            strHtml = "";
        }

        return strHtml;
    }

    /// <summary>
    /// 获取订单列表
    /// </summary>
    private void GetOrderList()
    {
        DataTable dt = DatabaseProvider.GetInstance().GetOrder();
        StringBuilder builder = new StringBuilder();
        Guid guid = new Guid();

        builder.Append("<?xml version='1.0' encoding='gb2312'?>\r\n");

        for (int i = 0; i < 1; i++)
        {
            builder.Append("<Order>\r\n");
            builder.Append("<Ver>1.0</Ver>\r\n");
            builder.Append("<OrderID>a4fd5a4fda1f2a1d</OrderID>\r\n");
            builder.Append("<DateTime>" + dt.Rows[i]["Date"].ToString() + "</DateTime>\r\n");
            builder.Append("<BuyerID>test" + dt.Rows[i]["UserName"].ToString() + "</BuyerID>\r\n");
            builder.Append("<BuyerName>" + dt.Rows[i]["ReceiveName"].ToString() + "</BuyerName>\r\n");
            builder.Append("<Country>中国</Country>\r\n");
            builder.Append("<Province>浙江</Province>\r\n");
            builder.Append("<City>杭州</City>\r\n");
            builder.Append("<Town>西湖区</Town>\r\n");
            builder.Append("<Adr>" + dt.Rows[i]["ReceiveAddress"].ToString() + "</Adr>\r\n");
            builder.Append("<Zip>210000" + dt.Rows[i]["ReceivePostCode"].ToString() + "</Zip>\r\n");
            builder.Append("<Email>aaa@bb.com" + dt.Rows[i]["SendEmail"].ToString() + "</Email>\r\n");
            builder.Append("<Phone>" + dt.Rows[i]["ReceiveMobile"].ToString() + "</Phone>\r\n");
            builder.Append("<Total>" + dt.Rows[i]["Price"].ToString() + "</Total>\r\n");
            builder.Append("<Postage>" + dt.Rows[i]["Fee"].ToString() + "</Postage>\r\n");
            builder.Append("<Remark>111111" + dt.Rows[i]["UserRemark"].ToString() + "</Remark>\r\n");
            builder.Append("<TradeNO>" + dt.Rows[i]["Number"].ToString() + "</TradeNO>\r\n");
            builder.Append("<TradeURL>http://www.baidu.com</TradeURL>\r\n");

            DataTable dtProduct = DatabaseProvider.GetInstance().GetProductByOrderNumber(dt.Rows[i]["Number"].ToString());
            for (int j = 0; j < dtProduct.Rows.Count; j++)
            {
                builder.Append("<Item>\r\n");
                builder.Append("<GoodsID>" + dtProduct.Rows[j]["Number"].ToString() + "</GoodsID>\r\n");
                builder.Append("<GoodsName>" + dtProduct.Rows[j]["Name"].ToString() + "</GoodsName>\r\n");
                builder.Append("<Price>" + dtProduct.Rows[j]["MemberPrice"].ToString() + "</Price>\r\n");
                builder.Append("<Count>" + dtProduct.Rows[j]["Count"].ToString() + "</Count>\r\n");
                builder.Append("<Reserved3></Reserved3>\r\n");
                builder.Append("<Reserved4></Reserved4>\r\n");
                builder.Append("</Item>\r\n");
            }

            builder.Append("</Order>\r\n");
        }

        Response.Write(builder.ToString());
        Response.End();
    }

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