/// <summary>
        /// 部门收货确认
        /// </summary>
        /// <param name="po"></param>
        /// <param name="userID"></param>
        public void CheckInOrder(int orderId, int userID, List <Model.PurchasingOrderDetailModel> ListOrderDetailIDAndActualCount)
        {
            if (orderId == 0)
            {
                throw new ArgumentNullException();
            }

            DateTime dateTimeNow = DateTime.Now;
            int      status      = (int)EnumPurchasingOrderState.DeparmentCheckIn;
            int      auditType   = (int)EnumPurchasingAuditType.DeparmentCheckIn;

            var dbcontext = ServiceProvider.GetDbcontext <IPurocumentDbcontext>();

            if (dbcontext.PurchasingOrder.Count(c => orderId.Equals(c.ID) && (c.PurchasingOrderStatusID.Equals((int)EnumPurchasingAuditType.VendorShipped) || c.PurchasingOrderStatusID.Equals((int)EnumPurchasingAuditType.DeparmentCheckIn))) > 0)
            {
                throw new Exception("订单状态不正确");
            }

            if (ListOrderDetailIDAndActualCount.Count < 1) //待更新的明细为0
            {
                return;
            }

            PurchasingOrder order = dbcontext.PurchasingOrder.Single <PurchasingOrder>(w => orderId.Equals(w.ID));

            order.PurchasingOrderStatusID = status;
            order.UpdateUserID            = userID;
            order.UpdateTime = dateTimeNow;
            dbcontext.Update(order);

            var updatePPDIDs = ListOrderDetailIDAndActualCount.Select(s => s.ID);
            var details      = dbcontext.PurchasingOrder.Include(r => r.Details).FirstOrDefault(en => en.ID == orderId);
            var pods         = from f in ListOrderDetailIDAndActualCount
                               join d in details.Details
                               on f.ID equals d.ID
                               select d;

            foreach (var item in pods)
            {
                item.ActualCount            = ListOrderDetailIDAndActualCount.FirstOrDefault(i => i.ID == item.ID).ActualCount;
                item.ActualSubtotal         = item.Price * item.ActualCount;
                item.PurchasingOrderStateID = status;
                item.UpdateUsrID            = userID;
                item.UpdateTime             = dateTimeNow;
            }
            dbcontext.UpdateRange(pods);

            PurchasingAudit pa = new PurchasingAudit()
            {
                PlanID     = order.PurchasingPlanID,
                UserID     = userID,
                CreateTime = dateTimeNow,
                //Desc = Desc,
                Result = auditType
            };

            dbcontext.Add(pa);

            dbcontext.SaveChanges();
        }
        public static PurchasingResult Save(PurchasingOrderHeaderViewModel models)
        {
            PurchasingResult result = new PurchasingResult();

            try
            {
                using (var db = new BuahSayurContext())
                {
                    string newRef = GetNewReference();
                    result.Reference = newRef;

                    PurchasingOrder purchasingOrder = new PurchasingOrder
                    {
                        Id             = 1,
                        Supplier_Code  = models.Supplier_Code,
                        Reference      = newRef,
                        PurchasingDate = models.PurchasingDate
                    };
                    db.PurchasingOrders.Add(purchasingOrder);

                    foreach (var item in models.PurchasingDetails)
                    {
                        PurchasingOrderDetail purchasingDetail = new PurchasingOrderDetail
                        {
                            PurchasingOrder_Id = purchasingOrder.Id,
                            Item_Code          = item.Item_Code,
                            Quantity           = item.Quantity,
                            Price = item.Price,
                            Total = item.Quantity * item.Price
                        };
                        result.Total += (item.Quantity * item.Price);
                        db.PurchasingOrderDetails.Add(purchasingDetail);
                        // Update Stock
                        Item Stock = db.Items.Where(x => x.Code == item.Item_Code).FirstOrDefault();
                        if (Stock != null)
                        {
                            Stock.Stock = Stock.Stock + item.Quantity;
                        }
                    }
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                result.Success = false;
                Message        = ex.Message;
            }
            return(result);
        }
        /// <summary>
        /// 供应商发货确认
        /// </summary>
        public void ComfirmDelivery(int orderId, int userID, bool isPass, string Desc)
        {
            if (orderId == 0)
            {
                throw new ArgumentNullException();
            }

            DateTime dateTimeNow = DateTime.Now;
            int      status      = (int)EnumPurchasingOrderState.VendorShipped; //isPass ? (int)PurchasingOrderStateEnum.VendorShipped : (int)PurchasingOrderStateEnum.Other;
            int      auditType   = (int)EnumPurchasingAuditType.VendorShipped;  //isPass ? (int)AuditTypeEnum.VendorShipped : (int)AuditTypeEnum.Other;

            var dbcontext = ServiceProvider.GetDbcontext <IPurocumentDbcontext>();

            if (dbcontext.PurchasingOrder.Count(c => orderId.Equals(c.ID) && c.PurchasingOrderStatusID != (int)EnumPurchasingOrderState.VendorConfirmed) > 0)
            {
                throw new Exception("订单状态不正确");
            }

            PurchasingOrder order = dbcontext.PurchasingOrder.Single <PurchasingOrder>(w => orderId.Equals(w.ID));

            order.PurchasingOrderStatusID = status;
            order.UpdateUserID            = userID;
            order.UpdateTime = dateTimeNow;
            dbcontext.Update(order);

            PurchasingAudit pa = new PurchasingAudit()
            {
                PlanID     = order.PurchasingPlanID,
                UserID     = userID,
                CreateTime = dateTimeNow,
                Desc       = Desc,
                Result     = auditType
            };

            dbcontext.Add(pa);

            dbcontext.SaveChanges();
        }
        //提交三审 生成与供应商的订单及订单明细
        public void ComfirmPlanAndSubmitOrder(int planId, int userID, bool isPass, string Desc)
        {
            var dbcontext = ServiceProvider.GetDbcontext <IPurocumentDbcontext>();
            var plan      = dbcontext.PurchasingPlan.SingleOrDefault(s => s.ID == planId);

            if (plan == null)
            {
                throw new Exception("采购计划不存在");
            }

            DateTime dateTimeNow   = DateTime.Now;
            int      planStatus    = isPass ? (int)EnumPurchasingPlanState.PlanAudit3Pass : (int)EnumPurchasingPlanState.PlanAudit3Rejected;
            int      planAuditType = isPass ? (int)EnumPurchasingAuditType.PlanAudit3Pass : (int)EnumPurchasingAuditType.PlanAudit3Rejected;

            int orderStatus = isPass ? (int)EnumPurchasingOrderState.AwaitVendorConfirm : (int)EnumPurchasingOrderState.AwaitVendorConfirm;


            //保存审核结果和修改计划状态
            plan.Status       = planStatus;
            plan.UpdateTime   = dateTimeNow;
            plan.UpdateUserID = userID;
            dbcontext.Update(plan);///

            PurchasingAudit insertPA = new PurchasingAudit
            {
                PlanID     = plan.ID,
                UserID     = userID,
                CreateTime = dateTimeNow,
                Result     = planAuditType,//审核状态 若复审通过及订单生成 故仅生成一条
                Desc       = Desc
            };

            dbcontext.Add(insertPA);///

            ///下面这段代码写得乱 性能应该也不高 Linq to EF语法不熟悉 后面要改

            if (isPass)  //审核通过
            {
                List <PurchasingOrder>       insertListPOs  = new List <PurchasingOrder>();
                List <PurchasingOrderDetail> insertListPODs = new List <PurchasingOrderDetail>();

                //采购计划的部门
                var entityD = dbcontext.Department.SingleOrDefault(s => s.ID == plan.DepartmentID);

                //采购计划明细
                var entityPP = dbcontext.PurchasingPlan.Include(i => i.Details).SingleOrDefault(s => s.ID == plan.ID);

                //按供应商分组,循环操作
                var vendorIDs = entityPP.Details.Where(s => s.VendorID.HasValue).Select(s => s.VendorID).Distinct().ToList();//之前未去重,导致严重逻辑错误
                //特殊情况下供应商尚未报价
                if (vendorIDs == null || vendorIDs.Count == 0)
                {
                    throw new Exception("不存在或未选定供应商");
                }

                //按供应商的循环操作
                foreach (var vendorID in vendorIDs)
                {
                    //按供应商的订单明细集合
                    var verdorPPDs = entityPP.Details.Where(w => vendorIDs.Contains(w.VendorID));

                    int     itemCount = 0;
                    decimal?total     = 0;
                    //一个供应商生成一个订单(因采购计划能为一种业务类型 要么食材 要么办公用品 故这里不按照业务类型再做拆分 数据库设计其实是支持的)
                    PurchasingOrder po = new PurchasingOrder
                    {
                        Code                    = StrPOPrefix + DateTime.Now.ToString(StrPOSuffixFormat), //[2][17]
                        PurchasingPlanID        = plan.ID,
                        PurchasingOrderStatusID = orderStatus,                                            //订单状态
                        VendorID                = vendorID.Value,
                        DepartmentID            = plan.DepartmentID,
                        Tel          = entityD?.Tel,
                        Addr         = entityD?.Address,
                        BizTypeID    = plan.BizTypeID,
                        CreateUsrID  = userID,
                        CreateTime   = dateTimeNow,
                        UpdateUserID = userID,
                        UpdateTime   = dateTimeNow,
                        Total        = total,
                        ItemCount    = itemCount
                    };
                    foreach (var vendorPPD in verdorPPDs)
                    {
                        //生成每个供应商分配的采购明细
                        PurchasingOrderDetail pod = new PurchasingOrderDetail
                        {
                            PurchasingOrder        = po,
                            PurchasingOrderStateID = orderStatus,//订单状态
                            GoodsClassID           = vendorPPD.GoodsClassID,
                            GoodsID                = vendorPPD.GoodsID,
                            Count                  = vendorPPD.PurchasingCount,
                            Price                  = vendorPPD.Price,
                            Subtotal               = vendorPPD.PurchasingCount * vendorPPD.Price,//这里之前没有结果
                            ActualCount            = 0,
                            ActualSubtotal         = 0,
                            CreateUsrID            = userID,
                            CreateTime             = dateTimeNow,
                            UpdateUsrID            = userID,
                            UpdateTime             = dateTimeNow,
                            PurchasingPlanDetailID = vendorPPD.ID
                        };
                        insertListPODs.Add(pod);  //订单明细

                        po.Total += pod.Subtotal; //累计每种商品的小计金额
                        po.ItemCount++;           //明细数量

                        //更新采购计划、采购计划明细状态
                        vendorPPD.Status = planStatus; //采购状态

                        dbcontext.Update(vendorPPD);   /// 更新PPD
                    }

                    insertListPOs.Add(po);///  // 订单
                }
                dbcontext.AddRange(insertListPOs);
                dbcontext.AddRange(insertListPODs);
            }

            dbcontext.SaveChanges();
        }