Ejemplo n.º 1
0
        public async Task <CarriageWithEquipment> GetByIdWithEquipment(int id)
        {
            using (var conn = new SqlConnection(AppSettings.ConnectionString))
            {
                var carriage = await GetById(id);

                var mr        = new ModelRepository(_logger);
                var equipment = await mr.GetEquipmentByModel(carriage.Model);

                var sqlr       = new EquipmentRepository(_logger);
                var sqlLabelsR = new LabelRepository();

                var listeq = new List <LocationEquipmentAlgo>();
                foreach (var loc in equipment)
                {
                    string nameFromParent = null;
                    if (loc.ParentId != 0)
                    {
                        nameFromParent = sqlr.GetByParentId(loc.ParentId).Name;
                    }

                    //названия типов чеклистов, прицепленных к данному оборудованию
                    var checkListEquipment = await sqlr.GetCheckListByEquipmentModelId(loc.Id);

                    var algosNameList = (from algo in checkListEquipment.Algorithms
                                         where algo.NameTask != null
                                         select TranslateChecklistType(algo.CheckListType)).ToList();

                    listeq.Add(new LocationEquipmentAlgo
                    {
                        Location  = nameFromParent,
                        Equipment = loc.Equipment.Name,
                        Algorithm = String.Join(", ", algosNameList.Distinct()),
                        Labels    = (await sqlLabelsR.GetByCarriageId(carriage.Id, loc.Id)).ToArray().Select(e => e.Rfid)
                                    .ToArray()
                    });
                }

                var ce = new CarriageWithEquipment
                {
                    Carriage  = carriage,
                    Equipment = listeq.ToArray()
                };

                return(ce);
            }
        }
        //public async Task<EquipmentCategoryPaging> GetAll(int skip, int limit)
        //{
        //    var sql = Sql.SqlQueryCach["CategoryEquipment.All"];
        //    var result = await _db.Connection.QueryAsync<EquipmentCategory>(sql, new { skip = skip, limit = limit });
        //    var sqlc = Sql.SqlQueryCach["CategoryEquipment.CountAll"];
        //    var count = _db.Connection.ExecuteScalar<int>(sqlc);
        //    var output = new EquipmentCategoryPaging
        //    {
        //        Data = result.ToArray(),
        //        Total = count
        //    };

        //    return output;
        //}


        /// <summary>
        /// Получить категории оборудовани
        /// </summary>
        public async Task <EquipmentCategoryPaging> GetAll(int skip, int limit, FilterBody[] filters = null)
        {
            var crunchFilter = "equipmentname";
            var crunch       = new List <FilterBody>();

            _sql  = Sql.SqlQueryCach["CategoryEquipment.All"];
            _sqlc = Sql.SqlQueryCach["CategoryEquipment.CountAll"];

            #region Filter



            if (filters != null)
            {
                crunch = filters.Where(e => e.Filter.ToLower().Equals(crunchFilter)).ToList();
                //Категории можно фильтровать только по имени
                var filter = filters.Where(e => e.Filter.ToLower().Equals("name")).ToArray();
                if (filter.Any())
                {
                    CreateFilter(filter, out _sqlfilter, out _sql);
                    _sqlc = $"{CategoryEquipmentCommon.sqlCountCommon} {_sqlfilter}";
                }
            }

            #endregion

            List <EquipmentCategory> sqlResult;
            int count = 0;
            if (crunch.Any())
            {
                sqlResult = (await _db.Connection.QueryAsync <EquipmentCategory>(_sql, new { skip = 0, limit = MaxValue })).ToList();
            }
            else
            {
                //using (var conn = new SqlConnection(AppSettings.ConnectionString))
                //{
                sqlResult = (await _db.Connection.QueryAsync <EquipmentCategory>(_sql, new { skip = skip, limit = limit })).ToList();
            }
            count = _db.Connection.ExecuteScalar <int>(_sqlc);
            //}


            var result = new EquipmentCategoryPaging
            {
                Data  = sqlResult.ToList(),
                Total = count
            };


            //Теперь мегокастыль. Проверяем есть ли фильтр на оборудование. Если есть то фильтруем по нему
            if (crunch.Any())
            {
                result.Data = new List <EquipmentCategory>();
                var eR = new EquipmentRepository(_logger);
                foreach (var equipmentCategory in sqlResult)
                {
                    var equipments = await eR.GetByCategory(equipmentCategory);

                    if (equipments.Data.Any(e => e.Name.ToLower().Contains(crunch.FirstOrDefault()?.Value.ToLower())))
                    {
                        result.Data.Add(equipmentCategory);
                    }
                }
                result.Total = result.Data.Count;
                result.Data  = result.Data.TakeWhile((e, i) => i >= skip && i <= skip + limit).ToList();
            }


            return(result);
        }