DataReportController.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.Text.Json;
  5. using static molilian.core.ChartDataCore;
  6. using TencentCloud.Soe.V20180724.Models;
  7. using TencentCloud.Mrs.V20200910.Models;
  8. using System.Xml.Linq;
  9. using System.Security.Cryptography;
  10. using TencentCloud.Bi.V20220105.Models;
  11. using static Microsoft.Extensions.Logging.EventSource.LoggingEventSource;
  12. using molilian.core.PushDataReport;
  13. using System.Threading;
  14. using OfficeOpenXml.DataValidation.Exceptions;
  15. namespace molilian.api.Controllers
  16. {
  17. [ApiController]
  18. [MyAuthorize("admin")]
  19. [Route("api/[controller]/[action]")]
  20. public class DataReportController : ControllerBase
  21. {
  22. readonly IAuthorizationProvider provider = new AdminProvider();
  23. protected IHttpContextAccessor _accessor;
  24. public DataReportController(IHttpContextAccessor accessor)
  25. {
  26. _accessor = accessor;
  27. }
  28. [HttpPost]
  29. public ActionResult list([FromBody] JsonElement form)
  30. {
  31. var token = provider.Get(_accessor.HttpContext);
  32. int page = form.Read("current", 1);
  33. int size = form.Read("pageSize", 10);
  34. bool getTotal = form.Read("getTotal", true);
  35. string sort = form.Read<string>("sort");
  36. string order = form.Read<string>("order");
  37. string filter = string.Empty;
  38. // 临时锁定所有口令推送数据
  39. //string filter = "type<>1";
  40. int type = form.Read("type", 0);
  41. if (type > 0) filter += $" AND type = @type";
  42. int status = form.Read("status", -1);
  43. if (status != -1) filter += $" AND status = @status";
  44. int is_settlement = form.Read("is_settlement", -1);
  45. if (is_settlement != -1) filter += $" AND is_settlement = @is_settlement";
  46. int platform = form.Read("platform", 0);
  47. if (platform > 0) filter += $" AND platform = @platform";
  48. var report_date = form.Read<DateTime>("report_date", DateTime.MinValue);
  49. if (report_date != DateTime.MinValue) filter += $" AND report_date = @report_date";
  50. filter = filter.StringTrimStart(" AND ");
  51. string orderBy = "id DESC";
  52. if (!string.IsNullOrEmpty(order))
  53. {
  54. order = "descending".Equals(order) ? "DESC" : "ASC";
  55. orderBy = sort switch
  56. {
  57. _ => $"{sort} {order}",
  58. };
  59. }
  60. var result = new DBContext.Table("push_data_report")
  61. .Where(filter, new { type, report_date, is_settlement, status, platform })
  62. .Page(size, page)
  63. .Order(orderBy)
  64. .PageList<dynamic>(getTotal);
  65. return new APIResult(new { data = result });
  66. }
  67. [HttpPost]
  68. public async Task<ActionResult> push([FromBody] JsonElement form)
  69. {
  70. int id = form.Read<int>("id");
  71. using var conn = DBContext.GetOpenConnection();
  72. var item = new DBContext.Table(conn, "push_data_report").Get<PushDataReportDTO>("id=@id", new { id });
  73. if (item == null) return new APIResult(new { data = new { success = false, msg = "PUSH失败,记录不存在" } });
  74. // 临时锁定所有口令推送数据
  75. //if (item.type == 1) return new APIResult(new { data = new { success = false, msg = "PUSH失败,类型错误" } });
  76. PushDataReportPush plus = new PushDataReportPush();
  77. item.request_id = Guid.NewGuid().ToString();
  78. var response = await plus.PushReportData(item);
  79. bool success = response.code == 200;
  80. if (!success)
  81. {
  82. item.request_id = $"{item.request_id}:{response.message}";
  83. }
  84. new DBContext.Table("push_data_report")
  85. .Add("status", success)
  86. .Add("request_id", item.request_id)
  87. .Add("update_time", DateTime.Now)
  88. .Add("last_push_time", DateTime.Now)
  89. .Where("id=@id", new { id })
  90. .Update();
  91. return new APIResult(new
  92. {
  93. data = new { success, msg = success ? "Push成功" : "Push失败" },
  94. });
  95. }
  96. [HttpPost]
  97. public async Task<ActionResult> delete([FromBody] JsonElement form)
  98. {
  99. int id = form.Read<int>("id");
  100. using var conn = DBContext.GetOpenConnection();
  101. var item = new DBContext.Table(conn, "push_data_report").Get<PushDataReportDTO>("id=@id", new { id });
  102. if (item == null) return new APIResult(new { data = new { success = false, msg = "删除失败,记录不存在" } });
  103. // 临时锁定所有口令推送数据
  104. //if (item.type == 1) return new APIResult(new { data = new { success = false, msg = "删除失败,类型错误" } });
  105. var result = new DBContext.Table("push_data_report").Delete("id=@id", new { id });
  106. bool success = result > 0;
  107. return new APIResult(new
  108. {
  109. data = new { success, msg = success ? "删除成功" : "删除失败" },
  110. });
  111. }
  112. [HttpPost]
  113. public async Task<ActionResult> save([FromBody] JsonElement from)
  114. {
  115. var clientIp = _accessor.HttpContext.GetUserIp();
  116. var token = provider.Get(_accessor.HttpContext);
  117. using var conn = DBContext.GetOpenConnection();
  118. int result = 0;
  119. bool success = false;
  120. try
  121. {
  122. int id = from.Read("id", 0);
  123. var report_date = from.Read<DateTime>("report_date", DateTime.Now).Date;
  124. var platform = from.Read<int>("platform", 0);
  125. var type = from.Read<int>("type", 0);
  126. // 临时锁定所有口令推送数据
  127. //if (type == 1) return new APIResult(new { data = new { success = false, msg = "更新失败,类型错误" } });
  128. var update = new DBContext.Table("push_data_report");
  129. //PushDataReportCore core = new();
  130. //await core.FillCpsCouponData(report_date);
  131. //return new APIResult(new
  132. //{
  133. // data = new { success = false, msg = $"更新失败,test" },
  134. //});
  135. update.Fill(from);
  136. update.Add("report_date", report_date);
  137. update.Loader<bool>("is_settlement");
  138. update.Loader<int>("status");
  139. update.Loader<int>("type");
  140. update.Loader<int>("sub_type");
  141. update.Loader<int>("platform");
  142. update.Loader<string>("platform_name");
  143. update.Loader<string>("adId");
  144. update.Loader<string>("adzoneId");
  145. update.Loader<int>("distribution");
  146. update.Loader<int>("req_count");
  147. update.Loader<int>("dp_transition_succ_count");
  148. update.Loader<int>("transition_succ_count");
  149. update.Loader<int>("expose_count");
  150. update.Loader<int>("expose_user_count");
  151. update.Loader<int>("click_count");
  152. update.Loader<int>("click_user_count");
  153. update.Loader<int>("app_open_count");
  154. update.Loader<int>("app_open_user_count");
  155. update.Loader<int>("order_count");
  156. update.Loader<int>("order_user_count");
  157. update.Loader<decimal>("gmv");
  158. update.Loader<decimal>("income");
  159. if (id == 0)
  160. {
  161. result = update.Create();
  162. success = result > 0;
  163. if (success)
  164. {
  165. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"push_data_report:{report_date}:{platform}:{type}", from.Convert2Json(), "新增");
  166. }
  167. }
  168. else
  169. {
  170. var item = new DBContext.Table(conn, "push_data_report").Get<PushDataReportDTO>("id=@id", new { id });
  171. if (item == null) return new APIResult(new { data = new { success = false, msg = "更新失败,记录不存在" } });
  172. result = update.Where("id=@id", new { id }).Update();
  173. success = result > 0;
  174. if (success)
  175. {
  176. var newItem = new DBContext.Table(conn, "push_data_report").Get<PushDataReportDTO>("id=@id", new { id });
  177. string desc = ObjectComparer.PrintCompareToString(item, newItem).Trim();
  178. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"push_data_report:{report_date}:{platform}:{type}", item.Convert2Json(), desc);
  179. }
  180. }
  181. }
  182. catch (Exception ex)
  183. {
  184. return new APIResult(new
  185. {
  186. data = new { success = false, msg = $"更新失败,{ex.Message}" },
  187. });
  188. }
  189. return new APIResult(new
  190. {
  191. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  192. });
  193. }
  194. }
  195. }