InventoryApp.cs 45.3 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 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
using Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using WebRepository;
using System.Xml;
using System.Configuration;

namespace WebApp
{
    /// <summary>
    /// 库存表
    /// </summary>

    public partial class InventoryApp
    {
        private IUnitWork _unitWork;
        public IRepository<Inventory> _app;
        public IRepository<Task> _appt;
        public IRepository<TaskDetail> _apptd;
        private static IHostingEnvironment _hostingEnvironment;
        ExcelHelper imp = new ExcelHelper(_hostingEnvironment);

        public InventoryApp(IUnitWork unitWork, IRepository<Inventory> repository, IRepository<Task> repositoryt, IRepository<TaskDetail> repositorytd, IHostingEnvironment hostingEnvironment)
        {
            _unitWork = unitWork;
            _app = repository;
            _appt = repositoryt;
            _apptd = repositorytd;
            _hostingEnvironment = hostingEnvironment;
        }

        public InventoryApp SetLoginInfo(LoginInfo loginInfo)
        {
            _app._loginInfo = loginInfo;
            _appt._loginInfo = loginInfo;
            _apptd._loginInfo = loginInfo;
            return this;
        }

        public TableData Load(PageReq pageRequest, Inventory entity)
        {
            //批次先进先出
            TableData tab = _app.Load(pageRequest, entity);
            List<Inventory> list = tab.data;
            if (list != null)
            {
                tab.data = list.OrderBy(n => n.CreateTime);
            }
            //return tab;
            //玻布禁止出库
            //TableData tab = _app.Load(pageRequest, entity);
            //List<Inventory> list = tab.data;
            //if (list != null)
            //{
            //    tab.data = list.Where(n => n.WarehouseType != "YCL_WareCell");
            //}
            return tab;
        }

        public void Ins(Inventory entity)
        {
            _app.Add(entity);
        }

        public void Upd(Inventory entity)
        {
            _app.Update(entity);
        }

        public void DelByIds(int[] ids)
        {
            _app.Delete(u => ids.Contains(u.Id.Value));
        }

        public Inventory FindSingle(Expression<Func<Inventory, bool>> exp)
        {
            return _app.FindSingle(exp);
        }

        public IQueryable<Inventory> Find(Expression<Func<Inventory, bool>> exp)
        {
            return _app.Find(exp);
        }

        public Response ImportIn(IFormFile excelfile)
        {
            Response result = new Infrastructure.Response();
            List<Inventory> exp = imp.ConvertToModel<Inventory>(excelfile);
            string sErrorMsg = "";

            for (int i = 0; i < exp.Count; i++)
            {
                try
                {
                    Inventory e = exp[i];
                    e.Id = null;
                    _app.Add(e);
                }
                catch (Exception ex)
                {
                    sErrorMsg += "第" + (i + 2) + "行:" + ex.Message + "<br>";
                    result.Message = sErrorMsg;
                    break;
                }
            }
            if (sErrorMsg.Equals(string.Empty))
            {
                if (exp.Count == 0)
                {
                    sErrorMsg += "没有发现有效数据, 请确定模板是否正确, 或是否有填充数据!";
                    result.Message = sErrorMsg;
                }
                else
                {
                    result.Message = "导入完成";
                }
            }
            else
            {
                result.Status = false;
                result.Message = result.Message;
            }
            return result;
        }

        public TableData ExportData(Inventory entity)
        {
            return _app.ExportData(entity);
        }

        public TableData Query(Inventory entity)
        {
            var result = new TableData();
            var data = _app.Find(EntityToExpression<Inventory>.GetExpressions(entity));

            GetData(data, result);
            result.count = data.Count();

            return result;
        }

        public void GetData(IQueryable<Inventory> data, TableData result, PageReq pageRequest = null)
        {
            _app.GetData(data, result, pageRequest);
        }

        #region 自定义方法
        /// <summary>
        /// 出库查看
        /// </summary>
        /// <param name="entitys"></param>
        //public void GoodsOut(List<Inventory> entitys)
        //{
        //    var taskNo = _app.GetTaskNo(TaskNo.出库自动分配);
        //    var task = new Task
        //    {
        //        TaskNo = taskNo,
        //        OrderCode = taskNo,
        //        BusinessType = BusinessType.出库_其他出库单
        //    };
        //    task.FirstStatus = task.LastStatus = TaskStatus.待下发任务;

        //    _unitWork.Add(task);

