Files
WCS/Plugins/Wcs/Plugin.Cowain.Wcs/Services/FindFlowTaskService.cs
2026-03-02 09:13:29 +08:00

106 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Cowain.Base.DBContext;
using Cowain.Base.Helpers;
using Cowain.Base.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Plugin.Cowain.Driver.Abstractions;
using Plugin.Cowain.Wcs.IServices;
using Plugin.Cowain.Wcs.Models.Dto;
using Plugin.Cowain.Wcs.Models.Enum;
namespace Plugin.Cowain.Wcs.Services;
public class FindFlowTaskService : BaseService, IFindFlowTaskService
{
private IDeviceMonitor _deviceMonitor;
private IProcessService _processService;
private IServiceScopeFactory _scopeFactory;
private readonly IMemoryCache _memoryCache;
private readonly ILogger<FindFlowTaskService> _logger;
public FindFlowTaskService(SqlDbContext dbContext, IServiceScopeFactory scopeFactory, IMemoryCache memoryCache, IDeviceMonitor deviceMonitor, IProcessService processService, ILogger<FindFlowTaskService> logger) : base(dbContext)
{
_memoryCache = memoryCache;
_deviceMonitor = deviceMonitor;
_processService = processService;
_scopeFactory = scopeFactory;
_logger = logger;
}
public async Task FindTaskAsync()
{
var taskSet = _dBContext.GetDbSet<TaskDataDto>();
var unfinishedTask = await taskSet.AsNoTracking().FirstOrDefaultAsync(t => !t.IsFinished);
if (unfinishedTask != null)
{
_logger.LogInformation($"已有未完成的任务任务ID{unfinishedTask.Id},任务内容:{JsonConvert.SerializeObject(unfinishedTask)}");
return;
}
var processSet = _dBContext.GetDbSet<ProcessDto>();
var flowSet = _dBContext.GetDbSet<ProcessFlowDto>();
var fromStationSet = _dBContext.GetDbSet<StationDto>();
var toStationSet = _dBContext.GetDbSet<StationDto>();
var query = from fromStation in fromStationSet
join process in processSet on fromStation.ProcessName equals process.Name
join flow in flowSet on new { processId = process.Id, fromStation = fromStation.Id, fromStatus = fromStation.Status } equals new { processId = flow.ProcessId, fromStation = flow.FromStationId, fromStatus = flow.FromStatus }
join toStation in toStationSet on new { process = process.Name, toStation = flow.ToStationId, toStatus = flow.ToStatus } equals new { process = toStation.ProcessName, toStation = toStation.Id, toStatus = toStation.Status }
where fromStation.NextStationId <= 0 && fromStation.Enable && toStation.Enable
orderby flow.Priority descending
select new TaskDataDto
{
ProcessId = process.Id,
ProcessName = process.Name,
Priority = flow.Priority,
ExecuteAction = 0,
FromStationId = flow.FromStationId,
ToStationId = flow.ToStationId,
FromStatus = flow.FromStatus,
ToStatus = flow.ToStatus,
QrCode = fromStation.QrCode,
IsFinished = false,
Action = flow.Action,
};
string sql = query.ToQueryString();
var findTask = await query.AsNoTracking().FirstOrDefaultAsync();
if (findTask == null)
{
_logger.LogInformation($"没有需要执行的任务");
return;
}
await taskSet.AddAsync(findTask);
var saveCount = await _dBContext.SaveChangesAsync();
if (saveCount > 0)
{
_logger.LogInformation($"找到并添加任务:{JsonConvert.SerializeObject(findTask)}");
var getJson = await _processService.GetJsonData(findTask.ProcessId);
if (getJson.IsSuccess)
{
var taskJsonParam = _deviceMonitor.GetActionParam(getJson.Data!.PlcName!, getJson.Data!.Address!);
if (taskJsonParam.IsSuccess)
{
using var scope = _scopeFactory.CreateScope();
var taskService = scope.ServiceProvider.GetRequiredService<IRgvTaskService>();
var execute = await taskService.ExecuteAsync(taskJsonParam.Data, RgvUpdateSourceEnum.First);
}
}
else
{
_logger.LogError(getJson.ErrorMessage);
}
}
else
{
_logger.LogError("任务添加失败");
}
}
}