You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1478 lines
65 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sog;
using ProtoCSStruct;
using MySql.Data.MySqlClient;
namespace DB
{
public class MySqlDBOperator : DBOperator
{
private MySqlDB m_mySqlDb;
private const int BuffLengthMax = 500 * 1024;
//private long maxUserIdByIndex = 0;
//table表格做索引做Key,m_maxUserIdByIndex的值做value
private Dictionary<int, long> m_maxUserIdDict = new Dictionary<int, long>();
public MySqlDBOperator(string db, string ip, string user, string password)
{
m_mySqlDb = new MySqlDB(db, ip, user, password);
}
//销毁的时候置空
public override void Dispose()
{
m_mySqlDb.Dispose();
m_mySqlDb = null;
}
public override void KeepAlive()
{
string strSql = "select * from tbaccount_1 where accountType=0 and accountId=?accountId";
MySqlParameter p_accountId = new MySqlParameter("?accountId", MySqlDbType.VarChar, 200) {Value = "keep_alive"};
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_accountId);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.KeepAlive no record in account table, return reader is null!");
return;
}
//记住一定要Close
reader.Close();
TraceLog.Trace("MySqlDBOperator.KeepAlive exec at {0}", DateTime.Now);
}
private void UpdateMaxUserIdByIndexDict(int accountTableIndex, long maxUserId)
{
//缓存下,最多应该10个
if (m_maxUserIdDict.ContainsKey(accountTableIndex))
{
m_maxUserIdDict[accountTableIndex] = maxUserId;
}
else
{
m_maxUserIdDict.Add(accountTableIndex, maxUserId);
}
}
public override tbAccount QueryAccount(int accountType, string accountID)
{
string tableName = DBServerUtils.GetAccountTableName(accountType, accountID);
TraceLog.Trace("MySqlDBOperator.QueryAccount type {0} accountid {1} table {2}", accountType, accountID, tableName);
string strSql = string.Format("select * from {0} where accountType=?accountType and accountId=?accountId", tableName);
MySqlParameter p_accountType = new MySqlParameter("?accountType", MySqlDbType.Int32) {Value = accountType};
MySqlParameter p_accountId = new MySqlParameter("?accountId", MySqlDbType.VarChar, 200) {Value = accountID};
TraceLog.Trace("MySqlDBOperator.QueryAccount exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_accountType, p_accountId);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.QueryAccount no record in table {0}, return reader is null!", tableName);
return null;
}
if (reader.HasRows == false)
{
TraceLog.Trace("MySqlDBOperator.QueryAccount no record in table {0}", tableName);
//记住一定要Close
reader.Close();
return null;
}
tbAccount tbAccountRecord = null;
try
{
reader.Read();
tbAccountRecord = new tbAccount();
tbAccountRecord.AccountType = reader.GetInt32("accountType");
tbAccountRecord.AccountID = reader.GetString("accountId");
tbAccountRecord.AccountToken = reader.GetString("accountToken");
tbAccountRecord.Nick = reader.GetString("nick");
tbAccountRecord.Gender = reader.GetInt32("gender");
tbAccountRecord.Icon = reader.GetString("icon");
tbAccountRecord.LastLoginRealm = reader.GetInt32("lastLoginRealm");
tbAccountRecord.CreateTime = reader.GetInt64("createTime");
tbAccountRecord.CreateIpAddr = reader.GetUInt32("createIpAddr");
tbAccountRecord.Grade = reader.GetInt32("grade");
int contentIndex = reader.GetOrdinal("exData");
if(!reader.IsDBNull(contentIndex))
{
byte[] buffer = new byte[BuffLengthMax];
long contentLength = reader.GetBytes(contentIndex, 0, buffer, 0, buffer.Length);
tbAccountRecord.exData = new byte[contentLength];
Buffer.BlockCopy(buffer, 0, tbAccountRecord.exData, 0, (int)contentLength);
}
else
{
tbAccountRecord.exData = new byte[] { };
}
TraceLog.Trace("MySqlDBOperator.QueryAccount query success AccountID {0}", tbAccountRecord.AccountID);
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return tbAccountRecord;
}
public override bool UpdateAccountLastLoginRealm(int accountType, string accountID, tbAccount accountRecord, int lastLoginRealm)
{
string tableName = DBServerUtils.GetAccountTableName(accountType, accountID);
TraceLog.Trace("MySqlDBOperator.UpdateAccountLastLoginRealm type {0} accountid {1} table {2}", accountType, accountID,
tableName);
string strSql = string.Format(
"update {0} set lastLoginRealm={1} where accountType={2} and accountId=?accountId"
, tableName, lastLoginRealm, accountType);
MySqlParameter p_accountId = new MySqlParameter("?accountId", MySqlDbType.VarChar, 200) {Value = accountID};
TraceLog.Trace("MySqlDBOperator.UpdateAccountLastLoginRealm exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_accountId);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.UpdateAccountLastLoginRealm no record in table {0}, return reader is null!", tableName);
return false;
}
reader.Close();
return true;
}
public override bool UpdateAccountGrade(int accountType, string accountID, tbAccount accountRecord, int Grade)
{
string tableName = DBServerUtils.GetAccountTableName(accountType, accountID);
TraceLog.Trace("MySqlDBOperator.UpdateAccountGrade type {0} accountid {1} table {2}", accountType, accountID,
tableName);
string strSql = string.Format(
"update {0} set grade={1} where accountType={2} and accountId=?accountId"
, tableName, Grade, accountType);
MySqlParameter p_accountId = new MySqlParameter("?accountId", MySqlDbType.VarChar, 200) { Value = accountID };
TraceLog.Trace("MySqlDBOperator.UpdateAccountGrade exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_accountId);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.UpdateAccountGrade no record in table {0}, return reader is null!", tableName);
return false;
}
reader.Close();
return true;
}
public override bool UpdateAccountExData(int accountType, string accountID, tbAccount accountRecord, ref DBAccountExData exdata)
{
string tableName = DBServerUtils.GetAccountTableName(accountType, accountID);
TraceLog.Trace("MySqlDBOperator.UpdateAccountExData type {0} accountid {1} table {2} channel {3} channelId {4}"
, accountType, accountID, tableName, exdata.Channel, exdata.ChannelId);
string strSql = string.Format("update {0} set exData=?exData where accountType={1} and accountId=?accountId"
, tableName, accountType);
MySqlParameter p_accountId = new MySqlParameter("?accountId", MySqlDbType.VarChar, 200) { Value = accountID };
byte[] contentByte = StructMessageParseUtils.ToByteArray(ref exdata);
MySqlParameter p_content = new MySqlParameter("?exData", MySqlDbType.Blob) { Value = contentByte };
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_accountId, p_content);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.UpdateAccountExData no record in table {0}, return reader is null!", tableName);
return false;
}
reader.Close();
return true;
}
public override bool InsertAccount(int accountType, string accountID, string accountToken, long createTime,
string deviceId, uint ipAddr, string nick, int gender, string icon, int grade, ref DBAccountExData exData)
{
string tableName = DBServerUtils.GetAccountTableName(accountType, accountID);
TraceLog.Trace("MySqlDBOperator.InsertAccount type {0} accountid {1} table {2} channel {3} channelId {4}"
, accountType, accountID, tableName, exData.Channel, exData.ChannelId);
string strSql = "insert into "
+ tableName
+ "(accountType,accountId,accountToken,createTime,createDeviceId,createIpAddr,nick,gender,icon,lastLoginRealm,grade,exData)"
+ " values(?accountType,?accountId,?accountToken,?createTime,?createDeviceId,?createIpAddr,?nick,?gender,?icon,0,?grade,?exData)";
MySqlParameter p_accountType = new MySqlParameter("?accountType", MySqlDbType.Int32) {Value = accountType};
MySqlParameter p_accountId = new MySqlParameter("?accountId", MySqlDbType.VarChar, 200) {Value = accountID};
MySqlParameter p_accountToken =
new MySqlParameter("?accountToken", MySqlDbType.VarChar, 300) {Value = accountToken};
MySqlParameter p_createTime = new MySqlParameter("?createTime", MySqlDbType.UInt64) {Value = createTime};
MySqlParameter p_createDeviceId =
new MySqlParameter("?createDeviceId", MySqlDbType.VarChar, 100) {Value = deviceId};
MySqlParameter p_createIpAddr =
new MySqlParameter("?createIpAddr", MySqlDbType.UInt32, 100) {Value = ipAddr};
MySqlParameter p_nick = new MySqlParameter("?nick", MySqlDbType.VarChar, 200) {Value = nick};
MySqlParameter p_gender = new MySqlParameter("?gender", MySqlDbType.Int32) {Value = gender};
MySqlParameter p_icon = new MySqlParameter("?icon", MySqlDbType.VarChar, 200) {Value = icon};
MySqlParameter p_grade = new MySqlParameter("?grade", MySqlDbType.Int32) { Value = grade };
byte[] exDataByte = StructMessageParseUtils.ToByteArray(ref exData);
MySqlParameter p_exData = new MySqlParameter("?exData", MySqlDbType.Blob) { Value = exDataByte };
TraceLog.Trace("MySqlDBOperator.InsertAccount exec sql: {0}", strSql);
try
{
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_accountType, p_accountId, p_accountToken
, p_createTime, p_createDeviceId, p_createIpAddr, p_nick, p_gender, p_icon, p_grade, p_exData);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.InsertAccount failed table {0}, return reader is null!", tableName);
return false;
}
reader.Close();
}
catch(Exception ex)
{
p_nick = new MySqlParameter("?nick", MySqlDbType.VarChar, 200) { Value = string.Empty };
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_accountType, p_accountId, p_accountToken
, p_createTime, p_createDeviceId, p_createIpAddr, p_nick, p_gender, p_icon, p_grade, p_exData);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.InsertAccount failed table {0}, return reader is null after set nick to null", tableName);
return false;
}
TraceLog.Error("MySqlDBOperator.InsertAccount table {0} set nick to empty success", tableName);
TraceLog.Exception(ex);
reader.Close();
}
TraceLog.Trace("MySqlDBOperator.InsertAccount type {0} accountid {1} table {2} success", accountType
, accountID, tableName);
return true;
}
// 注意这个调用所有的DBServer只能有一个,如果不是的话请自己写id增长逻辑,不要用数据库的自动增长功能
// select max(userId) 的效率很低,请后期优化
//hurs - 这里有错误,之前是10个表10条线程,10个m_maxUserIdByIndex,一一对应没有问题,现在是100张表,10条线程,10个m_maxUserIdByIndex产生冲突(实际测试已经产生了冲突)
//2018-10-29 现在这个用于直接产生uid
public override long GenNewGameID(int accountTableIndex)
{
TraceLog.Trace("MySqlDBOperator.GenNewGameID create new player UID");
long newGameId = 0;
string strSql = null;
MySqlDataReader reader = null;
long maxUserIdByIndex = 0;
m_maxUserIdDict.TryGetValue(accountTableIndex, out maxUserIdByIndex);
try
{
//为0表示没有初始化,需要先查表
if (maxUserIdByIndex == 0)
{
TraceLog.Trace("MySqlDBOperator.GenNewGameID need init m_maxUserIdByIndex, accountTableIndex {0}", accountTableIndex);
strSql = "select * from tbuid where subIndex=" + accountTableIndex.ToString();
reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.GenNewGameID select from tbuid failed, subIndex {0}", accountTableIndex);
return 0;
}
if (reader.HasRows == false)
{
//没有记录,需要初始化,id从100000开始
reader.Close();
newGameId = 100000 + accountTableIndex;
TraceLog.Trace("MySqlDBOperator.GenNewGameID new tbuid is {0} subIndex {1}", newGameId, accountTableIndex);
strSql = string.Format("insert into tbuid values({0},{1})", accountTableIndex, newGameId);
TraceLog.Trace("MySqlDBOperator.GenNewGameID sql is {0}", strSql);
reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.GenNewGameID insert to tbuid failed, subIndex {0}", accountTableIndex);
return 0;
}
reader.Close();
TraceLog.Trace("MySqlDBOperator.GenNewGameID insert into tbuid success");
maxUserIdByIndex = newGameId;
UpdateMaxUserIdByIndexDict(accountTableIndex, maxUserIdByIndex);
return newGameId;
}
else
{
reader.Read();
long lastGameId = reader.GetInt64("uid");
reader.Close();
maxUserIdByIndex = lastGameId;
}
}
newGameId = maxUserIdByIndex + TableIndexCalc.AccountTableCount;
TraceLog.Trace("MySqlDBOperator.GenNewGameID new uid is {0} subIndex {1}", newGameId, accountTableIndex);
strSql = string.Format("update tbuid set uid={0} where subIndex={1}", newGameId, accountTableIndex);
TraceLog.Trace("MySqlDBOperator.GenNewGameID sql is {0}", strSql);
reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.GenNewGameID update tbuid failed, subIndex {0}", accountTableIndex);
return 0;
}
reader.Close();
TraceLog.Trace("MySqlDBOperator.GenNewGameID update tbuid success");
maxUserIdByIndex = newGameId;
UpdateMaxUserIdByIndexDict(accountTableIndex, maxUserIdByIndex);
return newGameId;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
if (reader != null)
{
reader.Close();
}
}
return 0;
}
public override List<tbacc_realmuid_link> QueryAccountUid(int accountType, string accountID, int realmId)
{
string tableName = DBServerUtils.GetAccountRealmUidLinkTableName(accountType, accountID);
TraceLog.Trace("MySqlDBOperator.QueryAccountUid accountType {0} accountId {1} table {2}", accountType, accountID,
tableName);
string strSql = "select * from " + tableName;
if (realmId == 0)
{
strSql += " where accountType=?accountType and accountId=?accountId";
}
else
{
strSql += " where accountType=?accountType and accountId=?accountId and realm=?realmId";
}
TraceLog.Trace("MySqlDBOperator.QueryAccountUid exec sql: {0}", strSql);
MySqlParameter p_accountType = new MySqlParameter("?accountType", MySqlDbType.Int32) {Value = accountType};
MySqlParameter p_accountId = new MySqlParameter("?accountId", MySqlDbType.VarChar, 200) {Value = accountID};
MySqlDataReader reader = null;
if (realmId == 0)
{
reader = m_mySqlDb.ExecReader(strSql, p_accountType, p_accountId);
}
else
{
MySqlParameter p_realmId = new MySqlParameter("?realmId", MySqlDbType.Int32) {Value = realmId};
reader = m_mySqlDb.ExecReader(strSql, p_accountType, p_accountId, p_realmId);
}
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.QueryAccountUid no record in table {0}, return reader is null!", tableName);
return null;
}
if (reader.HasRows == false)
{
TraceLog.Trace("MySqlDBOperator.QueryAccountUid no record in table {0}", tableName);
reader.Close();
return null;
}
List<tbacc_realmuid_link> list = new List<tbacc_realmuid_link>();
try
{
while (reader.Read())
{
tbacc_realmuid_link tbAcclink = new tbacc_realmuid_link();
tbAcclink.accountType = reader.GetInt32("accountType");
tbAcclink.accountId = reader.GetString("accountId");
tbAcclink.realmId = reader.GetInt32("realm");
tbAcclink.uid = reader.GetUInt32("uid");
tbAcclink.nick = reader.GetString("nick");
tbAcclink.icon = reader.GetString("icon");
tbAcclink.level = reader.GetInt32("level");
tbAcclink.iconFrameId = reader.GetInt32("iconFrameId");
tbAcclink.lastLoginTime = reader.GetInt64("lastLoginTime");
list.Add(tbAcclink);
TraceLog.Trace("MySqlDBOperator.QueryAccountUid query success uid {0}", tbAcclink.uid);
}
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return list;
}
public override tbacc_realmuid_link QueryAccountUidbyUid(int uid, int realmId)
{
if(uid == 0)
{
return null;
}
tbacc_realmuid_link link = new tbacc_realmuid_link();
string tableName = DBServerUtils.GetAccountRealmUidLinkTableNameByUid(uid);
string strSql = $"select * from {tableName} where uid={uid}";
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.QueryAccountUidbyUid no record uid {0}, return reader is null!", uid);
return null;
}
if (reader.HasRows == false)
{
TraceLog.Error("MySqlDBOperator.QueryAccountUidbyUid no record uid {0}", uid);
reader.Close();
return null;
}
try
{
reader.Read();
link.realmId = reader.GetInt32("realm");
link.uid = reader.GetInt64("uid");
link.accountType = reader.GetInt32("accountType");
link.accountId = reader.GetString("accountId");
link.nick = reader.GetString("nick");
link.icon = reader.GetString("icon");
link.level = reader.GetInt32("level");
link.iconFrameId = reader.GetInt32("iconFrameId");
link.lastLoginTime = reader.GetInt64("lastLoginTime");
return link;
}
catch (Exception ex)
{
TraceLog.Error("MySqlDBOperator.QueryAccountUidbyUid error :{0}", ex.Message);
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return null;
}
public override bool InsertAccRealmUidLink(int accountType, string accountID, int realmId, long uid)
{
string tableName = DBServerUtils.GetAccountRealmUidLinkTableName(accountType, accountID);
TraceLog.Trace("MySqlDBOperator.InsertAccRealmUidLink type {0} accountid {1} table {2}", accountType
, accountID, tableName);
string strSql = "insert into "
+ tableName
+ "(accountType,accountId,realm,uid,lastLoginTime)"
+ " values(?accountType,?accountId,?realmId,?uid,?lastLoginTime)";
long lNow = DBServerUtils.GetTimeSecond();
MySqlParameter p_accountType = new MySqlParameter("?accountType", MySqlDbType.Int32) {Value = accountType};
MySqlParameter p_accountId = new MySqlParameter("?accountId", MySqlDbType.VarChar, 200) {Value = accountID};
MySqlParameter p_realm = new MySqlParameter("?realmId", MySqlDbType.Int32) {Value = realmId};
MySqlParameter p_uid = new MySqlParameter("?uid", MySqlDbType.Int64) {Value = uid};
MySqlParameter p_time = new MySqlParameter("?lastLoginTime", MySqlDbType.UInt64) { Value = lNow };
TraceLog.Trace("MySqlDBOperator.InsertAccRealmUidLink exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_accountType, p_accountId, p_realm, p_uid, p_time);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.InsertAccRealmUidLink failed table {0}, return reader is null!", tableName);
return false;
}
reader.Close();
TraceLog.Trace("MySqlDBOperator.InsertAccRealmUidLink type {0} accountid {1} table {2} success", accountType
, accountID, tableName);
return true;
}
public override bool UpdateAccRealmUidLink(int accountType, string accountID, ref CSRoleBrief roleBrief)
{
string tableName = DBServerUtils.GetAccountRealmUidLinkTableName(accountType, accountID);
TraceLog.Trace("MySqlDBOperator.UpdateAccRealmUidLink type {0} accountid {1} table {2}", accountType, accountID, tableName);
string strSql = $"update {tableName} set nick=?nick,icon=?icon,lastLoginTime={roleBrief.LastLoginTime},level={roleBrief.Level},iconFrameId={roleBrief.IconFrameId} where accountType={accountType} and accountId=?accountId and uid={roleBrief.Uid}";
TraceLog.Trace("MySqlDBOperator.UpdateAccRealmUidLink exec sql: {0}", strSql);
MySqlParameter p_nick = new MySqlParameter("?nick", MySqlDbType.VarChar, 200) {Value = roleBrief.Nick.GetString()};
MySqlParameter p_icon = new MySqlParameter("?icon", MySqlDbType.VarChar, 200) {Value = roleBrief.Icon.GetString()};
MySqlParameter p_accountId = new MySqlParameter("?accountId", MySqlDbType.VarChar, 200) {Value = accountID};
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_nick, p_icon, p_accountId);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.UpdateAccRealmUidLink failed table {0}, return reader is null!", tableName);
return false;
}
reader.Close();
TraceLog.Trace("MySqlDBOperator.UpdateAccRealmUidLink type {0} accountid {1} table {2} success"
, accountType, accountID, tableName);
return true;
}
public override bool UpdateAccRealmLinkUid(int accountType, string accountID, int realmId, long newUid)
{
string tableName = DBServerUtils.GetAccountRealmUidLinkTableName(accountType, accountID);
TraceLog.Trace("MySqlDBOperator.UpdateAccRealmLinkUid type {0} accountid {1} table {2}", accountType, accountID, tableName);
string strSql = $"update {tableName} set uid={newUid} where accountType={accountType} and accountId=?accountId";
TraceLog.Trace("MySqlDBOperator.UpdateAccRealmLinkUid exec sql: {0}", strSql);
MySqlParameter p_accountId = new MySqlParameter("?accountId", MySqlDbType.VarChar, 200) { Value = accountID };
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_accountId);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.UpdateAccRealmLinkUid failed table {0}, return reader is null!", tableName);
return false;
}
reader.Close();
TraceLog.Trace("MySqlDBOperator.UpdateAccRealmLinkUid type {0} accountId {1} table {2} new uid {3} succ"
, accountType, accountID, tableName, newUid);
return true;
}
//public override bool UpdateAccRealmUidLinkLogoutTime(int accountType, string accountID,
// int realmId, long uid, int lastLogoutTime)
//{
// string tableName = DBServerUtils.GetAccountRealmUidLinkTableName(accountType, accountID);
// TraceLog.Trace("MySqlDBOperator.UpdateAccRealmUidLinkLogoutTime type {0} accountid {1} table {2}", accountType, accountID, tableName);
// string strSql = $"update {tableName} set lastLogoutTime={lastLogoutTime} where accountType={accountType} and accountId=?accountId and uid={uid}";
// TraceLog.Trace("MySqlDBOperator.UpdateAccRealmUidLinkLogoutTime exec sql: {0}", strSql);
// MySqlParameter p_accountId = new MySqlParameter("?accountId", MySqlDbType.VarChar, 200) { Value = accountID };
// MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_accountId);
// if (reader == null)
// {
// TraceLog.Trace("MySqlDBOperator.UpdateAccRealmUidLinkLogoutTime failed table {0}, return reader is null!", tableName);
// return false;
// }
// reader.Close();
// TraceLog.Trace("MySqlDBOperator.UpdateAccRealmUidLinkLogoutTime uid {0} lastLogoutTime {1} succ", uid, lastLogoutTime);
// return true;
//}
// pay
public override bool InsertPayReq(ref SSPayGoogleReq payReq)
{
long lNow = DBServerUtils.GetTimeSecond();
string tableName = DBServerUtils.GetPayTableName(payReq.Uid);
string strSql = string.Format("insert into {5} (orderId,payType,productId,purchaseTime,uid,status,diamond,money,exData) values(?orderId,{0},?productId,{1},{2},0,{3},{4},?exData)"
, payReq.PayType, lNow, payReq.Uid, payReq.Diamond, payReq.Money, tableName);
TraceLog.Trace("MySqlDBOperator.InsertPayReq exec sql: {0}", strSql);
MySqlParameter p_orderId = new MySqlParameter("?orderId", MySqlDbType.VarChar, 200) {Value = payReq.OrderId.GetString()};
MySqlParameter p_productId = new MySqlParameter("?productId", MySqlDbType.VarChar, 100) {Value = payReq.ItemID.ToString()};
// 额外数据
DBPayExData exData = new DBPayExData();
for (int i = 0; i < payReq.SelectItems.Count; i++)
{
exData.PlayerSelectItem.Add(ref payReq.SelectItems[i].Item);
}
byte[] exDataByte = StructMessageParseUtils.ToByteArray(ref exData);
MySqlParameter p_exData = new MySqlParameter("?exData", MySqlDbType.TinyBlob) {Value = exDataByte};
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_orderId, p_productId, p_exData);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.InsertPayReq failed, return reader is null!");
return false;
}
reader.Close();
TraceLog.Trace("MySqlDBOperator.InsertPayReq uid {0} orderid {1} to table success", payReq.Uid, payReq.OrderId);
return true;
}
public override tbpay SelectPayRecord(long uid, string orderId)
{
string tableName = DBServerUtils.GetPayTableName(uid);
string strSql = $"select * from {tableName} where orderId=?orderId";
MySqlParameter p_orderId = new MySqlParameter("?orderId", MySqlDbType.VarChar, 200) {Value = orderId};
TraceLog.Trace("MySqlDBOperator.SelectPayRecord exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_orderId);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.SelectPayRecord no record orderid {0}, return reader is null!", orderId);
return null;
}
if (reader.HasRows == false)
{
TraceLog.Error("MySqlDBOperator.SelectPayRecord no record orderid {0}", orderId);
reader.Close();
return null;
}
try
{
reader.Read();
tbpay tbRecord = new tbpay();
tbRecord.Uid = reader.GetInt64("uid");
tbRecord.PayType = reader.GetInt32("payType");
tbRecord.Status = reader.GetInt32("status");
tbRecord.Money = reader.GetInt32("money");
tbRecord.OrderID = reader.GetString("orderId");
// productId之前是string, 后改为int, 这里转换一下
string productId = reader.GetString("productId");
int.TryParse(productId, out int tmp);
tbRecord.ProductID = tmp;
tbRecord.OrderId3rd = reader.GetString("orderId3rd");
tbRecord.PayTime3rd = reader.GetInt64("purchaseTime");
tbRecord.IsTestPay = reader.GetInt32("isTestPay");
tbRecord.RefundTime = reader.GetInt64("refundTime");
tbRecord.Amount = reader.GetInt32("amount");
tbRecord.Currency = reader.GetString("currency");
tbRecord.Currency = reader.GetString("currency");
tbRecord.SubPayType = reader.GetInt32("subPayType");
tbRecord.AmountExchange = reader.GetInt32("amountExchange");
tbRecord.DiamondExchange = reader.GetInt64("diamondExchange");
long exDataLength = 0;
int exDataIndex = reader.GetOrdinal("exData");
if (! reader.IsDBNull(exDataIndex))
{
byte[] buffer = new byte[512];
exDataLength = reader.GetBytes(exDataIndex, 0, buffer, 0, buffer.Length);
byte[] exDataByte = new byte[exDataLength];
Buffer.BlockCopy(buffer, 0, exDataByte, 0, (int) exDataLength);
StructMessageParseUtils.ParseFrom(ref tbRecord.ExData, exDataByte);
}
TraceLog.Trace("MySqlDBOperator.SelectPayRecord query success uid {0} status {1} money {2} payType {3} orderId3rd {4} isTestPay {5} exData len {6}"
, tbRecord.Uid, tbRecord.Status, tbRecord.Money, tbRecord.PayType
, tbRecord.OrderId3rd, tbRecord.IsTestPay, exDataLength);
return tbRecord;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return null;
}
public override List<tbpay> SelectPaySuccRecord(long uid)
{
string tableName = DBServerUtils.GetPayTableName(uid);
// 以防万一, 只返回前10条记录, 应该不会出现连充10笔订单没有到账还继续充的人吧
string strSql = $"select * from {tableName} where uid={uid} and status=1 limit 10";
TraceLog.Trace("MySqlDBOperator.SelectPayRecord exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.SelectPayRecord uid {0} pay status 1 no record, return reader is null", uid);
return null;
}
if (reader.HasRows == false)
{
TraceLog.Trace("MySqlDBOperator.SelectPayRecord uid {0} pay status 1 no record", uid);
//记住一定要Close
reader.Close();
return null;
}
List<tbpay> payList = new List<tbpay>();
try
{
byte[] buffer = new byte[512];
while (reader.Read())
{
tbpay tbRecord = new tbpay();
tbRecord.Uid = reader.GetInt64("uid");
tbRecord.PayType = reader.GetInt32("payType");
tbRecord.Status = reader.GetInt32("status");
tbRecord.Money = reader.GetInt32("money");
tbRecord.OrderID = reader.GetString("orderId");
// productId之前是string, 后改为int, 这里转换一下
string productId = reader.GetString("productId");
int.TryParse(productId, out int tmp);
tbRecord.ProductID = tmp;
tbRecord.OrderId3rd = reader.GetString("orderId3rd");
tbRecord.PayTime3rd = reader.GetInt64("purchaseTime");
tbRecord.IsTestPay = reader.GetInt32("isTestPay");
tbRecord.RefundTime = reader.GetInt64("refundTime");
tbRecord.Amount = reader.GetInt32("amount");
tbRecord.Currency = reader.GetString("currency");
tbRecord.Currency = reader.GetString("currency");
tbRecord.SubPayType = reader.GetInt32("subPayType");
tbRecord.AmountExchange = reader.GetInt32("amountExchange");
tbRecord.DiamondExchange = reader.GetInt64("diamondExchange");
long exDataLength = 0;
int exDataIndex = reader.GetOrdinal("exData");
if (! reader.IsDBNull(exDataIndex))
{
exDataLength = reader.GetBytes(exDataIndex, 0, buffer, 0, buffer.Length);
byte[] exDataByte = new byte[exDataLength];
Buffer.BlockCopy(buffer, 0, exDataByte, 0, (int) exDataLength);
StructMessageParseUtils.ParseFrom(ref tbRecord.ExData, exDataByte);
}
payList.Add(tbRecord);
TraceLog.Trace("MySqlDBOperator.SelectPayRecord query success uid {0} status {1} money {2} payType {3} orderId3rd {4} payTime3rd {5} exData len {6}"
, tbRecord.Uid, tbRecord.Status, tbRecord.Money, tbRecord.PayType
, tbRecord.OrderId3rd, tbRecord.PayTime3rd, exDataLength);
}
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return payList;
}
public override tbpay SelectPayRecordByOrderId3rd(long uid, string orderId3rd)
{
string tableName = DBServerUtils.GetPayTableName(uid);
string strSql = $"select * from {tableName} where orderId3rd=?orderId3rd";
MySqlParameter p_orderId3rd =
new MySqlParameter("?orderId3rd", MySqlDbType.VarChar, 200) {Value = orderId3rd};
TraceLog.Trace("MySqlDBOperator.SelectPayRecordByOrderId3rd exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_orderId3rd);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.SelectPayRecordByOrderId3rd no record orderId3rd {0}, return reader is null!", p_orderId3rd);
return null;
}
if (reader.HasRows == false)
{
TraceLog.Debug("MySqlDBOperator.SelectPayRecordByOrderId3rd no record orderId3rd {0}", orderId3rd);
//记住一定要Close
reader.Close();
return null;
}
try
{
reader.Read();
tbpay tbRecord = new tbpay();
tbRecord.Uid = reader.GetInt64("uid");
tbRecord.PayType = reader.GetInt32("payType");
tbRecord.Status = reader.GetInt32("status");
tbRecord.Money = reader.GetInt32("money");
tbRecord.OrderID = reader.GetString("orderId");
// productId之前是string, 后改为int, 这里转换一下
string productId = reader.GetString("productId");
int.TryParse(productId, out int tmp);
tbRecord.ProductID = tmp;
tbRecord.OrderId3rd = reader.GetString("orderId3rd");
tbRecord.PayTime3rd = reader.GetInt64("purchaseTime");
tbRecord.IsTestPay = reader.GetInt32("isTestPay");
TraceLog.Trace(
"MySqlDBOperator.SelectPayRecordByOrderId3rd query success uid {0} status {1} money {2} payType {3} orderId3rd {4}"
, tbRecord.Uid, tbRecord.Status, tbRecord.Money, tbRecord.PayType,
tbRecord.OrderId3rd);
return tbRecord;
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return null;
}
public override bool UpdatePayProductId(long uid, string orderId, int productId)
{
string tableName = DBServerUtils.GetPayTableName(uid);
string strSql = $"update {tableName} set productId=?productId where orderId=?orderId";
MySqlParameter p_productId = new MySqlParameter("?productId", MySqlDbType.VarChar, 100) { Value = productId.ToString() };
MySqlParameter p_orderId = new MySqlParameter("?orderId", MySqlDbType.VarChar, 200) { Value = orderId };
TraceLog.Trace("MySqlDBOperator.UpdatePayProductId exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_productId, p_orderId);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.UpdatePayProductId return reader is null!");
return false;
}
reader.Close();
TraceLog.Trace("MySqlDBOperator.UpdatePayProductId succ, order {0} productId {1}", orderId, productId);
return true;
}
public override bool UpdatePayStatus(long uid, string orderId, int newStatus,
uint amountExchange, uint diamondExchange, int newItemId, uint money)
{
string tableName = DBServerUtils.GetPayTableName(uid);
string strSql = null;
MySqlDataReader reader = null;
MySqlParameter p_orderId = new MySqlParameter("?orderId", MySqlDbType.VarChar, 200) { Value = orderId };
if (newItemId != 0)
{
// 点卡支付金额不满足商品时, 会根据规则替换itemId
strSql = $"update {tableName} set productId=?productId, status={newStatus}, money={money}, amountExchange={amountExchange}, diamondExchange={diamondExchange} where orderId=?orderId";
TraceLog.Trace("MySqlDBOperator.UpdatePayStatus exec sql: {0}", strSql);
MySqlParameter p_productId = new MySqlParameter("?productId", MySqlDbType.VarChar, 100) { Value = newItemId.ToString() };
reader = m_mySqlDb.ExecReader(strSql, p_productId, p_orderId);
}
else
{
strSql = $"update {tableName} set status={newStatus}, money={money}, amountExchange={amountExchange}, diamondExchange={diamondExchange} where orderId=?orderId";
TraceLog.Trace("MySqlDBOperator.UpdatePayStatus exec sql: {0}", strSql);
reader = m_mySqlDb.ExecReader(strSql, p_orderId);
}
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.UpdatePayStatus no record orderid {0}, return reader is null!", orderId);
return false;
}
reader.Close();
TraceLog.Debug("MySqlDBOperator.UpdatePayStatus orderid {0} status {1} to table success", orderId, newStatus);
return true;
}
public override bool UpdatePayStatus(long uid, string orderId, int newStatus, string orderId3rd, long lTime,
uint amount, bool isTestPay, string currency, int subPayType)
{
string tableName = DBServerUtils.GetPayTableName(uid);
string strSql = $"update {tableName} set status={newStatus}, purchaseTime={lTime}, amount={amount}, isTestPay={(isTestPay ? 1 : 0)}, subPayType={subPayType}, orderId3rd=?orderId3rd, currency=?currency where orderId=?orderId";
MySqlParameter p_orderId = new MySqlParameter("?orderId", MySqlDbType.VarChar, 200) {Value = orderId};
MySqlParameter p_orderId3rd = new MySqlParameter("?orderId3rd", MySqlDbType.VarChar, 200) {Value = orderId3rd};
MySqlParameter p_currency = new MySqlParameter("?currency", MySqlDbType.VarChar, 16) {Value = currency};
TraceLog.Trace("MySqlDBOperator.UpdatePayStatus exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_orderId, p_orderId3rd, p_currency);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.UpdatePayStatus no record orderid {0}, return reader is null!", orderId);
return false;
}
reader.Close();
TraceLog.Debug("MySqlDBOperator.UpdatePayStatus orderid {0} status {1} to table success", orderId, newStatus);
return true;
}
public override bool SetRefundTime(long uid, string orderId, string orderId3rd, long refundTime)
{
string tableName = DBServerUtils.GetPayTableName(uid);
string strSql =
$"update {tableName} set refundTime={refundTime} where orderId=?orderId and orderId3rd=?orderId3rd";
MySqlParameter p_orderId = new MySqlParameter("?orderId", MySqlDbType.VarChar, 200) {Value = orderId};
MySqlParameter p_orderId3rd =
new MySqlParameter("?orderId3rd", MySqlDbType.VarChar, 200) {Value = orderId3rd};
TraceLog.Trace("MySqlDBOperator.SetRefundTime exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_orderId, p_orderId3rd);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.SetRefundTime no record orderid {0}, return reader is null!", orderId);
return false;
}
reader.Close();
TraceLog.Debug("MySqlDBOperator.SetRefundTime orderid {0} to table success", orderId);
return true;
}
public override Tuple<int, List<SSUserPayRecordOne>> SelectAllPayRecords(SSGmQueryUserPayRecordReq req)
{
var TableName = DBServerUtils.GetPayTableName(req.UID);
List<SSUserPayRecordOne> paylist = new List<SSUserPayRecordOne>();
StringBuilder str = new StringBuilder($"select * from {TableName} where 1=1 ");
StringBuilder strsql = new StringBuilder($"select COUNT(*) as num from {TableName} where 1 = 1 ");
if (req.UID != 0)
{
str = str.Append($" and uid= {req.UID} ");
strsql = strsql.Append($" and uid= {req.UID} ");
}
if (req.Status != -999)
{
str = str.Append($" and status= {req.Status} ");
strsql = strsql.Append($" and status= {req.Status} ");
}
if (req.StartTime != 0)
{
str = str.Append($" and purchaseTime >= {req.StartTime} ");
strsql = strsql.Append($" and purchaseTime >= {req.StartTime} ");
}
if (req.EndTime != 0)
{
str = str.Append($" and purchaseTime <= {req.EndTime} ");
strsql = strsql.Append($" and purchaseTime <= {req.EndTime} ");
}
if (req.OrderId.Length != 0)
{
str = str.Append($" and orderId = '{req.OrderId.GetString()}' ");
strsql = strsql.Append($" and orderId = '{req.OrderId.GetString()}' ");
}
if (req.OrderId3rd.Length != 0)
{
str = str.Append($" and orderId3rd = '{req.OrderId3rd.GetString()}' ");
strsql = strsql.Append($" and orderId3rd = '{req.OrderId3rd.GetString()}' ");
}
//时间倒序分页
str = str.Append(
$" ORDER BY purchaseTime DESC limit {req.Pagesize * (req.Pageindex - 1)},{req.Pagesize};");
//读取页大小
MySqlDataReader pagereader = m_mySqlDb.ExecReader(strsql.ToString());
int PageTotal = 0;
while (pagereader.Read())
{
PageTotal = pagereader.GetInt32("num");
break;
}
pagereader.Close();
// string strSql = string.Format(" where uid={0} and status=2", uid);
TraceLog.Trace("MySqlDBOperator.SelectAllPayRecords exec sql: {0}", strsql.ToString());
var nulldata = new Tuple<int, List<SSUserPayRecordOne>>(item1: 0, new List<SSUserPayRecordOne>());
MySqlDataReader reader = m_mySqlDb.ExecReader(str.ToString());
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.SelectAllPayRecords no record uid {0}, return reader is null!", strsql.ToString());
return nulldata;
}
if (reader.HasRows == false)
{
TraceLog.Error("MySqlDBOperator.SelectAllPayRecords no record uid {0}", strsql.ToString());
//记住一定要Close
reader.Close();
return nulldata;
}
try
{
while (reader.Read())
{
SSUserPayRecordOne record = new SSUserPayRecordOne();
record.Amount = reader.GetUInt32("amount");
record.Currency.SetString(reader.GetString("currency"));
record.Diamond = reader.GetUInt32("diamond");
record.DiamondGift = reader.GetUInt32("diamondGift");
record.IsTestPay = reader.GetUInt32("isTestPay");
record.Money = reader.GetUInt32("money");
record.OrderId.SetString(reader.GetString("OrderId"));
record.OrderId3rd.SetString(reader.GetString("OrderId3rd"));
record.PayType = reader.GetUInt32("PayType");
// productId之前是string, 后改为int, 这里转换一下
string productId = reader.GetString("productId");
int.TryParse(productId, out int tmp);
record.ProductId = tmp;
record.TimeSecond = reader.GetUInt32("PurchaseTime");
record.PurchaseTime = reader.GetUInt32("PurchaseTime");
record.RefundTime = reader.GetUInt32("RefundTime");
record.Status = reader.GetUInt32("Status");
record.Uid = reader.GetUInt32("Uid");
if (record.OrderId3rd.IsEmpty() == false)
{
paylist.Add(record);
}
}
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return new Tuple<int, List<SSUserPayRecordOne>>(item1: PageTotal, item2: paylist);
}
public override bool ReplacePayHttpContent(ref SSPayHeroSaveHttpContentReq req)
{
string strSql =
$"replace into tbpay_http (orderId, uid, isTestPay, method, url, content) values(?orderId, {req.Http.Uid}, {req.Http.IsTestPay}, {req.Http.Method},?url,?content)";
string orderId = req.Http.OrderId.GetString();
MySqlParameter p_orderId = new MySqlParameter("?orderId", MySqlDbType.VarChar, 200) {Value = orderId};
MySqlParameter p_url =
new MySqlParameter("?url", MySqlDbType.VarChar, 200) {Value = req.Http.Url.GetString()};
MySqlParameter p_content =
new MySqlParameter("?content", MySqlDbType.VarChar, 10240) {Value = req.Http.Content.GetString()};
TraceLog.Trace("MySqlDBOperator.ReplacePayHttpContent exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_orderId, p_url, p_content);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.ReplacePayHttpContent failed, return reader is null!");
return false;
}
reader.Close();
TraceLog.Trace("MySqlDBOperator.ReplacePayHttpContent orderId {0} to table success", orderId);
return true;
}
public override void SelectPayHttpContent(ref SSPayHeroSelectHttpContentRes res)
{
// 以防万一, 每次只返回10条记录
string strSql = "select * from tbpay_http limit 10";
TraceLog.Trace("MySqlDBOperator.SelectPayHttpContent exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.SelectPayHttpContent return reader is null");
return;
}
if (reader.HasRows == false)
{
TraceLog.Trace("MySqlDBOperator.SelectPayHttpContent reader no rows");
reader.Close();
return;
}
try
{
while (reader.Read() && res.Http.Count < res.Http.GetMaxCount())
{
var http = new SSHttpRequest();
http.Method = reader.GetInt32("method");
http.IsTestPay = reader.GetInt32("isTestPay");
http.Uid = reader.GetInt64("uid");
string orderId = reader.GetString("orderId");
http.OrderId.SetString(orderId);
http.Url.SetString(reader.GetString("url"));
http.Content.SetString(reader.GetString("content"));
res.Http.Add(ref http);
TraceLog.Trace("MySqlDBOperator.SelectPayHttpContent orderId {0}", orderId);
}
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
}
public override void DeletePayHttpContent(ref SSPayHeroDeleteHttpContentNotify notify)
{
string strSql = "delete from tbpay_http where orderId=?orderId";
string orderId = notify.OrderId.GetString();
MySqlParameter p_orderId = new MySqlParameter("?orderId", MySqlDbType.VarChar, 200) {Value = orderId};
TraceLog.Trace("MySqlDBOperator.DeletePayHttpContent exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_orderId);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.DeletePayHttpContent failed, return reader is null!");
return;
}
reader.Close();
TraceLog.Trace("MySqlDBOperator.DeletePayHttpContent orderId {0} success", orderId);
}
public override void QueryRealmUidData(int realmId, ref SSQueryRealmUidDataRes res)
{
TraceLog.Trace("MySqlDBOperator.QueryRealmUidData realmId {0}", realmId);
res.Ret = -1;
res.RealmId = realmId;
string strSql = string.Format("select * from tb_realm_uid_data where realmId={0}", realmId);
TraceLog.Trace("MySqlDBOperator.QueryRealmUidData exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.QueryRealmUidData no record in table, return reader is null!");
return;
}
if (reader.HasRows == false)
{
TraceLog.Trace("MySqlDBOperator.QueryRealmUidData no record in table");
reader.Close();
res.Ret = 0;
return;
}
try
{
reader.Read();
res.Ret = 0;
res.OpenTime = reader.GetInt32("openTime");
res.VisibleTime = reader.GetInt32("visibleTime");
res.PlayerCount = reader.GetInt32("playerCount");
res.IsFull = reader.GetInt32("isFull");
long uidDataLength = 0;
int uidDataIndex = reader.GetOrdinal("uidData");
if (!reader.IsDBNull(uidDataIndex))
{
byte[] buffer = new byte[BuffLengthMax];
uidDataLength = reader.GetBytes(uidDataIndex, 0, buffer, 0, buffer.Length);
byte[] uidDataByte = new byte[uidDataLength];
Buffer.BlockCopy(buffer, 0, uidDataByte, 0, (int)uidDataLength);
StructMessageParseUtils.ParseFrom(ref res.UidData, uidDataByte);
}
else
{
res.UidData = new DBRealmUidData();
}
TraceLog.Trace("MySqlDBOperator.QueryRealmUidData succ, realmId {0} playerCount {1}", realmId, res.PlayerCount);
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
}
public override int SaveRealmUidData(ref SSSaveRealmUidDataReq req)
{
TraceLog.Trace("MySqlDBOperator.SaveRealmUidData realmId {0}", req.RealmId);
string strSql = $"replace into tb_realm_uid_data (realmId, openTime, visibleTime, playerCount, isFull, uidListCount, uidData) values({req.RealmId}, {req.OpenTime}, {req.VisibleTime}, {req.PlayerCount}, {req.IsFull}, {req.UidData.UidList.Count}, ?uidData)";
byte[] uidDataByte = StructMessageParseUtils.ToByteArray(ref req.UidData);
MySqlParameter p_uidData = new MySqlParameter("?uidData", MySqlDbType.Blob) { Value = uidDataByte };
TraceLog.Trace("MySqlDBOperator.SaveRealmUidData exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_uidData);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.SaveRealmUidData no record in table, return reader is null!");
return -1;
}
reader.Close();
TraceLog.Trace("MySqlDBOperator.SaveRealmUidData succ, realmId {0} openTime {1} visibleTime {2} playerCount {3} isFull {4} uidListCount {5}"
, req.RealmId, req.OpenTime, req.VisibleTime, req.PlayerCount, req.IsFull, req.UidData.UidList.Count);
return 0;
}
public override void QueryRealmDBData( ref SSRealmDBQueryRes res)
{
TraceLog.Trace("MySqlDBOperator.QueryRealmDBData All realm");
res.Ret = -1;
string strSql = string.Format("select * from tb_realmcfg");
TraceLog.Trace("MySqlDBOperator.QueryRealmUidData exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.QueryRealmDBData no record in table, return reader is null!");
return;
}
if (reader.HasRows == false)
{
TraceLog.Trace("MySqlDBOperator.QueryRealmDBData no record in table");
reader.Close();
res.Ret = 0;
return;
}
try
{
res.Ret = 0;
int allCount = 0;
while (reader.Read())
{
OneDBRealmConfig realmCfg = new OneDBRealmConfig();
//realmCfg.BusinessName.SetString(reader.GetString("businessName"));
realmCfg.AreaName.SetString(reader.GetString("areaName"));
realmCfg.WorldId = reader.GetInt32("worldId");
realmCfg.RealmId = reader.GetInt32("realmId");
realmCfg.LogicWorldId = reader.GetInt32("logicWorldId");
realmCfg.RealmName.SetString(reader.GetString("realmName"));
realmCfg.ShowSeq = reader.GetInt32("showSeq");
realmCfg.Status = reader.GetInt32("status");
realmCfg.VisibleOnlyWhiteList = reader.GetInt32("visibleOnlyWhiteList");
realmCfg.BdcName.SetString(reader.GetString("bdcName"));
realmCfg.OpenTime.SetString(reader.GetString("openTime"));
realmCfg.VisibleTime.SetString(reader.GetString("visibleTime"));
realmCfg.BigRealmId = reader.GetInt32("bigRealmId");
realmCfg.RegMax = reader.GetInt32("RegMax");
realmCfg.RegNum = reader.GetInt32("RegNum");
res.Configs.Add(realmCfg);
TraceLog.Trace("MySqlDBOperator.QueryRealmDBData query success uid {0}", realmCfg.RealmId);
if(res.Configs.Count == 500)
{
if(allCount == 0)
{
res.IsBegin = 1;
}
DBServerUtils.GetPacketSender().SendToServerByID(res.RemoteAppID, (int)SSMsgID.SsRealmDbqueryRes, ref res, 0);
res.Configs.Clear();
allCount++;
res.IsBegin = 0;
}
}
if(allCount == 0)
{
res.IsBegin = 1;
}
TraceLog.Trace("MySqlDBOperator.QueryRealmDBData succ,res.Configs {0}", res.Configs.Count);
}
catch (Exception ex)
{
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
}
public override int UpdateRealmPlayerNum(int realmId, int regNum)
{
string strSql = " update tb_realmcfg"
+ " set regNum = ?regNum where realmId = ?realmId";
MySqlParameter p_realmId = new MySqlParameter("?realmId", MySqlDbType.Int32) { Value = realmId };
MySqlParameter p_regNum = new MySqlParameter("?regNum", MySqlDbType.Int32) { Value = regNum };
TraceLog.Trace("MySqlDBOperator.UpdateRealmPlayerNum exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_regNum, p_realmId );
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.UpdateRealmPlayerNum no record in table, return reader is null!");
return -1;
}
reader.Close();
return 0;
}
public override int UpdateRealmDBData(ref SSRealmDBSaveReq req,ref SSRealmDBSaveRes res)
{
TraceLog.Trace("MySqlDBOperator.UpdateRealmDBData realmId {0}", req.RealmId);
//string strSql = $"replace into tb_realmcfg (areaName, worldId, realmId, logicWorldId, realmName, showSeq, status, visibleOnlyWhiteList, bdcName, openTime, visibleTime, bigRealmId) " +
// $"values({req.Cfg.AreaName}, {req.Cfg.WorldId}, {req.Cfg.RealmId}, {req.Cfg.LogicWorldId},{req.Cfg.RealmName}, {req.Cfg.ShowSeq}, {req.Cfg.Status}, {req.Cfg.VisibleOnlyWhiteList}, {req.Cfg.BdcName}, '{req.Cfg.OpenTime.ToString()}', '{req.Cfg.VisibleTime.ToString()}', {req.Cfg.BigRealmId})";
res.Ret = -1;
string strSql = " replace into tb_realmcfg"
+ "(areaName, worldId, realmId, logicWorldId, realmName, showSeq, status, visibleOnlyWhiteList, bdcName, openTime, visibleTime, bigRealmId,regMax, regNum)"
+ " values(?areaName, ?worldId, ?realmId, ?logicWorldId, ?realmName, ?showSeq, ?status, ?visibleOnlyWhiteList, ?bdcName, ?openTime, ?visibleTime, ?bigRealmId,?regMax, ?regNum)";
MySqlParameter p_areaName = new MySqlParameter("?areaName", MySqlDbType.String) { Value = req.Cfg.AreaName };
MySqlParameter p_worldId = new MySqlParameter("?worldId", MySqlDbType.Int32) { Value = req.Cfg.WorldId };
MySqlParameter p_realmId = new MySqlParameter("?realmId", MySqlDbType.Int32) { Value = req.Cfg.RealmId };
MySqlParameter p_logicWorldId = new MySqlParameter("?logicWorldId", MySqlDbType.Int32) { Value = req.Cfg.LogicWorldId };
MySqlParameter p_realmName = new MySqlParameter("?realmName", MySqlDbType.String) { Value = req.Cfg.RealmName };
MySqlParameter p_showSeq = new MySqlParameter("?showSeq", MySqlDbType.Int32) { Value = req.Cfg.ShowSeq };
MySqlParameter p_status = new MySqlParameter("?status", MySqlDbType.Int32) { Value = req.Cfg.Status };
MySqlParameter p_visibleOnlyWhiteList = new MySqlParameter("?visibleOnlyWhiteList", MySqlDbType.Int32) { Value = req.Cfg.VisibleOnlyWhiteList };
MySqlParameter p_bdcName = new MySqlParameter("?bdcName", MySqlDbType.String) { Value = req.Cfg.BdcName };
MySqlParameter p_openTime = new MySqlParameter("?openTime", MySqlDbType.String) { Value = req.Cfg.OpenTime };
MySqlParameter p_visibleTime = new MySqlParameter("?visibleTime", MySqlDbType.String) { Value = req.Cfg.VisibleTime };
MySqlParameter p_bigRealmId = new MySqlParameter("?bigRealmId", MySqlDbType.Int32) { Value = req.Cfg.BigRealmId };
MySqlParameter p_RegMax = new MySqlParameter("?RegMax", MySqlDbType.Int32) { Value = req.Cfg.RegMax };
MySqlParameter p_RegNum = new MySqlParameter("?RegNum", MySqlDbType.Int32) { Value = req.Cfg.RegNum };
TraceLog.Trace("MySqlDBOperator.SaveRealmUidData exec sql: {0}", strSql);
MySqlDataReader reader = m_mySqlDb.ExecReader(strSql, p_areaName, p_worldId, p_realmId
, p_logicWorldId, p_realmName, p_showSeq, p_status, p_visibleOnlyWhiteList, p_bdcName, p_openTime, p_visibleTime, p_bigRealmId, p_RegMax, p_RegNum);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.UpdateRealmDBData no record in table, return reader is null!");
return -1;
}
res.Ret = 0;
res.Cfg = req.Cfg;
res.Id = req.Id;
res.IsEnd = req.IsEnd;
res.Notreload = req.Notreload;
reader.Close();
TraceLog.Trace("MySqlDBOperator.SaveRealmUidData succ, realmId {0} openTime {1} visibleTime {2}"
, req.RealmId, req.Cfg.OpenTime, req.Cfg.VisibleTime);
return 0;
}
}
}