wangxinhui
2025-08-30 cd78554c3da306ed8c866d971bee94457665f80c
ÏîÄ¿´úÂë/WMS/WMSServices/WIDESEA_TaskInfoService/TaskService_Inbound.cs
@@ -1,13 +1,16 @@
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WIDESEA_Common.CommonEnum;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.MaterielEnum;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
@@ -15,6 +18,7 @@
using WIDESEA_Core;
using WIDESEA_Core.Helper;
using WIDESEA_DTO;
using WIDESEA_DTO.Basic;
using WIDESEA_DTO.Task;
using WIDESEA_Model.Models;
@@ -22,123 +26,6 @@
{
    public partial class TaskService
    {
        /// <summary>
        /// å…¥ç©ºç®±
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="address"></param>
        /// <param name="WarehouseId"></param>
        /// <returns></returns>
        public WebResponseContent InEmpty(string barcode, string address, int WarehouseId)
        {
            try
            {
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == WarehouseId);
                if (warehouse == null)
                {
                    return WebResponseContent.Instance.Error($"未找到库区");
                }
                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == barcode && x.StockStatus != StockStatusEmun.出库完成.ObjToInt());
                if (stockInfo != null) throw new Exception($"托盘号已存在");
                stockInfo = new Dt_StockInfo()
                {
                    PalletCode = barcode,
                    StockStatus = StockStatusEmun.入库确认.ObjToInt(),
                    WarehouseId = WarehouseId,
                    PalletType = PalletTypeEnum.Empty.ObjToInt(),
                    Details = new List<Dt_StockInfoDetail>()
                };
                Dt_LocationInfo locationInfo = _basicRepository.LocationInfoRepository.QueryFirst(x => x.WarehouseId == WarehouseId);
                if (locationInfo == null) return WebResponseContent.Instance.Error($"未找到当前库区货位信息");
                locationInfo = _basicService.LocationInfoService.AssignLocation(locationInfo.RoadwayNo, ((PalletTypeEnum)stockInfo.PalletType).ObjToInt(), stockInfo.WarehouseId);
                if (locationInfo == null)
                {
                    return WebResponseContent.Instance.Error($"货位分配失败,未找到可分配货位");
                }
                Dt_Task newTask = new Dt_Task()
                {
                    CurrentAddress = address,
                    Grade = 0,
                    NextAddress = locationInfo.LocationCode,
                    PalletCode = barcode,
                    Roadway = locationInfo.RoadwayNo,
                    SourceAddress = address,
                    TargetAddress = locationInfo.LocationCode,
                    TaskType = TaskTypeEnum.InEmpty.ObjToInt(),
                    TaskStatus = TaskStatusEnum.New.ObjToInt(),
                    WarehouseId = stockInfo.WarehouseId,
                    PalletType = stockInfo.PalletType
                };
                locationInfo.LocationStatus = LocationStatusEnum.Lock.ObjToInt();
                _unitOfWorkManage.BeginTran();
                int taskId = BaseDal.AddData(newTask);
                newTask.TaskId = taskId;
                _basicRepository.LocationInfoRepository.UpdateData(locationInfo);
                _stockRepository.StockInfoRepository.AddData(stockInfo);
                _unitOfWorkManage.CommitTran();
                PushTasksToWCS(new List<Dt_Task> { newTask });
                PutFinish(address, newTask.PalletCode, newTask.TaskNum.ToString());
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="qty"></param>
        /// <param name="address"></param>
        /// <param name="WarehouseId"></param>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public WebResponseContent OutEmpty(int qty, string address, int WarehouseId, string barcode)
        {
            try
            {
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == WarehouseId);
                if (warehouse == null)
                {
                    return WebResponseContent.Instance.Error($"未找到库区");
                }
                List<Dt_StockInfo> stockInfos = null;
                if (string.IsNullOrEmpty(barcode))
                {
                    stockInfos = _stockRepository.StockInfoRepository.QueryData(x => x.WarehouseId == WarehouseId && x.PalletType == PalletTypeEnum.Empty.ObjToInt() && x.StockStatus == StockStatusEmun.入库完成.ObjToInt(), qty, nameof(Dt_StockInfo.CreateDate));
                }
                else
                {
                    stockInfos = _stockRepository.StockInfoRepository.QueryData(x => x.WarehouseId == WarehouseId && x.PalletType == PalletTypeEnum.Empty.ObjToInt() && x.StockStatus == StockStatusEmun.入库完成.ObjToInt() && x.PalletCode == barcode);
                    if (stockInfos.Count == 0) return WebResponseContent.Instance.Error($"{warehouse.WarehouseName}未找到空箱【{barcode}】");
                }
                if (stockInfos.Count < qty) return WebResponseContent.Instance.Error($"{warehouse.WarehouseName}空箱库存不足,库存数【{stockInfos.Count}】");
                List<Dt_Task> tasks = GetTasks(stockInfos, TaskTypeEnum.OutEmpty);
                stockInfos.ForEach(x =>
                {
                    x.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
                });
                tasks.ForEach(x =>
                {
                    x.TargetAddress = address;
                    x.NextAddress = address;
                });
                _unitOfWorkManage.BeginTran();
                BaseDal.AddData(tasks);
                _stockRepository.StockInfoRepository.UpdateData(stockInfos);
                _unitOfWorkManage.CommitTran();
                PushTasksToWCS(tasks);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// ä»…申请任务,让WCS根据路由确定下一地址
        /// </summary>
@@ -166,7 +53,7 @@
                {
                    return WebResponseContent.Instance.Error($"未找到组盘信息");
                }
                if (stockInfo.StockStatus != StockStatusEmun.退库.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.MES退库.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.MES空托退库.ObjToInt())
                if (stockInfo.StockStatus != StockStatusEmun.MES余料退库.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.MES空托退库.ObjToInt())
                {
                    return WebResponseContent.Instance.Error($"该托盘状态不正确,不可申请入库");
                }
@@ -195,22 +82,6 @@
                {
                    stockInfo.StockStatus = StockStatusEmun.手动组盘入库确认.ObjToInt();
                }
                else if (stockInfo.StockStatus == StockStatusEmun.MES退库.ObjToInt())
                {
                    newTask.TaskType = TaskTypeEnum.MesMatReturn.ObjToInt();
                }
                else if (stockInfo.StockStatus == StockStatusEmun.MES空托退库.ObjToInt())
                {
                    //todo å°æ‰˜ç›˜æš‚时未启用
                    if (stockInfo.PalletType == PalletTypeEnum.MediumPallet.ObjToInt())
                    {
                        newTask.TaskType = TaskTypeEnum.MesPalletLargeReturn.ObjToInt();
                    }
                    else
                    {
                        newTask.TaskType = TaskTypeEnum.MesPalletSmallReturn.ObjToInt();
                    }
                }
                else
                {
                    stockInfo.StockStatus = StockStatusEmun.入库确认.ObjToInt();
@@ -234,6 +105,246 @@
            }
        }
        /// <summary>
        /// åŽŸæ–™ç”³è¯·å…¥åº“
        /// </summary>
        /// <param name="stationCode">起始站点</param>
        /// <param name="palletCode">托盘</param>
        /// <returns></returns>
        public WebResponseContent RequestYLWMSTaskSimple(string stationCode, string palletCode)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_Task task = Repository.QueryFirst(x => x.PalletCode == palletCode);
                if (task != null)
                {
                    PushTasksToWCS(new List<Dt_Task> { task });
                    return content.OK($"该托盘已生成任务", _mapper.Map<WMSTaskDTO>(task));
                }
                if (Repository.QueryFirst(x => x.SourceAddress == stationCode && x.TaskStatus == TaskStatusEnum.New.ObjToInt()) != null)
                {
                    return content.Error($"该站点已有未执行的任务");
                }
                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
                if (stockInfo == null)
                {
                    return content.Error($"未找到组盘信息");
                }
                if (stockInfo.StockStatus != StockStatusEmun.手动组盘暂存.ObjToInt())
                {
                    return content.Error($"该托盘状态不正确,不可申请入库");
                }
                string rowWay = AssignYLRoadwayNo(stockInfo.PalletCode);
                if (string.IsNullOrEmpty(rowWay))
                {
                    return content.Error($"未找到可分配巷道");
                }
                Dt_Task newTask = new Dt_Task()
                {
                    CurrentAddress = stationCode,
                    Grade = 0,
                    NextAddress = "",
                    PalletCode = palletCode,
                    Roadway = rowWay,
                    SourceAddress = stationCode,
                    TargetAddress = "",
                    TaskType = TaskTypeEnum.Inbound.ObjToInt(),
                    TaskStatus = TaskStatusEnum.New.ObjToInt(),
                    WarehouseId = stockInfo.WarehouseId,
                    PalletType = stockInfo.PalletType,
                };
                if (stockInfo.StockStatus == StockStatusEmun.手动组盘暂存.ObjToInt())
                {
                    stockInfo.StockStatus = StockStatusEmun.手动组盘入库确认.ObjToInt();
                }
                _unitOfWorkManage.BeginTran();
                int taskId = BaseDal.AddData(newTask);
                newTask.TaskId = taskId;
                _stockRepository.StockInfoRepository.UpdateData(stockInfo);
                _unitOfWorkManage.CommitTran();
                PushTasksToWCS(new List<Dt_Task> { newTask });
                WMSTaskDTO wMSTaskDTO = _mapper.Map<WMSTaskDTO>(newTask);
                return content.OK(data: wMSTaskDTO);
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
        public string AssignYLRoadwayNo(string palletCode)
        {
            try
            {
                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
                if (stockInfo==null)
                {
                    throw new Exception($"组盘库存不存在");
                }
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == stockInfo.WarehouseId);
                if (warehouse == null)
                {
                    throw new Exception($"未找到巷道对应仓库信息");
                }
                string roadwayNo = "";
                if (stockInfo.MaterielThickness > 800)
                {
                    List<LocationCount> locationCounts = Db.Queryable<Dt_LocationInfo>().Where(x => x.WarehouseId == warehouse.WarehouseId && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && (x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt() || x.EnableStatus == EnableStatusEnum.Normal.ObjToInt()) && x.RoadwayNo.Contains("YL")).GroupBy(x => x.RoadwayNo).Select(x => new LocationCount { RoadwayNo = x.RoadwayNo, Count = SqlFunc.AggregateCount(x) }).ToList();
                    roadwayNo = locationCounts.OrderByDescending(x => x.Count).FirstOrDefault()?.RoadwayNo ?? "";
                    if (stockInfo.MaterielWide > 2200)
                    {
                        locationCounts = Db.Queryable<Dt_LocationInfo>().Where(x => x.WarehouseId == warehouse.WarehouseId && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && (x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt() || x.EnableStatus == EnableStatusEnum.Normal.ObjToInt()) && x.RoadwayNo=="SC02_YL").GroupBy(x => x.RoadwayNo).Select(x => new LocationCount { RoadwayNo = x.RoadwayNo, Count = SqlFunc.AggregateCount(x) }).ToList();
                        roadwayNo = locationCounts.OrderByDescending(x => x.Count).FirstOrDefault()?.RoadwayNo ?? "";
                    }
                }
                return !string.IsNullOrEmpty(roadwayNo) ? (roadwayNo) : throw new Exception("未找到可分配巷道");
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// å…¥åº“完成
        /// </summary>
        public WebResponseContent InboundTaskCompleted(Dt_Task task)
        {
            try
            {
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == task.WarehouseId);
                if (warehouse.WarehouseCode == WarehouseEnum.LLDCP.ToString()) //成品完成
                {
                    Dt_LocationInfo locationInfoEnd = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                    if (locationInfoEnd == null)
                    {
                        return WebResponseContent.Instance.Error($"未找到对应的终点货位信息");
                    }
                    Dt_AGVStationInfo agvstation = _agvStationInfoRepository.QueryFirst(x => x.AGVStationCode == task.SourceAddress && ( x.StationArea == AGVStationAreaEnum.AreaA.ObjToInt() || x.StationArea == AGVStationAreaEnum.AreaC.ObjToInt()));
                    if (agvstation != null)
                    {
                        agvstation.IsOccupied = WhetherEnum.False.ObjToInt();
                    }
                    Dt_ProStockInfo proStockInfo = _stockRepository.ProStockInfoRepository.Db.Queryable<Dt_ProStockInfo>().Where(x => x.PalletCode == task.PalletCode).Includes(x=>x.proStockInfoDetails).First();
                    if (proStockInfo != null && proStockInfo.StockStatus == StockStatusEmun.MES空托退库.ObjToInt())
                    {
                        task.TaskStatus = TaskStatusEnum.Finish.ObjToInt();
                        _unitOfWorkManage.BeginTran();
                        if (agvstation != null)
                        {
                            _agvStationInfoRepository.UpdateData(agvstation);
                        }
                        proStockInfo.LocationCode = locationInfoEnd.LocationCode;
                        proStockInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                        _stockRepository.ProStockInfoRepository.UpdateData(proStockInfo);
                        _basicService.LocationInfoService.UpdateLocationStatus(locationInfoEnd, proStockInfo.PalletType, LocationStatusEnum.InStock, proStockInfo.WarehouseId);
                        BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId > 0 ? WIDESEA_Core.Enums.OperateTypeEnum.人工完成 : WIDESEA_Core.Enums.OperateTypeEnum.自动完成);
                        _unitOfWorkManage.CommitTran();
                    }
                    else if (proStockInfo != null && proStockInfo.StockStatus == StockStatusEmun.手动组盘入库确认.ObjToInt())
                    {
                        task.TaskStatus = TaskStatusEnum.Finish.ObjToInt();
                        proStockInfo.proStockInfoDetails.ForEach(x =>
                        {
                            x.ProStockDetailStatus = StockStatusEmun.入库完成.ObjToInt();
                        });
                        _unitOfWorkManage.BeginTran();
                        if (agvstation != null)
                        {
                            _agvStationInfoRepository.UpdateData(agvstation);
                        }
                        proStockInfo.LocationCode = locationInfoEnd.LocationCode;
                        proStockInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                        _stockRepository.ProStockInfoRepository.UpdateData(proStockInfo);
                        _stockRepository.ProStockInfoDetailRepository.UpdateData(proStockInfo.proStockInfoDetails);
                        _basicService.LocationInfoService.UpdateLocationStatus(locationInfoEnd, proStockInfo.PalletType, LocationStatusEnum.InStock, proStockInfo.WarehouseId);
                        BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId > 0 ? WIDESEA_Core.Enums.OperateTypeEnum.人工完成 : WIDESEA_Core.Enums.OperateTypeEnum.自动完成);
                        _unitOfWorkManage.CommitTran();
                    }
                    else
                    {
                        return WebResponseContent.Instance.Error($"未找到对应库存信息");
                    }
                }
                else//原料库完成
                {
                    Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.PalletCode == task.PalletCode);
                    if (stockInfo == null)
                    {
                        return WebResponseContent.Instance.Error($"未找到对应库存信息");
                    }
                    Dt_LocationInfo locationInfoEnd = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                    if (locationInfoEnd == null)
                    {
                        return WebResponseContent.Instance.Error($"未找到对应的终点货位信息");
                    }
                    task.TaskStatus = TaskStatusEnum.Finish.ObjToInt();
                    _unitOfWorkManage.BeginTran();
                    if (task.TaskType == TaskTypeEnum.InPick.ObjToInt())
                    {
                        Dt_LocationInfo locationInfoStart = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
                        _basicService.LocationInfoService.UpdateLocationStatus(locationInfoStart, stockInfo.PalletType, LocationStatusEnum.Free, stockInfo.WarehouseId);
                    }
                    stockInfo.LocationCode = locationInfoEnd.LocationCode;
                    stockInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                    _stockService.StockInfoService.Repository.UpdateData(stockInfo);
                    _basicService.LocationInfoService.UpdateLocationStatus(locationInfoEnd, stockInfo.PalletType, LocationStatusEnum.InStock, stockInfo.WarehouseId);
                    BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId > 0 ? WIDESEA_Core.Enums.OperateTypeEnum.人工完成 : WIDESEA_Core.Enums.OperateTypeEnum.自动完成);
                    _unitOfWorkManage.CommitTran();
                }
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// åˆ†é…æˆå“å··é“
        /// </summary>
        /// <returns></returns>
        public string AssignCPRoadwayNo()
        {
            try
            {
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == 2);
                if (warehouse == null)
                {
                    throw new Exception($"未找到巷道对应仓库信息");
                }
                string roadwayNo = "";
                List<LocationCount> locationCounts = Db.Queryable<Dt_LocationInfo>().Where(x => x.WarehouseId == warehouse.WarehouseId && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && (x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt() || x.EnableStatus == EnableStatusEnum.Normal.ObjToInt()) && x.RoadwayNo.Contains("CP")).GroupBy(x => x.RoadwayNo).Select(x => new LocationCount { RoadwayNo = x.RoadwayNo, Count = SqlFunc.AggregateCount(x) }).ToList();
                //筛选当前入库任务中已分配巷道任务数量
                List<LocationCount> useLocationCounts = Db.Queryable<Dt_Task>().Where(x => x.WarehouseId == warehouse.WarehouseId
                && locationCounts.Select(j=>j.RoadwayNo).Contains(x.Roadway)
                && TaskInboundTypes.Contains(x.TaskType)).GroupBy(x => x.Roadway).Select(x => new LocationCount { RoadwayNo = x.Roadway, Count = SqlFunc.AggregateCount(x) }).ToList();
                foreach (var item in locationCounts)
                {
                    LocationCount? count = useLocationCounts.FirstOrDefault(x => x.RoadwayNo == item.RoadwayNo);
                    if (count!=null)
                    {
                        item.Count -= count.Count;
                    }
                }
                roadwayNo = locationCounts.OrderByDescending(x => x.Count).FirstOrDefault()?.RoadwayNo ?? "";
                return !string.IsNullOrEmpty(roadwayNo) ? (roadwayNo) : throw new Exception("未找到可分配巷道");
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// å…¥åº“任务申请分配货位
        /// </summary>
        /// <param name="taskNum">任务号</param>
@@ -253,7 +364,7 @@
                {
                    return WebResponseContent.Instance.OK(data: task.TargetAddress);
                }
                //分配货位
                Dt_LocationInfo? locationInfo = _basicService.LocationInfoService.AssignLocation(roadwayNo, task.PalletType, task.WarehouseId);
                if (locationInfo == null)
                {
@@ -270,10 +381,10 @@
                {
                    task.TaskStatus = TaskStatusEnum.SC_Execute.ObjToInt();
                }
                LocationStatusEnum lastStatus = (LocationStatusEnum)locationInfo.LocationStatus;
                //更新锁定货位
                _unitOfWorkManage.BeginTran();
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, lastStatus, LocationStatusEnum.Lock, LocationChangeType.InboundAssignLocation);
                _basicService.LocationInfoService.UpdateLocationStatus(locationInfo, task.PalletType, LocationStatusEnum.Lock, task.WarehouseId);
@@ -335,30 +446,27 @@
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public readonly string[] OutStartPonits = { "5230", "5237", "5244" };
        /// <summary>
        /// æˆå“ä½™æ–™é€€åº“入仓
        /// ç©ºæ¡†å›žåº“
        /// </summary>
        /// <param name="barCode"></param>
        /// <param name="startPoint"></param>
        /// <returns></returns>
        public WebResponseContent BackProductTask(string barCode, string startPoint)
        public WebResponseContent EmptyBackTask(string barCode, string startPoint)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                string palletCode = @"^C\d{5}$";  // æ­£åˆ™è¡¨è¾¾å¼
                bool isValid = Regex.IsMatch(barCode, palletCode);
                if (!isValid)
                {
                    return content.Error($"框码格式错误{barCode}");
                }
                if (!OutStartPonits.Contains(startPoint))
                //string palletCode = @"^C\d{5}$";  // æ­£åˆ™è¡¨è¾¾å¼
                //bool isValid = Regex.IsMatch(barCode, palletCode);
                //if (!isValid)
                //{
                //    return content.Error($"框码格式错误{barCode}");
                //}
                Dt_AGVStationInfo agvstation = _agvStationInfoRepository.QueryFirst(x=>x.AGVStationCode==startPoint);
                if (agvstation==null)
                {
                    return content.Error($"起点错误{startPoint}");
                }
                //判断当前点位是否重复
                Dt_Task taskOldPoint = BaseDal.QueryFirst(x=>x.SourceAddress==startPoint && (x.TaskStatus == TaskStatusEnum.New.ObjToInt()||x.TaskStatus==TaskStatusEnum.Line_Execute.ObjToInt()|| x.TaskStatus == TaskStatusEnum.Line_Executing.ObjToInt() || x.TaskStatus == TaskStatusEnum.AGV_Takeing.ObjToInt()));
                Dt_Task taskOldPoint = BaseDal.QueryFirst(x => x.SourceAddress == startPoint &&( x.TaskStatus == TaskStatusEnum.New.ObjToInt() || x.TaskStatus==TaskStatusEnum.AGV_Executing.ObjToInt()));
                if (taskOldPoint != null)
                {
                    return content.Error($"站点{startPoint}已存在任务");
@@ -366,76 +474,225 @@
                Dt_Task taskOld = BaseDal.QueryFirst(x => x.PalletCode == barCode);
                if (taskOld != null)
                {
                    return content.Error($"胶框{barCode}任务已存在");
                    return content.Error($"托盘{barCode}任务已存在");
                }
                //获取胶框余料
                Dt_ProStockInfo proStockInfo = _stockRepository.ProStockInfoRepository.Db.Queryable<Dt_ProStockInfo>().Where(x=>x.PalletCode== barCode).Includes(x=>x.proStockInfoDetails).First();
                if (proStockInfo==null)
                Dt_ProStockInfo proStockInfoOld = _stockRepository.ProStockInfoRepository.QueryFirst(x => x.PalletCode == barCode);
                if (proStockInfoOld!=null)
                {
                    return content.Error($"未找到{barCode}胶框信息");
                    return content.Error($"托盘{barCode}已存在");
                }
                if (proStockInfo.proStockInfoDetails==null || proStockInfo.proStockInfoDetails.Count<=0)
                //分配巷道
                string roadWay=AssignCPRoadwayNo();
                //获取成品库
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.LLDCP.ToString());
                Dt_ProStockInfo proStockInfo = new Dt_ProStockInfo()
                {
                    return content.Error($"{barCode}胶框信息为空");
                }
                if (proStockInfo.StockStatus!=StockStatusEmun.出库完成.ObjToInt())
                {
                    return content.Error($"{barCode}胶框信息状态异常");
                }
                //获取成品平库
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA101.ToString());
                if (proStockInfo.WarehouseId != warehouse.WarehouseId && proStockInfo.LocationCode!= "成品包装拣货区")
                {
                    return content.Error($"{barCode}胶框信息不在拣货区");
                }
                //获取当前的库存属性
                Dt_Warehouse? warePoint = null;
                switch (proStockInfo.ProStockAttribute)
                {
                    case (int)ProStockAttributeEnum.成品:
                        warePoint = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA71.ToString());
                        break;
                    case (int)ProStockAttributeEnum.尾数:
                        warePoint = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA72.ToString());
                        break;
                    case (int)ProStockAttributeEnum.研发:
                        warePoint = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA73.ToString());
                        break;
                    default:
                        throw new Exception($"未找到{barCode}胶框信息库存属性");
                }
                Dt_Warehouse warehouseLocation = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA71.ToString());
                Dt_LocationInfo locationInfo = _basicRepository.LocationInfoRepository.QueryFirst(x => x.WarehouseId == warehouseLocation.WarehouseId);
                if (locationInfo == null)
                {
                    return content.Error($"未找到{warehouse.WarehouseCode}货位信息");
                }
                proStockInfo.StockStatus = StockStatusEmun.成品余料回库.ObjToInt();
                proStockInfo.proStockInfoDetails.ForEach(x =>
                {
                    x.ProOutDetailStatus = StockStatusEmun.成品余料回库.ObjToInt();
                });
                    PalletCode=barCode,
                    ProStockAttribute=ProStockAttributeEnum.空托.ObjToInt(),
                    PalletType=1,
                    LocationCode="",
                    WarehouseId=warehouse.WarehouseId,
                    StockStatus=StockStatusEmun.MES空托退库.ObjToInt()
                };
                Dt_Task newTask = new Dt_Task()
                {
                    CurrentAddress = startPoint,
                    Grade = 0,
                    NextAddress = "",
                    PalletCode = proStockInfo.PalletCode,
                    Roadway = locationInfo.RoadwayNo,
                    PalletCode = barCode,
                    Roadway = roadWay,
                    SourceAddress = startPoint,
                    TargetAddress = "",
                    TaskType = TaskTypeEnum.InProductBack.ObjToInt(),
                    TaskType = TaskTypeEnum.EmptyProductBack.ObjToInt(),
                    TaskStatus = TaskStatusEnum.New.ObjToInt(),
                    WarehouseId = warePoint.WarehouseId,
                    PalletType = proStockInfo.PalletType,
                    MaterielCode = proStockInfo.proStockInfoDetails.Where(x => x.ProStockId == proStockInfo.Id).FirstOrDefault()?.ProductCode,
                    Quantity = (float)proStockInfo.proStockInfoDetails.Where(x => x.ProStockId == proStockInfo.Id).Sum(x => x.StockPcsQty)
                    WarehouseId = warehouse.WarehouseId,
                    PalletType = 1
                };
                _unitOfWorkManage.BeginTran();
                int taskId = BaseDal.AddData(newTask);
                _stockRepository.ProStockInfoRepository.AddData(proStockInfo);
                newTask.TaskId = taskId;
                _stockRepository.ProStockInfoRepository.UpdateData(proStockInfo);
                _stockRepository.ProStockInfoDetailRepository.UpdateData(proStockInfo.proStockInfoDetails);
                _unitOfWorkManage.CommitTran();
                //推送任务
                PushTasksToWCS(new List<Dt_Task> { newTask },"AGV");
                content.OK("发送成功");
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;
        }
        private readonly static object _lockerFL = new object();
        /// <summary>
        /// è¾…料入库
        /// </summary>
        /// <returns></returns>
        public WebResponseContent InboundFLTask(string barCode, string startPoint,string matCode,int matCount)
        {
            lock (_lockerFL)
            {
                WebResponseContent content = new WebResponseContent();
                try
                {
                    //string palletCode = @"^C\d{5}$";  // æ­£åˆ™è¡¨è¾¾å¼
                    //bool isValid = Regex.IsMatch(barCode, palletCode);
                    //if (!isValid)
                    //{
                    //    return content.Error($"框码格式错误{barCode}");
                    //}
                    Dt_MaterielInfo materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterielCode == matCode && x.MaterielInvOrgId == MaterielInvOrgEnum.新厂.ObjToInt() && x.MaterielSourceType == MaterielTypeEnum.辅料备件.ObjToInt());
                    if (materielInfo == null)
                    {
                        return content.Error($"未找到物料信息{matCode}");
                    }
                    Dt_AGVStationInfo agvstation = _agvStationInfoRepository.QueryFirst(x => x.AGVStationCode == startPoint);
                    if (agvstation == null)
                    {
                        return content.Error($"起点错误{startPoint}");
                    }
                    //判断当前点位是否重复
                    Dt_Task taskOldPoint = BaseDal.QueryFirst(x => x.SourceAddress == startPoint && (x.TaskStatus == TaskStatusEnum.New.ObjToInt() || x.TaskStatus == TaskStatusEnum.AGV_Executing.ObjToInt()));
                    if (taskOldPoint != null)
                    {
                        return content.Error($"站点{startPoint}已存在任务");
                    }
                    Dt_Task taskOld = BaseDal.QueryFirst(x => x.PalletCode == barCode);
                    if (taskOld != null)
                    {
                        return content.Error($"托盘{barCode}任务已存在");
                    }
                    Dt_ProStockInfo proStockInfoOld = _stockRepository.ProStockInfoRepository.QueryFirst(x => x.PalletCode == barCode);
                    if (proStockInfoOld != null)
                    {
                        return content.Error($"托盘{barCode}已存在");
                    }
                    //分配巷道
                    string roadWay = AssignCPRoadwayNo();
                    //获取成品库
                    Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.LLDCP.ToString());
                    Dt_ProStockInfoDetail proStockInfoDetail = new Dt_ProStockInfoDetail()
                    {
                        ProductCode = matCode,
                        StockQty = matCount,
                        ProStockDetailStatus = StockStatusEmun.手动组盘入库确认.ObjToInt()
                    };
                    Dt_ProStockInfo proStockInfo = new Dt_ProStockInfo()
                    {
                        PalletCode = barCode,
                        ProStockAttribute = ProStockAttributeEnum.辅料备件.ObjToInt(),
                        PalletType = 1,
                        LocationCode = "",
                        WarehouseId = warehouse.WarehouseId,
                        StockStatus = StockStatusEmun.手动组盘入库确认.ObjToInt(),
                        proStockInfoDetails = new List<Dt_ProStockInfoDetail> { proStockInfoDetail }
                    };
                    Dt_Task newTask = new Dt_Task()
                    {
                        CurrentAddress = startPoint,
                        Grade = 0,
                        NextAddress = "",
                        PalletCode = barCode,
                        Roadway = roadWay,
                        SourceAddress = startPoint,
                        TargetAddress = "",
                        TaskType = TaskTypeEnum.Inbound.ObjToInt(),
                        TaskStatus = TaskStatusEnum.New.ObjToInt(),
                        WarehouseId = warehouse.WarehouseId,
                        PalletType = 1
                    };
                    _unitOfWorkManage.BeginTran();
                    int taskId = BaseDal.AddData(newTask);
                    BaseDal.Db.InsertNav(proStockInfo).Include(x => x.proStockInfoDetails).ExecuteCommand();
                    newTask.TaskId = taskId;
                    _unitOfWorkManage.CommitTran();
                    //推送任务
                    PushTasksToWCS(new List<Dt_Task> { newTask }, "AGV");
                    content.OK("发送成功");
                }
                catch (Exception ex)
                {
                    _unitOfWorkManage.RollbackTran();
                    content.Error(ex.Message);
                }
                return content;
            }
        }
        /// <summary>
        /// å°åˆ·ä½™æ–™é€€æ–™ä»»åŠ¡
        /// </summary>
        public WebResponseContent PrintBackInbound(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var barcode = saveModel.MainData["palletCode"].ToString();
                var warehouseId = saveModel.MainData["warehouseId"].ObjToInt();
                var materSn = saveModel.MainData["materSn"];
                //直径
                var thickness = saveModel.MainData["thickness"].ObjToInt();
                var weight = saveModel.MainData["weight"].ObjToInt();
                var address = saveModel.MainData["address"].ToString();
                //幅宽
                var wide = saveModel.MainData["wide"].ObjToInt();
                Dt_MaterielInfo? materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterielCode == materSn);
                if (materielInfo == null)
                {
                    return content.Error("未找到物料信息");
                }
                Dt_StockInfo stockInfoOld = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == barcode);
                if (stockInfoOld != null)
                {
                    return content.Error("托盘信息已存在");
                }
                //生成库存组盘信息
                Dt_StockInfo stockInfo = new Dt_StockInfo()
                {
                    MaterielInvOrgId = materielInfo.MaterielInvOrgId,
                    PalletCode = barcode,
                    LocationCode = "",
                    PalletType = 1,
                    WarehouseId = warehouseId,
                    StockAttribute = materielInfo.MaterielSourceType,
                    StockStatus = StockStatusEmun.手动组盘入库确认.ObjToInt(),
                    MaterielSpec = materielInfo.MaterielSpec,
                    Unit = materielInfo.MaterielUnit,
                    MaterielThickness = thickness,
                    MaterielWide = wide,
                    MaterielWeight = weight,
                    MaterielCode = materielInfo.MaterielCode,
                    MaterielName = materielInfo.MaterielName,
                };
                if (wide > 1200)
                {
                    stockInfo.PalletType = 2;
                }
                //生成印刷余料退料任务
                Dt_Task newTask = new Dt_Task()
                {
                    CurrentAddress = address,
                    Grade = 0,
                    NextAddress = "",
                    PalletCode = barcode,
                    Roadway = "",
                    SourceAddress = address,
                    TargetAddress = "",
                    TaskType = TaskTypeEnum.PrintYLBackInbound.ObjToInt(),
                    TaskStatus = TaskStatusEnum.New.ObjToInt(),
                    WarehouseId = warehouseId,
                    PalletType = 1,
                    TaskLength= wide
                };
                _unitOfWorkManage.BeginTran();
                int taskId = BaseDal.AddData(newTask);
                _stockRepository.StockInfoRepository.AddData(stockInfo);
                newTask.TaskId = taskId;
                _unitOfWorkManage.CommitTran();
                //推送任务
                PushTasksToWCS(new List<Dt_Task> { newTask });
