1
wankeda
3 天以前 844c7c3ce2c39139490a6ecb0f35170f6cade290
WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/PartialTaskService_Inbound.cs
@@ -1,30 +1,35 @@
using System;
using MailKit.Search;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Enums;
using WIDESEA_Core;
using WIDESEA_Model.Models;
using WIDESEA_Core.Helper;
using Microsoft.Extensions.Logging;
using MailKit.Search;
using System.Reflection.Metadata;
using static WIDESEA_ITaskInfoService.ITaskService;
using static WIDESEA_Common.HouseInventoryIn;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WIDESEA_Common;
using Parameter = WIDESEA_Common.Parameter;
using WIDESEA_Core.LogHelper;
using WIDESEA_DTO.Task;
using WIDESEA_Core.TaskEnum;
using WIDESEA_Model.Models.Inbound;
using WIDESEA_Common.Log;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Core;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.LogHelper;
using WIDESEA_Core.TaskEnum;
using WIDESEA_DTO.Task;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.Inbound;
using static WIDESEA_Common.HouseInventoryIn;
using static WIDESEA_ITaskInfoService.ITaskService;
using Parameter = WIDESEA_Common.Parameter;
namespace WIDESEA_TaskInfoService
{
    public partial class TaskService
    {
        /// <summary>
        /// 仅申请任务,让WCS根据路由确定下一地址
@@ -39,13 +44,28 @@
                Dt_Task task = Repository.QueryFirst(x => x.PalletCode == palletCode);
                if (task != null)
                {
                    //PushTasksToWCS(new List<Dt_Task> { task });
                    _hubContext.Clients.All.SendAsync("ReceiveMessage", $"该托盘{palletCode}已生成任务");
                    return WebResponseContent.Instance.Error($"该托盘{palletCode}已生成任务");
                }
                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletCode == palletCode).Includes(x => x.Details).First();
                if (stockInfo == null)
                {
                    _hubContext.Clients.All.SendAsync("ReceiveMessage", $"未找到组盘信息");
                    return WebResponseContent.Instance.Error($"未找到组盘信息");
                }
                if (stockInfo.StockStatus == StockStatusEmun.合托出库完成.ObjToInt())
                {
                }
                if (stockInfo.StockStatus == StockStatusEmun.盘点出库完成.ObjToInt())
                {
                    _hubContext.Clients.All.SendAsync("ReceiveMessage", $"该托盘{palletCode}已生成任务");
                    return WebResponseContent.Instance.Error($"该托盘{palletCode}未盘点");
                }
                ///判断是否存在回库单,进行回原库位逻辑
                string returnOrderNo = stockInfo.Details?.Where(x => x.StockId == stockInfo.Id).FirstOrDefault()?.OrderNo;
@@ -56,6 +76,8 @@
                    Dt_LocationInfo locationInfo1 = _locationInfoService.Repository.QueryFirst(x => x.LocationCode == returnOrder.LocationCode);
                    if (locationInfo1 == null)
                    {
                        _hubContext.Clients.All.SendAsync("ReceiveMessage", $"回库单中该库位{returnOrder.LocationCode}未找到");
                        return WebResponseContent.Instance.Error($"回库单中该库位{returnOrder.LocationCode}未找到");
                    }
                    returnRoadwayInfo = _basicRepository.RoadwayInfoRepository.QueryFirst(x => x.RoadwayNo == locationInfo1.RoadwayNo);
@@ -63,22 +85,30 @@
                if (Repository.QueryFirst(x => x.SourceAddress == (returnRoadwayInfo.InStationCode != null ? returnRoadwayInfo.InStationCode : stationCode) && x.TaskStatus == TaskStatusEnum.New.ObjToInt()) != null)
                {
                    _hubContext.Clients.All.SendAsync("ReceiveMessage", $"该站点已有未执行的任务");
                    return WebResponseContent.Instance.Error($"该站点已有未执行的任务");
                }
                var details = stockInfo.Details.FirstOrDefault();
                if (!string.IsNullOrEmpty(stockInfo.LocationCode))
                {
                    _hubContext.Clients.All.SendAsync("ReceiveMessage", $"该托盘已绑定货位");
                    return WebResponseContent.Instance.Error($"该托盘已绑定货位");
                }
                Dt_RoadwayInfo roadwayInfo = _basicRepository.RoadwayInfoRepository.QueryFirst(x => x.InStationCode == (returnRoadwayInfo.InStationCode != null ? returnRoadwayInfo.InStationCode : stationCode));
                if (roadwayInfo == null)
                {
                    _hubContext.Clients.All.SendAsync("ReceiveMessage", $"未找到该入库站台地址");
                    return WebResponseContent.Instance.Error($"未找到该入库站台地址");
                }
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == roadwayInfo.RoadwayNo);
                if (warehouse == null)
                {
                    _hubContext.Clients.All.SendAsync("ReceiveMessage", $"未找到该仓库");
                    return WebResponseContent.Instance.Error("未找到该仓库");
                }
                Dt_LocationInfo? locationInfo = new Dt_LocationInfo();
