using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using xabaraCOM;

public partial class WebXabara_money_passMoney : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        sqlServer sql = new sqlServer();
        admin a = new admin();
        ipAddress ip = new ipAddress();
        javasrcipt js = new javasrcipt();

        DataTable dt;

        a.check("|moneyPass|");

        string id = Request.QueryString["id"];
        long fid = xabara.isNum(Request.QueryString["flagID"]);

        string strDim = "@adminPass,@adminID,@adminDate,@adminIP";
        string[] strValue = new string[4];
        strValue[0] = fid.ToString();
        strValue[1] = a.uid;
        strValue[2] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        strValue[3] = ip.address();

        string updateStr = sql.update("moneyBankRemit", strDim, strValue, "rID=" + id + " and adminPass=0");  //更新汇款表

        if (updateStr.Equals("更新成功"))
        {
            if (fid < 2)
            {
                dt = sql.getDataTable("select * from moneyBankRemit where rID=" + id); //读出汇款表相关数据
                string rndID = dt.Rows[0]["rndID"].ToString().Trim();
                string m = dt.Rows[0]["remitMoney"].ToString().Trim();
                string uID = dt.Rows[0]["uID"].ToString().Trim();

                dt = sql.getDataTable("select * from moneyDetail where orderID='" + rndID + "'"); //财务明细表检查充值记录
                if (dt.Rows.Count > 0)
                {
                    js.div(false, "", "text", "该汇款单已被充值！", 350, 150, "default.aspx", "goto");
                }
                else
                {
                    dt = sql.getDataTable("select top 1 nowMoney,buyMoney from moneyDetail where uID='" + uID + "' order by dID DESC"); //取上一次的余额及消费额度
                    double nMoney = 0;
                    double bMoney = 0;
                    if (dt.Rows.Count > 0)
                    {
                        nMoney = Convert.ToDouble(dt.Rows[0]["nowMoney"]);
                        bMoney = Convert.ToDouble(dt.Rows[0]["buyMoney"]);
                    }

                    strDim = "@uID,@orderID,@addMoney,@subtractMoney,@nowMoney,@buyMoney,@moneyInfo,@adminID,@postDate,@postIP";
                    string[] strValueMoney = new string[10];
                    strValueMoney[0] = uID;
                    strValueMoney[1] = rndID;
                    strValueMoney[2] = m;
                    strValueMoney[3] = "0";
                    strValueMoney[4] = (nMoney + Convert.ToDouble(m)).ToString(); //入款
                    strValueMoney[5] = bMoney.ToString();
                    strValueMoney[6] = "汇款入帐";
                    strValueMoney[7] = a.uid;
                    strValueMoney[8] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    strValueMoney[9] = ip.address();

                    if (sql.add("moneyDetail", strDim, strValueMoney).Equals("发布成功"))
                    {
                        js.div(false, "", "text", "帐户款项增加成功！", 350, 150, "default.aspx?pass=1", "goto");
                    }
                    else
                    {
                        js.div(false, "", "text", "帐户款项增加失败！", 350, 150, "default.aspx", "goto");
                    }

                    sql.log(a.uid, "admin", "财务审核：" + uID);
                }
                dt.Dispose();
            }
            else
            {
                js.div(false, "", "text", "作废成功！", 350, 150, "default.aspx", "goto");
            }            
        }
        else
        {
            js.div(false, "", "text", "该汇款单已处理，不要重复操作！", 350, 150, "default.aspx", "goto");
        }
    }
}
