刘磊
4 天以前 e3aaaa22a7cee2b7c7a33e98f843abc0cd15fb1b
ÏîÄ¿´úÂë/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/Task/Partial/Dt_TaskService.cs
@@ -1,11 +1,15 @@
using log4net.Core;
using Masuit.Tools;
using System.Collections.Generic;
using WIDESEA_Comm.AGVTask;
using WIDESEA_Comm.WCSInterface.Requst;
using WIDESEA_Common;
using WIDESEA_Core.Const;
using WIDESEA_DTO.WMS;
using WIDESEA_IStorageBasicRepository;
using WIDESEA_StorageBasicRepository;
using WIDESEA_StorageTaskRepository;
using WIDESEA_StoragIntegrationServices;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob.Models;
@@ -19,73 +23,104 @@
    /// </summary>
    /// <param name="input">请求模型</param>
    /// <returns>包含任务信息的响应内容</returns>
    public async Task<WebResponseContent> RequestInTask(RequestTaskDto input)
    public async Task<WebResponseContent> RequestInTask(SaveModel saveModel)
    {
        // åˆ›å»ºä¸€ä¸ªWebResponseContent对象
        WebResponseContent content = new WebResponseContent();
        try
        {
            string palletCode = saveModel.MainData["palletCode"].ToString();
            string station = saveModel.MainData["station"].ToString();
            // è°ƒç”¨BaseDal.QueryFirstAsync方法,查询任务
            var task = await BaseDal.QueryFirstAsync(x => x.PalletCode == input.PalletCode);
            var task = await BaseDal.QueryFirstAsync(x => x.PalletCode == palletCode);
            if (task != null)
            {
                {
                    WMSTaskDTO taskDTO = CreateTaskDTO(task);
                    return content.OK(data: taskDTO);
                }
                throw new Exception($"托盘{palletCode}已存在任务");
            }
            var stationInfo = _stationManagerRepository.QueryFirst(x => x.stationChildCode == station);
            if (stationInfo == null)
            {
                throw new Exception($"站台{station}不存在");
            }
            var boxingInfo = _boxingInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
            if (boxingInfo == null)
            {
                throw new Exception($"托盘{palletCode}组盘信息不存在");
            }
            if (boxingInfo.CurrentStatue != 1)
            {
                throw new Exception($"托盘{palletCode}当前状态不允许入库");
            }
            // èŽ·å–åº“ä½
            var location = RequestLocation(stationInfo.Roadway);
            if (location == null)
            {
                return content.Error("无法获取货位信息或库位已满");
            }
            string agvId = Guid.NewGuid().ToString().Replace("-", "").Take(16);
            var newtask = new Dt_Task
            {
                CurrentAddress = input.Position,
                CurrentAddress = station,
                Grade = 1,
                Roadway = input.Roadways,
                TargetAddress = input.Roadways,
                Roadway = stationInfo.Roadway,
                TargetAddress = location.LocationCode,
                Dispatchertime = DateTime.Now,
                MaterialNo = "",
                NextAddress = input.Roadways,
                NextAddress = stationInfo.Roadway,
                OrderNo = null,
                PalletCode = "",
                SourceAddress = input.Position,
                PalletCode = palletCode,
                SourceAddress = stationInfo.stationLocation,
                TaskState = (int)TaskInStatusEnum.InNew,
                TaskType = (int)TaskInboundTypeEnum.Inbound,
                TaskNum = await BaseDal.GetTaskNo(),
                Creater = "Systeam"
                Creater = "Systeam",
                AGVtaskId = agvId,
            };
            // å°è¯•添加新任务
            if (newtask == null) return content.Error();
            var taskId = await BaseDal.AddDataAsync(newtask);
            bool isResult = taskId > 0;
            if (isResult)
            addtask schedulingTask = new addtask
            {
                // åˆ›å»ºWMS任务
                WMSTaskDTO taskDTO = new WMSTaskDTO()
                {
                    TaskNum = newtask.TaskNum.Value,
                    Grade = newtask.Grade.Value,
                    PalletCode = newtask.PalletCode,
                    RoadWay = newtask.Roadway,
                    SourceAddress = newtask.SourceAddress,
                    TargetAddress = newtask.TargetAddress,
                    TaskState = newtask.TaskState.Value,
                    Id = 0,
                    TaskType = newtask.TaskType,
                };
                task_id = newtask.AGVtaskId,
                task_type = newtask.TaskType == (int)TaskTypeEnum.Inbound ? "push" : "pop",
                work_begin = newtask.SourceAddress,
                work_end = newtask.TargetAddress
            };
                //inWheelsInfo.Wheels_CurrentStatue = "1";
                //_InWheels_MesRepository.UpdateData(inWheelsInfo);
            string address = AGV_Interface + "add_task";
                content.OK(data: taskDTO);
            string result = HttpsClient.PostAsync(address, schedulingTask.ToDictionary()).Result;
            content = JsonConvert.DeserializeObject<WebResponseContent>(result);
            if (content.ack != 0)
            {
                Console.WriteLine($"请求RCS异常:{content.msg}");
                LogFactory.GetLog("下发AGV任务").Info(true, $"\r\r--------------------------------------");
                LogFactory.GetLog("下发AGV任务").Info(true, $"请求参数:{schedulingTask.ToJsonString()}");
                LogFactory.GetLog("下发AGV任务").Info(true, $"响应参数:{content.ToJsonString()}");
                throw new Exception(content.msg);
            }
            else
                content.Error("添加任务失败");
            return content;
            _unitOfWorkManage.BeginTran();
            BaseDal.AddData(newtask);
            location.LocationStatus = (int)LocationEnum.InStockDisable;
            _locationRepository.UpdateData(location);
            boxingInfo.CurrentStatue = 3;
            _boxingInfoRepository.UpdateData(boxingInfo);
            _unitOfWorkManage.CommitTran();
            content.OK("申请入库成功:请等待AGV取料");
        }
        catch (Exception err)
        catch (Exception er)
        {
            _unitOfWorkManage.RollbackTran();
            // å¦‚果发生异常,则调用content.Error方法,记录错误信息,并输出错误信息
            content.Error(err.Message);
            Console.WriteLine(err.Message);
            content.Error($"入库申请失败:{er.Message}");
            Console.WriteLine(er.Message);
        }
        // è¿”回content
        return content;
@@ -101,40 +136,19 @@
    /// <param name="requestTask"></param>
    /// <param name="locationInfos"></param>
    /// <returns></returns>
    public DtLocationInfo RequestLocation(RequestTaskDto requestTask, List<DtLocationInfo> locationInfos = null)
    public DtLocationInfo RequestLocation(string roadwayNo)
    {
        lock (objLOCK)
        {
            try
            {
                //List<DtLocationInfo> locations = new List<DtLocationInfo>();
                if (locationInfos == null || locationInfos.Count == 0)
                {
                    locationInfos = _locationRepository.QueryData(x => x.LocationStatus == (int)LocationEnum.Free && x.RoadwayNo == "SC1" && x.EnalbeStatus == 1 && x.LocationType == 1);
                }
                List<DtLocationInfo> locations = new List<DtLocationInfo>();
                locations = _locationRepository.QueryData(x => x.RoadwayNo == roadwayNo && x.LocationStatus == (int)LocationEnum.Free && x.EnalbeStatus == 1);   //&& x.LocationType == 1
                var location = GetEmptyLocation(locationInfos);
                if (location != null)
                {
                    if (location.Depth == 2)
                    {
                        int row = location.Row;
                        int relativeLine = row % 2 == 1 ? row + 1 : row - 1;
                        var insideLocation = _locationRepository.QueryFirst(x => x.Row == relativeLine && x.Layer == location.Layer && x.Column == location.Column);
                        if (insideLocation.LocationStatus != (int)LocationEnum.Free /*|| insideLocation.EnalbeStatus ==*/ )
                        {
                            locationInfos.Remove(location);
                            if (locationInfos.Count == 0) return null;
                            RequestLocation(requestTask, locationInfos);
                        }
                    }
                }
                var location = GetEmptyLocation(locations);
                if (location == null)
                {
                    return null;
                    throw new Exception("库位已满");
                }
                return location;
            }
@@ -149,7 +163,7 @@
    private DtLocationInfo GetEmptyLocation(List<DtLocationInfo> dtLocationInfos)
    {
        var locationinfo = dtLocationInfos.Where(x => x.LocationStatus == (int)LocationEnum.Free && x.RoadwayNo == "SC1" && x.EnalbeStatus == 1 && x.LocationType == 1).OrderBy(x => x.Layer).ThenByDescending(x => x.Depth).ThenBy(x => x.Row).ThenBy(x => x.Column).FirstOrDefault();
        var locationinfo = dtLocationInfos.Where(x => x.LocationStatus == (int)LocationEnum.Free && x.EnalbeStatus == 1).OrderBy(x => x.Layer).ThenByDescending(x => x.Depth).ThenBy(x => x.Row).ThenBy(x => x.Column).FirstOrDefault();
        return locationinfo;
    }
@@ -157,4 +171,25 @@
    #endregion èŽ·å–è´§ä½
    #endregion åº“位分配
    public WebResponseContent confirmTask(int taskNum)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            var taskInfo = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
            if (taskInfo != null)
            {
                BaseDal.DeleteData(taskInfo);
            }
            content.OK("确认完成");
        }
        catch (Exception ex)
        {
            content.Error($"确认异常:{ex.Message}");
        }
        return content;
    }
}