Ejemplo n.º 1
0
        /// <summary>
        /// 根据系统和黑名单值查询黑名单列表
        /// </summary>
        /// <param name="blockSystem">系统名称</param>
        /// <param name="blockValue">黑名单值</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task <ActionResult> SearchBlockList(string blockSystem, string blockValue, int pageIndex = 1, int pageSize = 20)
        {
            var configService = new BlockListConfigService();
            var result        = await configService.SearchPagedBlockList(blockSystem, blockValue, pageIndex, pageSize);

            return(Content(JsonConvert.SerializeObject(new PagedDataModel <BlockListItem>
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                TotalSize = result.Pager.Total,
                Data = result.Source?.ToArray(),
                Status = 1
            },
                                                       DefaultJsonSerializerSettings), "application/json", Encoding.UTF8));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> ImportExcel(string blockSystem)
        {
            var files = Request.Files;

            if (files == null || files.Count <= 0)
            {
                return(Json(new { code = 1, status = false, msg = "请先选择文件上传" }));
            }

            var file = files[0];

            if (file.ContentType != "application/vnd.ms-excel" &&
                file.ContentType != "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                return(Json(new { code = 1, status = false, msg = "文件格式不正确, 请上传Excel文件" }));
            }

            var blockDict = new Dictionary <string, BlockListItem>();

            using (var stream = file.InputStream)
            {
                // 读取Excel表格数据
                var buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                var workBook = new XSSFWorkbook(new MemoryStream(buffer));
                var sheet    = workBook.GetSheetAt(0);

                for (var rowIndex = sheet.FirstRowNum + 1; rowIndex <= sheet.LastRowNum; rowIndex++)
                {
                    var row = sheet.GetRow(rowIndex);
                    if (row != null)
                    {
                        var blockTypeName = GetCellStringValue(row.GetCell(0));
                        if (string.IsNullOrEmpty(blockTypeName) || !TypeName2TypeId.ContainsKey(blockTypeName))
                        {
                            return(Json(new { code = 1, status = false, msg = $"第{rowIndex + 1}行黑名单类型不正确" }));
                        }

                        var tempOutParameter = new Guid();
                        var blockValue       = GetCellStringValue(row.GetCell(1));
                        if (blockTypeName.Equals("用户Id") && !Guid.TryParse(blockValue, out tempOutParameter))
                        {
                            return(Json(new { code = 1, status = false, msg = $"第{rowIndex + 1}行用户Id应为Guid格式" }));
                        }

                        try
                        {
                            // 避免导入重复记录
                            if (!blockDict.ContainsKey(blockValue))
                            {
                                blockDict[blockValue] = new BlockListItem
                                {
                                    BlockSystem    = blockSystem,
                                    BlockType      = GetTypeIdByTypeName(blockTypeName),
                                    BlockValue     = blockValue,
                                    BlockBeginTime = row.GetCell(2)?.DateCellValue,
                                    BlockEndTime   = row.GetCell(3)?.DateCellValue,
                                    Reason         = GetCellStringValue(row.GetCell(4)),
                                    Remark         = GetCellStringValue(row.GetCell(5)),
                                    UpdateBy       = User.Identity.Name
                                };
                            }
                        }
                        catch (InvalidOperationException)
                        {
                            return(Json(new { code = 1, status = false, msg = $"第{rowIndex + 1}行时间格式不正确" }));
                        }
                    }
                }
            }

            if (blockDict.Any())
            {
                // 将黑名单列表入库
                var configService = new BlockListConfigService();
                foreach (var item in blockDict)
                {
                    await configService.AddBlockListItem(item.Value);
                }

                return(Json(new { code = 1, status = true }));
            }

            return(Json(new { code = 1, status = false, msg = "导入数据不能为空" }));
        }