wzp
2021-09-16 91912ad24d477ff24be56f0a22438303a8eae552
web/web/ExportDocument.aspx.cs
New file
@@ -0,0 +1,336 @@
using Dao;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Reflection;
public partial class ExportDocument : PageBase<SysUser>, IRequiresSessionState
{
    protected void Page_Load(object sender, EventArgs e)
    {
        switch (this.AppContext.GetString("action"))
        {
            case "exPortGwSp":
                this.ExPortGwSp();
                break;
            case "exPortGwSm":
                this.ExportGwSmData();
                break;
            case "exportGwStatisV3"://导出统计报表
                this.ExprortGwStatisV3();
                break;
        }
    }
    #region 统计报表导出
    /// <summary>
    /// 统计报表导出
    /// </summary>
    private void ExprortGwStatisV3()
    {
        string apMid = this.AppContext.GetString("ApMID");
        string opid = this.AppContext.GetString("OPID");
        string spid = this.AppContext.GetString("SpID");
        string clientId = this.AppContext.GetString("ClientID");
        string beginTime = this.AppContext.GetString("BeginTime");
        if (string.IsNullOrEmpty(beginTime))
            beginTime = DateTime.Now.AddHours(-1.0).Day == DateTime.Now.Day ? DateTime.Now.AddHours(-1.0).ToString("HH:mm:ss") : "00:00:00";
        string endTime = this.AppContext.GetString("EndTime");
        string statistype = this.AppContext.GetString("Statistype");
        StatisOption statisoption = (StatisOption)this.AppContext.GetInt("Statisoption");
        List<GwOp> opList;
        using (GwOpDao dao = new GwOpDao())
        {
            opList = dao.LoadInfoList();
        }
        List<GwClient> clientList;
        using (GwClientDao dao = new GwClientDao())
        {
            clientList = dao.LoadInfoList("", "", "", "");
        }
        List<GwAp> apList;
        using (GwApDao dao = new GwApDao())
        {
            int recordCount = 0;
            apList = dao.LoadInfoList("", "", "", out recordCount, 99999, 1);
        }
        GwStatisV3Dao _Dao = new GwStatisV3Dao();
        var list = _Dao.Query(Convert.ToDateTime(beginTime), Convert.ToDateTime(endTime), clientId, spid, apMid, opid, statistype, (StatisOption)statisoption);
        List<Model.GwStatisV3> statisList = new List<Model.GwStatisV3>();
        foreach (GwStatisV3Item item in list)
        {
            Model.GwStatisV3 ss = new Model.GwStatisV3();
            if ((statisoption & StatisOption.APID) == StatisOption.APID)
            {
                ss.ApName = GetApName(apList, item.APID);
            }
            if ((statisoption & StatisOption.OPID) == StatisOption.OPID)
            {
                ss.OpName = GetOpName(opList, item.OPID);
            }
            if ((statisoption & StatisOption.SpID) == StatisOption.SpID)
            {
                ss.SpId = item.SpID;
            }
            if ((statisoption & StatisOption.ClientID) == StatisOption.ClientID)
            {
                ss.ClientId = GetClientName(clientList, item.ClientID);
            }
            int opTotal = item.OPStatus0 + item.OPStatus1 + item.OPStatus2 + item.OPStatus3 + item.OPStatus4 + item.OPStatus5;
            int apTotal = item.APStatus0 + item.APStatus1 + item.APStatus2 + item.APStatus3 + item.APStatus4 + item.APStatus5;
            ss.ApReciveTotal = apTotal.ToString();
            ss.OpSubTotal = opTotal.ToString();
            ss.ApSubFailed = item.APStatus1.ToString();
            ss.ApNotForwarded = item.APStatus0.ToString();
            ss.ApFailed = (item.APStatus3 + item.APStatus5).ToString();
            ss.ApUnkown = item.APStatus3.ToString();
            ss.ApNeedReturn = (item.APStatus3 + item.APStatus2 + item.APStatus5 + item.APStatus0).ToString();
            ss.OpFailed = item.OPStatus5.ToString();
            ss.OpUnkown = item.OPStatus2.ToString();
            ss.OpSuccess = item.OPStatus4.ToString();
            ss.OpSuccessRate = Divide(item.OPStatus4 * 100, opTotal).ToString()+"%";
             ss.OpReportRate = (100 - Divide(item.OPStatus2 * 100, opTotal)).ToString() + "%";
             ss.Date = item.Title;
            statisList.Add(ss);
        }
        if (statisList.Count > 0)
        {
            DataTable dt = ToDataTable<Model.GwStatisV3>(statisList);
            WebTool webtool = new WebTool();
            Dictionary<string, string> dicTitle = new Dictionary<string, string>();
            dicTitle["日期"] = "Date";
            dicTitle["接入点"] = "ApName";
            dicTitle["通道"] = "OpName";
            dicTitle["账号"] = "SpId";
            dicTitle["客户"] = "ClientId";
            dicTitle["AP接收总量"] = "ApReciveTotal";
            dicTitle["AP提交失败"] = "ApSubFailed";
            dicTitle["AP未转发"] = "ApNotForwarded";
            dicTitle["AP失败"] = "ApFailed";
            dicTitle["AP未知"] = "ApUnkown";
            dicTitle["AP需返还"] = "ApNeedReturn";
            dicTitle["OP提交总量"] = "OpSubTotal";
            dicTitle["OP失败"] = "OpFailed";
            dicTitle["OP未知"] = "OpUnkown";
            dicTitle["OP成功"] = "OpSuccess";
            dicTitle["OP成功率"] = "OpSuccessRate";
            dicTitle["OP状态率"] = "OpReportRate";
            webtool.Export(dt,dicTitle,"统计报表",this.Context.Response);
        }
    }
    public decimal Divide(decimal v, decimal total)
    {
        if (total == 0)
        {
            return 0;
        }
        return Math.Round(v / total, 2);
    }
    private string GetApName(List<GwAp> aplist, int apID)
    {
        if (aplist == null)
            return string.Empty;
        Model.GwAp x = aplist.Find(delegate(Model.GwAp ap) { return ap.ApID == apID; });
        return x == null ? string.Empty : string.Format("{0}-{1}", x.ApID, x.ApName);
    }
    private string GetOpName(List<GwOp> oplist, int opID)
    {
        if (oplist == null)
            return string.Empty;
        Model.GwOp x = oplist.Find(delegate(Model.GwOp op) { return op.OpID == opID; });
        return x == null ? string.Empty : string.Format("{0}-{1}", x.OpID, x.OpName);
    }
    private string GetClientName(List<GwClient> clientlist, string clientID)
    {
        if (clientlist == null)
            return string.Empty;
        Model.GwClient x = clientlist.Find(delegate(Model.GwClient client) { return client.ClientID == clientID; });
        return x == null ? string.Format("{0}", clientID) : string.Format("{0}-{1}", x.ClientID, x.ClientName);
    }
    //=================================================
    /// <summary>
    /// Convert a List{T} to a DataTable.
    /// </summary>
    private DataTable ToDataTable<T>(List<T> items)
    {
        var tb = new DataTable(typeof(T).Name);
        PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (PropertyInfo prop in props)
        {
            Type t = GetCoreType(prop.PropertyType);
            tb.Columns.Add(prop.Name, t);
        }
        foreach (T item in items)
        {
            var values = new object[props.Length];
            for (int i = 0; i < props.Length; i++)
            {
                values[i] = props[i].GetValue(item, null);
            }
            tb.Rows.Add(values);
        }
        return tb;
    }
    /// <summary>
    /// Determine of specified type is nullable
    /// </summary>
    public static bool IsNullable(Type t)
    {
        return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
    }
    /// <summary>
    /// Return underlying type if type is Nullable otherwise return the type
    /// </summary>
    public static Type GetCoreType(Type t)
    {
        if (t != null && IsNullable(t))
        {
            if (!t.IsValueType)
            {
                return t;
            }
            else
            {
                return Nullable.GetUnderlyingType(t);
            }
        }
        else
        {
            return t;
        }
    }
    //=============================end===================================
    #endregion
    private void ExPortGwSp()
    {
        string string1 = this.AppContext.GetString("SpID");
        string string2 = this.AppContext.GetString("ApID");
        string string3 = this.AppContext.GetString("ClientID");
        using (GwSpDao gwSpDao = new GwSpDao())
        {
            DataTable dataTable = gwSpDao.LoadSpDataTable(string1, string2, string3);
            WebTool webTool = new WebTool();
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary["客户编码"] = "CLIENT_ID";
            dictionary["账号"] = "SP_ID";
            dictionary["余额"] = "BALANCE";
            dictionary["单价"] = "PRICE";
            dictionary["付款方式"] = "CHARGE_TYPE";
            dictionary["鉴权地址"] = "CLIENT_IP";
            dictionary["门限"] = "THRESHOLD";
            dictionary["密码"] = "PASSWORD";
            webTool.Export(dataTable, dictionary, "SP_" + (object)DateTime.Now.Ticks, this.Context.Response);
        }
    }
    private void ExportGwSmData()
    {
        string apMid = this.AppContext.GetString("ApMID");
        string sDate = this.AppContext.GetString("QueryDate");
        if (string.IsNullOrEmpty(sDate))
            sDate = DateTime.Today.ToString("yyyy-MM-dd");
        string content = this.AppContext.GetString("Content");
        string opstat = this.AppContext.GetString("OpStat");
        string opName = this.AppContext.GetString("OPName");
        string mobile = this.AppContext.GetString("DestnationID");
        string spid = this.AppContext.GetString("SpID");
        string clientId = this.AppContext.GetString("ClientID");
        string beginTime = this.AppContext.GetString("BeginTime");
        if (string.IsNullOrEmpty(beginTime))
            beginTime = DateTime.Now.AddHours(-1.0).Day == DateTime.Now.Day ? DateTime.Now.AddHours(-1.0).ToString("HH:mm:ss") : "00:00:00";
        string endTime = this.AppContext.GetString("EndTime");
        if (string.IsNullOrEmpty(endTime))
            endTime = DateTime.Now.Date.AddSeconds(-1.0).ToString("HH:mm:ss");
        int @int = this.AppContext.GetInt("TStatus", -1);
        using (GwSmDao gwSmDao = new GwSmDao())
        {
            int recordcount = 0;
            if (gwSmDao.LoadInfoListcount(apMid, opName, clientId, spid, mobile, sDate, beginTime, endTime, content, opstat, @int, out recordcount).Count > 1000000)
            {
                this.Response.Write("<script type='text/javascript'>alert('导出数据不能大于100万条!');location.href='GwSm.aspx';</script>");
                this.Response.End();
            }
            DataTable dataTable = gwSmDao.LoadSpDataTable(apMid, opName, clientId, spid, mobile, sDate, beginTime, endTime, content, @int, opstat);
            WebTool webTool = new WebTool();
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary["AP消息ID"] = "AP_MID";
            dictionary["代理商"] = "CLIENT_ID";
            dictionary["账号"] = "SP_ID";
            dictionary["接入点ID"] = "AP_ID";
            dictionary["通道名"] = "OP_NAME";
            dictionary["码号"] = "ACCESS_CODE";
            dictionary["接收号码"] = "MOBILE";
            dictionary["PKN "] = "PK_NUMBER";
            dictionary["PKT "] = "PK_NUMBER";
            dictionary["AP计费"] = "AP_FEE_COUNT";
            dictionary["OP计费 "] = "OP_FEE_COUNT";
            dictionary["UDH"] = "TPUDHI";
            dictionary["优先级"] = "PRIORITY";
            dictionary["FMT"] = "MSG_FORMAT";
            dictionary["长度"] = "MSG_LENGTH";
            dictionary["AP结果 "] = "AP_RESULT";
            dictionary["OP消息ID "] = "OP_MID";
            dictionary["OP结果 "] = "OP_RESULT";
            dictionary["客户提交时间"] = "AP_SUBMIT_TIME";
            dictionary["对外转发时间"] = "OP_SUBMIT_TIME";
            dictionary["状态报告回执时间"] = "OP_DELIVER_TIME";
            dictionary["内容"] = "MSG_CONTENT";
            dictionary["状态"] = "STAT";
            dictionary["耗时"] = "OUT_TIME";
            webTool.Export(dataTable, dictionary, "日志_" + sDate, this.Context.Response);
        }
    }
}