<%@ Page Language="C#" AutoEventWireup="true" %>

<script runat="server">
    bool MastSelectGame = false;//必须选择游戏?
    string pid = "S1000";//平台ID
    int m_to_cur = 100;//1人民币兑换到平台币的数量




    protected override void OnLoad(EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            game.Items.Add(new ListItem() { Text = "-请选择-", Value = "-1" });
            com.xykj.business.View.Games.Game _games = new com.xykj.business.View.Games.Game();

            foreach (var item in _games.GetGame())
            {
                game.Items.Add(new ListItem() { Text = item.Name, Value = item.ID + "" });
            }
        }
        base.OnLoad(e);
    }

    protected void OutPutData(object sender, EventArgs e)
    {
        if (date1.SelectedDate < new DateTime(2010) || date2.SelectedDate < new DateTime(2010))
        {
            Response.Write("<font style='color:#f00'>选择的时间不正确</font>");
            return;
        }

        if (date2.SelectedDate < date1.SelectedDate)
        {
            Response.Write("<font style='color:#f00'>结束时间不能小于起始时间</font>");
            return;
        }
        if (game.SelectedValue == "-1" && MastSelectGame)
        {
            Response.Write("<font style='color:#f00'>请选择游戏</font>");
            return;
        }
        StringBuilder sp = new StringBuilder();
        sp.AppendLine("country_name\tplat_id\tgame_id\tserver_id\toldserver_id\tuser_id\torder_id\torder_amount\tcurrency_coe\tpay_time\trecharge_type\tplat_currency_amount\tpc2cc_rate");

        string sql = "select * from xy_order where 1=1 and ([Staus]='已发放' or [Staus]='已完结' or [Staus]='补单')";
        sql += " and PayTime >= '" + date1.SelectedDate + "'";
        sql += " and PayTime <= '" + date2.SelectedDate.AddDays(1) + "'";
        if (game.SelectedValue != "-1")
        {
            sql += " and gameid = " + game.SelectedValue;
        }
        int check_size = 1;
        var table = new com.xykj.database.General().GetDataTable(sql);
        if (table != null)
        {
            foreach (System.Data.DataRow item in table.Rows)
            {
                string dz_sql = "select Info4,Info6 from xy_ports where gameid = " + item["gameid"];
                var sdfsdf = new com.xykj.database.General().GetDataTable(dz_sql);
                
                check_size++;
                sp.AppendLine("CHN\t" + sdfsdf.Rows[0]["Info4"] + "\t" + sdfsdf.Rows[0]["Info6"] + "\t" + item["serverid"] + "\t" + item["serverid"] + "\t" + item["UserId"] + "\t"
                    + item["OrderId"] + "\t" + de(item["PayMoney"]) + "\t" + CrrCode(item["CurrCode"]) + "\t" + te(item["PayTime"])
                    + "\t" + recharge_type(item["PayType"]) + "\t" + curr_money(item["PayType"], item["PayMoney"], item["CurrCode"]));

            }
        }
        string dir = Request.MapPath("/upload/recharge/");
        if (!System.IO.Directory.Exists(dir))
        {
            System.IO.Directory.CreateDirectory(dir);
        }
        string ret_file =dir+ string.Format("recharge_{0}_{1}.", date1.SelectedDate.ToString("yyyyMMdd"), date2.SelectedDate.ToString("yyyyMMdd"));
        System.IO.File.WriteAllText(ret_file + "txt", sp.ToString(), System.Text.Encoding.GetEncoding("utf-8"));
        System.IO.File.WriteAllText(ret_file + "check", check_size.ToString(), System.Text.Encoding.GetEncoding("utf-8"));

        Response.Write("<font style='color:#494'>已输出到 上传目录的 recharge 文件夹</font>");
        return;
        /*
        var bts= System.Text.Encoding.GetEncoding("UTF-8").GetBytes(sp.ToString());
        Response.ContentType = "text/plain";
        Response.AppendHeader("Content-Disposition", string.Format("attachment;filename=recharge_{0}_{1}.txt", date1.SelectedDate.ToString("yyyyMMdd"), date2.SelectedDate.ToString("yyyyMMdd")));
        Response.AddHeader("Content-Length", (bts.Length) + ""); 
        Response.BinaryWrite(bts);
         * */
    }
    private string CrrCode(object obj)
    {
        if (string.IsNullOrEmpty(obj + ""))
        {
            return "CNY";
        }
        return obj + "";
    }
    private string te(object d)
    {
        return DateTime.Parse(d + "").ToString("yyyy-MM-dd HH:mm:ss");
    }
    private string de(object d)
    {
        return decimal.Parse(d + "").ToString("f2");
    }
    private string curr_money(object obj, object my, object cur)
    {
        var code = app.Setting("curr_na_" + cur);
        decimal my_a = 0, cur_co = 1;
        decimal.TryParse(my + "", out my_a);
        decimal.TryParse(code, out cur_co);


        switch (obj + "")
        {
            case "平台币(平台币)":
                return "0\t" + de(my_a * cur_co * m_to_cur);
            default:
                return "0\t0";
        }
    }
    private string recharge_type(object obj)
    {
        return "0";
        switch (obj + "")
        {
            case "前台内充消耗分成":
                return "1";
            case "平台币(平台币)":
                return "1";
            default:
                return "0";
        }
    }
</script>
<style>
    * { font-size: 14px; color: #555; }
    a { text-decoration: none; }
</style>
<form runat="server">
<p>
    如果数据量大,请缩短时间范围,导出多个文件,否则可能无法导出</p>
<div style="padding: 10px 0px 0px 20px;">
    <label>
        指定游戏</label>
    <asp:dropdownlist runat="server" id="game" />
    <asp:button runat="server" text="导出数据" onclick="OutPutData" />
</div>
<p>
    选择时间范围</p>
<div style="float: left">
    <asp:calendar id="date1" runat="server"></asp:calendar>
</div>
<div style="float: left; margin-left: 10px;">
    <asp:calendar id="date2" runat="server"></asp:calendar>
</div>
</form>
