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.
 
 
 
 
 
 

218 lines
7.1 KiB

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;
namespace Win_in.Sfs.Wms.DataExchange.Fawtyg.MesAgent.Incoming;
public class InjectionMoldingRequestReader : IReader
{
private readonly IInjectionRequestAppService _injectionRequest;
private readonly IItemBasicAppService _itemService;
private readonly ILocationAppService _locService;
private readonly IIncomingFromExternalManager _incomingFromExternalManager;
private readonly ILogger<InjectionMoldingRequestReader> _logger;
private readonly IOptions<InjectionMoldingTaskOptions> _options;
private readonly IHttpClientFactory _httpClientFactory;
public InjectionMoldingRequestReader(
IInjectionRequestAppService injectionRequest
, IIncomingFromExternalManager incomingFromExternalManager
, ILogger<InjectionMoldingRequestReader> logger
,IOptions<InjectionMoldingTaskOptions> options
, IHttpClientFactory httpClientFactory
,IItemBasicAppService itemService
,ILocationAppService locService
)
{
_injectionRequest = injectionRequest;
_incomingFromExternalManager = incomingFromExternalManager;
_logger = logger;
_options = options;
_httpClientFactory = httpClientFactory;
_itemService=itemService;
_locService = locService;
}
public virtual async Task<List<IncomingFromExternal>> ReadAsync()
{
try
{
var jobCondition = new SfsStoreRequestInputBase();
Filter filter = new Filter()
{
Action = "<>",
Column = "JobStatus",
Logic = EnumFilterLogic.And.ToString(),
Value = ((int)EnumJobStatus.Done).ToString()
};
jobCondition.Condition.Filters.Add(filter);
var jobs = await _injectionRequest.GetAllListByFilterAsync(jobCondition).ConfigureAwait(false);
List<InjectionRequestEditInput> joblist = new List<InjectionRequestEditInput>();
if (jobs.Count == 0)
{
string camera =await ReaderCameraApi().ConfigureAwait(false);
List<InjectionRequest> cameraList = new List<InjectionRequest>();
if (camera == "Error occured")
{
_logger.LogError($"没有读取到摄像头信息{DateTime.Now},请检查网络");
return new List<IncomingFromExternal>();
}
cameraList = System.Text.Json.JsonSerializer.Deserialize<List<InjectionRequest>>(camera);//camera转注塑叫料明细任务数据
InjectionRequestEditInput input=new InjectionRequestEditInput();
List<InjectionRequestDetailInput> injectionRequestDetails = new List<InjectionRequestDetailInput>();
foreach (var job in cameraList) {
var detailInput = new InjectionRequestDetailInput()
{
ItemCode = job.ItemCode,
ToLocationCode = job.ToLocCode,
Qty = job.Qty,
};
injectionRequestDetails.Add(detailInput);
}
input.Details.AddRange(injectionRequestDetails);
var errors=await BindAsync(input.Details).ConfigureAwait(false);//零件仓库赋值
if (errors.Count > 0)
{
foreach (var error in errors) {
_logger.LogError(error);
}
return new List<IncomingFromExternal>();
}
await _injectionRequest.CreateAsync(input).ConfigureAwait(false);
}
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
return new List<IncomingFromExternal>();
}
private async Task<List<string>> BindAsync(List<InjectionRequestDetailInput> p_list)
{
List<string> errors = new List<string>();
foreach (var request in p_list)
{
var itm =await _itemService.GetByCodeAsync(request.ItemCode).ConfigureAwait(false);
if(itm == null) { errors.Add($"编号:{request.ItemCode}零件表中没找到!" ); }else
{
request.ItemDesc1 = itm.Desc1;
request.ItemDesc2 = itm.Desc2;
request.ItemName = itm.Name;
}
var loc = await _locService.GetByCodeAsync(request.ToLocationCode).ConfigureAwait(false);
if (loc == null) { errors.Add($"编号:{request.ToLocationCode}库位表中没找到!"); }else
{
request.ToLocationCode = loc.Code;
request.ToLocationGroup = loc.LocationGroupCode;
request.ToLocationErpCode = loc.ErpLocationCode;
request.ToWarehouseCode= loc.WarehouseCode;
}
}
return errors;
}
/// <summary>
///
/// </summary>
/// <param name="p_type"></param>
/// <returns></returns>
public async Task<string> ReaderCameraApi()
{
var address = _options.Value.AutoRemote.IpAddress;
var username = _options.Value.AutoRemote.UserName;
var password = _options.Value.AutoRemote.Password;
var token = _options.Value.AutoRemote.Token;
var client = _httpClientFactory.CreateClient();
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
var response = await client.GetAsync("https://example.com/api/data").ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
return "Error occurred";
}
public class InjectionRequest
{
/// <summary>
/// 零件吗
/// </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; }
}
}