You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
9.3 KiB

1 year ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp.Application.Services;
using Win_in.Sfs.Basedata.Application.Contracts;
using Win_in.Sfs.Shared.Domain;
using Win_in.Sfs.Shared.Domain.Shared;
using Win_in.Sfs.Wms.DataExchange.Domain;
using Win_in.Sfs.Wms.DataExchange.Domain.Shared;
using Win_in.Sfs.Wms.DataExchange.WMS.PCK;
using Win_in.Sfs.Wms.Store.Application.Contracts;
using System.Text.Json.Serialization;
using System.IdentityModel.Tokens.Jwt;
using Volo.Abp;
1 year ago
namespace Win_in.Sfs.Wms.DataExchange.Fawtyg.InjectionMoldingTaskAgent.Incoming;
1 year ago
public class InjectionMoldingRequestReader : IReader
{
private readonly IInjectionRequestAppService _injectionRequest;
private readonly IItemBasicAppService _itemService;
private readonly ILocationAppService _locService;
1 year ago
1 year ago
private readonly ILogger<InjectionMoldingRequestReader> _logger;
private readonly IOptions<InjectionMoldingTaskOptions> _options;
private readonly IHttpClientFactory _httpClientFactory;
public InjectionMoldingRequestReader(
IInjectionRequestAppService injectionRequest
1 year ago
1 year ago
, ILogger<InjectionMoldingRequestReader> logger
, IOptions<InjectionMoldingTaskOptions> options
1 year ago
, IHttpClientFactory httpClientFactory
, IItemBasicAppService itemService
, ILocationAppService locService
1 year ago
)
{
_injectionRequest = injectionRequest;
1 year ago
1 year ago
_logger = logger;
_options = options;
_httpClientFactory = httpClientFactory;
_itemService=itemService;
_locService = locService;
}
1 year ago
/// <summary>
/// 读取注塑叫料任务
/// </summary>
/// <returns></returns>
1 year ago
1 year ago
public virtual async Task<List<IncomingFromExternal>> ReadAsync()
{
try
{
1 year ago
// 创建 SfsStoreRequestInputBase 对象以设定作业条件
1 year ago
var jobCondition = new SfsStoreRequestInputBase();
Filter filter = new Filter()
{
Action = "<>",
Column = "RequestStatus",
1 year ago
Logic = EnumFilterLogic.And.ToString(),
Value = (EnumRequestStatus.Completed).ToString()
};
1 year ago
// 添加筛选条件:请求状态不等于已完成
jobCondition.Condition.Filters.Add(filter);
filter = new Filter()
{
Action = "==",
Column = "Type",
Logic = EnumFilterLogic.And.ToString(),
Value = "Vision"
1 year ago
};
1 year ago
// 添加筛选条件:类型为 Vision
1 year ago
jobCondition.Condition.Filters.Add(filter);
1 year ago
// 通过筛选条件获取作业列表
1 year ago
var jobs = await _injectionRequest.GetAllListByFilterAsync(jobCondition).ConfigureAwait(false);
List<InjectionRequestEditInput> joblist = new List<InjectionRequestEditInput>();
if (jobs.Count == 0)
{
1 year ago
// 调用 ReaderCameraApi 方法获取摄像头信息
string camera = await ReaderCameraApi().ConfigureAwait(false);
1 year ago
List<InjectionRequest> cameraList = new List<InjectionRequest>();
if (camera == "Error occured")
{
1 year ago
// 记录错误日志并返回空列表
1 year ago
_logger.LogError($"没有读取到摄像头信息{DateTime.Now},请检查网络");
return new List<IncomingFromExternal>();
}
1 year ago
// 将摄像头信息转换为注塑叫料明细任务数据
cameraList = System.Text.Json.JsonSerializer.Deserialize<List<InjectionRequest>>(camera);
1 year ago
1 year ago
InjectionRequestEditInput input = new InjectionRequestEditInput();
1 year ago
List<InjectionRequestDetailInput> injectionRequestDetails = new List<InjectionRequestDetailInput>();
1 year ago
foreach (var job in cameraList)
{
1 year ago
var detailInput = new InjectionRequestDetailInput()
{
ItemCode = job.ItemCode,
ToLocationCode = job.ToLocCode,
Qty = job.Qty,
};
1 year ago
// 添加注塑叫料明细任务数据
1 year ago
injectionRequestDetails.Add(detailInput);
}
input.Details.AddRange(injectionRequestDetails);
1 year ago
// 通过 BindAsync 方法对零件仓库进行赋值
var errors = await BindAsync(input.Details).ConfigureAwait(false);
1 year ago
if (errors.Count > 0)
{
1 year ago
// 记录错误日志并返回空列表
foreach (var error in errors)
{
1 year ago
_logger.LogError(error);
}
return new List<IncomingFromExternal>();
}
1 year ago
// 创建新的注塑请求并将数据写入数据库
1 year ago
await _injectionRequest.CreateAsync(input).ConfigureAwait(false);
}
}
1 year ago
// 捕获特定异常并记录日志
catch (AbpException ex)
1 year ago
{
1 year ago
_logger.LogError(ex.Message);
1 year ago
}
catch (JsonException ex)
{
_logger.LogError(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
1 year ago
// 返回空列表
1 year ago
return new List<IncomingFromExternal>();
}
1 year ago
/// <summary>
/// 绑定零件库位信息,如果对错误返回错误新列表
/// </summary>
/// <param name="p_list"></param>
/// <returns></returns>
1 year ago
private async Task<List<string>> BindAsync(List<InjectionRequestDetailInput> p_list)
{
1 year ago
// 异步方法,将输入的请求绑定到对应的零件和库位信息,返回错误列表
1 year ago
List<string> errors = new List<string>();
foreach (var request in p_list)
{
1 year ago
// 获取对应零件信息
var itm = await _itemService.GetByCodeAsync(request.ItemCode).ConfigureAwait(false);
if (itm == null) { errors.Add($"编号:{request.ItemCode}零件表中没找到!"); }
else
1 year ago
{
1 year ago
// 更新请求中的零件描述和名称
1 year ago
request.ItemDesc1 = itm.Desc1;
request.ItemDesc2 = itm.Desc2;
request.ItemName = itm.Name;
}
1 year ago
// 获取对应库位信息
1 year ago
var loc = await _locService.GetByCodeAsync(request.ToLocationCode).ConfigureAwait(false);
if (loc == null) { errors.Add($"编号:{request.ToLocationCode}库位表中没找到!"); }
else
1 year ago
{
1 year ago
// 更新请求中的库位相关信息
1 year ago
request.ToLocationCode = loc.Code;
request.ToLocationGroup = loc.LocationGroupCode;
request.ToLocationErpCode = loc.ErpLocationCode;
1 year ago
request.ToWarehouseCode = loc.WarehouseCode;
1 year ago
}
}
1 year ago
// 返回错误列表
1 year ago
return errors;
}
/// <summary>
/// 读取摄像头API
1 year ago
/// </summary>
/// <param name="p_type"></param>
/// <returns></returns>
public async Task<string> ReaderCameraApi()
{
1 year ago
// 从配置中获取远程摄像头的地址、用户名、密码和令牌
var address = _options.Value.AutoRemote.IpAddress;
var username = _options.Value.AutoRemote.UserName;
var password = _options.Value.AutoRemote.Password;
var token = _options.Value.AutoRemote.Token;
// 创建一个HttpClient实例
var client = _httpClientFactory.CreateClient();
// 将用户名和密码转换为Base64编码的凭据,并设置请求的身份验证信息
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
// 发送GET请求到远程摄像头地址并等待响应
var response = await client.GetAsync(address).ConfigureAwait(false);
// 如果请求成功,则返回响应内容,否则返回错误信息
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
1 year ago
return "Error occurred";
}
1 year ago
1 year ago
private List<InjectionRequest> Parse(string p_str)
{
1 year ago
List<InjectionRequest> requests = new List<InjectionRequest>();
1 year ago
return System.Text.Json.JsonSerializer.Deserialize<List<InjectionRequest>>(p_str);
}
1 year ago
1 year ago
public class InjectionRequest
1 year ago
{
/// <summary>
/// 零件M
1 year ago
/// </summary>
public string ItemCode { get; set; }
/// <summary>
/// 零件名称
/// </summary>
public string ItemName { get; set; }
/// <summary>
/// 发运库位
/// </summary>
public string ToLocCode { get; set; }
/// <summary>
/// 来源库位
/// </summary>
public string FromLocCode { get; set; }
/// <summary>
/// 数量
/// </summary>
public decimal Qty { get; set; }
}
}