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 _logger; private readonly IOptions _options; private readonly IHttpClientFactory _httpClientFactory; public InjectionMoldingRequestReader( IInjectionRequestAppService injectionRequest , IIncomingFromExternalManager incomingFromExternalManager , ILogger logger ,IOptions 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> 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 joblist = new List(); if (jobs.Count == 0) { string camera =await ReaderCameraApi().ConfigureAwait(false); List cameraList = new List(); if (camera == "Error occured") { _logger.LogError($"没有读取到摄像头信息{DateTime.Now},请检查网络"); return new List(); } cameraList = System.Text.Json.JsonSerializer.Deserialize>(camera);//camera转注塑叫料明细任务数据 InjectionRequestEditInput input=new InjectionRequestEditInput(); List injectionRequestDetails = new List(); 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(); } await _injectionRequest.CreateAsync(input).ConfigureAwait(false); } } catch (Exception ex) { _logger.LogError(ex.Message); } return new List(); } private async Task> BindAsync(List p_list) { List errors = new List(); 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; } /// /// /// /// /// public async Task 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 { /// /// 零件吗 /// public string ItemCode { get; set; } /// /// 零件名称 /// public string ItemName { get; set; } /// /// 发运库位 /// public string ToLocCode { get; set; } /// /// 来源库位 /// public string FromLocCode { get; set; } /// /// 数量 /// public decimal Qty { get; set; } } }