| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.AspNetCore.Mvc.Rendering; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | using Newtonsoft.Json; |
| | | 6 | | using OServices.FiltersServices.FilterModels; |
| | | 7 | | using OzonDomains; |
| | | 8 | | using OzonDomains.Models; |
| | | 9 | | using OzonOrdersWeb.Areas.Studio2.ViewModels.TransactionJobs; |
| | | 10 | | using OzonOrdersWeb.WebServices.TransactionJobs; |
| | | 11 | | using OzonRepositories.Context; |
| | | 12 | | using Services.BackgroundJobsServices; |
| | | 13 | | using Services.DataServices; |
| | | 14 | | |
| | | 15 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers; |
| | | 16 | | |
| | | 17 | | [Authorize(Roles = "Admin")] |
| | | 18 | | [Area("Studio2")] |
| | | 19 | | [Route("Studio2/TransactionJobs")] |
| | | 20 | | public class TransactionJobsController : Controller |
| | | 21 | | { |
| | | 22 | | private readonly IBackgroundJobScheduleService _scheduleService; |
| | | 23 | | private readonly IBackgroundJobLoggingService _loggingService; |
| | | 24 | | private readonly OzonClientServices _ozonClientServices; |
| | | 25 | | private readonly ApplicationDbContext _context; |
| | | 26 | | |
| | 0 | 27 | | public TransactionJobsController( |
| | 0 | 28 | | IBackgroundJobScheduleService scheduleService, |
| | 0 | 29 | | IBackgroundJobLoggingService loggingService, |
| | 0 | 30 | | OzonClientServices ozonClientServices, |
| | 0 | 31 | | ApplicationDbContext context) |
| | | 32 | | { |
| | 0 | 33 | | _scheduleService = scheduleService; |
| | 0 | 34 | | _loggingService = loggingService; |
| | 0 | 35 | | _ozonClientServices = ozonClientServices; |
| | 0 | 36 | | _context = context; |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | [HttpGet("")] |
| | | 40 | | public async Task<IActionResult> Index() |
| | | 41 | | { |
| | 0 | 42 | | var schedules = await _scheduleService.GetAllSchedulesAsync(); |
| | 0 | 43 | | return View(schedules); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | [HttpGet("Create")] |
| | | 47 | | public async Task<IActionResult> Create() |
| | | 48 | | { |
| | 0 | 49 | | await PopulateViewBagAsync(); |
| | 0 | 50 | | return View(); |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | [HttpPost("Create")] |
| | | 54 | | [ValidateAntiForgeryToken] |
| | | 55 | | public async Task<IActionResult> Create(BackgroundJobSchedule schedule) |
| | | 56 | | { |
| | 0 | 57 | | schedule.DaysBack = ReportDeliveryDateFilter.TransactionJobDaysBack; |
| | | 58 | | |
| | 0 | 59 | | if (!ModelState.IsValid) |
| | | 60 | | { |
| | 0 | 61 | | await PopulateViewBagAsync(); |
| | 0 | 62 | | return View(schedule); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | try |
| | | 66 | | { |
| | 0 | 67 | | await _scheduleService.CreateScheduleAsync(schedule); |
| | 0 | 68 | | TempData["Success"] = "Расписание успешно создано"; |
| | 0 | 69 | | return RedirectToAction(nameof(Index)); |
| | | 70 | | } |
| | 0 | 71 | | catch (Exception ex) |
| | | 72 | | { |
| | 0 | 73 | | ModelState.AddModelError("", ex.Message); |
| | 0 | 74 | | await PopulateViewBagAsync(); |
| | 0 | 75 | | return View(schedule); |
| | | 76 | | } |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | [HttpGet("Edit/{id}")] |
| | | 80 | | public async Task<IActionResult> Edit(int id) |
| | | 81 | | { |
| | 0 | 82 | | var schedule = await _scheduleService.GetScheduleByIdAsync(id); |
| | 0 | 83 | | if (schedule == null) |
| | | 84 | | { |
| | 0 | 85 | | TempData["Error"] = "Расписание не найдено"; |
| | 0 | 86 | | return RedirectToAction(nameof(Index)); |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | await PopulateViewBagAsync(); |
| | 0 | 90 | | return View(schedule); |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | [HttpPost("Edit/{id}")] |
| | | 94 | | [ValidateAntiForgeryToken] |
| | | 95 | | public async Task<IActionResult> Edit(int id, BackgroundJobSchedule schedule) |
| | | 96 | | { |
| | 0 | 97 | | if (id != schedule.Id) |
| | | 98 | | { |
| | 0 | 99 | | TempData["Error"] = "Неверный ID расписания"; |
| | 0 | 100 | | return RedirectToAction(nameof(Index)); |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | schedule.DaysBack = ReportDeliveryDateFilter.TransactionJobDaysBack; |
| | | 104 | | |
| | 0 | 105 | | if (!ModelState.IsValid) |
| | | 106 | | { |
| | 0 | 107 | | await PopulateViewBagAsync(); |
| | 0 | 108 | | return View(schedule); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | try |
| | | 112 | | { |
| | 0 | 113 | | await _scheduleService.UpdateScheduleAsync(schedule); |
| | 0 | 114 | | TempData["Success"] = "Расписание успешно обновлено"; |
| | 0 | 115 | | return RedirectToAction(nameof(Index)); |
| | | 116 | | } |
| | 0 | 117 | | catch (Exception ex) |
| | | 118 | | { |
| | 0 | 119 | | ModelState.AddModelError("", ex.Message); |
| | 0 | 120 | | await PopulateViewBagAsync(); |
| | 0 | 121 | | return View(schedule); |
| | | 122 | | } |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | [HttpPost("Delete/{id}")] |
| | | 126 | | [ValidateAntiForgeryToken] |
| | | 127 | | public async Task<IActionResult> Delete(int id) |
| | | 128 | | { |
| | | 129 | | try |
| | | 130 | | { |
| | 0 | 131 | | await _scheduleService.DeleteScheduleAsync(id); |
| | 0 | 132 | | TempData["Success"] = "Расписание успешно удалено"; |
| | 0 | 133 | | } |
| | 0 | 134 | | catch (Exception ex) |
| | | 135 | | { |
| | 0 | 136 | | TempData["Error"] = $"Ошибка удаления: {ex.Message}"; |
| | 0 | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | return RedirectToAction(nameof(Index)); |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | [HttpGet("Executions")] |
| | | 143 | | public IActionResult Executions( |
| | | 144 | | int? scheduleId = null, |
| | | 145 | | string? status = null, |
| | | 146 | | string? triggerType = null, |
| | | 147 | | DateTime? from = null, |
| | | 148 | | DateTime? to = null, |
| | | 149 | | int page = 1, |
| | | 150 | | int pageSize = 50) => |
| | 0 | 151 | | RedirectToAction("Index", "JobLogs", new |
| | 0 | 152 | | { |
| | 0 | 153 | | area = "Studio2", |
| | 0 | 154 | | tab = "transactions", |
| | 0 | 155 | | scheduleId, |
| | 0 | 156 | | status, |
| | 0 | 157 | | triggerType, |
| | 0 | 158 | | from = from?.ToString("yyyy-MM-dd"), |
| | 0 | 159 | | to = to?.ToString("yyyy-MM-dd"), |
| | 0 | 160 | | page, |
| | 0 | 161 | | pageSize |
| | 0 | 162 | | }); |
| | | 163 | | |
| | | 164 | | [HttpGet("ExecutionDetails/{id}")] |
| | | 165 | | public async Task<IActionResult> ExecutionDetails(long id) |
| | | 166 | | { |
| | 0 | 167 | | var execution = await _context.BackgroundJobExecutions |
| | 0 | 168 | | .AsNoTracking() |
| | 0 | 169 | | .Include(e => e.Schedule) |
| | 0 | 170 | | .FirstOrDefaultAsync(e => e.Id == id); |
| | | 171 | | |
| | 0 | 172 | | if (execution == null) |
| | | 173 | | { |
| | 0 | 174 | | TempData["Error"] = "Запуск не найден"; |
| | 0 | 175 | | return RedirectToAction(nameof(Executions)); |
| | | 176 | | } |
| | | 177 | | |
| | 0 | 178 | | var steps = await _context.BackgroundJobSteps |
| | 0 | 179 | | .AsNoTracking() |
| | 0 | 180 | | .Where(s => s.ExecutionId == id) |
| | 0 | 181 | | .OrderBy(s => s.StepNumber) |
| | 0 | 182 | | .ToListAsync(); |
| | | 183 | | |
| | 0 | 184 | | var reportLoads = await _context.BackgroundReportLoads |
| | 0 | 185 | | .AsNoTracking() |
| | 0 | 186 | | .Include(r => r.OzonClient) |
| | 0 | 187 | | .Where(r => r.ExecutionId == id) |
| | 0 | 188 | | .OrderByDescending(r => r.CreatedAt) |
| | 0 | 189 | | .ToListAsync(); |
| | | 190 | | |
| | 0 | 191 | | var transactionResults = await _context.BackgroundTransactionResults |
| | 0 | 192 | | .AsNoTracking() |
| | 0 | 193 | | .Include(r => r.Order) |
| | 0 | 194 | | .Where(r => r.ExecutionId == id) |
| | 0 | 195 | | .OrderByDescending(r => r.CreatedAt) |
| | 0 | 196 | | .ToListAsync(); |
| | | 197 | | |
| | 0 | 198 | | return View(new BackgroundJobExecutionDetailsViewModel |
| | 0 | 199 | | { |
| | 0 | 200 | | Execution = execution, |
| | 0 | 201 | | Schedule = execution.Schedule, |
| | 0 | 202 | | Grid = ExecutionDetailsGridRows.Build(execution, execution.Schedule, steps, reportLoads, transactionResults) |
| | 0 | 203 | | }); |
| | 0 | 204 | | } |
| | | 205 | | |
| | | 206 | | [HttpGet("ExecutionDetails/{id}/Orders")] |
| | | 207 | | public async Task<IActionResult> ExecutionOrders(long id) |
| | | 208 | | { |
| | 0 | 209 | | var shipmentNumbers = await _context.BackgroundTransactionResults |
| | 0 | 210 | | .AsNoTracking() |
| | 0 | 211 | | .Where(r => r.ExecutionId == id) |
| | 0 | 212 | | .Select(r => r.Order != null ? r.Order.ShipmentNumber : null) |
| | 0 | 213 | | .Where(n => !string.IsNullOrWhiteSpace(n)) |
| | 0 | 214 | | .Select(n => n!.Trim()) |
| | 0 | 215 | | .Distinct() |
| | 0 | 216 | | .ToListAsync(); |
| | | 217 | | |
| | 0 | 218 | | var filterData = new OrderFilterModel |
| | 0 | 219 | | { |
| | 0 | 220 | | ShipmentNumber = shipmentNumbers.Any() ? string.Join(",", shipmentNumbers) : null, |
| | 0 | 221 | | GroupByShipment = true, |
| | 0 | 222 | | IncludeArchived = false, |
| | 0 | 223 | | SyncClientStatusesOnLoad = false, |
| | 0 | 224 | | OnlyWithZasyl = false |
| | 0 | 225 | | }; |
| | | 226 | | |
| | 0 | 227 | | Response.Cookies.Append("FilterData", JsonConvert.SerializeObject(filterData)); |
| | | 228 | | |
| | 0 | 229 | | return RedirectToAction("Index", "Orders", new { area = "Studio2" }); |
| | 0 | 230 | | } |
| | | 231 | | |
| | | 232 | | private async Task PopulateViewBagAsync() |
| | | 233 | | { |
| | | 234 | | // Только клиенты с типом Ozon |
| | 0 | 235 | | var allClients = await _ozonClientServices.GetOzonClients(); |
| | 0 | 236 | | var ozonClients = allClients.Where(c => c.ClientType == ClientType.OZON).ToList(); |
| | 0 | 237 | | ViewBag.OzonClients = new SelectList(ozonClients, "Id", "Name"); |
| | | 238 | | |
| | | 239 | | // Поддерживаемые типы фоновых заданий транзакций |
| | 0 | 240 | | ViewBag.JobTypes = new SelectList(new[] |
| | 0 | 241 | | { |
| | 0 | 242 | | new { Value = "ShippedToCommissioner", Text = "Отгружен комиссионеру" }, |
| | 0 | 243 | | new { Value = "CustomerReturn", Text = "Возврат покупателя" } |
| | 0 | 244 | | }, "Value", "Text"); |
| | 0 | 245 | | } |
| | | 246 | | } |