@@ -87,6 +117,8 @@
                    locationInfo = _locationInfoService.Repository.QueryFirst(x => x.LocationCode == returnOrder.LocationCode);
                    if (locationInfo.LocationStatus != LocationStatusEnum.Lock.ObjToInt() || locationInfo.EnableStatus == EnableStatusEnum.Disable.ObjToInt() || locationInfo.EnableStatus == EnableStatusEnum.OnlyOut.ObjToInt())
                    {
                        _hubContext.Clients.All.SendAsync("ReceiveMessage", $"该回库单货位{locationInfo.LocationCode}非锁定状态或货位启用状态不正确,请检查");
                        return WebResponseContent.Instance.Error($"该回库单货位{locationInfo.LocationCode}非锁定状态或货位启用状态不正确,请检查");
                    }
                }
@@ -95,6 +127,8 @@
                    locationInfo = _basicService.LocationInfoService.AssignLocation(roadwayInfo.RoadwayNo, stockInfo.PalletType, warehouse.WarehouseId, "", heightType);//, stockInfo.WarehouseId
                    if (locationInfo == null)
                    {
                        _hubContext.Clients.All.SendAsync("ReceiveMessage", $"货位分配失败,未找到可分配货位");
                        return WebResponseContent.Instance.Error($"货位分配失败,未找到可分配货位");
                    }
                }
@@ -156,20 +190,43 @@
                //        newTask.TaskType = TaskTypeEnum.MesPalletSmallReturn.ObjToInt();
                //    }
                //}
                //else
                //{
                 //{
                stockInfo.StockStatus = StockStatusEmun.入库确认.ObjToInt();
                locationInfo.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                //}
                _unitOfWorkManage.BeginTran();
                int taskId = BaseDal.AddData(newTask);
                _unitOfWorkManage.BeginTran();
                int taskId = BaseDal.AddData(newTask);
                newTask.TaskId = taskId;
                 List<Dt_StockInfoDetail> stockInfoDetails = stockInfo.Details;
                foreach (var stockInfoDetail in stockInfoDetails)
                {
                    WriteLog.Write_Log("API", "入库—请求LED", "请求成功", new { 请求报文 = 1 });
                    var postData = new
                    {
                        DeviceCode = "1005",    // 屏幕编号
                        MaterialCode = stockInfoDetail.MaterielCode,//物料类型
                        SendNum = stockInfoDetail.OutboundQuantity,//发料数
                        BackNum = stockInfoDetail.StockQuantity - stockInfoDetail.OutboundQuantity//回库数
                    };
                    WriteLog.Write_Log("API", "入库—请求LED", "请求成功", new { 请求报文2 = postData });
                    string json = Newtonsoft.Json.JsonConvert.SerializeObject(postData);
                    WriteLog.Write_Log("API", "入库—请求LED", "请求成功", new { 请求报文3 = json });
                    using (var client = new HttpClient())
                    {
                        var content = new StringContent(json, Encoding.UTF8, "application/json");
                        var response = client.PostAsync(LEDAPI, content).Result;
                        string result = response.Content.ReadAsStringAsync().Result;
                        WriteLog.Write_Log("API", "入库—请求LED", "请求成功", new { 请求报文 = response, 接收报文 = result });
                    }
                    WriteLog.Write_Log("API", "入库—请求LED", "请求成功", new { 请求报文 = 4 });
                }
                if (returnOrder != null)
                {
                    returnOrder.OrderStatus = InOrderStatusEnum.入库中.ObjToInt();
                    returnOrder.OrderStatus = InOrderStatusEnum.入库中.ObjToInt();
                    _returnOrderRepository.UpdateData(returnOrder);
                }
                }
                _stockRepository.StockInfoRepository.UpdateData(stockInfo);
                _locationInfoService.UpdateData(locationInfo);