        //    foreach (var item in entitys)
        //    {
        //        var taskdetail = new TaskDetail
        //        {
        //            TaskNo = taskNo,
        //            OrderCode = taskNo,
        //            TaskType = TaskType.出库查看,
        //            ContainerCode = item.ContainerCode,
        //            MaterialCode = item.MaterialCode,
        //            MaterialName = _unitWork.Find<Material>(n => n.Code == item.MaterialCode).Select(a => a.Name).FirstOrDefault(),
        //            OderQty = item.Qty,
        //            HadQty = 0,
        //            ContainerQty = 0,
        //            InventoryId = item.Id,
        //            SourceLocation = item.LocationCode,
        //            Roadway = _unitWork.FindSingle<Location>(n => n.Code == item.LocationCode).Roadway,
        //            Status = TaskStatus.待下发任务,
        //            Priority = 0,
        //            CreateTime = DateTime.Now
        //        };
        //        _unitWork.Add(taskdetail);


        //        //建立盘点容器出库任务
        //        int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == item.ContainerCode && n.TaskType == TaskType.容器出库).Count();
        //        if (ptdcount < 1)
        //        {

        //            Task ptask = new Task();
        //            TaskDetail ptaskDetail = new TaskDetail();
        //            var ctaskNo = _app.GetTaskNo(TaskNo.查看容器出库);
        //            ptask.TaskNo = ctaskNo;
        //            ptask.OrderCode = ctaskNo;
        //            ptask.BusinessType = BusinessType.出库_其他出库单;
        //            ptask.FirstStatus = TaskStatus.待下发任务;
        //            ptask.LastStatus = TaskStatus.待下发任务;
        //            ptask.CreateTime = DateTime.Now;
        //            _appt.Add(ptask);

        //            ptaskDetail.TaskNo = ctaskNo;
        //            ptaskDetail.OrderCode = ctaskNo;
        //            ptaskDetail.TaskType = TaskType.出库查看;
        //            ptaskDetail.ContainerCode = item.ContainerCode;
        //            ptaskDetail.SourceLocation = item.LocationCode;
        //            ptaskDetail.OderQty = 0;
        //            ptaskDetail.ContainerQty = 0;
        //            ptaskDetail.HadQty = 0;
        //            ptaskDetail.Roadway = _unitWork.Find<Location>(n => n.Code == item.LocationCode).Select(a => a.Roadway).FirstOrDefault();
        //            ptaskDetail.Status = TaskStatus.待下发任务;
        //            ptaskDetail.Priority = 0;
        //            ptaskDetail.CreateTime = DateTime.Now;
        //            _apptd.Add(ptaskDetail);

        //        }

        //    }

        //}

        public TableData CheckLoad(PageReq pageRequest, Inventory entity)
        {
            var result = new TableData();
            var inventory = _app.Find(EntityToExpression<Inventory>.GetExpressions(entity)).Where(u => u.TaskStatus != "1");

            GetData(inventory, result, pageRequest);
            result.count = inventory.Count();

            return result;
        }

