LEIYI 2 周之前
父節點
當前提交
470072070b

+ 171 - 0
OASystem/OASystem.Api/Controllers/SystemController.cs

@@ -1,9 +1,13 @@
+using Aspose.Cells;
+using NPOI.POIFS.Crypt;
 using OASystem.Domain.AesEncryption;
 using OASystem.Domain.AesEncryption;
 using OASystem.Domain.Entities.Customer;
 using OASystem.Domain.Entities.Customer;
 using OASystem.Domain.Entities.Financial;
 using OASystem.Domain.Entities.Financial;
 using OASystem.Domain.Entities.Groups;
 using OASystem.Domain.Entities.Groups;
 using System.Collections;
 using System.Collections;
 using System.Data;
 using System.Data;
+using System.Dynamic;
+using System.Linq;
 using static OASystem.API.OAMethodLib.GeneralMethod;
 using static OASystem.API.OAMethodLib.GeneralMethod;
 using static OASystem.API.OAMethodLib.JWTHelper;
 using static OASystem.API.OAMethodLib.JWTHelper;
 
 
@@ -3053,5 +3057,172 @@ And u.UId = {0} And u.FId = 1 ", dto.UserId);
 
 
         #endregion
         #endregion
 
 
+        #region 动态读取excel并去重
+
+        /// <summary>
+        /// 客户名单excel导入
+        /// </summary>
+        /// <returns></returns>
+        [HttpPost]
+        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
+        public async Task<IActionResult> DynamicReadExcel(IFormFile file )
+        {
+            // 检查文件是否为空
+            if (file == null || file.Length == 0)
+            {
+                return BadRequest("No file uploaded.");
+            }
+
+            // 保存文件到服务器
+            var uploadsFolder = Path.Combine(Directory.GetCurrentDirectory(), "File");
+            if (!Directory.Exists(uploadsFolder))
+            {
+                Directory.CreateDirectory(uploadsFolder);
+            }
+
+            var filePath = Path.Combine(uploadsFolder, file.FileName);
+            using (var stream = new FileStream(filePath, FileMode.Create))
+            {
+                await file.CopyToAsync(stream);
+            }
+
+            Workbook workbook = new Workbook(filePath);
+
+            // 获取第一个工作表
+            Worksheet worksheet = workbook.Worksheets[0];
+
+            // 获取表头(第一行作为列名)
+            int headerRowIndex = 0; // 假设第一行是表头
+            Row headerRow = worksheet.Cells.Rows[headerRowIndex];
+            int colCount = worksheet.Cells.MaxDataColumn + 1;
+
+            // 动态存储列名
+            var clients = new List<Crm_NewClientData>();
+
+            var lvData = _sqlSugar.Queryable<Sys_SetData>().Where(x => x.IsDel == 0 && x.STid == 33).ToList(); 
+
+            // 遍历数据行(从第二行开始)
+            int rowCount = worksheet.Cells.MaxDataRow + 1;
+            for (int row = headerRowIndex + 1; row < rowCount; row++)
+            {
+                var cellVal1 = worksheet.Cells[row, 0].Value;  //序号
+                var cellVal2 = worksheet.Cells[row, 1].Value;  //区域分类
+                var cellVal3 = worksheet.Cells[row, 2].Value;  //省级分类
+                var cellVal4 = worksheet.Cells[row, 3].Value;  //单位名称
+                var cellVal5 = worksheet.Cells[row, 4].Value;  //单位详细地址
+                var cellVal6 = worksheet.Cells[row, 5].Value;  //座机号
+                var cellVal7 = worksheet.Cells[row, 6].Value;  //所在城市
+                var cellVal8 = worksheet.Cells[row, 7].Value;  //分配人Id
+
+                int userId = !string.IsNullOrEmpty(cellVal8?.ToString()) ? int.Parse(cellVal8?.ToString()) : 0;
+                var rowData = new Crm_NewClientData() {
+                    Number = userId,
+                    Lvlid = lvData.FirstOrDefault(x => x.Name.Equals(cellVal2?.ToString()))?.Id ?? 0,//区域分类
+                    Client = cellVal4?.ToString().Trim(),//单位名称
+                    Weight = "C",//客户权重
+                    Phone = cellVal6?.ToString(), //座机号
+                    Location = cellVal7?.ToString(),//所在城市
+                    Address = cellVal5?.ToString(),//单位详细地址
+                    LastUpdateUserId = 4, //最后更新人
+                    LastUpdateTime = DateTime.Now, //最后更新时间
+                    CreateUserId = 4, //创建人
+                };
+
+                clients.Add(rowData); // 将当前行数据添加到集合中
+            }
+
+
+            if (clients.Any())
+            {
+                _sqlSugar.BeginTran();
+                try
+                {
+                    int total = 0;
+                    foreach (var item in clients)
+                    {
+
+                        var userId = item.Number;
+                        item.Number = 0;
+
+                        var insertId = _sqlSugar.Insertable(item).ExecuteReturnIdentity();
+                        if (insertId < 1)
+                        {
+                            _sqlSugar.RollbackTran();
+                            return Ok(JsonView(false));
+                        }
+
+                        var linkUser = new Crm_ClientDataAndUser() {
+                            usersId = userId,
+                            NewClientDataId = insertId,
+                            CreateUserId = 4
+                        };
+
+                        var insertSub = _sqlSugar.Insertable(linkUser).ExecuteReturnIdentity();
+                        if (insertSub < 1)
+                        {
+                            _sqlSugar.RollbackTran();
+                            return Ok(JsonView(false));
+                        }
+                        total++;
+                    }
+
+                    _sqlSugar.CommitTran();
+                    return Ok(JsonView(true,$"操作成功!成功条数:{total}"));
+
+                }
+                catch (Exception)
+                {
+                    _sqlSugar.RollbackTran();
+                    return Ok(JsonView(false));
+                }
+            }
+            return Ok(JsonView(false));
+        }
+
+
+
+        /// <summary>
+        /// 客户数据加密
+        /// </summary>
+        /// <returns></returns>
+        [HttpGet]
+        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
+        public async Task<IActionResult> DynamicReadExcel1()
+        {
+
+            // 动态存储列名
+            var clients = await _sqlSugar.Queryable<Crm_NewClientData>().Where(x => x.IsDel == 0 && x.Id > 6789).ToListAsync();
+
+            if (clients.Any())
+            {
+                foreach (var item in clients)
+                {
+                    item.Weight = AesEncryptionHelper.Encrypt("391");
+                    //Encrypted
+                    //EncryptionProcessor.EncryptProperties(item);
+                }
+
+                //加密后更改
+                var update = _sqlSugar.Updateable(clients).UpdateColumns(it => new { 
+                    //it.Client, 
+                    it.Weight,
+                    //it.Phone,
+                    //it.Address,
+                    // it.Location,
+                }).ExecuteCommand();
+
+
+                if (update > 0)
+                {
+                    return Ok(JsonView(true,$"数据加密成功!加密条数:{update}"));
+                }
+
+            }
+
+            return Ok(JsonView(false));
+        }
+
+        #endregion
+
     }
     }
 }
 }

