api性能优化
This commit is contained in:
parent
f54e6f2dc4
commit
f1850ac37e
@ -2,6 +2,7 @@ using AxibugEmuOnline.Web.Common;
|
|||||||
using AxibugProtobuf;
|
using AxibugProtobuf;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
|
using System.Xml.Linq;
|
||||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||||
|
|
||||||
namespace AxibugEmuOnline.Web.Controllers
|
namespace AxibugEmuOnline.Web.Controllers
|
||||||
@ -60,37 +61,40 @@ namespace AxibugEmuOnline.Web.Controllers
|
|||||||
UID = tokenData.UID;
|
UID = tokenData.UID;
|
||||||
}
|
}
|
||||||
|
|
||||||
string searchPattern = $"%{SearchKey}%";
|
bool bHadSearchKey = !string.IsNullOrEmpty(SearchKey);
|
||||||
|
|
||||||
Resp_GameList resp = new Resp_GameList();
|
Resp_GameList resp = new Resp_GameList();
|
||||||
resp.gameList = new List<Resp_RomInfo>();
|
resp.gameList = new List<Resp_RomInfo>();
|
||||||
MySqlConnection conn = SQLPool.DequeueSQLConn("RomList");
|
MySqlConnection conn = SQLPool.DequeueSQLConn("RomList");
|
||||||
{
|
{
|
||||||
string platformCond = "";
|
List<string> condition = new List<string>();
|
||||||
|
|
||||||
|
if (bHadSearchKey)
|
||||||
|
condition.Add("r.`Name` like ?searchPattern");
|
||||||
|
|
||||||
if (Ptype > (int)RomPlatformType.Invalid && Ptype < (int)RomPlatformType.All)
|
if (Ptype > (int)RomPlatformType.Invalid && Ptype < (int)RomPlatformType.All)
|
||||||
{
|
condition.Add($"r.PlatformType = '{Ptype}'");
|
||||||
platformCond = $" and PlatformType = '{Ptype}' ";
|
|
||||||
}
|
|
||||||
|
|
||||||
GameType SearchGType = (GameType)GType;
|
GameType SearchGType = (GameType)GType;
|
||||||
string GameTypeCond = "";
|
|
||||||
switch (SearchGType)
|
switch (SearchGType)
|
||||||
{
|
{
|
||||||
case GameType.NONE:
|
case GameType.NONE:
|
||||||
GameTypeCond = string.Empty;
|
|
||||||
break;
|
break;
|
||||||
case GameType.ALLINONE:
|
case GameType.ALLINONE:
|
||||||
GameTypeCond = $" and GameType = 'ºÏ¿¨' ";
|
condition.Add($"r.GameType = 'ºÏ¿¨' ");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
GameTypeCond = $" and GameType = '{SearchGType.ToString()}' ";
|
condition.Add($"r.GameType = '{SearchGType.ToString()}' ");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
string query = "SELECT count(id) FROM romlist where `Name` like ?searchPattern " + platformCond + GameTypeCond;
|
string query = "SELECT count(id) FROM romlist r " + GetWhereCmd(condition);
|
||||||
using (var command = new MySqlCommand(query, conn))
|
using (var command = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
// 设置参数值
|
// 设置参数值
|
||||||
command.Parameters.AddWithValue("?searchPattern", searchPattern);
|
if (bHadSearchKey)
|
||||||
|
command.Parameters.AddWithValue("?searchPattern", $"%{SearchKey}%");
|
||||||
|
|
||||||
// 执行查询并处理结果
|
// 执行查询并处理结果
|
||||||
using (var reader = command.ExecuteReader())
|
using (var reader = command.ExecuteReader())
|
||||||
{
|
{
|
||||||
@ -106,14 +110,23 @@ namespace AxibugEmuOnline.Web.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string HotOrderBy = "ORDER BY r.playcount DESC, id ASC";
|
||||||
|
|
||||||
string HotOrderBy = "ORDER BY playcount DESC, id ASC";
|
if (UID > 0)
|
||||||
|
{
|
||||||
|
query = $"SELECT r.id,r.`Name`,r.GameType,r.Note,r.RomUrl,r.ImgUrl,r.`Hash`,r.`playcount`,r.`stars`,r.`PlatformType`,r.`parentids`,IF(s.uid IS NOT NULL, TRUE, FALSE) AS is_collected FROM romlist r left join rom_stars s on r.id = s.romid {GetWhereCmd(condition)} {HotOrderBy} LIMIT ?offset, ?pageSize;";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
query = $"SELECT r.id,r.`Name`,r.GameType,r.Note,r.RomUrl,r.ImgUrl,r.`Hash`,r.`playcount`,r.`stars`,r.`PlatformType`,r.`parentids` FROM romlist r {GetWhereCmd(condition)} {HotOrderBy} LIMIT ?offset, ?pageSize;";
|
||||||
|
}
|
||||||
|
|
||||||
query = $"SELECT id,`Name`,GameType,Note,RomUrl,ImgUrl,`Hash`,`playcount`,`stars`,`PlatformType`,`parentids` FROM romlist where `Name` like ?searchPattern {platformCond} {GameTypeCond} {HotOrderBy} LIMIT ?offset, ?pageSize;";
|
|
||||||
using (var command = new MySqlCommand(query, conn))
|
using (var command = new MySqlCommand(query, conn))
|
||||||
{
|
{
|
||||||
// 设置参数值
|
// 设置参数值
|
||||||
command.Parameters.AddWithValue("?searchPattern", searchPattern);
|
if (bHadSearchKey)
|
||||||
|
command.Parameters.AddWithValue("?searchPattern", $"%{SearchKey}%");
|
||||||
|
|
||||||
command.Parameters.AddWithValue("?offset", Page * PageSize);
|
command.Parameters.AddWithValue("?offset", Page * PageSize);
|
||||||
command.Parameters.AddWithValue("?pageSize", PageSize);
|
command.Parameters.AddWithValue("?pageSize", PageSize);
|
||||||
// 执行查询并处理结果
|
// 执行查询并处理结果
|
||||||
@ -145,10 +158,10 @@ namespace AxibugEmuOnline.Web.Controllers
|
|||||||
data.parentRomIdsList.Add(arr[i]);
|
data.parentRomIdsList.Add(arr[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (UID > 0)
|
if (UID > 0)
|
||||||
{
|
{
|
||||||
if (CheckIsRomStar(data.id, UID))
|
data.isStar = reader.GetInt32(11) > 0 ? 1 : 0;
|
||||||
data.isStar = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resp.gameList.Add(data);
|
resp.gameList.Add(data);
|
||||||
@ -160,6 +173,20 @@ namespace AxibugEmuOnline.Web.Controllers
|
|||||||
return new JsonResult(resp);
|
return new JsonResult(resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string GetWhereCmd(List<string> conditionCmd)
|
||||||
|
{
|
||||||
|
string wherecmd = string.Empty;
|
||||||
|
for (int i = 0; i < conditionCmd.Count; i++)
|
||||||
|
{
|
||||||
|
if (i == 0)
|
||||||
|
wherecmd += " where ";
|
||||||
|
else
|
||||||
|
wherecmd += " and ";
|
||||||
|
|
||||||
|
wherecmd += conditionCmd[i];
|
||||||
|
}
|
||||||
|
return wherecmd;
|
||||||
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public JsonResult MarkList(string SearchKey, int Ptype, int GType, int Page, int PageSize, string Token)
|
public JsonResult MarkList(string SearchKey, int Ptype, int GType, int Page, int PageSize, string Token)
|
||||||
|
Loading…
Reference in New Issue
Block a user