@@ -188,6 +245,8 @@
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                _hubContext.Clients.All.SendAsync("ReceiveMessage", $"{ex.Message}");
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
@@ -199,47 +258,72 @@
                Dt_Task task = Repository.QueryFirst(x => x.PalletCode == palletCode);
                if (task != null)
                {
                    _hubContext.Clients.All.SendAsync("ReceiveMessage", $"该托盘{palletCode}已生成任务");
                    return WebResponseContent.Instance.Error($"该托盘{palletCode}已生成任务");
                }
                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletCode == palletCode).Includes(x => x.Details).First();
                if (stockInfo == null)
                {
                    _hubContext.Clients.All.SendAsync("ReceiveMessage", $"未找到组盘信息");
                    return WebResponseContent.Instance.Error($"未找到组盘信息");
                }
                Dt_RoadwayInfo returnRoadwayInfo = new Dt_RoadwayInfo();
                if (stockInfo.StockStatus == StockStatusEmun.合托出库完成.ObjToInt())
                {
                    Dt_LocationInfo locationInfo1 = _locationInfoService.Repository.QueryFirst(x => x.LocationCode == stockInfo.LocationCode);
                    if (locationInfo1 == null)
                    {
                        _hubContext.Clients.All.SendAsync("ReceiveMessage", $"回库单中该库位{stockInfo.LocationCode}未找到");
                        return WebResponseContent.Instance.Error($"回库单中该库位{stockInfo.LocationCode}未找到");
                    }
                    returnRoadwayInfo = _basicRepository.RoadwayInfoRepository.QueryFirst(x => x.RoadwayNo == locationInfo1.RoadwayNo && x.InStationCode == (returnRoadwayInfo.InStationCode != null ? returnRoadwayInfo.InStationCode : stationCode));
                }
                ///判断是否存在回库单,进行回原库位逻辑
                string returnOrderNo = stockInfo.Details?.Where(x => x.StockId == stockInfo.Id).FirstOrDefault()?.OrderNo;
                Dt_ReturnOrder returnOrder = _returnOrderRepository.QueryFirst(x => x.OrderNo == returnOrderNo && x.OrderStatus == InOrderStatusEnum.未开始.ObjToInt());
                Dt_RoadwayInfo returnRoadwayInfo = new Dt_RoadwayInfo();
                Dt_ReturnOrder returnOrder = _returnOrderRepository.QueryFirst(x => (x.OrderNo == returnOrderNo || x.LPNNo == palletCode) && x.OrderStatus == InOrderStatusEnum.未开始.ObjToInt());
                if (returnOrder != null && !string.IsNullOrEmpty(returnOrder.LocationCode))
                {
                    Dt_LocationInfo locationInfo1 = _locationInfoService.Repository.QueryFirst(x => x.LocationCode == returnOrder.LocationCode);
                    if (locationInfo1 == null)
                    {
                        _hubContext.Clients.All.SendAsync("ReceiveMessage", $"回库单中该库位{returnOrder.LocationCode}未找到");
                        return WebResponseContent.Instance.Error($"回库单中该库位{returnOrder.LocationCode}未找到");
                    }
                    returnRoadwayInfo = _basicRepository.RoadwayInfoRepository.QueryFirst(x => x.RoadwayNo == locationInfo1.RoadwayNo);
                    returnRoadwayInfo = _basicRepository.RoadwayInfoRepository.QueryFirst(x => x.RoadwayNo == locationInfo1.RoadwayNo && x.InStationCode == (returnRoadwayInfo.InStationCode != null ? returnRoadwayInfo.InStationCode : stationCode));
                }
                if (Repository.QueryFirst(x => x.SourceAddress == (returnRoadwayInfo.InStationCode != null ? returnRoadwayInfo.InStationCode : stationCode) && x.TaskStatus == TaskStatusEnum.New.ObjToInt()) != null)
                {
                    _hubContext.Clients.All.SendAsync("ReceiveMessage", $"该站点已有未执行的任务");
                    return WebResponseContent.Instance.Error($"该站点已有未执行的任务");
                }
                var details = stockInfo.Details.FirstOrDefault();
                if (!string.IsNullOrEmpty(stockInfo.LocationCode))
                {
                    _hubContext.Clients.All.SendAsync("ReceiveMessage", $"该托盘已绑定货位");
                    return WebResponseContent.Instance.Error($"该托盘已绑定货位");
                }
                Dt_RoadwayInfo roadwayInfo = _basicRepository.RoadwayInfoRepository.QueryFirst(x => x.InStationCode == (returnRoadwayInfo.InStationCode != null ? returnRoadwayInfo.InStationCode : stationCode));
                if (roadwayInfo == null)
                {
                    _hubContext.Clients.All.SendAsync("ReceiveMessage", $"未找到该入库站台地址");
                    return WebResponseContent.Instance.Error($"未找到该入库站台地址");
                }
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == roadwayInfo.RoadwayNo);
                if (warehouse == null)
                {
                    _hubContext.Clients.All.SendAsync("ReceiveMessage", $"未找到该仓库");
                    return WebResponseContent.Instance.Error("未找到该仓库");
                }
