public async Task <EquipmentUI> AddOrUpdateEquipment(EquipmentUI ces)
        {
            using (var conn = new SqlConnection(AppSettings.ConnectionString))
            {
                if (ces.Id == 0)
                {
                    //insert new EquipmentModel, get Id
                    var em = new EquipmentModel
                    {
                        ModelId     = ces.ModelId,
                        EquipmentId = ces.Equipment.Id,
                        ParentId    = ces.ParentId,
                        IsMark      = ces.IsMark
                    };
                    var mr = new ModelRepository(_logger);
                    em = await mr.AddEquipmentToModel(em);

                    ces.Id = em.Id;
                }
                else
                {
                    var mr = new ModelRepository(_logger);
                    await mr.UpdateEquipment(new EquipmentModel
                    {
                        EquipmentId = ces.Equipment.Id,
                        Id          = ces.Id,
                        IsMark      = ces.IsMark
                    });
                }

                var shit = await GetEquipmentModelById(ces.Id);

                return(shit);
            }
        }
        public async Task <CheckListEquipmentUI> AddOrUpdateEquipmentWithCheckLists(CheckListEquipmentUI ces)
        {
            if (ces == null)
            {
                throw new Exception("Не указано наименование");
            }

            //Начинаем блядь КВН
            using (var transaction = new TransactionScope(asyncFlowOption: TransactionScopeAsyncFlowOption.Enabled))
            {
                using (var conn = new SqlConnection(AppSettings.ConnectionString))
                {
                    if (ces.Id == 0)
                    {
                        //insert new EquipmentModel, get Id
                        var em = new EquipmentModel
                        {
                            ModelId     = ces.ModelId,
                            EquipmentId = ces.Equipment.Id,
                            ParentId    = ces.ParentId,
                            IsMark      = ces.IsMark
                        };
                        var mr = new ModelRepository(_logger);
                        em = await mr.AddEquipmentToModel(em);

                        ces.Id         = em.Id;
                        ces.Algorithms = new List <Algorithm>().ToArray();
                    }
                    else
                    {
                        var mr = new ModelRepository(_logger);
                        await mr.UpdateEquipment(new EquipmentModel
                        {
                            EquipmentId = ces.Equipment.Id,
                            Id          = ces.Id,
                            IsMark      = ces.IsMark
                        });
                    }

                    //
                    var listAdded = new List <CheckListType>();
                    foreach (var algo in ces.Algorithms)
                    {
                        //if (string.IsNullOrEmpty(algo.NameTask) || algo.FaultType == null)
                        //    throw new Exception("Не заполнены Алгоритмы");
                        if (string.IsNullOrEmpty(algo.NameTask) || algo.Value == null || algo.ValueType == null)
                        {
                            continue;
                        }

                        await AddOrUpdateCheckListToEquipment(ces.Id, algo);

                        listAdded.Add(algo.CheckListType);
                    }

                    foreach (CheckListType cType in Enum.GetValues(typeof(CheckListType)))
                    {
                        if (cType == CheckListType.Surrender)
                        {
                            continue;
                        }

                        var already = listAdded.Where(item => item == cType).ToList();
                        if (already.Count == 0)
                        {
                            await DeleteCheckListFromEquipment(new CheckListEquipment
                            {
                                CheckListType    = cType,
                                EquipmentModelId = ces.Id
                            });

                            /*var algo = new Algorithm
                             * {
                             *  CheckListType = cType
                             * };
                             * await AddOrUpdateCheckListToEquipment(ces.Id, algo);*/
                        }
                    }

                    var shit = await GetCheckListByEquipmentModelId(ces.Id);

                    transaction.Complete();
                    return(shit);
                }
            }
        }