Exemplo n.º 1
0
        public string Save()
        {
            if (Name.IsNullOrEmpty())
            {
                Result = "Необходимо указать название последовательности";
            }
            else if (Count == 0)
            {
                Result = "Необходимо добавить заказы в последовательность";
            }
            else
            {
                Result = "";
            }

            if (Result.IsFilled())
            {
                return(Result);
            }

            var serial = DB.GraphSerials.FirstOrDefault(x => x.ID == ID);

            if (serial == null || ID == 0)
            {
                serial = new GraphSerial()
                {
                    Name   = Name,
                    UserID = CurrentUser.ID,
                };
                DB.GraphSerials.InsertOnSubmit(serial);
            }
            else
            {
                serial.Name = Name;
            }

            DB.SubmitChanges();
            var uids   = Orders.Select(x => x.ID).ToList();
            var forDel = serial.GraphSerialsOrders.Where(x => !uids.Contains(x.OrderID)).ToList();

            if (forDel.Any())
            {
                DB.GraphSerialsOrders.DeleteAllOnSubmit(forDel);
                DB.SubmitChanges();
            }
            var forAdd = uids.Where(x => serial.GraphSerialsOrders.All(z => z.OrderID != x)).ToList();

            if (forAdd.Any())
            {
                var add = forAdd.Select(x => new GraphSerialsOrder()
                {
                    GraphSerial = serial, OrderID = x
                }).ToList();
                DB.GraphSerialsOrders.InsertAllOnSubmit(add);
                DB.SubmitChanges();
            }

            uids = GraphList.Select(x => x.ID).ToList();
            var forDelG = serial.GraphSerialsRels.Where(x => !uids.Contains(x.GraphID)).ToList();

            if (forDelG.Any())
            {
                DB.GraphSerialsRels.DeleteAllOnSubmit(forDelG);
                DB.SubmitChanges();
            }
            var forAddG = uids.Where(x => serial.GraphSerialsRels.All(z => z.GraphID != x)).ToList();

            if (forAddG.Any())
            {
                var add = forAddG.Select(x => new GraphSerialsRel()
                {
                    GraphSerial = serial, GraphID = x, IsHidden = false
                }).ToList();
                DB.GraphSerialsRels.InsertAllOnSubmit(add);
                DB.SubmitChanges();
            }
            return(Result);
        }