二進制
OASystem/OASystem.Api/File/YN整合版.xlsx


二進制
OASystem/OASystem.Api/File/xxxxxx.xls


+ 3 - 12
OASystem/OASystem.Api/Middlewares/ExceptionHandlingMiddleware.cs

@@ -24,9 +24,8 @@ namespace OASystem.API.Middlewares
         /// 执行中间件
         /// 执行中间件
         /// </summary>
         /// </summary>
         /// <param name="httpContext"></param>
         /// <param name="httpContext"></param>
-        /// <param name="db"></param>
         /// <returns></returns>
         /// <returns></returns>
-        public async Task InvokeAsync(HttpContext httpContext, ISqlSugarClient db)
+        public async Task InvokeAsync(HttpContext httpContext)
         {
         {
             try
             try
             {
             {
@@ -34,7 +33,7 @@ namespace OASystem.API.Middlewares
             }
             }
             catch (Exception ex)
             catch (Exception ex)
             {
             {
-                await HandleExceptionAsync(httpContext, ex, db); // 捕获异常了 在HandleExceptionAsync中处理
+                await HandleExceptionAsync(httpContext, ex); // 捕获异常了 在HandleExceptionAsync中处理
             }
             }
         }
         }
 
 
@@ -43,16 +42,9 @@ namespace OASystem.API.Middlewares
         /// </summary>
         /// </summary>
         /// <param name="context"></param>
         /// <param name="context"></param>
         /// <param name="exception"></param>
         /// <param name="exception"></param>
-        /// <param name="db"></param>
         /// <returns></returns>
         /// <returns></returns>
-        private async Task HandleExceptionAsync(HttpContext context, Exception exception, ISqlSugarClient db)
+        private async Task HandleExceptionAsync(HttpContext context, Exception exception)
         {
         {
-            //验证异常是否存在异常处理,存在则回滚事务
-            if (db != null && db.Ado.Transaction != null)
-            {
-                db.Ado.RollbackTran();
-            }
-
 
 
             context.Response.ContentType = "application/json";  // 返回json 类型
             context.Response.ContentType = "application/json";  // 返回json 类型
             var response = context.Response;
             var response = context.Response;
@@ -62,7 +54,6 @@ namespace OASystem.API.Middlewares
                 Data = ""
                 Data = ""
             };  
             };  
             
             
-            
             // 自定义的异常错误信息类型
             // 自定义的异常错误信息类型
             switch (exception)
             switch (exception)
             {
             {