|
|
@@ -1,6 +1,7 @@
|
|
|
using System.Text.Json;
|
|
|
using System.Threading.Tasks;
|
|
|
using CSRedis;
|
|
|
+using Dapper;
|
|
|
using dodohold.core;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
@@ -13,6 +14,9 @@ namespace molilian.api.Controllers
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
public class TracksAdminController : ControllerBase
|
|
|
{
|
|
|
+ private const string PddTrackWhere = "(LOWER(r.platform)='pdd' OR r.platform='拼多多' OR r.track_link_id IN (21,22) OR r.unique_id LIKE '9|%')";
|
|
|
+ private const string VisiblePddAccountWhere = "NOT EXISTS (SELECT 1 FROM pdd_pool hp WHERE hp.id=r.account_id AND hp.is_hide=1)";
|
|
|
+
|
|
|
readonly IAuthorizationProvider provider = new AdminProvider();
|
|
|
protected IHttpContextAccessor _accessor;
|
|
|
|
|
|
@@ -81,29 +85,131 @@ namespace molilian.api.Controllers
|
|
|
DateTime? start = form.Read<DateTime?>("start", null);
|
|
|
DateTime? end = form.Read<DateTime?>("end", null);
|
|
|
|
|
|
- string filter = string.Empty;
|
|
|
- if (linkId > 0) filter += " AND track_link_id=@linkId";
|
|
|
- if (accountBreakdownOnly) filter += " AND account_id>0";
|
|
|
- else if (accountId >= 0) filter += " AND account_id=@accountId";
|
|
|
- else if (!includeAccountBreakdown) filter += " AND account_id=0";
|
|
|
- if (!string.IsNullOrWhiteSpace(eventType)) filter += " AND event_type=@eventType";
|
|
|
- if (!string.IsNullOrWhiteSpace(platform)) filter += " AND platform=@platform";
|
|
|
- if (!string.IsNullOrWhiteSpace(typename)) filter += " AND typename=@typename";
|
|
|
- if (!string.IsNullOrWhiteSpace(scene)) filter += " AND scene=@scene";
|
|
|
- if (!string.IsNullOrWhiteSpace(uniqueId)) filter += " AND unique_id=@uniqueId";
|
|
|
- if (start.HasValue) filter += " AND report_date>=@start";
|
|
|
- if (end.HasValue) filter += " AND report_date<=@end";
|
|
|
- filter = filter.StringTrimStart(" AND ");
|
|
|
+ page = Math.Max(page, 1);
|
|
|
+ size = Math.Max(size, 1);
|
|
|
|
|
|
- string orderBy = "report_date DESC";
|
|
|
+ var parameters = new DynamicParameters(new
|
|
|
+ {
|
|
|
+ linkId,
|
|
|
+ accountId,
|
|
|
+ eventType,
|
|
|
+ platform,
|
|
|
+ typename,
|
|
|
+ scene,
|
|
|
+ uniqueId,
|
|
|
+ start,
|
|
|
+ end,
|
|
|
+ offset = (page - 1) * size,
|
|
|
+ size
|
|
|
+ });
|
|
|
|
|
|
- var result = new DBContext.Table("track_daily_report")
|
|
|
- .Where(filter, new { linkId, accountId, eventType, platform, typename, scene, uniqueId, start, end })
|
|
|
- .Page(size, page)
|
|
|
- .Order(orderBy)
|
|
|
- .PageList<TrackDailyReportDTO>(getTotal);
|
|
|
+ var baseWhere = new List<string>();
|
|
|
+ if (linkId > 0) baseWhere.Add("r.track_link_id=@linkId");
|
|
|
+ if (!string.IsNullOrWhiteSpace(eventType)) baseWhere.Add("r.event_type=@eventType");
|
|
|
+ if (!string.IsNullOrWhiteSpace(platform)) baseWhere.Add("r.platform=@platform");
|
|
|
+ if (!string.IsNullOrWhiteSpace(typename)) baseWhere.Add("r.typename=@typename");
|
|
|
+ if (!string.IsNullOrWhiteSpace(scene)) baseWhere.Add("r.scene=@scene");
|
|
|
+ if (!string.IsNullOrWhiteSpace(uniqueId)) baseWhere.Add("r.unique_id=@uniqueId");
|
|
|
+ if (start.HasValue) baseWhere.Add("r.report_date>=@start");
|
|
|
+ if (end.HasValue) baseWhere.Add("r.report_date<=@end");
|
|
|
|
|
|
- return new APIResult(new { data = result });
|
|
|
+ string unionSql = BuildTrackReportSql(baseWhere, accountId, includeAccountBreakdown, accountBreakdownOnly);
|
|
|
+ string dataSql = $@"
|
|
|
+SELECT *
|
|
|
+FROM ({unionSql}) t
|
|
|
+ORDER BY t.report_date DESC, t.id DESC
|
|
|
+LIMIT @offset,@size";
|
|
|
+
|
|
|
+ using var conn = DBContext.GetOpenConnection();
|
|
|
+ var list = SqlMapper.Query<TrackDailyReportDTO>(conn, dataSql, parameters).ToList();
|
|
|
+ int count = list.Count;
|
|
|
+ if (getTotal)
|
|
|
+ {
|
|
|
+ count = conn.ExecuteScalar<int>($"SELECT COUNT(1) FROM ({unionSql}) t", parameters);
|
|
|
+ }
|
|
|
+
|
|
|
+ return new APIResult(new { data = new { list, count } });
|
|
|
+ }
|
|
|
+
|
|
|
+ private static string BuildTrackReportSql(List<string> baseWhere, int accountId, bool includeAccountBreakdown, bool accountBreakdownOnly)
|
|
|
+ {
|
|
|
+ var selects = new List<string>();
|
|
|
+
|
|
|
+ if (accountBreakdownOnly)
|
|
|
+ {
|
|
|
+ selects.Add(BuildStoredReportSelect([.. baseWhere, "r.account_id>0", $"(NOT {PddTrackWhere} OR {VisiblePddAccountWhere})"]));
|
|
|
+ }
|
|
|
+ else if (accountId > 0)
|
|
|
+ {
|
|
|
+ selects.Add(BuildStoredReportSelect([.. baseWhere, "r.account_id=@accountId", $"(NOT {PddTrackWhere} OR {VisiblePddAccountWhere})"]));
|
|
|
+ }
|
|
|
+ else if (accountId == 0 || !includeAccountBreakdown)
|
|
|
+ {
|
|
|
+ selects.Add(BuildStoredReportSelect([.. baseWhere, "r.account_id=0", $"NOT {PddTrackWhere}"]));
|
|
|
+ selects.Add(BuildPddTotalReportSelect([.. baseWhere, "r.account_id>0", PddTrackWhere, VisiblePddAccountWhere]));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ selects.Add(BuildStoredReportSelect([.. baseWhere, $"NOT {PddTrackWhere}"]));
|
|
|
+ selects.Add(BuildStoredReportSelect([.. baseWhere, "r.account_id>0", PddTrackWhere, VisiblePddAccountWhere]));
|
|
|
+ selects.Add(BuildPddTotalReportSelect([.. baseWhere, "r.account_id>0", PddTrackWhere, VisiblePddAccountWhere]));
|
|
|
+ }
|
|
|
+
|
|
|
+ return string.Join("\nUNION ALL\n", selects);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static string BuildStoredReportSelect(List<string> where)
|
|
|
+ {
|
|
|
+ return $@"
|
|
|
+SELECT
|
|
|
+ r.id,
|
|
|
+ r.report_date,
|
|
|
+ r.event_type,
|
|
|
+ r.platform,
|
|
|
+ r.typename,
|
|
|
+ r.scene,
|
|
|
+ r.unique_id,
|
|
|
+ r.track_link_id,
|
|
|
+ r.account_id,
|
|
|
+ r.event_count,
|
|
|
+ r.create_time,
|
|
|
+ r.update_time
|
|
|
+FROM track_daily_report r
|
|
|
+{BuildWhere(where)}";
|
|
|
+ }
|
|
|
+
|
|
|
+ private static string BuildPddTotalReportSelect(List<string> where)
|
|
|
+ {
|
|
|
+ return $@"
|
|
|
+SELECT
|
|
|
+ MIN(r.id) AS id,
|
|
|
+ r.report_date,
|
|
|
+ r.event_type,
|
|
|
+ r.platform,
|
|
|
+ r.typename,
|
|
|
+ r.scene,
|
|
|
+ r.unique_id,
|
|
|
+ r.track_link_id,
|
|
|
+ 0 AS account_id,
|
|
|
+ CAST(SUM(r.event_count) AS SIGNED) AS event_count,
|
|
|
+ MIN(r.create_time) AS create_time,
|
|
|
+ MAX(r.update_time) AS update_time
|
|
|
+FROM track_daily_report r
|
|
|
+{BuildWhere(where)}
|
|
|
+GROUP BY
|
|
|
+ r.report_date,
|
|
|
+ r.event_type,
|
|
|
+ r.platform,
|
|
|
+ r.typename,
|
|
|
+ r.scene,
|
|
|
+ r.unique_id,
|
|
|
+ r.track_link_id
|
|
|
+HAVING SUM(r.event_count)>0";
|
|
|
+ }
|
|
|
+
|
|
|
+ private static string BuildWhere(List<string> where)
|
|
|
+ {
|
|
|
+ return where.Count == 0 ? string.Empty : $"WHERE {string.Join(" AND ", where)}";
|
|
|
}
|
|
|
|
|
|
[HttpPost]
|