using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.IO; using System.Text; using Sog; using MySql.Data.MySqlClient; namespace bill_statistics { public class ExportToMysql : Singleton { private MySqlDB m_mySqlDb; private Dictionary m_createdSqlDict = new Dictionary(); private Dictionary m_activeDataUserNewAdd = new Dictionary(); private Dictionary m_activeDataUserActive = new Dictionary(); private Dictionary m_incomeDataDaily = new Dictionary(); private List m_exchangeCoupon = new List(); //电话卡 private Dictionary m_NewUserRecharge = new Dictionary(); private Dictionary m_PrintTreasureDraw = new Dictionary(); private Dictionary m_PrintChipDiamond = new Dictionary(); private Dictionary m_PrintChipDiamondRange = new Dictionary(); //按分钟的时间,导入数据库记录,不能重复了 private Dictionary m_onlineAlreadyExportToSql = new Dictionary(); //销毁的时候置空 public void Dispose() { m_mySqlDb.Dispose(); m_mySqlDb = null; } public ExportToMysql() { } public void Init(string db, string ip, string user, string password) { m_mySqlDb = new MySqlDB(db, ip, user, password); } public MySqlDB GetMySqlDB() { return m_mySqlDb; } public bool CheckThisHourAlreadyInSql(string fileName) { bool result = false; FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader streamReader = new StreamReader(fileStream, Encoding.UTF8); fileStream.Seek(0, SeekOrigin.Begin); string content = streamReader.ReadLine(); while (content != null) { string[] splitStr = content.Split('|'); if (splitStr.Length > 2) { int billType = BillTypeUtils.GetBillType(splitStr[1]); //取online日志,判断数据库里是否存在 if (billType == 3001) { DateTime dateTime = BillTypeUtils.GetTime(splitStr[0]); if (BillTypeUtils.NeedExportOnlineByTime(dateTime)) { string sql = BillSqlBuilder.GenSelectOnlineByTime(splitStr[0]); try { MySqlDataReader reader = m_mySqlDb.ExecReader(sql); if (reader == null) { //说明不存在 break; } if (reader.HasRows) { result = true; } //记住一定要Close reader.Close(); } catch (Exception) { break; } break; } } } content = streamReader.ReadLine(); } streamReader.Dispose(); fileStream.Dispose(); return result; } public void DoExportBillType(string[] splitStr) { DateTime dateTime = BillTypeUtils.GetTime(splitStr[0]); string strTimeMinute = splitStr[0].Substring(0, 16); //获取这一天的标准时间 DateTime thisdayBeginTime = dateTime - dateTime.TimeOfDay; int billType = BillTypeUtils.GetBillType(splitStr[1]); string tablename = "bill_" + billType.ToString(); //在线就1张表 if (billType == 3001) { //online表只需要5分钟一条就可以了,其他的可以不入库 if (BillTypeUtils.NeedExportOnlineByTime(dateTime) == false) { return; } //重复一分钟的记录已经导了一条了,第二条不要 if (m_onlineAlreadyExportToSql.ContainsKey(strTimeMinute)) { return; } } else { string beginTimeStr = thisdayBeginTime.ToString("u"); //2016-10-09 beginTimeStr = beginTimeStr.Substring(0, 10); tablename = tablename + "_" + beginTimeStr; } string createSql = BillSqlBuilder.GenCreateTableSql(splitStr, billType, tablename); if (m_createdSqlDict.ContainsKey(createSql) == false) { m_createdSqlDict.Add(createSql, 1); MySqlDataReader reader = m_mySqlDb.ExecReader(createSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } { List paramList = new List(); string insertSql = BillSqlBuilder.GenInsertTableSql(splitStr, tablename, paramList); if (insertSql != null) { MySqlDataReader reader = m_mySqlDb.ExecReader(insertSql, paramList.ToArray()); if (reader == null) { return; } //记住一定要Close reader.Close(); if (billType == 3001) { if (m_onlineAlreadyExportToSql.ContainsKey(strTimeMinute) == false) { m_onlineAlreadyExportToSql.Add(strTimeMinute, 1); } } } } } public void AddOneDailyNewAddUserInfo(ActiveData_userNewAdd data) { if (m_activeDataUserNewAdd.ContainsKey(data.dateId)) { m_activeDataUserNewAdd[data.dateId] = data; } else { m_activeDataUserNewAdd.Add(data.dateId, data); } } //新进用户统计结果表 public void ExportActUserNew() { string tablename = "daily_activedata_usernewadd"; string createSql = BillSqlBuilder.GenCreateTableActiveUserNewSql(tablename); if (m_createdSqlDict.ContainsKey(createSql) == false) { m_createdSqlDict.Add(createSql, 1); MySqlDataReader reader = m_mySqlDb.ExecReader(createSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } foreach (var dailyNewUser in m_activeDataUserNewAdd.Values) { string insertSql = BillSqlBuilder.GenInsertTableActiveUserNewSql(tablename, dailyNewUser); if (insertSql != null) { MySqlDataReader reader = m_mySqlDb.ExecReader(insertSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } } } public void AddOneDailyActiveUserInfo(ActiveData_userActive data) { if (m_activeDataUserActive.ContainsKey(data.dateId)) { m_activeDataUserActive[data.dateId] = data; } else { m_activeDataUserActive.Add(data.dateId, data); } } //活跃用户统计结果表 public void ExportActUserActive() { string tablename = "daily_activedata_useractive"; string createSql = BillSqlBuilder.GenCreateTableActiveUserSql(tablename); if (m_createdSqlDict.ContainsKey(createSql) == false) { m_createdSqlDict.Add(createSql, 1); MySqlDataReader reader = m_mySqlDb.ExecReader(createSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } foreach (var dailyUser in m_activeDataUserActive.Values) { string insertSql = BillSqlBuilder.GenInsertTableActiveUserSql(tablename, dailyUser); if (insertSql != null) { MySqlDataReader reader = m_mySqlDb.ExecReader(insertSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } } } public void AddOneDailyPaymentInfo(IncomeData_daily data) { if (m_incomeDataDaily.ContainsKey(data.dateId)) { m_incomeDataDaily[data.dateId] = data; } else { m_incomeDataDaily.Add(data.dateId, data); } } //付费统计 public void ExportPayment() { string tablename = "daily_incomedata"; string createSql = BillSqlBuilder.GenCreateTablePaymentSql(tablename); if (m_createdSqlDict.ContainsKey(createSql) == false) { m_createdSqlDict.Add(createSql, 1); MySqlDataReader reader = m_mySqlDb.ExecReader(createSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } foreach (var payment in m_incomeDataDaily.Values) { string insertSql = BillSqlBuilder.GenInsertTablePaymentSql(tablename, payment); if (insertSql != null) { MySqlDataReader reader = m_mySqlDb.ExecReader(insertSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } } } //电话卡 public void AddExchangeCouponInfo(Coupon_Info data) { m_exchangeCoupon.Add(data); /* if (m_exchangeCoupon.ContainsKey(data.dateId)) { m_exchangeCoupon[data.dateId] = data; } else { m_exchangeCoupon.Add(data.dateId, data); } */ } public void ExportCoupon() { string tablename = "daily_coupon"; string createSql = BillSqlBuilder.GenCreateTableCouponsql(tablename); if (m_createdSqlDict.ContainsKey(createSql) == false) { m_createdSqlDict.Add(createSql, 1); MySqlDataReader reader = m_mySqlDb.ExecReader(createSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } foreach (var couponInfo in m_exchangeCoupon) { List paramList = new List(); string insertSql = BillSqlBuilder.GenInsertTableCouponSql(tablename, couponInfo, paramList); if (insertSql != null) { MySqlDataReader reader = m_mySqlDb.ExecReader(insertSql, paramList.ToArray()); if (reader == null) { return; } //记住一定要Close reader.Close(); } } } public void AddNewUserRechargeInfo(NewUserRecharge_Info data) { if (m_NewUserRecharge.ContainsKey(data.dateId)) { m_NewUserRecharge[data.dateId] = data; } else { m_NewUserRecharge.Add(data.dateId, data); } } //新用户付费 public void ExportNewUserRecharge() { string tablename = "daily_newuserrecharge"; string createSql = BillSqlBuilder.GenCreateTableNewUserRechargeSql(tablename); if (m_createdSqlDict.ContainsKey(createSql) == false) { m_createdSqlDict.Add(createSql, 1); MySqlDataReader reader = m_mySqlDb.ExecReader(createSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } foreach (var NewUserRecharge in m_NewUserRecharge.Values) { string insertSql = BillSqlBuilder.GenInsertTableNewUserRechargeSql(tablename, NewUserRecharge); if (insertSql != null) { MySqlDataReader reader = m_mySqlDb.ExecReader(insertSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } } } public void AddPrintTreasureDrawInfo(PrintTreasureDraw_Info data) { if (m_PrintTreasureDraw.ContainsKey(data.dateId)) { m_PrintTreasureDraw[data.dateId] = data; } else { m_PrintTreasureDraw.Add(data.dateId, data); } } //夺宝 public void ExportPrintTreasureDraw() { string tablename = "daily_printtreasuredraw"; string createSql = BillSqlBuilder.GenCreateTablePrintTreasureDrawSql(tablename); if (m_createdSqlDict.ContainsKey(createSql) == false) { m_createdSqlDict.Add(createSql, 1); MySqlDataReader reader = m_mySqlDb.ExecReader(createSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } foreach (var PrintTreasureDraw in m_PrintTreasureDraw.Values) { string insertSql = BillSqlBuilder.GenInsertTablePrintTreasureDrawSql(tablename, PrintTreasureDraw); if (insertSql != null) { MySqlDataReader reader = m_mySqlDb.ExecReader(insertSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } } } public void AddPrintChipDiamondInfo(PrintChipDiamond_Info data) { if (m_PrintChipDiamond.ContainsKey(data.dateId)) { m_PrintChipDiamond[data.dateId] = data; } else { m_PrintChipDiamond.Add(data.dateId, data); } } //筹码钻石产出消耗 public void ExportPrintChipDiamond() { string tablename = "daily_printchipdiamond"; string createSql = BillSqlBuilder.GenCreateTablePrintChipDiamondSql(tablename); if (m_createdSqlDict.ContainsKey(createSql) == false) { m_createdSqlDict.Add(createSql, 1); MySqlDataReader reader = m_mySqlDb.ExecReader(createSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } foreach (var PrintChipDiamond in m_PrintChipDiamond.Values) { string insertSql = BillSqlBuilder.GenInsertTablePrintChipDiamondSql(tablename, PrintChipDiamond); if (insertSql != null) { MySqlDataReader reader = m_mySqlDb.ExecReader(insertSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } } } public void AddPrintChipDiamondRangeInfo(PrintChipDiamondRange_Info data) { if (m_PrintChipDiamondRange.ContainsKey(data.dateId)) { m_PrintChipDiamondRange[data.dateId] = data; } else { m_PrintChipDiamondRange.Add(data.dateId, data); } } public void ExportPrintChipDiamondRange() { string tablename = "daily_printchipdiamondrange"; string createSql = BillSqlBuilder.GenCreateTablePrintChipDiamondRangeSql(tablename); if (m_createdSqlDict.ContainsKey(createSql) == false) { m_createdSqlDict.Add(createSql, 1); MySqlDataReader reader = m_mySqlDb.ExecReader(createSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } foreach (var PrintChipDiamondRange in m_PrintChipDiamondRange.Values) { string insertSql = BillSqlBuilder.GenInsertTablePrintChipDiamondRangeSql(tablename, PrintChipDiamondRange); if (insertSql != null) { MySqlDataReader reader = m_mySqlDb.ExecReader(insertSql); if (reader == null) { return; } //记住一定要Close reader.Close(); } } } } }