using LogLibrary.Log; 
 | 
using Masuit.Tools; 
 | 
using Newtonsoft.Json; 
 | 
using WIDESEA_Common; 
 | 
using WIDESEA_Core; 
 | 
using WIDESEA_Core.Const; 
 | 
using WIDESEA_Core.Helper; 
 | 
using WIDESEA_DTO; 
 | 
using WIDESEA_DTO.MOM; 
 | 
using WIDESEA_IBusinessesRepository; 
 | 
using WIDESEA_IServices; 
 | 
using WIDESEA_IStorageBasicRepository; 
 | 
using WIDESEA_IStorageTaskRepository; 
 | 
using WIDESEA_IStoragIntegrationServices; 
 | 
using WIDESEA_Model.Models; 
 | 
  
 | 
namespace WIDESEA_StoragIntegrationServices; 
 | 
  
 | 
public class ProcessApplyService : IProcessApplyService 
 | 
{ 
 | 
    private readonly LogFactory LogFactory = new LogFactory(); 
 | 
    private readonly ISys_ConfigService _configService; 
 | 
    private readonly IDt_AreaInfoRepository _areaInfoRepository; 
 | 
    private readonly ICellStateService _cellStateService; 
 | 
    private readonly IStockInfoRepository _stockInfoRepository; 
 | 
    private readonly IAgingInOrOutInputService _gingInOrOutInputService; 
 | 
  
 | 
    public ProcessApplyService(ISys_ConfigService configRepository, IDt_AreaInfoRepository areaInfoRepository, ICellStateService cellStateService, IDt_TaskRepository taskRepository, IStockInfoRepository stockInfoRepository, IAgingInOrOutInputService gingInOrOutInputService) 
 | 
    { 
 | 
        _configService = configRepository; 
 | 
        _areaInfoRepository = areaInfoRepository; 
 | 
        _cellStateService = cellStateService; 
 | 
        _stockInfoRepository = stockInfoRepository; 
 | 
        _gingInOrOutInputService = gingInOrOutInputService; 
 | 
    } 
 | 
  
 | 
    /// <summary> 
 | 
    /// 工艺路线申请 
 | 
    /// </summary> 
 | 
    /// <param name="input"></param> 
 | 
    /// <returns></returns> 
 | 
    public async Task<WebResponseContent> GetProcessApplyAsync(ProcessApplyDto input) 
 | 
    { 
 | 
        WebResponseContent content = new WebResponseContent(); 
 | 
        try 
 | 
        { 
 | 
            input.SessionId = Guid.NewGuid().ToString(); 
 | 
            input.EmployeeNo = "MITest"; 
 | 
            input.RequestTime = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now).ToString("yyyy-MM-ddTHH:mm:ss.fffZ"); 
 | 
            var inputJson = input.ToDictionary(); 
 | 
            var configs = _configService.GetConfigsByCategory(CateGoryConst.SYS_MOMIPAddress); 
 | 
            var wmsBase = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.MOMBaseIP)?.ConfigValue; 
 | 
            var ipAddress = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.ProcessApply)?.ConfigValue; 
 | 
            if (wmsBase == null || ipAddress == null) 
 | 
            { 
 | 
                throw new InvalidOperationException("WMS IP 未配置"); 
 | 
            } 
 | 
            var wmsIpAddress = wmsBase + ipAddress; 
 | 
  
 | 
            var result = HttpsClient.PostAsync(wmsIpAddress, inputJson).Result; 
 | 
            //var x = await HttpsClient.PostAsync("http://c24-cellmi3:12020/api/MachineIntegration/ProcessApply", inputJson); 
 | 
            if (result != null) 
 | 
            { 
 | 
                //var ResultProcess = JsonConvert.DeserializeObject<ResultProcessApply>(x); 
 | 
                content.OK(data: result); 
 | 
            } 
 | 
            LogFactory.GetLog("工艺路线申请").Info(true, $"\r\r--------------------------------------"); 
 | 
            LogFactory.GetLog("工艺路线申请").Info(true, result); 
 | 
        } 
 | 
        catch (Exception ex) 
 | 
        { 
 | 
            Console.WriteLine(ex.Message); 
 | 
            LogFactory.GetLog("工艺路线申请").Error(true, $"\r\r--------------------------------------"); 
 | 
            LogFactory.GetLog("工艺路线申请").Error(true, ex.StackTrace); 
 | 
        } 
 | 
        return content; 
 | 
    } 
 | 
  
 | 
    /// <summary> 
 | 
    /// 补录出库数据 
 | 
    /// </summary> 
 | 
    /// <param name="palletCode"></param> 
 | 
    /// <returns></returns> 
 | 
    public async Task<WebResponseContent> StockOutDataBackfillInterfaceAsync(string palletCode, int areaID) 
 | 
    { 
 | 
        WebResponseContent content = new WebResponseContent(); 
 | 
        try 
 | 
        { 
 | 
            var area = _areaInfoRepository.QueryFirst(x => x.AreaID == areaID); 
 | 
            var trayCells = new TrayCellsStatusDto() 
 | 
            { 
 | 
                Software = area.Spare3, 
 | 
                TrayBarcode = palletCode, 
 | 
                EquipmentCode = area.Spare2, 
 | 
                SceneType = area.Spare4 
 | 
            }; 
 | 
            content = await _cellStateService.GetTrayCellStatusAsync(trayCells); 
 | 
            if (!content.Status) return content; 
 | 
  
 | 
            var result = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(content.Data.ToString()); 
 | 
            if (result.SerialNos.Count > 0) 
 | 
            { 
 | 
                var stockHty = await SqlSugarHelper.Db.Queryable<DtStockInfo>().IncludesAllFirstLayer().FirstAsync(); 
 | 
                if (stockHty != null) 
 | 
                { 
 | 
                    var parameterInfo = JsonConvert.DeserializeObject<List<ParameterInfo>>(stockHty.ParameterInfos).FirstOrDefault(y => y.Description.Contains("时间")); 
 | 
                    if (parameterInfo == null) throw new Exception(""); 
 | 
  
 | 
                    var outHours = (DateTime.Now - (stockHty.LinedProcessFeedbackTime == null ? stockHty.ModifyDate.Value : stockHty.LinedProcessFeedbackTime.ToDateTime())).TotalHours; 
 | 
                    var isNG = outHours > parameterInfo.LowerSpecificationsLimit.ToDouble() && outHours < parameterInfo.UpperSpecificationsLimit.ToDouble(); 
 | 
  
 | 
                    var defectCode = string.Empty; 
 | 
                    if (!isNG) defectCode = "TQCK"; 
 | 
                    var outputDto = new AgingOutputDto 
 | 
                    { 
 | 
                        OpFlag = 1, 
 | 
                        Software = area.Spare3, 
 | 
                        EquipmentCode = area.Spare2, 
 | 
                        TrayBarcode = palletCode, 
 | 
                        SerialNos = result.SerialNos.Select(x => new SerialNoOutDto 
 | 
                        { 
 | 
                            SlotNo = x.PositionNo, 
 | 
                            SerialNo = x.SerialNo, 
 | 
                            SerialNoResult = true, //isNG, 
 | 
                            ParameterInfo = new List<ParameterInfoOutput> { 
 | 
                                new ParameterInfoOutput() { 
 | 
                                    Value = outHours.ToString(), 
 | 
                                    ParameterCode =parameterInfo.ParameterCode, 
 | 
                                    ParameterDesc = parameterInfo.Description, 
 | 
                                    ParameterResult  = "OK", //isNG.ToString(), 
 | 
                                    TargetValue = parameterInfo.TargetValue, 
 | 
                                    LowerLomit = parameterInfo.LowerSpecificationsLimit, 
 | 
                                    UpperLimit = parameterInfo.UpperSpecificationsLimit, 
 | 
                                    DefectCode = defectCode, 
 | 
                                    UOMCode = parameterInfo.UOMCode, 
 | 
                                } 
 | 
                            } 
 | 
                        }).ToList() 
 | 
                    }; 
 | 
                    content = await _gingInOrOutInputService.GetOCVOutputAsync(outputDto); 
 | 
                } 
 | 
            } 
 | 
            else 
 | 
            { 
 | 
                content.Error("电芯数据为空"); 
 | 
            } 
 | 
        } 
 | 
        catch (Exception ex) 
 | 
        { 
 | 
        } 
 | 
        return content; 
 | 
    } 
 | 
  
 | 
    /// <summary> 
 | 
    /// 补录入库数据 
 | 
    /// </summary> 
 | 
    /// <param name="palletCode"></param> 
 | 
    /// <param name="areaID"></param> 
 | 
    /// <returns></returns> 
 | 
    public async Task<WebResponseContent> StockInDataBackfillInterfaceAsync(string palletCode, int areaID) 
 | 
    { 
 | 
        WebResponseContent content = new WebResponseContent(); 
 | 
        try 
 | 
        { 
 | 
            var area = _areaInfoRepository.QueryFirst(x => x.AreaID == areaID); 
 | 
            var trayCells = new TrayCellsStatusDto() 
 | 
            { 
 | 
                Software = area.Spare3, 
 | 
                TrayBarcode = palletCode, 
 | 
                EquipmentCode = area.Spare2, 
 | 
                SceneType = area.Spare4 
 | 
            }; 
 | 
            content = await _cellStateService.GetTrayCellStatusAsync(trayCells); 
 | 
            if (!content.Status) return content; 
 | 
  
 | 
            var result = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(content.Data.ToString()); 
 | 
            if (result.SerialNos.Count > 0 && result.Success) 
 | 
            { 
 | 
                var stockInfo = await _stockInfoRepository.QueryFirstNavAsync(x => x.PalletCode == palletCode); 
 | 
                if (stockInfo != null) 
 | 
                { 
 | 
                    stockInfo.IsFull = true; 
 | 
                    stockInfo.StockInfoDetails = result.SerialNos.Select(serialNoObj => new DtStockInfoDetail 
 | 
                    { 
 | 
                        SerialNumber = serialNoObj.SerialNo, 
 | 
                        OrderNo = serialNoObj.PositionNo.ToString(), 
 | 
                        Status = serialNoObj.SerialNoStatus, 
 | 
                        MaterielCode = result.BindCode, 
 | 
                        Remark = result.TrayBarcodePropertys.ToJsonString(), 
 | 
                    }).ToList(); 
 | 
                    stockInfo.Remark = stockInfo.StockInfoDetails.Count().ToString(); 
 | 
  
 | 
                    // 处理请求参数 
 | 
                    AgingInputDto agingInputDto = new AgingInputDto() 
 | 
                    { 
 | 
                        SerialNos = stockInfo.StockInfoDetails 
 | 
                            .Select(item => new SerialNoInDto { SerialNo = item.SerialNumber, PositionNo = item.OrderNo }) 
 | 
                            .ToList(), 
 | 
                        TrayBarcode = palletCode, 
 | 
                        OpFlag = 1, 
 | 
                        EquipmentCode = area.Spare2, 
 | 
                        Software = area.Spare3 
 | 
                    }; 
 | 
  
 | 
                    content = _gingInOrOutInputService.GetOCVInputAsync(agingInputDto).Result; 
 | 
                    var respone = JsonConvert.DeserializeObject<ResponeAgingInputDto>(content.Data.ToString()); 
 | 
  
 | 
                    stockInfo.LinedProcessFeedbackTime = respone.LinedProcessFeedbackTime; 
 | 
                    stockInfo.SpecialParameterDuration = respone.SpecialParameterDuration; 
 | 
                    //2024年11月16日:新增字段计算应出库时间 
 | 
                    stockInfo.OutboundTime = Convert.ToDateTime(respone.LinedProcessFeedbackTime == null ? DateTime.Now : respone.LinedProcessFeedbackTime).AddHours(Convert.ToDouble(respone.SpecialParameterDuration)); 
 | 
                    stockInfo.ProductionLine = respone.ProductionLine; 
 | 
                    stockInfo.ParameterInfos = respone.ParameterInfos.ToJsonString(); 
 | 
                    stockInfo.StockStatus = 1; 
 | 
  
 | 
                    var isResult = await _stockInfoRepository.UpdateDataNavAsync(stockInfo); 
 | 
                    if (isResult) 
 | 
                        content.OK("更新数据成功"); 
 | 
                    else 
 | 
                        content.Error("更新数据失败"); 
 | 
                } 
 | 
                else 
 | 
                { 
 | 
                    content.Error("未找到库存"); 
 | 
                } 
 | 
            } 
 | 
            else 
 | 
            { 
 | 
                return content; 
 | 
            } 
 | 
        } 
 | 
        catch (Exception ex) 
 | 
        { 
 | 
            content.Error(ex.Message); 
 | 
        } 
 | 
        return content; 
 | 
    } 
 | 
} 
 |