IStackerStatusApp.cs 28.4 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
using System;
using System.Collections.Generic;
using System.Linq;
using Infrastructure;
using WebRepository;

namespace WebApp
{
    /// <summary>
    /// 堆垛机运行节点接口App
    /// </summary>

    public partial class IStackerStatusApp : ApiApp
    {
        public IRepository<ReceiptHeader> _apprh;
        public IRepository<ReceiptDetail> _apprd;
        public IRepository<Inventory> _appi;

        public IStackerStatusApp(IUnitWork unitWork, IRepository<Inventory> inventory, IAuth auth, BaseDBContext context, IRepository<ReceiptDetail> receiptDetail, IRepository<ReceiptHeader> receiptHeader) : base(unitWork, auth, context)
        {
            _apprd = receiptDetail;
            _apprh = receiptHeader;
            _appi = inventory;

            //系统字段赋值
            _apprd._loginInfo = _apprh._loginInfo = _loginInfo;
            _appi._loginInfo = _apprh._loginInfo = _loginInfo;
        }

        public string SetStackerStatus(STKStatusModel sTKStatusModel)
        {
            using (var tran = _context.Database.BeginTransaction())
            {
                Response<TaskDetail> Response = new Response<TaskDetail>();
                try
                {
                    TaskDetail td = null;
                    if (sTKStatusModel.Status == 30)
                    {
                        td = _unitWork.FindSingle<TaskDetail>(u => u.TaskNo == sTKStatusModel.TaskNo && u.Status >= TaskStatus.新建任务 && u.Status < TaskStatus.已经完成);
                    }
                    else if (sTKStatusModel.Status == 40)
                    {
                        td = _unitWork.FindSingle<TaskDetail>(u => u.TaskNo == sTKStatusModel.TaskNo && u.Status >= TaskStatus.新建任务 && u.Status < TaskStatus.已经完成);
                    }
                    else
                    {
                        Response.Code = 500;
                        Response.Status = false;
                        Response.Message = "不识别的任务状态!";
                    }

                    if (td != null)
                    {
                        List<TaskDetail> taskDetails = _unitWork.Find<TaskDetail>(u => u.ContainerCode == td.ContainerCode && u.Status >= TaskStatus.新建任务 && u.Status < TaskStatus.已经完成).ToList();
                        if (sTKStatusModel.Status == 30)   //堆垛机出库完成容器到达站台
                        {
                            foreach (TaskDetail t in taskDetails)
                            {
                                if (t.TaskType == TaskType.容器出库)
                                {
                                    t.Status = TaskStatus.已经完成;   //更新托盘出库任务为已完成
                                    t.UpdateBy = "wms";
                                    t.UpdateTime = DateTime.Now;
                                    _unitWork.Update(t);

                                    Task pouttask = _unitWork.FindSingle<Task>(u => u.TaskNo == t.TaskNo);
                                    if (pouttask == null)
                                    {
                                        throw new Exception("任务主表:" + t.TaskNo + "不存在!");
                                    }
                                    pouttask.FirstStatus = TaskStatus.已经完成;
                                    pouttask.LastStatus = TaskStatus.已经完成;
                                    pouttask.UpdateBy = "wms";
                                    pouttask.UpdateTime = DateTime.Now;
                                    _unitWork.Update(pouttask);

                                    //已完成托盘出库任务迁移至历史任务表
                                    TaskHistory taskHistory = new TaskHistory();
                                    pouttask.CopyTo(taskHistory);
                                    taskHistory.Id = null;
                                    _unitWork.Add(taskHistory);
                                    TaskDetailHistory taskDetailHistory = new TaskDetailHistory();
                                    t.CopyTo(taskDetailHistory);
                                    taskDetailHistory.Id = null;
                                    _unitWork.Add(taskDetailHistory);

                                    //删除已完成的托盘出库任务
                                    _unitWork.Delete(pouttask);
                                    _unitWork.Delete(t);
                                }
                                else
                                {
                                    t.Status = TaskStatus.已经到站台;  //更新任务状态为已到达站台
                                    t.UpdateBy = "wms";
                                    t.UpdateTime = DateTime.Now;
                                    _unitWork.Update(t);

                                    //更新主任务头尾状态
                                    Task task = _context.Set<Task>().AsQueryable().Where(u => u.TaskNo == t.TaskNo).SingleOrDefault();
                                    if (task == null)
                                    {
                                        throw new Exception("任务主表:" + t.TaskNo + "不存在!");
                                    }
                                    // Task task = _unitWork.FindSingle<Task>(u => u.TaskNo == t.TaskNo);
                                    TaskDetail tdel = _unitWork.Find<TaskDetail>(u => u.TaskNo == t.TaskNo).OrderBy(a => a.Status).FirstOrDefault();//同一个任务号下面最小的任务状态,包含其它容器的任务,用于更新主任务的尾状态
                                    if (tdel == null)
                                    {
                                        throw new Exception("任务子表:" + t.TaskNo + "不存在!");
                                    }
                                    if (task.FirstStatus < t.Status)
                                    {
                                        task.FirstStatus = t.Status;
                                    }
                                    if (task.LastStatus < tdel.Status)
                                    {
                                        task.LastStatus = tdel.Status;
                                    }
                                    task.UpdateBy = "wms";
                                    task.UpdateTime = DateTime.Now;
                                    _unitWork.Update(task);
                                }
                            }
                            //托盘出库完成,容器占用站台
                            Station st = _unitWork.Find<Station>(u => u.Code == td.Station).FirstOrDefault();
                            if (st == null)
                            {
                                throw new Exception("站台:" + td.Station + "不存在!");
                            }
                            st.Containercode = td.ContainerCode;
                            st.UpdateBy = "wms";
                            st.UpdateTime = DateTime.Now;
                            _unitWork.Update(st);

                            //容器锁定
                            Container container = _unitWork.Find<Container>(n => n.Code == td.ContainerCode).FirstOrDefault(); ;
                            if (container == null)
                            {
                                throw new Exception("容器:" + td.ContainerCode + "不存在!");
                            }
                            _unitWork.Update(container);

                            //托盘出库完成,解锁并释放仓位
                            Location loc = _unitWork.Find<Location>(u => u.Code == td.SourceLocation).FirstOrDefault();
                            if (loc == null)
                            {
                                throw new Exception("仓位:" + td.SourceLocation + "不存在!");
                            }
                            loc.ContainerCode = "";
                            loc.Status = "empty";
                            loc.UpdateBy = "wms";
                            loc.UpdateTime = DateTime.Now;
                            _unitWork.Update(loc);
                            //   Response.Result = td;
                        }
                        else if (sTKStatusModel.Status == 40)  //堆垛机入库完成容器到达仓位
                        {
                            string locationStatus = ContainerStatus.;
                            foreach (TaskDetail t in taskDetails)
                            {
                                if (t.TaskType == TaskType.容器回库)
                                {
                                    t.Status = TaskStatus.已经完成;   //更新托盘入库任务为已完成
                                    t.UpdateBy = "wms";
                                    t.UpdateTime = DateTime.Now;
                                    _unitWork.Update(t);

                                    Task pintask = _unitWork.FindSingle<Task>(u => u.TaskNo == t.TaskNo);
                                    if (pintask == null)
                                    {
                                        throw new Exception("任务主表:" + t.TaskNo + "不存在!");
                                    }
                                    pintask.FirstStatus = TaskStatus.已经完成;
                                    pintask.LastStatus = TaskStatus.已经完成;
                                    pintask.UpdateBy = "wms";
                                    pintask.UpdateTime = DateTime.Now;
                                    _unitWork.Update(pintask);

                                    //已完成的托盘入库任务迁移至历史任务表
                                    TaskHistory taskHistory = new TaskHistory();
                                    pintask.CopyTo(taskHistory);
                                    taskHistory.Id = null;
                                    _unitWork.Add(taskHistory);
                                    TaskDetailHistory taskDetailHistory = new TaskDetailHistory();
                                    t.CopyTo(taskDetailHistory);
                                    taskDetailHistory.Id = null;
                                    _unitWork.Add(taskDetailHistory);

                                    //删除已完成的托盘入库任务
                                    _unitWork.Delete(pintask);
                                    _unitWork.Delete(t);
                                }
                                else
                                {
                                    t.Status = TaskStatus.已经完成;  //更新任务明细状态为已完成
                                    t.UpdateBy = "wms";
                                    t.UpdateTime = DateTime.Now;
                                    _unitWork.Update(t);
                                    //更新主任务头尾状态
                                    Task task = _context.Set<Task>().AsQueryable().Where(u => u.TaskNo == t.TaskNo).SingleOrDefault();
                                    if (task == null)
                                    {
                                        throw new Exception("任务主表:" + t.TaskNo + "不存在!");
                                    }
                                    // Task task = _unitWork.FindSingle<Task>(u => u.TaskNo == t.TaskNo);
                                    TaskDetail tdel = _unitWork.Find<TaskDetail>(u => u.TaskNo == t.TaskNo).OrderBy(a => a.Status).FirstOrDefault();//同一个任务号下面最小的任务状态,包含其它容器的任务,用于更新主任务的尾状态
                                    if (tdel == null)
                                    {
                                        throw new Exception("任务子表:" + t.TaskNo + "不存在!");
                                    }
                                    if (task.FirstStatus < t.Status)
                                    {
                                        task.FirstStatus = t.Status;
                                    }
                                    if (task.LastStatus < tdel.Status)
                                    {
                                        task.LastStatus = tdel.Status;
                                    }
                                    task.UpdateBy = "wms";
                                    task.UpdateTime = DateTime.Now;
                                    _unitWork.Update(task);

                                    //已完成的单据任务迁移至历史任务表
                                    if (task.LastStatus == TaskStatus.已经完成)
                                    {
                                        TaskHistory taskhis = new TaskHistory();
                                        task.CopyTo(taskhis);
                                        taskhis.Id = null;
                                        _unitWork.Add(taskhis);

                                        List<TaskDetail> taskdtls = _context.Set<TaskDetail>().AsQueryable().Where(u => u.TaskNo == task.TaskNo).ToList();
                                        foreach (TaskDetail tdl in taskdtls)
                                        {
                                            TaskDetailHistory taskdthis = new TaskDetailHistory();
                                            tdl.CopyTo(taskdthis);
                                            taskdthis.Id = null;
                                            _unitWork.Add(taskdthis);

                                            _unitWork.Delete(tdl);
                                        }
                                        _unitWork.Delete(task);
                                    }

                                    if (t.TaskType == TaskType.整盘出库 || t.TaskType == TaskType.分拣出库)//出库任务类型
                                    {
                                        //更新出库明细状态
                                        ShipmentDetail shipmentDetail = _context.Set<ShipmentDetail>().AsQueryable().Where(u => u.SourceCode == t.SourceCode && u.ShipmentCode == t.OrderCode && u.MaterialCode == t.MaterialCode && t.OderQty == t.HadQty).SingleOrDefault();
                                        //  ShipmentDetail shipmentDetail = _unitWork.Find<ShipmentDetail>(u => u.SourceCode == t.SourceCode && u.ShipmentCode == t.OrderCode && u.MaterialCode == t.MaterialCode).FirstOrDefault();
                                        if (shipmentDetail != null)
                                        {
                                            shipmentDetail.Status = ShipmentHeaderStatus.过账;
                                            shipmentDetail.UpdateBy = "wms";
                                            shipmentDetail.UpdateTime = DateTime.Now;
                                            _unitWork.Update(shipmentDetail);
                                            //更新主出库单头尾状态
                                            //  ShipmentHeader shipmentHeader = _unitWork.Find<ShipmentHeader>(u => u.Code == t.OrderCode).FirstOrDefault();
                                            ShipmentHeader shipmentHeader = _context.Set<ShipmentHeader>().AsQueryable().Where(u => u.Code == t.OrderCode).SingleOrDefault();
                                            ShipmentDetail shipdt = _unitWork.Find<ShipmentDetail>(u => u.ShipmentCode == t.OrderCode).OrderBy(a => a.Status).FirstOrDefault();
                                            if (shipmentHeader.FirstStatus < shipmentDetail.Status)
                                            {
                                                shipmentHeader.FirstStatus = shipmentDetail.Status;
                                            }
                                            if (shipmentHeader.LastStatus < shipdt.Status)
                                            {
                                                shipmentHeader.LastStatus = shipdt.Status;
                                            }
                                            shipmentHeader.UpdateBy = "wms";
                                            shipmentHeader.UpdateTime = DateTime.Now;
                                            _unitWork.Update(shipmentHeader);

                                            //已完成的出库单据迁移至历史出库单据,暂以过账状态,后期与ERP对接后以回传状态
                                            if (shipmentHeader.LastStatus == ShipmentHeaderStatus.过账)
                                            {
                                                ShipmentHeaderHistory shipmentHeaderHistory = new ShipmentHeaderHistory();
                                                shipmentHeader.CopyTo(shipmentHeaderHistory);
                                                shipmentHeaderHistory.Id = null;
                                                _unitWork.Add(shipmentHeaderHistory);

                                                List<ShipmentDetail> shipdtls = _context.Set<ShipmentDetail>().AsQueryable().Where(u => u.ShipmentCode == shipmentHeader.Code).ToList();
                                                //  List<ShipmentDetail> shipdtls = _unitWork.Find<ShipmentDetail>(u => u.ShipmentCode == shipmentHeader.Code).ToList();
                                                foreach (ShipmentDetail sd in shipdtls)
                                                {
                                                    ShipmentDetailHistory shipmentDetailHistory = new ShipmentDetailHistory();
                                                    sd.CopyTo(shipmentDetailHistory);
                                                    shipmentDetailHistory.Id = null;
                                                    shipmentDetailHistory.ShipmentId = shipmentHeaderHistory.Id;
                                                    _unitWork.Add(shipmentDetailHistory);

                                                    _unitWork.Delete(sd);
                                                }
                                                _unitWork.Delete(shipmentHeader);
                                            }
                                        }
                                    }
                                    else if (t.TaskType == TaskType.整盘入库 || t.TaskType == TaskType.补充入库)//入库任务类型
                                    {
                                        //更新入库明细单据状态
                                        ReceiptDetail receiptDetail = _unitWork.Find<ReceiptDetail>(u => u.SourceCode == t.SourceCode && u.ReceiptCode == t.OrderCode && u.MaterialCode == t.MaterialCode && t.OderQty == t.HadQty).FirstOrDefault();
                                        if (receiptDetail != null)
                                        {
                                            receiptDetail.Status = ReceiptHeaderStatus.过账;
                                            receiptDetail.UpdateBy = "wms";
                                            receiptDetail.UpdateTime = DateTime.Now;
                                            _apprd.UpdateByTracking(receiptDetail);
                                            //更新入库主单据头尾状态
                                            // ReceiptHeader receiptHeader = _unitWork.Find<ReceiptHeader>(u => u.Code == t.OrderCode).FirstOrDefault();
                                            ReceiptHeader receiptHeader = _context.Set<ReceiptHeader>().AsQueryable().Where(u => u.Code == t.OrderCode).SingleOrDefault();
                                            ReceiptDetail recepdt = _unitWork.Find<ReceiptDetail>(u => u.ReceiptCode == t.OrderCode).OrderBy(a => a.Status).FirstOrDefault();
                                            if (receiptHeader.FirstStatus < receiptDetail.Status)
                                            {
                                                receiptHeader.FirstStatus = receiptDetail.Status;
                                            }
                                            if (receiptHeader.LastStatus < recepdt.Status)
                                            {
                                                receiptHeader.LastStatus = recepdt.Status;
                                            }
                                            receiptHeader.UpdateBy = "wms";
                                            receiptHeader.UpdateTime = DateTime.Now;
                                            _apprh.UpdateByTracking(receiptHeader);
                                            //已完成的入库单据迁移至历史入库单据,暂以过账状态,后期与ERP对接后以回传状态
                                            if (receiptHeader.LastStatus == ReceiptHeaderStatus.过账)
                                            {
                                                ReceiptHeaderHistory receiptHeaderHistory = new ReceiptHeaderHistory();
                                                receiptHeader.CopyTo(receiptHeaderHistory);
                                                receiptHeaderHistory.Id = null;
                                                _unitWork.Add(receiptHeaderHistory);

                                                List<ReceiptDetail> recepdtls = _context.Set<ReceiptDetail>().AsQueryable().Where(u => u.ReceiptCode == receiptHeader.Code).ToList();
                                                // List<ReceiptDetail> recepdtls = _unitWork.Find<ReceiptDetail>(u => u.ReceiptCode == receiptHeader.Code).ToList();
                                                foreach (ReceiptDetail rd in recepdtls)
                                                {
                                                    ReceiptDetailHistory receiptDetailHistory = new ReceiptDetailHistory();
                                                    rd.CopyTo(receiptDetailHistory);
                                                    receiptDetailHistory.Id = null;
                                                    receiptDetailHistory.ReceiptId = receiptHeaderHistory.Id;
                                                    _unitWork.Add(receiptDetailHistory);

                                                    _unitWork.Delete(rd);
                                                }
                                                _unitWork.Delete(receiptHeader);
                                            }
                                        }
                                    }
                                    else if (t.TaskType == TaskType.空容器入库) //空容器入库任务类型
                                    {
                                        locationStatus = LocationStatus.空容器;
                                    }
                                    else if (t.TaskType == TaskType.出库查看)
                                    {

                                    }
                                    else if (t.TaskType == TaskType.盘点)
                                    {
                                        Inventory inventory = _unitWork.Find<Inventory>(n => n.ContainerCode == t.ContainerCode).FirstOrDefault();
                                        List<Inventory> inventories = _unitWork.Find<Inventory>(n => n.ContainerCode == inventory.ContainerCode).ToList();
                                        foreach (Inventory inv in inventories)
                                        {
                                            inv.TaskStatus = "cyclecountLock";
                                            _appi.UpdateByTracking(inv);
                                        }
                                        CyclecountHeader cycher = _unitWork.Find<CyclecountHeader>(n => n.Code == t.OrderCode).FirstOrDefault();
                                        CyclecountDetail cycdel = _unitWork.Find<CyclecountDetail>(n => n.Status == ReceiptHeaderStatus.过账 && n.MaterialCode == t.MaterialCode && n.ContainerCode == t.ContainerCode && n.Code == t.OrderCode).FirstOrDefault();
                                        CyclecountDetail cycdelEnd = _unitWork.Find<CyclecountDetail>(n => n.Code == t.OrderCode && n.Id != cycdel.Id).OrderBy(n => n.Status).FirstOrDefault();
                                        if (cycdel != null)
                                        {
                                            cycdel.Status = ReceiptHeaderStatus.回传;
                                            _unitWork.Update(cycdel);
                                            if (cycdelEnd.Status >= cycdel.Status)
                                            {
                                                cycher.Status = (short)cycdel.Status;
                                                _unitWork.Update(cycher);
                                            }
                                            if (t.OderQty < t.HadQty)
                                            {
                                                //erp传数据
                                            }
                                            else if (t.OderQty > t.HadQty)
                                            {
                                                //erp传数据
                                            }
                                        }
                                    }
                                    else
                                    {
                                        throw new Exception("未知任务类型:" + t.TaskType);
                                    }
                                }
                            }

                            //托盘入库完成,解锁并释放仓位
                            Location loc = _unitWork.Find<Location>(u => u.Code == td.DestinationLocation).FirstOrDefault();
                            if (loc == null)
                            {
                                throw new Exception("仓位:" + td.DestinationLocation + "不存在!");
                            }
                            loc.ContainerCode = td.ContainerCode;
                            loc.Status = locationStatus;
                            loc.UpdateBy = "wms";
                            loc.UpdateTime = DateTime.Now;
                            _unitWork.Update(loc);

                            //托盘入库完成,解锁并释放容器
                            Container container = _unitWork.Find<Container>(u => u.Code == td.ContainerCode).FirstOrDefault();
                            if (container == null)
                            {
                                throw new Exception("容器:" + td.ContainerCode + "不存在!");
                            }
                            container.LocationCode = td.DestinationLocation;
                            container.IsLock = 0;
                            container.UpdateBy = "wms";
                            container.UpdateTime = DateTime.Now;
                            _unitWork.Update(container);

                            //取消容器占用站台
                            Station st = _unitWork.Find<Station>(u => u.Code == td.Station).FirstOrDefault();
                            if (st == null)
                            {
                                throw new Exception("站台:" + td.Station + "不存在!");
                            }
                            st.Containercode = "";
                            st.UpdateBy = "wms";
                            st.UpdateTime = DateTime.Now;
                            _unitWork.Update(st);
                            //  Response.Result = td;
                        }
                        else
                        {
                            Response.Code = 500;
                            Response.Status = false;
                            Response.Message = "不识别的任务状态!";
                        }
                    }
                    else
                    {
                        Response.Code = 500;
                        Response.Status = false;
                        Response.Message = "获取任务号错误,未找到此容器的任务!";
                    }
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    Response.Code = 500;
                    Response.Status = false;
                    Response.Message = ex.Message;
                }

                return JsonHelper.Instance.Serialize(Response);
            }
        }
    }
}