SendWcsTaskAction.cs 26.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
using System;
using System.Collections.Generic;
using System.Data;
using Infrastructure;
using Quartz;
using WebApp;
using WebRepository;
using System.Linq;

namespace WebMvc
{
    /// <summary>
    /// 单据结果回传
    /// </summary>

    /// <summary>
    /// 入库单结果回传
    /// </summary>
    [PersistJobDataAfterExecution]
    [DisallowConcurrentExecution]
    public class SendWcsTaskAction
    {
        private string ConnString { set; get; }
        IJobExecutionContext Context { set; get; }

        public SendWcsTaskAction(string _ConnString, IJobExecutionContext _Context)
        {
            ConnString = _ConnString;
            Context = _Context;
        }

        public void Execute(JobContainer jobContainer)
        {
            UnitWork _unitWork = new UnitWork(AppSettingsJson.JobContext(ConnString));
            #region 任务下发逻辑
            try
            {
                List<TaskDetail> taskDetails = (from td in _unitWork.Find<TaskDetail>(n => n.Status == TaskStatus.待下发任务)
                                                join con in _unitWork.Find<Container>(n => n.Code != null)
                                                on td.ContainerCode equals con.Code
                                                where (new string[] { TaskType.容器出库, TaskType.直接出库 }.Contains(td.TaskType) && con.IsLock == 0) || (new string[] { TaskType.空容器入库, TaskType.容器回库, TaskType.站台到站台, TaskType.空容器出库, TaskType.盘点 }.Contains(td.TaskType))
                                                select td).OrderByDescending(n => n.Priority).ToList();
                List<TaskDetail> SStaskDetails = _unitWork.Find<TaskDetail>(n =>
                        n.Status == TaskStatus.待下发任务 && n.TaskType == TaskType.站台到站台 && n.ContainerCode == "empty")
                    .ToList();
                taskDetails = taskDetails.Concat(SStaskDetails).ToList();

                foreach (TaskDetail taskDetail in taskDetails)
                {
                    //创建移库任务
                    if ((taskDetail.TaskType == TaskType.容器出库 || taskDetail.TaskType == TaskType.直接出库 || taskDetail.TaskType == TaskType.空容器出库) && taskDetail.Roadway != 2)
                    {
                        var locContainerList = _unitWork.Find<Location>(n => n.ContainerCode == taskDetail.ContainerCode).ToList();
                        if (locContainerList.Count == 1)
                        {
                            var locContainer = locContainerList.FirstOrDefault();
                            if (locContainer.Code == taskDetail.SourceLocation)
                            {
                                //邻仓位,需要移库的托盘
                                Location CLoc = _unitWork.Find<Location>(n => n.Code == locContainer.ContiguousCode && n.ContainerCode != "" && n.Status != LocationStatus.空仓位 && n.Roadway != 2).FirstOrDefault();
                                if (CLoc != null)
                                {
                                    if (locContainer.RowIndex == 1)
                                    {
                                        Container con = _unitWork.Find<Container>(n => n.Code == CLoc.ContainerCode).FirstOrDefault();
                                        if (con == null)
                                        {
                                            taskDetail.Error = "无临仓位托盘";
                                            _unitWork.Update(taskDetail);
                                            continue;
                                        }

                                        if (_unitWork.IsExist<TaskDetail>(n => n.ContainerCode == CLoc.ContainerCode) || CLoc.Status == LocationStatus.任务锁定中)
                                        {
                                            taskDetail.Error = "邻仓位:" + CLoc.Code + "有任务";
                                            _unitWork.Update(taskDetail);
                                            continue;
                                        }
                                        Location loc = null;
                                        while (true)
                                        {
                                            var tasks = _unitWork.Find<TaskDetail>(n => n.TaskNo != null).ToList();
                                            loc = (from loca in _unitWork.Find<Location>(n => !tasks.Select(a => a.DestinationLocation).Contains(n.ContiguousCode) && !tasks.Select(a => a.SourceLocation).Contains(n.ContiguousCode) && n.Status == LocationStatus.空仓位 && n.IsStop == false && n.MaxHeight >= CLoc.MaxHeight && n.Roadway == CLoc.Roadway && n.ContainerCode == "")
                                                   join cLoca in _unitWork.Find<Location>(n => n.Status != LocationStatus.任务锁定中 && n.IsStop == false && n.MaxHeight >= CLoc.MaxHeight && n.Roadway == CLoc.Roadway)
                                                   on loca.ContiguousCode equals cLoca.Code
                                                   select loca)
                                                 .OrderBy(n => n.RowIndex == 1 ? 0 : n.RowIndex).ThenBy(n => Math.Abs((decimal)n.Row - (decimal)CLoc.Row)).ThenBy(b => b.MaxHeight).ThenBy(a => a.Layer).ThenBy(y => y.Row).FirstOrDefault();
                                            //更新仓位对应的容器,如果回库分配的不是原仓位则更新仓位且锁定
                                            if (loc == null)
                                            {
                                                taskDetail.Error = "未找到邻仓位" + CLoc.Code + "的移库仓位";
                                                _unitWork.Update(taskDetail);
                                                break;
                                            }
                                            else
                                            {
                                                int i = _unitWork.Update<Location>(n => n.Id == loc.Id && n.Version == loc.Version, n => new Location
                                                {
                                                    Status = LocationStatus.任务锁定中,
                                                    ContainerCode = CLoc.ContainerCode,
                                                    UpdateTime = DateTime.Now
                                                });

                                                int j = _unitWork.Update<Location>(n => n.Id == CLoc.Id && n.Version == CLoc.Version, n => new Location
                                                {
                                                    Status = LocationStatus.任务锁定中,
                                                    ContainerCode = CLoc.ContainerCode,
                                                    UpdateTime = DateTime.Now
                                                });

                                                if (j != 1)
                                                {
                                                    taskDetail.Error = "邻仓位更改失败";
                                                    _unitWork.Update(taskDetail);
                                                    break;
                                                }

                                                if (i == 1)
                                                {
                                                    break;
                                                }
                                            }
                                        }

                                        if (loc == null)
                                        {
                                            continue;
                                        }

                                        //建立移库任务
                                        Task task = new Task();
                                        string taskNo;
                                        TaskDetail taskDetailMove = new TaskDetail();
                                        taskNo = loc.Code + _unitWork.GetTaskNo(TaskNo.移库);
                                        task.TaskNo = taskNo;
                                        task.OrderCode = taskNo;
                                        task.BusinessType = BusinessType.入库_其他入库单;
                                        task.FirstStatus = TaskStatus.下达任务;
                                        task.LastStatus = TaskStatus.下达任务;
                                        task.CreateTime = DateTime.Now;
                                        _unitWork.Add(task);

                                        taskDetailMove.TaskNo = taskNo;
                                        taskDetailMove.OrderCode = taskNo;
                                        taskDetailMove.SourceCode = taskDetail.TaskNo;
                                        taskDetailMove.TaskType = TaskType.移库;
                                        taskDetailMove.ContainerCode = CLoc.ContainerCode;
                                        taskDetailMove.SourceLocation = CLoc.Code;
                                        taskDetailMove.DestinationLocation = loc.Code;
                                        taskDetailMove.OderQty = 0;
                                        taskDetailMove.ContainerQty = 0;
                                        taskDetailMove.HadQty = 0;
                                        taskDetailMove.MaterialName = "1";
                                        taskDetailMove.Roadway = taskDetail.Roadway;
                                        taskDetailMove.Station = null;
                                        taskDetailMove.Priority = 999;
                                        taskDetailMove.CreateTime = DateTime.Now;

                                        List<Inventory> inventories = _unitWork.Find<Inventory>(n => n.ContainerCode == taskDetailMove.ContainerCode).ToList();
                                        //更改容器
                                        con.LocationCode = loc.Code;
                                        con.LocationId = loc.Id;
                                        _unitWork.Update(con);

                                        //更新同个容器中其它物料对应新的回库仓位
                                        if (inventories.Count > 0)
                                        {
                                            foreach (Inventory inv in inventories)
                                            {
                                                if (inv.LocationCode != loc.Code)
                                                {
                                                    inv.LocationCode = loc.Code;
                                                    _unitWork.Update(inv);
                                                }
                                            }
                                        }

                                        ApiRequest apiRequestMove = new ApiRequest("WCS");
                                        //WCSResponse<WcsTask> _WCSResponseMove = new WCSResponse<WcsTask>
                                        //{
                                        //    Code = 200
                                        //};
                                        WCSResponse<WcsTask> _WCSResponseMove = apiRequestMove.Post<WCSResponse<WcsTask>>(JsonHelper.Instance.Serialize(taskDetailMove), "WcsWebApi/TaskAssign", "任务下发");

                                        if (_WCSResponseMove.Code == 200)
                                        {
                                            taskDetailMove.Status = TaskStatus.下达任务;

                                        }
                                        else
                                        {
                                            taskDetailMove.Status = TaskStatus.待下发任务;
                                            taskDetailMove.Error = _WCSResponseMove.Message;
                                        }
                                        _unitWork.Add(taskDetailMove);
                                        continue;
                                    }
                                }
                            }
                            else
                            {
                                taskDetail.Error = "仓位不一致,将仓位" + taskDetail.DestinationLocation + "换为:" + locContainer.Code;
                                taskDetail.DestinationLocation = locContainer.Code;
                                _unitWork.Update(taskDetail);
                                continue;
                            }
                        }
                        else if (locContainerList.Count > 1)
                        {
                            taskDetail.Error = "该托盘仓位正在移库任务或者同时获得两个仓位";
                            _unitWork.Update(taskDetail);
                            continue;
                        }
                        else
                        {
                            taskDetail.Error = "该托盘仓位有任务或者状态不正确!";
                            _unitWork.Update(taskDetail);
                            continue;
                        }
                    }

                    if (_unitWork.IsExist<Station>(n => n.Containercode == taskDetail.ContainerCode && n.Code != taskDetail.Station) && taskDetail.TaskType == TaskType.容器出库)
                    {
                        taskDetail.Error = "托盘:" + taskDetail.ContainerCode + "已占用别的站台" + ":" + DateTime.Now.ToString("HH:mm:ss");
                        _unitWork.Update(taskDetail);
                        continue;
                    }
                    
                    //暂存站台判断
                    if (_unitWork.IsExist<Station>(n => n.Type == StationType.暂存区站台 && n.Code == taskDetail.DestinationLocation))
                    {
                        Station bufferStation = null;
                        bufferStation = _unitWork.Find<Station>(n => n.Type == StationType.暂存区站台 && n.Code == taskDetail.DestinationLocation).FirstOrDefault();

                        if (bufferStation == null)
                        {
                            taskDetail.Error = "暂无暂存台,托盘:" + taskDetail.ContainerCode + ":" + DateTime.Now.ToString("HH:mm:ss");
                            _unitWork.Update(taskDetail);
                            continue;
                        }
                        else
                        {
                            taskDetail.Station = bufferStation.Code;
                            taskDetail.DestinationLocation = bufferStation.Code;
                        }
                    }

                    Station station = null;
                    Location location = null;
                    Container container = _unitWork.Find<Container>(n => n.Code == taskDetail.ContainerCode).FirstOrDefault();
                    if ((taskDetail.TaskType == TaskType.容器出库 || taskDetail.TaskType == TaskType.直接出库 || taskDetail.TaskType == TaskType.站台到站台 || taskDetail.TaskType == TaskType.盘点) && taskDetail.DestinationLocation != "ExitStationB01(P3100)")
                    {
                        station = _unitWork.Find<Station>(n => n.Code == taskDetail.Station && (n.Containercode == "" || n.Containercode == taskDetail.ContainerCode) && n.IsOut == 1 && n.IsStop == 0).FirstOrDefault();
                    }
                    else if (taskDetail.TaskType == TaskType.空容器出库 && taskDetail.DestinationLocation != "ExitStationB01(P3100)")
                    {
                        station = _unitWork.Find<Station>(n => n.Code == taskDetail.Station && (n.Containercode == "" || n.Containercode == taskDetail.ContainerCode) && n.IsOut == 1 && n.IsStop == 0 && n.IsEmpty == 1).FirstOrDefault();
                    }
                    else
                    {
                        station = _unitWork.Find<Station>(n => n.Code == taskDetail.Station && n.IsStop == 0).FirstOrDefault();
                    }

                    if (station != null)
                    {
                        if (taskDetail.TaskType == TaskType.容器出库 || taskDetail.TaskType == TaskType.直接出库 || taskDetail.TaskType == TaskType.盘点)
                        {
                            if (!_unitWork.IsExist<Location>(n => n.ContainerCode == taskDetail.ContainerCode && n.Code == taskDetail.SourceLocation))
                            {
                                taskDetail.Error = "WMS:容器出库起始仓位与实际仓位不符 托盘:" + taskDetail.ContainerCode + ":" + DateTime.Now.ToString("HH:mm:ss");
                                _unitWork.Update(taskDetail);
                                continue;
                            }
                        }
                    }
                    else
                    {
                        station = _unitWork.Find<Station>(n => n.Code == taskDetail.Station).FirstOrDefault();
                        if (station.Code != "ExitStationB01(P3100)" && station.Code != "ExitStationB02(P3328)")
                        {
                            taskDetail.Error = "站台:" + station.Name + "状态不正确(占用/出库/空补/禁用)" + ":" + DateTime.Now.ToString("HH:mm:ss");
                            _unitWork.Update(taskDetail);
                            continue;
                        }
                    }
                    if (taskDetail.DestinationLocation == "ExitStationB01(P3100)")
                    {
                        TaskDetail In_taskDetail = _unitWork.Find<TaskDetail>(n => n.SourceLocation == "EntranceStationB01(P3106)" || n.SourceLocation == "EntranceStationB02(P3101)").FirstOrDefault();
                        if (In_taskDetail != null)
                        {
                            taskDetail.Error = "提升机已被入库任务占用,等待入库任务执行完成" + ":" + DateTime.Now.ToString("HH:mm:ss");
                            _unitWork.Update(taskDetail);
                            continue;
                        }
                    }
                    if (container != null)
                    {
                       taskDetail.MaterialName = WoodType.普通板.ToString();
                    }

                    ApiRequest apiRequest = new ApiRequest("WCS");
                    //WCSResponse<WcsTask> _WCSResponse = new WCSResponse<WcsTask>
                    //{
                    //    Code = 200
                    //};
                    WCSResponse<WcsTask> _WCSResponse = apiRequest.Post<WCSResponse<WcsTask>>(JsonHelper.Instance.Serialize(taskDetail), "WcsWebApi/TaskAssign", "任务下发");
                    if (_WCSResponse.Code == 200)
                    {
                        //任务下发成功修改任务状态,修改库位状态
                        if (taskDetail.TaskType == TaskType.容器出库 || taskDetail.TaskType == TaskType.空容器出库 || taskDetail.TaskType == TaskType.直接出库 || taskDetail.TaskType == TaskType.盘点)
                        {
                            taskDetail.Status = TaskStatus.下达任务;
                            taskDetail.DestinationLocation = station.Code;

                            location = _unitWork.Find<Location>(n => n.Code == taskDetail.SourceLocation).FirstOrDefault();
                            location.Status = LocationStatus.任务锁定中;
                            //空板暂存区任务不占用站台
                            if (station.Type != StationType.空板暂存区站台)
                            {
                                station.Containercode = taskDetail.ContainerCode;
                            }
                            if (taskDetail.TaskType == TaskType.盘点)
                            {
                                container.IsLock = ContainerLock.盘点锁;
                            }
                            else
                            {
                                container.IsLock = ContainerLock.任务锁;
                            }
                        }
                        else if (taskDetail.TaskType == TaskType.空容器入库 && taskDetail.SourceLocation != "PP_SpecialPoint")
                        {
                            taskDetail.Status = TaskStatus.下达任务;
                            location = _unitWork.Find<Location>(n => n.Code == taskDetail.DestinationLocation).FirstOrDefault();
                            location.Status = LocationStatus.任务锁定中;
                            station.Containercode = taskDetail.ContainerCode;
                            container.IsLock = ContainerLock.任务锁;
                        }
                        else if (taskDetail.TaskType == TaskType.站台到站台)
                        {
                            taskDetail.Status = TaskStatus.下达任务;
                            taskDetail.DestinationLocation = station.Code;
                            if (taskDetail.ContainerCode != "woodPallet")
                            {
                                //2楼木板入库下发任务时不占用站台
                                if (taskDetail.SourceLocation != "OutStationB07(P3300)")
                                {
                                    station.Containercode = taskDetail.ContainerCode;
                                }
                                if (taskDetail.ContainerCode != "empty")
                                {
                                    if (taskDetail.TaskType == TaskType.盘点)
                                    {
                                        container.IsLock = ContainerLock.盘点锁;
                                    }
                                    else
                                    {
                                        container.IsLock = ContainerLock.任务锁;
                                    }
                                }
                            }
                        }
                        else
                        {
                            taskDetail.Status = TaskStatus.下达任务;
                        }

                        if (station != null)
                        {
                            _unitWork.Update(station);
                        }

                        if (location != null)
                        {
                            _unitWork.Update<Location>(n => n.Id == location.Id && n.Version == location.Version, n => new Location
                            {
                                Status = location.Status,
                                UpdateBy = "wms_wcs",
                                UpdateTime = DateTime.Now
                            });
                        }

                        if (container != null)
                        {
                            _unitWork.Update(container);
                        }

                        taskDetail.Error = "";
                        _unitWork.Update(taskDetail);

                        Task task = _unitWork.Find<Task>(n => n.TaskNo == taskDetail.TaskNo).FirstOrDefault(); ;
                        if (task != null)
                        {
                            if (!_unitWork.IsExist<TaskDetail>(n => n.TaskNo == taskDetail.TaskNo && n.Status > taskDetail.Status))
                            {
                                task.FirstStatus = taskDetail.Status;
                            }

                            if (!_unitWork.IsExist<TaskDetail>(n => n.TaskNo == taskDetail.TaskNo && n.Status < taskDetail.Status))
                            {
                                task.LastStatus = taskDetail.Status;
                            }
                            _unitWork.Update(task);

                        }

                        List<TaskDetail> list = new List<TaskDetail>();
                        if (taskDetail.TaskType == TaskType.容器出库 || taskDetail.TaskType == TaskType.直接出库)
                        {
                            list = _unitWork.Find<TaskDetail>(n => n.ContainerCode == taskDetail.ContainerCode && (n.TaskType == TaskType.整盘出库 || n.TaskType == TaskType.分拣出库)).ToList();
                        }
                        else if (taskDetail.TaskType == TaskType.容器回库)
                        {
                            list = _unitWork.Find<TaskDetail>(n => n.ContainerCode == taskDetail.ContainerCode && (n.TaskType == TaskType.整盘入库)).ToList();
                        }

                        if (list.Count > 0)
                        {
                            foreach (TaskDetail td in list)
                            {
                                td.Status = TaskStatus.下达任务;
                                _unitWork.Update(td);
                                Task task_2 = _unitWork.Find<Task>(n => n.TaskNo == td.TaskNo).FirstOrDefault();
                                if (task_2 != null)
                                {
                                    if (!_unitWork.IsExist<TaskDetail>(n => n.TaskNo == td.TaskNo && n.Status > taskDetail.Status))
                                    {
                                        task_2.FirstStatus = td.Status;
                                    }

                                    if (!_unitWork.IsExist<TaskDetail>(n => n.TaskNo == td.TaskNo && n.Status < taskDetail.Status))
                                    {
                                        task_2.LastStatus = td.Status;
                                    }
                                    _unitWork.Update(task_2);
                                }
                            }
                        }

                    }
                    else
                    {
                        taskDetail.Error += "WCS报错:" + _WCSResponse.Message + ":" + DateTime.Now.ToString("HH:mm:ss");
                        _unitWork.Update(taskDetail);
                        continue;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            #endregion
        }
    }
}