@@ -449,66 +706,78 @@
            return content;
        }
        /// <summary>
        /// ç©ºæ¡†å›žåº“
        /// å°åˆ·ä¸‹æ–™ä»»åŠ¡
        /// </summary>
        public WebResponseContent EmptyBackTask(string barCode, string startPoint)
        public WebResponseContent PrintInbound(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                string palletCode = @"^C\d{5}$";  // æ­£åˆ™è¡¨è¾¾å¼
                bool isValid = Regex.IsMatch(barCode, palletCode);
                if (!isValid)
                var barcode = saveModel.MainData["palletCode"].ToString();
                var warehouseId = saveModel.MainData["warehouseId"].ObjToInt();
                var materSn = saveModel.MainData["materSn"];
                //直径
                var thickness = saveModel.MainData["thickness"].ObjToInt();
                var weight = saveModel.MainData["weight"].ObjToInt();
                var address = saveModel.MainData["address"].ToString();
                //幅宽
                var wide = saveModel.MainData["wide"].ObjToInt();
                Dt_MaterielInfo? materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterielCode == materSn);
                if (materielInfo == null)
                {
                    return content.Error($"框码格式错误{barCode}");
                    return content.Error("未找到物料信息");
                }
                if (!OutStartPonits.Contains(startPoint))
                if (materielInfo.MaterielSourceType!=MaterielTypeEnum.半成品.ObjToInt())
                {
                    return content.Error($"起点错误{startPoint}");
                    return content.Error($"未找到物料{materielInfo.MaterielCode}半成品信息");
                }
                //判断当前点位是否重复
                Dt_Task taskOldPoint = BaseDal.QueryFirst(x => x.SourceAddress == startPoint && (x.TaskStatus == TaskStatusEnum.New.ObjToInt() || x.TaskStatus == TaskStatusEnum.Line_Execute.ObjToInt() || x.TaskStatus == TaskStatusEnum.Line_Executing.ObjToInt()));
                //if (taskOldPoint != null)
                //{
                //    return content.Error($"站点{startPoint}已存在任务");
                //}
                Dt_Task taskOld = BaseDal.QueryFirst(x => x.PalletCode == barCode);
                if (taskOld != null)
                Dt_StockInfo stockInfoOld = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == barcode);
                if (stockInfoOld != null)
                {
                    return content.Error($"胶框{barCode}任务已存在");
                    return content.Error("托盘信息已存在");
                }
                //if (_stockRepository.ProStockInfoRepository.QueryFirst(x => x.PalletCode == barCode) != null)
                //{
                //    return content.Error($"胶框{barCode}库存信息已存在");
                //}
                //todo:临时解绑
                Dt_ProStockInfo proStockInfo = _stockRepository.ProStockInfoRepository.QueryFirst(x => x.PalletCode == barCode);
                if (_stockRepository.ProStockInfoRepository.QueryFirst(x => x.PalletCode == barCode) != null)
                //生成库存组盘信息
                Dt_StockInfo stockInfo = new Dt_StockInfo()
                {
                    proStockInfo.PalletCode = proStockInfo.PalletCode + ":" + DateTime.Now.ToString("MM/dd");
                    MaterielInvOrgId = materielInfo.MaterielInvOrgId,
                    PalletCode = barcode,
                    LocationCode = "",
                    PalletType = 1,
                    WarehouseId = warehouseId,
                    StockAttribute = materielInfo.MaterielSourceType,
                    StockStatus = StockStatusEmun.手动组盘入库确认.ObjToInt(),
                    MaterielSpec = materielInfo.MaterielSpec,
                    Unit = materielInfo.MaterielUnit,
                    MaterielThickness = thickness,
                    MaterielWide = wide,
                    MaterielWeight = weight,
                    MaterielCode = materielInfo.MaterielCode,
                    MaterielName = materielInfo.MaterielName,
                };
                if (wide > 1200)
                {
                    stockInfo.PalletType = 2;
                }
                //获取成品平库
                Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA101.ToString());
                //生成印刷半成品入库任务
                Dt_Task newTask = new Dt_Task()
                {
                    CurrentAddress = startPoint,
                    CurrentAddress = address,
                    Grade = 0,
                    NextAddress = "",
                    PalletCode = barCode,
                    Roadway = "CL01_CP",
                    SourceAddress = startPoint,
                    PalletCode = barcode,
                    Roadway = "",
                    SourceAddress = address,
                    TargetAddress = "",
                    TaskType = TaskTypeEnum.EmptyProductBack.ObjToInt(),
                    TaskType = TaskTypeEnum.PrintYLInbound.ObjToInt(),
                    TaskStatus = TaskStatusEnum.New.ObjToInt(),
                    WarehouseId = warehouse.WarehouseId,
                    PalletType = 1
                    WarehouseId = warehouseId,
                    PalletType = 1,
                    TaskLength = wide
                };
                _unitOfWorkManage.BeginTran();
                int taskId = BaseDal.AddData(newTask);
                if (proStockInfo!=null)
                {
                    _stockRepository.ProStockInfoRepository.UpdateData(proStockInfo);
                }
                _stockRepository.StockInfoRepository.AddData(stockInfo);
                newTask.TaskId = taskId;
                _unitOfWorkManage.CommitTran();
                //推送任务
@@ -517,6 +786,7 @@
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content.Error(ex.Message);
            }
            return content;