WMS
dengjunjie
2024-12-18 12e2d0e41d52f36d0746118e24b3fed228f92f98
´úÂë¹ÜÀí/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
@@ -16,6 +16,7 @@
#endregion << ç‰ˆ æœ¬ æ³¨ é‡Š >>
using AutoMapper;
using HslCommunication.WebSocket;
using MailKit.Search;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
@@ -31,12 +32,15 @@
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.CommonEnum;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Log;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Stock;
using WIDESEA_IBasicRepository;
@@ -58,30 +62,153 @@
    {
        private readonly IMapper _mapper;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IStockRepository _stockRepository;
        private readonly IBasicService _basicService;
        private readonly IOutboundService _outboundService;
        private readonly IInboundService _inboundService;
        private readonly IRecordService _recordService;
        private readonly IStockService _stockService;
        private readonly ITask_HtyService _taskHtyService;
        private readonly ILocationInfoService _locationInfoService;
        public ITaskRepository Repository => BaseDal;
        public TaskService(ITaskRepository BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IBasicService basicService, IOutboundService outboundService, IInboundService inboundService, IRecordService recordService, IStockService stockService, ITask_HtyService taskHtyService, ILocationInfoService locationInfoService) : base(BaseDal)
        public TaskService(ITaskRepository BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IStockRepository stockRepository, IBasicService basicService, IRecordService recordService) : base(BaseDal)
        {
            _mapper = mapper;
            _unitOfWorkManage = unitOfWorkManage;
            _stockRepository = stockRepository;
            _basicService = basicService;
            _outboundService = outboundService;
            _inboundService = inboundService;
            _recordService = recordService;
            _stockService = stockService;
            _taskHtyService = taskHtyService;
            _locationInfoService = locationInfoService;
        }
        public WebResponseContent RequestInboundTask(string palletCode, string stationCode)
        {
            try
            {
                Dt_Task task = Repository.QueryFirst(x => x.PalletCode == palletCode);
                if (task != null)
                {
                    return WebResponseContent.Instance.Error($"该托盘已生成任务");
                }
                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
                if (stockInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到组盘信息");
                }
                if (stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt())
                {
                    return WebResponseContent.Instance.Error($"该托盘状态不正确,不可申请入库");
                }
                if (!string.IsNullOrEmpty(stockInfo.LocationCode))
                {
                    return WebResponseContent.Instance.Error($"该托盘已绑定货位");
                }
                //todo é€šè¿‡ç«™å°å·æ‰¾å··é“号
                string roadwayNo = "RSC01";
                PalletTypeEnum palletType = PalletTypeEnum.SmallPallet;
                Dt_LocationInfo? locationInfo = _basicService.LocationInfoService.AssignLocation(roadwayNo, palletType);
                if (locationInfo == null)
                {
                    return WebResponseContent.Instance.Error($"货位分配失败,未找到可分配货位");
                }
                Dt_Task newTask = new Dt_Task()
                {
                    CurrentAddress = stationCode,
                    Grade = 0,
                    NextAddress = "",
                    PalletCode = palletCode,
                    Roadway = roadwayNo,
                    SourceAddress = stationCode,
                    TargetAddress = locationInfo.LocationCode,
                    TaskType = TaskTypeEnum.Inbound.ObjToInt(),
                    TaskStatus = InTaskStatusEnum.InNew.ObjToInt(),
                };
                LocationStatusEnum lastStatus = (LocationStatusEnum)locationInfo.LocationStatus;
                stockInfo.StockStatus = StockStatusEmun.入库确认.ObjToInt();
                _unitOfWorkManage.BeginTran();
                int taskId = BaseDal.AddData(newTask);
                newTask.TaskId = taskId;
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, lastStatus, LocationChangeType.InboundAssignLocation);
                _basicService.LocationInfoService.UpdateLocationStatus(locationInfo, palletType, LocationStatusEnum.Lock);
                _stockRepository.StockInfoRepository.UpdateData(stockInfo);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK(data: newTask);
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent InboundTaskCompleted(int taskNum)
        {
            try
            {
                Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
                if (task == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该任务信息");
                }
                if (task.TaskType != TaskTypeEnum.Inbound.ObjToInt())
                {
                    return WebResponseContent.Instance.Error($"任务类型错误");
                }
                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletCode == task.PalletCode).Includes(x => x.Details).First();
                if (stockInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到托盘对应的组盘信息");
                }
                if (!string.IsNullOrEmpty(stockInfo.LocationCode))
                {
                    return WebResponseContent.Instance.Error($"该托盘已绑定货位");
                }
                if (stockInfo.Details == null || stockInfo.Details.Count == 0)
                {
                    return WebResponseContent.Instance.Error($"未找到该托盘库存明细信息");
                }
                Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
                if (locationInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到目标货位信息");
                }
                if (locationInfo.LocationStatus == LocationStatusEnum.InStock.ObjToInt())
                {
                    return WebResponseContent.Instance.Error($"货位状态不正确");
                }
                LocationStatusEnum lastStatus = (LocationStatusEnum)locationInfo.LocationStatus;
                locationInfo.LocationStatus = LocationStatusEnum.InStock.ObjToInt();
                stockInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                stockInfo.LocationCode = locationInfo.LocationCode;
                _unitOfWorkManage.BeginTran();
                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId > 0 ? OperateTypeEnum.人工完成 : OperateTypeEnum.自动完成);
                _basicService.LocationInfoService.Repository.UpdateData(locationInfo);
                _stockRepository.StockInfoRepository.UpdateData(stockInfo);
                _recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, lastStatus, LocationChangeType.InboundCompleted);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
    }
}