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.
 
 
 
 
 
 

525 lines
21 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;
using LitJson;
using SimpleHttpServer;
using System.Timers;
using PlayerOp;
namespace PlayerOp
{
public class MySqlDBOperator : DBOperator
{
private MySqlDB db;
private const int BuffLengthMax = 500 * 1024;
public MySqlDBOperator(string dbname, string ip, string user, string password)
{
db = new MySqlDB(dbname, ip, user, password);
}
//销毁的时候置空
public override void Dispose()
{
db.Dispose();
db = null;
}
public override void KeepAlive()
{
string strSql = "select Id from mail_box where Id=0";
MySqlDataReader reader = db.ExecReader(strSql);
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);
}
public override bool SaveExchangeCodeRecordData(ref ExchangeRecordConfigure data)
{
string tableName = PlayerOpServerUtils.GetExchangeRecordTableName(data.CodeId);
string strSql = "insert into " + tableName + " set codeId=?codeId,groupId=?groupId,codeIndex=?codeIndex,code=?code,uid=?uid";
MySqlParameter p_codeId = new MySqlParameter("?codeId", MySqlDbType.Int32) { Value = data.CodeId };
MySqlParameter p_codeIndex = new MySqlParameter("?codeIndex", MySqlDbType.Int32) { Value = data.Index };
MySqlParameter p_groupId = new MySqlParameter("?groupId", MySqlDbType.Int32) { Value = data.GroupId };
MySqlParameter p_code = new MySqlParameter("?code", MySqlDbType.String) { Value = data.Code };
MySqlParameter p_uid = new MySqlParameter("?uid", MySqlDbType.Int64) { Value = data.Uid };
MySqlDataReader reader = db.ExecReader(strSql, p_codeId, p_groupId, p_codeIndex, p_code, p_uid);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.SaveExchangeCodeRecordData no record in table, return reader is null");
return false;
}
reader.Close();
return true;
}
public override bool UpdateExchangeCodeRecord(int groupId, int codeId, int codeIndex, long uid, string code)
{
string tableName = PlayerOpServerUtils.GetExchangeRecordTableName(codeId);
string sqlStr = $"update {tableName} set uid={uid} where codeId={codeId} and code = '{code}' and uid=0";
MySqlDataReader reader = db.ExecReader(sqlStr);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.UpdateExchangeCodeRecord no record in table tbexchange_record , return reader is null!");
return false;
}
reader.Close();
return true;
}
public override bool QueryExchangeCodeRecord(int groupId, int codeId, int codeIndex, long uid, string code)
{
string tableName = PlayerOpServerUtils.GetExchangeRecordTableName(codeId);
string strSql = $"select * from {tableName} where codeId = {codeId} and code = '{code}' and uid = 0";
TraceLog.Trace("MySqlDBOperator.QueryExchangeCodeRecord exec sql: {0}", strSql);
MySqlDataReader reader = db.ExecReader(strSql);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.QueryExchangeCodeRecord return reader is null");
return false;
}
if (!reader.HasRows)
{
reader.Close();
return false;
}
reader.Close();
return true;
}
public override bool QueryCommonExchangeCodeRecord(int codeId, long uid)
{
string tableName = PlayerOpServerUtils.CalcCommonExchangeRecordIndex(codeId);
string strSql = $"select * from {tableName} where codeId = {codeId} and uid = {uid}";
MySqlDataReader reader = db.ExecReader(strSql);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.QueryCommonExchangeCodeRecord return reader is null");
return false;
}
if (reader.HasRows)
{
reader.Close();
return false;
}
reader.Close();
return true;
}
public override bool UpdateCommonExchangeCodeRecord(int codeId, long uid)
{
string tableName = PlayerOpServerUtils.CalcCommonExchangeRecordIndex(codeId);
string strSql = "insert into " + tableName + " set codeId=?codeId,uid=?uid";
MySqlParameter p_codeId = new MySqlParameter("?codeId", MySqlDbType.Int32) { Value = codeId };
MySqlParameter p_uid = new MySqlParameter("?uid", MySqlDbType.Int64) { Value = uid };
MySqlDataReader reader = db.ExecReader(strSql, p_codeId, p_uid);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.UpdateCommonExchangeCodeRecord no record in table tbexchange_record , return reader is null!");
return false;
}
reader.Close();
return true;
}
public override bool SelectUserExchangCount(int codeId, long uid, int maxCount)
{
string tableName = PlayerOpServerUtils.GetExchangeRecordTableName(codeId);
string strSql = $"select COUNT(*) as num from {tableName} where codeId = {codeId} and uid = {uid}";
TraceLog.Trace("MySqlDBOperator.SelectUserExchangCount exec sql: {0}", strSql);
MySqlDataReader reader = db.ExecReader(strSql);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.SelectUserExchangCount return reader is null");
return false;
}
while (reader.Read())
{
if(reader.GetInt32("num") >= maxCount)
{
reader.Close();
return false;
}
break;
}
reader.Close();
return true;
}
public override bool QueryExchangeCode(string codeString, out int codeId, out int maxCount,
out int type, ref ExchangeContent exchangeContent, out CSErrCode errCode)
{
codeId = 0;
maxCount = 0;
type = 0;
errCode = CSErrCode.None;
byte[] buffer = new byte[BuffLengthMax];
string preCode = codeString.ToLower().Substring(0, 4);
//// 老的兑换码数据
//if(codeString.Length < 16)
//{
// preCode = codeString.ToLower().Substring(0, 2);
//}
string sqlStr = $"select * from tbexchange_code where status=1 and exchangeCode in ('{codeString}','{preCode}');";
MySqlDataReader reader = db.ExecReader(sqlStr);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.QueryExchangeCode codeString:{0} in table ", codeString);
return false;
}
List<ExchangeConfigure> exchangeList = new List<ExchangeConfigure>();
if (reader.HasRows)
{
try
{
while (reader.Read())
{
ExchangeConfigure temp = new ExchangeConfigure();
temp.Id = reader.GetInt32("exchangeCodeId");
temp.ExchangeCode.SetString(reader.GetString("exchangeCode"));
temp.MaxExchangeNum = reader.GetInt32("maxExchangeNum");
temp.ExchangeNum = reader.GetInt32("exchangeNum");
temp.Type = reader.GetInt32("type");
temp.BeginTime = reader.GetInt64("beginTime");
temp.EndTime = reader.GetInt64("endTime");
temp.Status = reader.GetInt32("status");
int contentIndex = reader.GetOrdinal("content");
long contentLength = reader.GetBytes(contentIndex, 0, buffer, 0, buffer.Length);
byte[] contentByte = new byte[contentLength];
Buffer.BlockCopy(buffer, 0, contentByte, 0, (int)contentLength);
StructMessageParseUtils.ParseFrom(ref temp.Content, contentByte);
exchangeList.Add(temp);
}
}
catch (Exception e)
{
TraceLog.Error("MySqlDBOperator.QueryExchangeCode codeString:{0} error: {1}", codeString, e.Message);
reader.Close();
return false;
}
}
else
{
TraceLog.Error("MySqlDBOperator.QueryExchangeCode codeString:{0} is not exist!", codeString);
reader.Close();
return false;
}
if (reader != null)
reader.Close();
ExchangeConfigure exchangeConfigure = new ExchangeConfigure();
foreach (var item in exchangeList)
{
if (item.ExchangeCode.GetString().ToLower() == codeString.ToLower())
{
exchangeConfigure = item;
}
else if (item.ExchangeCode.GetString().ToLower() == preCode.ToLower() && exchangeConfigure.Id == 0)
{
exchangeConfigure = item;
}
}
codeId = exchangeConfigure.Id;
maxCount = exchangeConfigure.MaxExchangeNum;
type = exchangeConfigure.Type;
long nowTimeSec = PlayerOpServerUtils.GetTimeSecond();
if ((exchangeConfigure.ExchangeNum >= exchangeConfigure.MaxExchangeNum && exchangeConfigure.MaxExchangeNum > 0 && type != 1))
{
TraceLog.Debug("MySqlDBOperator.QueryExchangeCode codeString:{0} ,status != 1 or exchangeCount >= maxCount", codeString);
errCode = CSErrCode.ExchangeCodeBeOver;
return false;
}
if ((nowTimeSec < exchangeConfigure.BeginTime && exchangeConfigure.BeginTime > 0))
{
TraceLog.Debug("MySqlDBOperator.QueryExchangeCode codeString:{0} ,nowTimeSec{1} < beginTime {2}", codeString, nowTimeSec, exchangeConfigure.BeginTime);
errCode = CSErrCode.ExchangeCodeNotStart;
return false;
}
if (nowTimeSec > exchangeConfigure.EndTime && exchangeConfigure.EndTime > 0)
{
TraceLog.Debug("MySqlDBOperator.QueryExchangeCode codeString:{0} ,nowTimeSec{1} > endTime {2}", codeString, nowTimeSec, exchangeConfigure.EndTime);
errCode = CSErrCode.ExchangeCodeExpire;
return false;
}
exchangeContent.CopyFrom(ref exchangeConfigure.Content);
return true;
}
public override bool SaveExchangeCodeData(ref ExchangeConfigure data, out int codeId)
{
TraceLog.Trace("MySqlDBOperator.SaveExchangeCodeData id {0} code {1}", data.Id, data.ExchangeCode);
codeId = 0;
MySqlDataReader reader = null;
bool ret = true;
try
{
string strSql = "insert into tbexchange_code set "
+ "exchangeCode=?exchangeCode,maxExchangeNum=?maxExchangeNum,exchangeNum=?exchangeNum,mainline=?mainline,status=?status,"
+ "beginTime=?beginTime,endTime=?endTime,content=?content,type=?type;select @@last_insert_id exchangeCodeId;";
MySqlParameter p_exchangeCodeId = new MySqlParameter("?exchangeCodeId", MySqlDbType.Int32) { Value = data.Id };
MySqlParameter p_exchangeCode = new MySqlParameter("?exchangeCode", MySqlDbType.String) { Value = data.ExchangeCode.GetString() };
MySqlParameter p_maxExchangeNum = new MySqlParameter("?maxExchangeNum", MySqlDbType.Int32) { Value = data.MaxExchangeNum };
MySqlParameter p_exchangeNum = new MySqlParameter("?exchangeNum", MySqlDbType.Int32) { Value = data.ExchangeNum };
MySqlParameter p_mainline = new MySqlParameter("?mainline", MySqlDbType.Int32) { Value = data.Mainline };
MySqlParameter p_status = new MySqlParameter("?status", MySqlDbType.Int32) { Value = data.Status };
MySqlParameter p_beginTime = new MySqlParameter("?beginTime", MySqlDbType.Int64) { Value = data.BeginTime };
MySqlParameter p_endTime = new MySqlParameter("?endTime", MySqlDbType.Int64) { Value = data.EndTime };
MySqlParameter p_type = new MySqlParameter("?type", MySqlDbType.Int32) { Value = data.Type };
byte[] contentByte = StructMessageParseUtils.ToByteArray(ref data.Content);
MySqlParameter p_content = new MySqlParameter("?content", MySqlDbType.Blob) { Value = contentByte };
reader = db.ExecReader(strSql, p_exchangeCodeId, p_exchangeCode, p_maxExchangeNum
, p_exchangeNum, p_mainline, p_status, p_beginTime, p_endTime, p_content, p_type);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.SaveExchangeCodeData insert return reader is null");
return false;
}
if (reader.HasRows)
{
while(reader.Read())
{
codeId = (int)reader.GetInt32("exchangeCodeId");
data.Id = codeId;
break;
}
}
}
catch (Exception ex)
{
TraceLog.Error("MySqlDBOperator.SaveExchangeCodeData fail, id {0} code {1} - error:{3}", data.Id, data.ExchangeCode, ex.Message);
ret = false;
}
if (reader != null)
{
reader.Close();
}
TraceLog.Trace("MySqlDBOperator.SaveExchangeCodeData succ, id {0} code {1}", data.Id, data.ExchangeCode);
return ret;
}
public override bool UpdateExchangeCodeData(ref ExchangeConfigure data)
{
string strSql = "update tbexchange_code set "
+"maxExchangeNum=?maxExchangeNum,mainline=?mainline,status=?status,"
+ "beginTime=?beginTime,endTime=?endTime,content=?content where exchangeCodeId=?exchangeCodeId";
MySqlParameter p_exchangeCodeId = new MySqlParameter("?exchangeCodeId", MySqlDbType.Int32) { Value = data.Id };
MySqlParameter p_maxExchangeNum = new MySqlParameter("?maxExchangeNum", MySqlDbType.Int32) { Value = data.MaxExchangeNum };
MySqlParameter p_exchangeNum = new MySqlParameter("?exchangeNum", MySqlDbType.Int32) { Value = data.ExchangeNum };
MySqlParameter p_mainline = new MySqlParameter("?mainline", MySqlDbType.Int32) { Value = data.Mainline };
MySqlParameter p_status = new MySqlParameter("?status", MySqlDbType.Int32) { Value = data.Status };
MySqlParameter p_beginTime = new MySqlParameter("?beginTime", MySqlDbType.Int64) { Value = data.BeginTime };
MySqlParameter p_endTime = new MySqlParameter("?endTime", MySqlDbType.Int64) { Value = data.EndTime };
byte[] contentByte = StructMessageParseUtils.ToByteArray(ref data.Content);
MySqlParameter p_content = new MySqlParameter("?content", MySqlDbType.Blob) { Value = contentByte };
MySqlDataReader reader = db.ExecReader(strSql, p_exchangeCodeId, p_maxExchangeNum
, p_exchangeNum, p_mainline, p_status, p_beginTime, p_endTime, p_content);
if (reader == null)
{
TraceLog.Trace("MySqlDBOperator.UpdateExchangeCodeData no record in table, return reader is null");
return false;
}
reader.Close();
TraceLog.Trace("MySqlDBOperator.UpdateExchangeCodeData succ, id {0} code {1}", data.Id, data.ExchangeCode);
return true;
}
public override bool SelectExchangeCode(ref SSExchangeCodeQueryDbRes res)
{
string sqlStr = "select * from tbexchange_code";
// count 最大 100 条,防止出问题
int count = (res.EndIdx - res.BeginIdx) > 100 ? 100 : (res.EndIdx - res.BeginIdx);
if (res.CodeId == 0)
{
sqlStr += $" order by exchangeCodeId limit {res.BeginIdx},{count};";
}
else
{
sqlStr += $" where exchangeCodeId={res.CodeId};";
}
MySqlDataReader reader = db.ExecReader(sqlStr);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.SelectExchangeCode codeString:{0} in table ");
return false;
}
if (reader.HasRows == false) //没有数据也算成功
{
TraceLog.Trace("MySqlDBOperator.SelectExchangeCode reader no rows");
reader.Close();
return true;
}
bool ret = false;
try
{
byte[] buffer = new byte[BuffLengthMax];
while (reader.Read() && res.List.Count < res.List.GetMaxCount())
{
ExchangeConfigure one = new ExchangeConfigure();
one.Id = reader.GetInt32("exchangeCodeId");
one.ExchangeCode.SetString(reader.GetString("exchangeCode"));
one.MaxExchangeNum = reader.GetInt32("maxExchangeNum");
one.ExchangeNum = reader.GetInt32("exchangeNum");
one.Mainline = reader.GetInt32("mainline");
one.Status = reader.GetInt32("status");
one.BeginTime = reader.GetInt64("beginTime");
one.EndTime = reader.GetInt64("endTime");
one.Type = reader.GetInt32("type");
int contentIndex = reader.GetOrdinal("content");
long contentLength = reader.GetBytes(contentIndex, 0, buffer, 0, buffer.Length);
byte[] contentByte = new byte[contentLength];
Buffer.BlockCopy(buffer, 0, contentByte, 0, (int)contentLength);
//contentByte = UnzipData(contentByte);
StructMessageParseUtils.ParseFrom(ref one.Content, contentByte);
res.List.Add(ref one);
}
ret = true;
}
catch (Exception ex)
{
ret = false;
TraceLog.Exception(ex);
}
finally
{
reader.Close();
}
return ret;
}
public override bool SelectExchangCount(out int count)
{
// 默认 500
count = 500;
string strSql = "select COUNT(*) as num from tbexchange_code;";
TraceLog.Trace("MySqlDBOperator.SelectExchangCount exec sql: {0}", strSql);
MySqlDataReader reader = db.ExecReader(strSql);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.SelectExchangCount return reader is null");
return false;
}
try
{
while (reader.Read())
{
count = reader.GetInt32("num");
break;
}
}
catch(Exception e)
{
TraceLog.Error("MySqlDBOperator.SelectExchangCount error:{0} ", e.Message);
if (reader != null)
reader.Close();
return false;
}
if(reader != null)
reader.Close();
return true;
}
public override bool UpdateExchangCount(int codeId, int num)
{
string sqlStr = $"update tbexchange_code set exchangeNum = exchangeNum + {num} where exchangeCodeId={codeId}";
MySqlDataReader reader = db.ExecReader(sqlStr);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.UpdateExchangCount codeString:{0} in table ");
return false;
}
reader.Close();
return true;
}
public override bool UpdateExchangStatus(int codeId, int status)
{
string sqlStr = $"update tbexchange_code set status = {status} where exchangeCodeId={codeId}";
MySqlDataReader reader = db.ExecReader(sqlStr);
if (reader == null)
{
TraceLog.Error("MySqlDBOperator.UpdateExchangStatus codeString:{0} in table ");
return false;
}
reader.Close();
return true;
}
}
}