| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- using molilian.core;
- using dodohold.core;
- using Microsoft.AspNetCore.Mvc;
- using System.Text.Json;
- namespace molilian.api.Controllers
- {
- [ApiController]
- [MyAuthorize("admin")]
- [Route("api/[controller]/[action]")]
- public class TaobaoController : ControllerBase
- {
- readonly IAuthorizationProvider provider = new AdminProvider();
- protected IHttpContextAccessor _accessor;
- public TaobaoController(IHttpContextAccessor accessor)
- {
- _accessor = accessor;
- }
- [HttpPost]
- public async Task<ActionResult> updateCookies([FromBody] JsonElement form)
- {
- string cookie = form.Read<string>("cookie", string.Empty);
- string user_agent = _accessor.HttpContext.Request.UserAgent();
- var success = TkPoolCore.UpdateCookies(cookie, user_agent);
- return new APIResult(new
- {
- data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入的cookie" },
- });
- }
- [HttpPost]
- public ActionResult list([FromBody] JsonElement form)
- {
- var token = provider.Get(_accessor.HttpContext);
- int page = form.Read("current", 1);
- int size = form.Read("pageSize", 10);
- string keyword = form.Read("name", string.Empty);
- bool getTotal = form.Read("getTotal", true);
- string _status = form.Read("status", string.Empty);
- int status = _status switch
- {
- "online" => 1,
- "offline" => 0,
- _ => -1,
- };
- var lastTime = form.PathReadArray<string>("lastTime[]");
- string sort = form.Read<string>("sort");
- string order = form.Read<string>("order");
- string filter = string.Empty;
- if (!string.IsNullOrEmpty(keyword))
- {
- filter += $" AND (`name` LIKE @keyword OR `description` LIKE @keyword)";
- keyword = $"%{keyword}%";
- }
- DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
- if (lastTime.Count == 2)
- {
- if (DateTime.TryParse(lastTime[0], out stime) && DateTime.TryParse(lastTime[1], out etime))
- {
- etime = etime.AddDays(1).AddSeconds(-1);
- filter += $" AND last_time BETWEEN @stime AND @etime";
- }
- }
- if (status != -1)
- {
- filter += $" AND status=@status";
- }
- filter = filter.StringTrimStart(" AND ");
- //排序
- string orderBy = "id DESC";
- if (!string.IsNullOrEmpty(order))
- {
- order = "descending".Equals(order) ? "DESC" : "ASC";
- orderBy = sort switch
- {
- //"num" => $"num {order}",
- _ => $"{sort} {order}",
- };
- }
- using var conn = DBContext.GetOpenConnection();
- var result = new DBContext.Table(conn, "tk_pool")
- .Where(filter, new { keyword, status, stime, etime })
- .Page(size, page)
- .Order(orderBy)
- .PageList<TkPoolDTO>(getTotal);
- foreach (var item in result.List)
- {
- item.current_amt = Math.Round(TkPoolCore.GetIncomeAmt(item.name), 2);
- }
- return new APIResult(new { data = result });
- }
- [HttpPost]
- public ActionResult report_dailys([FromBody] JsonElement form)
- {
- int page = form.Read("current", 1);
- int size = form.Read("pageSize", 10);
- bool getTotal = form.Read("getTotal", true);
- string sort = form.Read<string>("sort");
- string order = form.Read<string>("order");
- var report_date = form.PathReadArray<string>("query_date[]");
- var accountName = form.Read("accountName", string.Empty);
- string dailys_accountName = "all";
- string filter = string.Empty;
- if (!string.IsNullOrEmpty(accountName))
- {
- filter += $" AND A.accountName=@accountName";
- }
- DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
- if (report_date.Count == 2)
- {
- if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
- {
- etime = etime.AddDays(1).AddSeconds(-1);
- filter += $" AND A.report_date BETWEEN @stime AND @etime";
- }
- }
- filter = filter.StringTrimStart(" AND ");
- //排序
- string orderBy = "A.report_date DESC";
- if (!string.IsNullOrEmpty(order))
- {
- order = "descending".Equals(order) ? "DESC" : "ASC";
- orderBy = sort switch
- {
- _ => $"A.{sort} {order}",
- };
- }
- var result = new DBContext.Table("v_tk_report_dailys")
- .LeftJoin("daily_logs",
- "B.log_date = A.report_date AND B.accountName=@dailys_accountName",
- "total_count, B.abandon_count, B.success_count, B.abandon_percentage, B.success_percentage")
- .Where(filter, new { accountName, dailys_accountName, stime, etime })
- .Page(size, page)
- .Order(orderBy)
- .PageList<TkReportDTO>(getTotal);
- foreach (var item in result.List)
- {
- if (item.total_count > 0) continue;
- item.total_count = RedisHelper.Get<int>($":total:all:{item.report_date:yyyyMMdd}");
- if (item.total_count == 0) continue;
- item.success_count = RedisHelper.Get<int>($":total:all:success:{item.report_date:yyyyMMdd}");
- item.abandon_count = RedisHelper.Get<int>($":total:all:放弃转链:{item.report_date:yyyyMMdd}");
- if (item.total_count > 0)
- {
- item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
- item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
- }
- }
- return new APIResult(new { data = result });
- }
- [HttpPost]
- public ActionResult report_list([FromBody] JsonElement form)
- {
- int page = form.Read("current", 1);
- int size = form.Read("pageSize", 10);
- bool getTotal = form.Read("getTotal", true);
- string sort = form.Read<string>("sort");
- string order = form.Read<string>("order");
- var report_date = form.PathReadArray<string>("query_date[]");
- var accountName = form.Read("accountName", string.Empty);
- string filter = string.Empty;
- if (!string.IsNullOrEmpty(accountName))
- {
- filter += $" AND A.accountName=@accountName";
- }
- DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
- if (report_date.Count == 2)
- {
- if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
- {
- etime = etime.AddDays(1).AddSeconds(-1);
- filter += $" AND A.report_date BETWEEN @stime AND @etime";
- }
- }
- filter = filter.StringTrimStart(" AND ");
- //排序
- string orderBy = "A.id DESC";
- if (!string.IsNullOrEmpty(order))
- {
- order = "descending".Equals(order) ? "DESC" : "ASC";
- orderBy = sort switch
- {
- _ => $"A.{sort} {order}",
- };
- }
- var result = new DBContext.Table("tk_report")
- .LeftJoin("daily_logs",
- "B.log_date = A.report_date AND B.accountName=A.accountName",
- "total_count, B.abandon_count, B.success_count, B.abandon_percentage, B.success_percentage")
- .Where(filter, new { accountName, stime, etime })
- .Page(size, page)
- .Order(orderBy)
- .PageList<TkReportDTO>(getTotal);
- foreach (var item in result.List)
- {
- if (item.total_count > 0) continue;
- item.total_count = RedisHelper.Get<int>($":total:{item.accountName}:{item.report_date:yyyyMMdd}");
- if (item.total_count == 0) continue;
- item.success_count = RedisHelper.Get<int>($":total:{item.accountName}:success:{item.report_date:yyyyMMdd}");
- item.abandon_count = RedisHelper.Get<int>($":total:{item.accountName}:放弃转链:{item.report_date:yyyyMMdd}");
- if (item.total_count > 0)
- {
- item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
- item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
- }
- }
- //public int api_req_num { get; set; } = 0;
- //public int abandon_num { get; set; } = 0;
- //public int success_num { get; set; } = 0;
- return new APIResult(new { data = result });
- }
- [HttpPost]
- public ActionResult report_hourtrend([FromBody] JsonElement form)
- {
- int page = form.Read("current", 1);
- int size = form.Read("pageSize", 10);
- bool getTotal = form.Read("getTotal", true);
- string sort = form.Read<string>("sort");
- string order = form.Read<string>("order");
- var report_date = form.PathReadArray<string>("query_date[]");
- var accountName = form.Read("accountName", string.Empty);
- string filter = string.Empty;
- if (!string.IsNullOrEmpty(accountName))
- {
- filter += $" AND accountName=@accountName";
- }
- DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
- if (report_date.Count == 2)
- {
- if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
- {
- etime = etime.AddDays(1).AddSeconds(-1);
- if (etime > DateTime.Now) etime = DateTime.Now;
- filter += $" AND report_date BETWEEN @stime AND @etime";
- }
- }
- filter = filter.StringTrimStart(" AND ");
- //排序
- string orderBy = "id ASC";
- if (!string.IsNullOrEmpty(order))
- {
- order = "descending".Equals(order) ? "DESC" : "ASC";
- orderBy = sort switch
- {
- _ => $"{sort} {order}",
- };
- }
- using var conn = DBContext.GetOpenConnection();
- var result = new DBContext.Table(conn, "tk_report_hourtrend")
- .Where(filter, new { accountName, stime, etime })
- .Page(size, page)
- .Order(orderBy)
- .PageList<TkReportDTO>(getTotal);
- foreach (var item in result.List)
- {
- if (item.total_count > 0) continue;
- item.total_count = RedisHelper.Get<int>($":total:{item.accountName}:{item.report_date:yyyyMMddHH}");
- if (item.total_count == 0) continue;
- item.success_count = RedisHelper.Get<int>($":total:{item.accountName}:success:{item.report_date:yyyyMMddHH}");
- item.abandon_count = RedisHelper.Get<int>($":total:{item.accountName}:放弃转链:{item.report_date:yyyyMMddHH}");
- if (item.total_count > 0)
- {
- item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
- item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
- }
- }
- return new APIResult(new { data = result });
- }
- [HttpPost]
- public ActionResult chartData([FromBody] JsonElement form)
- {
- var result = new List<dynamic>();
- var reason_result = new List<dynamic>();
- var report_date = form.PathReadArray<string>("query_date[]");
- var accountName = form.Read("accountName", string.Empty);
- var isHourtrend = form.Read<bool>("isLeaf", false);
- if (string.IsNullOrEmpty(accountName)) accountName = "all";
- DateTime stime = DateTime.MinValue;
- if (!DateTime.TryParse(report_date[0], out stime)) return new APIResult(new { data = result, data2 = reason_result });
- string _report_date = stime.ToString("yyyyMMdd");
- if (isHourtrend) _report_date = stime.ToString("yyyyMMddHH");
- string cacheKey = $":total:{accountName}:{_report_date}";
- double total = RedisHelper.Get<int>(cacheKey);
- if (total > 0)
- {
- string[] keys = new string[] { "success" };
- cacheKey = $":total:{accountName}:message:{_report_date}";
- string[] message_keys = RedisHelper.SMembers(cacheKey);
- string[] combinedKeys = keys.Union(message_keys).ToArray();
- foreach (var keyname in combinedKeys)
- {
- cacheKey = $":total:{accountName}:{keyname}:{_report_date}";
- int value = RedisHelper.Get<int>(cacheKey);
- if (value == 0) continue;
- result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
- }
- result = result.OrderByDescending(item => item.value).ToList();
- cacheKey = $":total:{accountName}:reason:{_report_date}";
- string[] reason_keys = RedisHelper.SMembers(cacheKey);
- foreach (var keyname in reason_keys)
- {
- cacheKey = $":total:{accountName}:{keyname}:{_report_date}";
- int value = RedisHelper.Get<int>(cacheKey);
- if (value == 0) continue;
- reason_result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
- }
- reason_result = reason_result.OrderByDescending(item => item.value).ToList();
- }
- return new APIResult(new { total, data = result, data2 = reason_result });
- //RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMM}");
- //RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMdd}");
- //RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMddHH}");
- //string result = success ? "success" : "fail";
- //RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
- //RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
- //RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
- //if (!string.IsNullOrEmpty(message))
- //{
- // RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
- // RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
- // RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
- // RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
- // RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
- //}
- //if (!string.IsNullOrEmpty(reason))
- //{
- // RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
- // RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
- // RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
- // RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
- // RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
- //}
- }
- }
- }
|