@@ -249,6 +333,8 @@
                    locationInfo = _locationInfoService.Repository.QueryFirst(x => x.LocationCode == returnOrder.LocationCode);
                    if (locationInfo.LocationStatus != LocationStatusEnum.Lock.ObjToInt() || locationInfo.EnableStatus == EnableStatusEnum.Disable.ObjToInt() || locationInfo.EnableStatus == EnableStatusEnum.OnlyOut.ObjToInt())
                    {
                        _hubContext.Clients.All.SendAsync("ReceiveMessage", $"该回库单货位{locationInfo.LocationCode}非锁定状态或货位启用状态不正确,请检查");
                        return WebResponseContent.Instance.Error($"该回库单货位{locationInfo.LocationCode}非锁定状态或货位启用状态不正确,请检查");
                    }
                }
@@ -257,15 +343,16 @@
                    locationInfo = _basicService.LocationInfoService.AssignLocation(roadwayInfo.RoadwayNo, stockInfo.PalletType, warehouse.WarehouseId, "", heightType);//, stockInfo.WarehouseId
                    if (locationInfo == null)
                    {
                        _hubContext.Clients.All.SendAsync("ReceiveMessage", $"货位分配失败,未找到可分配货位");
                        return WebResponseContent.Instance.Error($"货位分配失败,未找到可分配货位");
                    }
                }
                Dt_Task newTask = new Dt_Task()
                {
                    CurrentAddress = courceAddress,
                    Grade = 0,
                    NextAddress = "",
                    NextAddress = returnRoadwayInfo.InStationCode != null ? returnRoadwayInfo.InStationCode : stationCode,
                    PalletCode = palletCode,
                    OrderNo = details.OrderNo,
                    Roadway = roadwayInfo.RoadwayNo,
@@ -297,6 +384,10 @@
                {
                    newTask.TaskType = TaskTypeEnum.InInventory.ObjToInt();
                }
                if (stockInfo.StockStatus == StockStatusEmun.合托出库完成.ObjToInt())
                {
                    newTask.TaskType = TaskTypeEnum.InSyncretism.ObjToInt();
                }
                //if (stockInfo.StockStatus == StockStatusEmun.手动组盘暂存.ObjToInt())
                //{
                //    stockInfo.StockStatus = StockStatusEmun.手动组盘入库确认.ObjToInt();
@@ -320,13 +411,40 @@
                //}
                //else
                //{
                stockInfo.StockStatus = StockStatusEmun.入库确认.ObjToInt();
                else
                {
                    stockInfo.StockStatus = StockStatusEmun.入库确认.ObjToInt();
                }
                locationInfo.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                //}
                _unitOfWorkManage.BeginTran();
                int taskId = BaseDal.AddData(newTask);
                newTask.TaskId = taskId;
                List<Dt_StockInfoDetail> stockInfoDetails = stockInfo.Details;
                foreach (var stockInfoDetail in stockInfoDetails)
                {
                    WriteLog.Write_Log("API", "入库—请求LED", "请求成功", new { 请求报文 = 1 });
                    var postData = new
                    {
                        DeviceCode = newTask.CurrentAddress,    // 屏幕编号
                        MaterialCode = stockInfoDetail.MaterielCode,//物料类型
                        SendNum = stockInfoDetail.OutboundQuantity,//发料数
                        BackNum = stockInfoDetail.StockQuantity - stockInfoDetail.OutboundQuantity//回库数
                    };
                    WriteLog.Write_Log("API", "入库—请求LED", "请求成功", new { 请求报文2 = postData });
                    string json = Newtonsoft.Json.JsonConvert.SerializeObject(postData);
                    WriteLog.Write_Log("API", "入库—请求LED", "请求成功", new { 请求报文3 = json });
                    using (var client = new HttpClient())
                    {
                        var content = new StringContent(json, Encoding.UTF8, "application/json");
                        var response = client.PostAsync(LEDAPI, content).Result;
                        string result = response.Content.ReadAsStringAsync().Result;
                        WriteLog.Write_Log("API", "入库—请求LED", "请求成功", new { 请求报文 = response, 接收报文 = result });
                    }
                    WriteLog.Write_Log("API", "入库—请求LED", "请求成功", new { 请求报文 = 4 });
                }
                if (returnOrder != null)
                {
                    returnOrder.OrderStatus = InOrderStatusEnum.入库中.ObjToInt();
@@ -345,6 +463,8 @@
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                _hubContext.Clients.All.SendAsync("ReceiveMessage", $"{ex.Message}");
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
@@ -623,9 +743,8 @@
        //查询仓库托盘货物类型
        public int GetPalletType(Dt_Warehouse warehouse, string palletCode)
        {
            if (warehouse.WarehouseCode == WarehouseEnum.SC01_BC.ObjToString() || warehouse.WarehouseCode == WarehouseEnum.SC02_BC.ObjToString())
            if (warehouse.WarehouseCode.Contains("BC"))
            {
                Dt_PalletTypeInfo palletTypeInfo = _palletTypeInfoRepository.QueryFirst(x => x.CodeStartStr == palletCode.Substring(0, 3));
                if (palletTypeInfo == null)
                {
@@ -633,25 +752,16 @@
                }
                return palletTypeInfo.PalletType;
            }
            //else if (warehouse.WarehouseCode == WarehouseEnum.HA152.ObjToString())
            //{
            //    Dt_PalletTypeInfo palletTypeInfo = _palletTypeInfoRepository.QueryFirst(x => x.CodeStartStr == palletCode.Substring(0, 2));
            //    if (palletTypeInfo == null)
            //    {
            //        throw new Exception($"托盘号错误");
            //    }
            //    return palletTypeInfo.PalletType;
            //}
            //else if (warehouse.WarehouseCode == WarehouseEnum.HA57.ObjToString())
            //{
            //    Dt_PalletTypeInfo palletTypeInfo = _palletTypeInfoRepository.QueryFirst(x => x.CodeStartStr == palletCode.Substring(0, 3));
            //    if (palletTypeInfo == null)
            //    {
            //        throw new Exception($"托盘号错误");
            //    }
            //    return palletTypeInfo.PalletType;
            //}
            return -1;
            else
            {
                Dt_PalletTypeInfo palletTypeInfo = _palletTypeInfoRepository.QueryFirst(x => x.CodeStartStr == palletCode.Substring(0, 2));
                if (palletTypeInfo == null)
                {
                    throw new Exception($"托盘号错误");
                }
                return palletTypeInfo.PalletType;
            }
            return 0;
        }