        public TableData FindInventoryApp(string materialCode)
        {
            TableData tab = new TableData();
            try
            {
                List<Inventory> invs = (from inventory in _unitWork.Find<Inventory>(n => n.MaterialCode == materialCode && n.ContainerStatus == ContainerStatus.)
                                        join container in _unitWork.Find<Container>(x => x.IsLock == 0)
                                        on inventory.ContainerCode equals container.Code
                                        select inventory).ToList();
                tab.data = invs;
            }
            catch (Exception)
            {
                tab.data = null;
            }
            return tab;
        }
        /// <summary>
        /// 直接出库
        /// </summary>
        /// <param name="entitys"></param>
        /// <param name="station"></param>
        /// <returns></returns>
        public Response TaskOutNoBack(List<Inventory> entitys, string station)
        {
            Response response = new Response();
            try
            {
                var containerCodes = entitys.GroupBy(n => n.ContainerCode);
                foreach (var con in containerCodes)
                {
                    var ErpSourceCode = "";
                    var isErp = 0;
                    //建立盘点容器出库任务
                    Inventory item = _unitWork.Find<Inventory>(n => n.Id == con.First().Id).FirstOrDefault();
                    if (item.TaskStatus == InventoryTaskType.任务锁定中)
                    {
                        response.Code = 300;
                        response.Message = "托盘号"+ item.ContainerCode + "的物料仍在任务中,不允许出库";
                        return response;
                    }
                    Container ConType = _unitWork.Find<Container>(n => n.Code == item.ContainerCode).FirstOrDefault();
                    Station Sta = _unitWork.Find<Station>(n => n.Code == station).FirstOrDefault();
                    if (Sta == null)
                    {
                        response.Code = 300;
                        response.Message = "站台错误!!!";
                        return response;
                    }
                    if (item.ContainerCode.Contains("N5TB") && item.Retreat != "余料回库" && Sta.IsPass == 1)
                    {
                        response.Code = 300;
                        response.Message = "铜箔仓货物请在MES中出库!!!";
                        return response;
                    }
                    else if (item.ContainerCode.Contains("N5PP") && item.MaterialCode != "000" && Sta.IsPass == 1 && item.IsFold != "Isflod")
                    {
                        response.Code = 300;
                        response.Message = "PP仓货物请在MES中出库!!!";
                        return response;
                    }
                    //外卖PP出库需要想ERP申请单据号
                    else if (item.ContainerCode.Contains("N5PJ") && Sta.IsPass == 1)
                    {
                        //建立回传 XmlDocument
                        int i_doc_isgoodOut = 0;
                        XmlDocument docOut = new XmlDocument();
                        var configOut = AppSettingsJson.GetAppSettings();
                        //加载模板
                        docOut.Load(configOut.GetSection("ErpSourceCodeService:Url").Value);
                        //建立中转的XmlDocument(要以模板创建新的xml进行回传)
                        XmlDocument doc_isgood_copyOut = new XmlDocument();
                        ConfigXmlDocument ConfigOut = new ConfigXmlDocument();
                        ServiceReference7.TIPTOPServiceGateWayPortTypeClient OutBack = new ServiceReference7.TIPTOPServiceGateWayPortTypeClient();

                        XmlElement xmlElement_Document = doc_isgood_copyOut.CreateElement("Document");
                        doc_isgood_copyOut.AppendChild(xmlElement_Document);

                        //创建RecordSet标签
                        XmlElement xmlElement_RecordSet = doc_isgood_copyOut.CreateElement("RecordSet");
                        xmlElement_RecordSet.SetAttribute("id", (++i_doc_isgoodOut).ToString());
                        XmlNode node_RecordSet = doc_isgood_copyOut.SelectSingleNode("//Document").AppendChild(xmlElement_RecordSet);
                        //添加表头
                        XmlElement xmlElement_Master = doc_isgood_copyOut.CreateElement("Master");
                        xmlElement_Master.SetAttribute("name", "Master");
                        XmlNode node_Master = node_RecordSet.AppendChild(xmlElement_Master);
                        XmlElement xmlElement_HaerRecord = doc_isgood_copyOut.CreateElement("Record");
                        XmlNode node_HaerRecord = node_Master.AppendChild(xmlElement_HaerRecord);

                        //添加表单
                        XmlElement xmlElement_Detail = doc_isgood_copyOut.CreateElement("Detail");
                        xmlElement_Detail.SetAttribute("name", "report_detail");
                        node_Master = node_RecordSet.AppendChild(xmlElement_Detail);
                        XmlElement xmlElement_DeatilRecord = doc_isgood_copyOut.CreateElement("Record");
                        XmlNode node_DetailRecord = node_Master.AppendChild(xmlElement_DeatilRecord);


                        //获取表头数据
                        XmlNode node_Head = docOut.SelectSingleNode(string.Format("Request/RequestContent/Document/RecordSet/Master/Record"));
                        //获取表单
                        XmlNode node_Detail = docOut.SelectSingleNode(string.Format("Request/RequestContent/Document/RecordSet/Detail/Record"));
                        node_HaerRecord.InnerXml = node_Head.InnerXml;
                        node_DetailRecord.InnerXml = node_Detail.InnerXml;
                        //修改表头内容
                        XmlNodeList nodes_Hears = node_HaerRecord.ChildNodes;
                        foreach (XmlElement nodes_Hear in nodes_Hears)
                        {
                            //叫料类型
                            if (nodes_Hear.GetAttribute("name") == "type")
                            {
                                nodes_Hear.SetAttribute("value", "");
                            }
                            //MES单号
                            else if (nodes_Hear.GetAttribute("name") == "wo_no")
                            {
                                nodes_Hear.SetAttribute("value", "");
                            }
                            //调拨单别
                            else if (nodes_Hear.GetAttribute("name") == "imm01")
                            {
                                nodes_Hear.SetAttribute("value", "");
                            }
                            //调拨日期
                            else if (nodes_Hear.GetAttribute("name") == "imm02")
                            {
                                nodes_Hear.SetAttribute("value", "");
                            }
                            //部门
                            else if (nodes_Hear.GetAttribute("name") == "imm14")
                            {
                                nodes_Hear.SetAttribute("value", "");
                            }
                            //申请人
                            else if (nodes_Hear.GetAttribute("name") == "imm16")
                            {
                                nodes_Hear.SetAttribute("value", "");
                            }
                            //是否过账
                            else if (nodes_Hear.GetAttribute("name") == "is_posting")
                            {
                                nodes_Hear.SetAttribute("value", "");
                            }
                        }
                        //修改表单内容
                        XmlNodeList nodes_Deatils = node_DetailRecord.ChildNodes;
                        foreach (XmlElement nodes_Deatil in nodes_Deatils)
                        {
                            if (nodes_Deatil.GetAttribute("name") == "imn03")
                            {
                                nodes_Deatil.SetAttribute("value", item.MaterialCode);
                            }
                            //拨出仓库(起始点)
                            else if (nodes_Deatil.GetAttribute("name") == "imn04")
                            {
                                nodes_Deatil.SetAttribute("value", "");
                            }
                            //拨出库位
                            else if (nodes_Deatil.GetAttribute("name") == "imn05")
                            {
                                nodes_Deatil.SetAttribute("value", "");
                            }
                            //拨出批号
                            else if (nodes_Deatil.GetAttribute("name") == "imn06")
                            {
                                nodes_Deatil.SetAttribute("value", item.Lot);
                            }
                            //库存单位
                            else if (nodes_Deatil.GetAttribute("name") == "imn09")
                            {
                                nodes_Deatil.SetAttribute("value", item.Unit);
                            }
                            //拨出数量
                            else if (nodes_Deatil.GetAttribute("name") == "imn10")
                            {
                                nodes_Deatil.SetAttribute("value", item.Qty.ToString());
                            }
                            //拨入仓库(目的地)
                            else if (nodes_Deatil.GetAttribute("name") == "imn15")
                            {
                                nodes_Deatil.SetAttribute("value", "");
                            }
                            //拨入库位
                            else if (nodes_Deatil.GetAttribute("name") == "imn16")
                            {
                                nodes_Deatil.SetAttribute("value", "");
                            }
                            //拨入批号
                            else if (nodes_Deatil.GetAttribute("name") == "imn17")
                            {
                                nodes_Deatil.SetAttribute("value", item.Lot);
                            }
                            //库存单位
                            else if (nodes_Deatil.GetAttribute("name") == "imn20")
                            {
                                nodes_Deatil.SetAttribute("value", "");
                            }
                            //单位转换率
                            else if (nodes_Deatil.GetAttribute("name") == "imn21")
                            {
                                nodes_Deatil.SetAttribute("value", "");
                            }
                            //转换后数量
                            else if (nodes_Deatil.GetAttribute("name") == "imn22")
                            {
                                nodes_Deatil.SetAttribute("value", "");
                            }
                            //拨出条码
                            else if (nodes_Deatil.GetAttribute("name") == "barcode_out")
                            {
                                nodes_Deatil.SetAttribute("value", item.QrCode);
                            }
                            //拨入条码
                            else if (nodes_Deatil.GetAttribute("name") == "barcode_in")
                            {
                                nodes_Deatil.SetAttribute("value", item.QrCode);
                            }
                            //厂商
                            else if (nodes_Deatil.GetAttribute("name") == "sfa36")
                            {
                                nodes_Deatil.SetAttribute("value", item.Supplier);
                            }
                            //
                            else if (nodes_Deatil.GetAttribute("name") == "checkud")
                            {
                                nodes_Deatil.SetAttribute("value", "");
                            }
                            //备注
                            else if (nodes_Deatil.GetAttribute("name") == "remark")
                            {
                                nodes_Deatil.SetAttribute("value", "");
                            }
                        }

                        docOut.SelectSingleNode("//RequestContent").RemoveAll();
                        docOut.SelectSingleNode("//RequestContent").InnerXml = doc_isgood_copyOut.InnerXml;

                        //回传数据
                        var responseOut = OutBack.CreateMesAimt324(docOut.InnerXml);
                        //处理调用后回传数据
                        ConfigOut.LoadXml(responseOut);
                        XmlElement elementOut = (XmlElement)ConfigOut.SelectSingleNode("//Status");
                        string codeOut = elementOut.GetAttribute("code");
                        string msgOut = elementOut.GetAttribute("description");
                        //string codeOut = "0";
                        //string msgOut = "";
                        //申请单号成功
                        if (codeOut == "0")
                        {
                            InterfaceLog SourceCodeInterfaceLog = new InterfaceLog();
                            SourceCodeInterfaceLog.TaskNo = "";
                            SourceCodeInterfaceLog.Request = docOut.InnerXml;
                            SourceCodeInterfaceLog.Type = "手动出库申请单号成功";
                            SourceCodeInterfaceLog.Status = 1;
                            SourceCodeInterfaceLog.Message = "";
                            SourceCodeInterfaceLog.CreateTime = DateTime.Now;
                            _unitWork.Add(SourceCodeInterfaceLog);
                            

                            ErpSourceCode = "";
                            isErp = 1;
                        }
                        //申请单号失败
                        else
                        {
                            response.Code = 300;
                            response.Message = "外卖PP仓货物申请出库单号失败";
                            return response;
                        }
                    }
                    var Roadway = 0;
                    if (item.TaskStatus == InventoryTaskType.有盘点任务)
                    {
                        response.Code = 300;
                        response.Message = "库存托盘:" + item.ContainerCode + "正在盘点;";
                        continue;
                    }
                    int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == item.ContainerCode).Count();
                    if (ptdcount < 1 && item.TaskStatus == InventoryTaskType.无任务)
                    {
                        Task ptask = new Task();
                        var ctaskNo = _app.GetTaskNo(TaskNo.容器出库);
                        ptask.TaskNo = ctaskNo;
                        ptask.OrderCode = ctaskNo;
                        ptask.BusinessType = BusinessType.出库_其他出库单;
                        ptask.FirstStatus = TaskStatus.待下发任务;
                        ptask.LastStatus = TaskStatus.待下发任务;
                        ptask.CreateTime = DateTime.Now;
                        ptask.SourceCode = ErpSourceCode;
                        if (isErp == 1)
                        {
                            ptask.CreateBy = "HandTaskApi";
                        }

                        TaskDetail ptaskDetail = new TaskDetail();
                        Station stat = _unitWork.Find<Station>(n => n.Code == item.LocationCode && n.Type == StationType.暂存区站台).FirstOrDefault();
                        if (stat != null)
                        {
                            ptaskDetail.TaskType = TaskType.站台到站台;
                            ptaskDetail.Roadway = _unitWork.Find<StationRoadway>(n => n.StationCode == item.LocationCode).Select(a => a.RoadWay).FirstOrDefault();
                        }
                        else
                        {
                            ptaskDetail.TaskType = TaskType.直接出库;
                            ptaskDetail.Roadway = _unitWork.Find<Location>(n => n.Code == item.LocationCode).Select(a => a.Roadway).FirstOrDefault();
                        }
                        ptaskDetail.TaskNo = ctaskNo;
                        ptaskDetail.OrderCode = ctaskNo;
                        ptaskDetail.ContainerCode = item.ContainerCode;
                        ptaskDetail.SourceLocation = item.LocationCode;
                        ptaskDetail.SourceCode = ErpSourceCode;
                        if (_unitWork.IsExist<Station>(n => n.Code == station && n.Type == StationType.暂存区站台))
                        {
                            Station s = new Station();
                            if (ptaskDetail.Roadway == 4)
                            {
                                s = (from sta in _unitWork.Find<Station>(n => n.Type == StationType.暂存区站台 && n.Containercode == "" && n.IsStop == 0)
                                     join sr in _unitWork.Find<StationRoadway>(a => a.RoadWay == ptaskDetail.Roadway)
                                     on sta.Code equals sr.StationCode
                                     select sta).OrderByDescending(x => x.Code).FirstOrDefault();
                            }
                            else
                            {
                                s = (from sta in _unitWork.Find<Station>(n => n.Type == StationType.暂存区站台 && n.Containercode == "" && n.IsStop == 0)
                                     join sr in _unitWork.Find<StationRoadway>(a => a.RoadWay == ptaskDetail.Roadway)
                                     on sta.Code equals sr.StationCode
                                     select sta).FirstOrDefault();
                            }
                            if (s != null)
                            {
                                //s.Containercode = item.ContainerCode;
                                //_unitWork.Update(s);
                            }
                            else
                            {
                                response.Code = 300;
                                response.Message += item.ContainerCode + "未找到缓存站台!";
                                continue;
                            }
                            ptaskDetail.DestinationLocation = s.Code;
                            ptaskDetail.Station = s.Code;
                        }
                        else
                        {
                            ptaskDetail.DestinationLocation = station;
                            ptaskDetail.Station = station;
                        }
                        _appt.Add(ptask);

                        ptaskDetail.OderQty = 0;
                        ptaskDetail.ContainerQty = 0;
                        ptaskDetail.HadQty = 0;
                        ptaskDetail.Status = TaskStatus.待下发任务;
                        ptaskDetail.Priority = 99;
                        ptaskDetail.CreateTime = DateTime.Now;
                        if (isErp == 1)
                        {
                            ptaskDetail.CreateBy = "HandTaskApi";
                        }
                        _apptd.Add(ptaskDetail);

                        item.TaskStatus = InventoryTaskType.任务锁定中;
                        _unitWork.Update(item);

                        List<Inventory> inventories = _unitWork.Find<Inventory>(n => n.ContainerCode == item.ContainerCode && n.Id != item.Id).ToList();
                        foreach (Inventory inv in inventories)
                        {
                            inv.TaskStatus = InventoryTaskType.任务锁定中;
                            _unitWork.Update(inv);
                        }
                    }
                    else
                    {
                        response.Code = 300;
                        response.Message = "托盘:" + item.ContainerCode + "已经有任务或库存任务不正确;";
                        continue;
                    }
                }
            }
            catch (Exception ex)
            {
                response.Code = 300;
                response.Message = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 盘点出库
        /// </summary>
        /// <param name="entitys"></param>
        /// <param name="station"></param>
        /// <returns></returns>
        public Response CYCOutNoBack(List<Inventory> entitys, string station)
        {
            Response response = new Response();
            try
            {
                var containerCodes = entitys.GroupBy(n => n.ContainerCode);
                foreach (var con in containerCodes)
                {
                    //建立盘点容器出库任务
                    Inventory item = con.FirstOrDefault();
                    if (item.TaskStatus == InventoryTaskType.有盘点任务)
                    {
                        response.Code = 300;
                        response.Message += "托盘:" + item.ContainerCode + "正在盘点或者已盘点;";
                        continue;
                    }
                    int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == item.ContainerCode).Count();
                    if (ptdcount < 1 && item.TaskStatus == InventoryTaskType.无任务)
                    {
                        Task ptask = new Task();
                        var ctaskNo = _app.GetTaskNo(TaskNo.盘点);
                        ptask.TaskNo = ctaskNo;
                        ptask.OrderCode = ctaskNo;
                        ptask.BusinessType = BusinessType.出库_盘点出库;
                        ptask.FirstStatus = TaskStatus.待下发任务;
                        ptask.LastStatus = TaskStatus.待下发任务;
                        ptask.CreateTime = DateTime.Now;


                        TaskDetail ptaskDetail = new TaskDetail();
                        ptaskDetail.TaskType = TaskType.盘点;
                        ptaskDetail.Roadway = _unitWork.Find<Location>(n => n.Code == item.LocationCode).Select(a => a.Roadway).FirstOrDefault();
                        ptaskDetail.TaskNo = ctaskNo;
                        ptaskDetail.OrderCode = ctaskNo;
                        ptaskDetail.ContainerCode = item.ContainerCode;
                        ptaskDetail.SourceLocation = item.LocationCode;
                        ptaskDetail.DestinationLocation = station;
                        ptaskDetail.Station = station;
                        _appt.Add(ptask);

                        ptaskDetail.OderQty = 0;
                        ptaskDetail.ContainerQty = 0;
                        ptaskDetail.HadQty = 0;
                        ptaskDetail.Status = TaskStatus.待下发任务;
                        ptaskDetail.Priority = 99;
                        ptaskDetail.CreateTime = DateTime.Now;
                        _apptd.Add(ptaskDetail);

                        item.TaskStatus = InventoryTaskType.有盘点任务;
                        _unitWork.Update(item);

                        List<Inventory> inventories = _unitWork.Find<Inventory>(n => n.ContainerCode == item.ContainerCode && n.Id != item.Id).ToList();
                        foreach (Inventory inv in inventories)
                        {
                            inv.TaskStatus = InventoryTaskType.有盘点任务;
                            _unitWork.Update(inv);
                        }
                    }
                    else
                    {
                        response.Code = 300;
                        response.Message += "托盘:" + item.ContainerCode + "已经有任务;";
                        continue;
                    }
                }
            }
            catch (Exception ex)
            {
                response.Code = 300;
                response.Message = ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 关闭盘点
        /// </summary>
        /// <param name="entitys"></param>
        /// <param name="station"></param>
        /// <returns></returns>
        public Response CloseCYC(string region)
        {
            Response response = new Response();
            try
            {
                List<Inventory> list = _unitWork.Find<Inventory>(n => n.TaskStatus == InventoryTaskType.有盘点任务 && n.WarehouseType == region).ToList();
                foreach (Inventory inv in list)
                {
                    int ptdcount = _unitWork.Find<TaskDetail>(n => (n.Status >= TaskStatus.新建任务 && n.Status < TaskStatus.已经完成) && n.ContainerCode == inv.ContainerCode).Count();
                    if (ptdcount > 1)
                    {
                        inv.TaskStatus = InventoryTaskType.任务锁定中;
                    }
                    else
                    {
                        inv.TaskStatus = InventoryTaskType.无任务;
                    }
                    _unitWork.Update(inv);
                }
            }
            catch (Exception ex)
            {
                response.Code = 300;
                response.Message = ex.Message;
            }
            return response;
        }
        /// <summary>
        ///删除库存数据
        /// </summary>
        public Response DelInv(List<Inventory> Invs, string Name)
        {
            Response response = new Response();
            response.Code = 200;
            try
            {
                foreach (var Inv in Invs)
                {
                    Inventory inventory = _unitWork.Find<Inventory>(n => n.Id == Inv.Id).FirstOrDefault();
                    if (inventory != null)
                    {
                        _unitWork.Delete(inventory);
                        InventoryTransaction inventoryTransaction = new InventoryTransaction
                        {
                            Type = "手动删除库存",
                            SourceCode = inventory.SourceCode,
                            ContainerCode = inventory.ContainerCode,
                            MaterialCode = inventory.MaterialCode,
                            Batch = inventory.Batch,
                            Lot = inventory.Lot,
                            Qty = inventory.Qty,
                            QrCode = inventory.QrCode,
                            Supplier = inventory.Supplier,
                            ResidualWeight = inventory.ResidualWeight,
                            Retreat = inventory.Retreat,
                            Specification = inventory.Specification,
                            NameDescription = inventory.NameDescription,
                            Unit = inventory.Unit,
                            Size = inventory.Size,
                            NumberofSheets = inventory.NumberofSheets,
                            Groupnum = inventory.Groupnum,
                            OldBatch = inventory.OldBatch,
                            Combo = inventory.Combo,
                            Status = inventory.Status,
                            LocationCode = inventory.LocationCode,
                            StackingNumber = inventory.StackingNumber,
                            CreateTime = inventory.CreateTime,
                            UpdateTime = DateTime.Now,
                            UpdateBy = Name
                        };
                        _unitWork.Add(inventoryTransaction);
                    }
                }
            }
            catch (Exception ex)
            {
                response.Code = 300;
                response.Message = ex.Message;
            }
            return response;
        }

        /// <summary>
        ///修改库存备注
        /// </summary>
        public Response UpRemark(List<Inventory> Invs, string Name)
        {
            Response response = new Response();
            response.Code = 200;
            try
            {
                foreach (var Inv in Invs)
                {
                    Inventory inventory = _unitWork.Find<Inventory>(n => n.Id == Inv.Id).FirstOrDefault();
                    if (inventory != null && Name != "")
                    {
                        inventory.Remark = Name;
                        _unitWork.Update(inventory);
                    }
                }
            }
            catch (Exception ex)
            {
                response.Code = 300;
                response.Message = ex.Message;
            }
            return response;
        }
        /// <summary>
        ///修改仓库
        /// </summary>
        public Response UpSpecialWarehouse(List<Inventory> Invs, string Warehouse)
        {
            Response response = new Response();
            response.Code = 200;
            try
            {
                foreach (var Inv in Invs)
                {
                    Inventory inventory = _unitWork.Find<Inventory>(n => n.Id == Inv.Id).FirstOrDefault();
                    if (inventory != null && Warehouse != "")
                    {
                        var a = inventory.SpecialWarehouse;
                        inventory.SpecialWarehouse = Warehouse;
                        _unitWork.Update(inventory);
                        InventoryTransaction inventoryTransaction = new InventoryTransaction
                        {
                            Type = "修改",
                            SourceCode = inventory.SourceCode,
                            ContainerCode = inventory.ContainerCode,
                            MaterialCode = inventory.MaterialCode,
                            QrCode = "手动将仓库" + a + "修改为" + Warehouse,
                            Batch = inventory.Batch,
                            Lot = inventory.Lot,
                            Qty = inventory.Qty,
                            Supplier = inventory.Supplier,
                            ResidualWeight = inventory.ResidualWeight,
                            Retreat = inventory.Retreat,
                            Specification = inventory.Specification,
                            NameDescription = inventory.NameDescription,
                            Unit = inventory.Unit,
                            Size = inventory.Size,
                            NumberofSheets = inventory.NumberofSheets,
                            Groupnum = inventory.Groupnum,
                            OldBatch = inventory.OldBatch,
                            Combo = inventory.Combo,
                            Status = inventory.Status,
                            LocationCode = inventory.LocationCode,
                            StackingNumber = inventory.StackingNumber,
                            CreateTime = inventory.CreateTime,
                            IsoutControl = inventory.IsoutControl,
                            SpecialWarehouse = inventory.SpecialWarehouse,
                            UpdateTime = DateTime.Now
                        };
                        _unitWork.Add(inventoryTransaction);
                    }
                }
            }
            catch (Exception ex)
            {
                response.Code = 300;
                response.Message = ex.Message;
            }
            return response;
        }
        /// <summary>
        ///修改库存数量
        /// </summary>
        public Response UpQty(List<Inventory> Invs, decimal? Qty)
        {
            Response response = new Response();
            response.Code = 200;
            try
            {
                foreach (var Inv in Invs)
                {
                    Inventory inventory = _unitWork.Find<Inventory>(n => n.Id == Inv.Id).FirstOrDefault();
                    if (inventory != null && Qty != null)
                    {
                        var a = inventory.Qty;
                        inventory.Qty = Qty;
                        _unitWork.Update(inventory);
                        InventoryTransaction inventoryTransaction = new InventoryTransaction
                        {
                            Type = "修改",
                            SourceCode = inventory.SourceCode,
                            ContainerCode = inventory.ContainerCode,
                            MaterialCode = inventory.MaterialCode,
                            QrCode = "手动将数量" + a + "修改为" + Qty,
                            Batch = inventory.Batch,
                            Lot = inventory.Lot,
                            Qty = inventory.Qty,
                            Supplier = inventory.Supplier,
                            ResidualWeight = inventory.ResidualWeight,
                            Retreat = inventory.Retreat,
                            Specification = inventory.Specification,
                            NameDescription = inventory.NameDescription,
                            Unit = inventory.Unit,
                            Size = inventory.Size,
                            NumberofSheets = inventory.NumberofSheets,
                            Groupnum = inventory.Groupnum,
                            OldBatch = inventory.OldBatch,
                            Combo = inventory.Combo,
                            Status = inventory.Status,
                            LocationCode = inventory.LocationCode,
                            StackingNumber = inventory.StackingNumber,
                            CreateTime = inventory.CreateTime,
                            IsoutControl = inventory.IsoutControl,
                            SpecialWarehouse = inventory.SpecialWarehouse,
                            UpdateTime = DateTime.Now
                        };
                        _unitWork.Add(inventoryTransaction);
                    }
                }
            }
            catch (Exception ex)
            {
                response.Code = 300;
                response.Message = ex.Message;
            }
            return response;
        }
        /// <summary>
        ///修改库存状态
        /// </summary>
        public Response UpGoods(List<Inventory> Invs)
        {
            Response response = new Response();
            response.Code = 200;
            try
            {
                foreach (var Inv in Invs)
                {
                    Inventory inventory = _unitWork.Find<Inventory>(n => n.Id == Inv.Id).FirstOrDefault();
                    if (inventory != null)
                    {
                        if (inventory.Status == InventoryStatus.良品)
                        {
                            inventory.Status = InventoryStatus.报废品;
                        }
                        else if (inventory.Status == InventoryStatus.报废品)
                        {
                            inventory.Status = InventoryStatus.良品;
                        }
                        inventory.UpdateTime = DateTime.Now;
                        _unitWork.Update(inventory);
                        InventoryTransaction inventoryTransaction = new InventoryTransaction
                        {
                            Type = "修改",
                            SourceCode = inventory.SourceCode,
                            ContainerCode = inventory.ContainerCode,
                            MaterialCode = inventory.MaterialCode,
                            QrCode = "手动将库存状态修改为" + inventory.Status,
                            Batch = inventory.Batch,
                            Lot = inventory.Lot,
                            Qty = inventory.Qty,
                            Supplier = inventory.Supplier,
                            ResidualWeight = inventory.ResidualWeight,
                            Retreat = inventory.Retreat,
                            Specification = inventory.Specification,
                            NameDescription = inventory.NameDescription,
                            Unit = inventory.Unit,
                            Size = inventory.Size,
                            NumberofSheets = inventory.NumberofSheets,
                            Groupnum = inventory.Groupnum,
                            OldBatch = inventory.OldBatch,
                            Combo = inventory.Combo,
                            Status = inventory.Status,
                            LocationCode = inventory.LocationCode,
                            StackingNumber = inventory.StackingNumber,
                            CreateTime = inventory.CreateTime,
                            IsoutControl = inventory.IsoutControl,
                            SpecialWarehouse = inventory.SpecialWarehouse,
                            UpdateTime = DateTime.Now
                        };
                        _unitWork.Add(inventoryTransaction);
                    }
                }
            }
            catch (Exception ex)
            {
                response.Code = 300;
                response.Message = ex.Message;
            }
            return response;
        }
        #endregion
    }
}