Просмотр исходного кода

Merge branch 'develop' of http://132.232.92.186:3000/XinXiBu/OA2023 into develop

Lyyyi 2 часов назад
Родитель
Сommit
f7cdef2788

+ 95 - 0
OASystem/OASystem.Api/Controllers/MarketCustomerResourcesController.cs

@@ -1581,6 +1581,101 @@ namespace OASystem.API.Controllers
             });
         }
 
+
+        /// <summary>
+        /// 读取excel分配客户
+        /// </summary>
+        /// <returns></returns>
+        [HttpPost]
+        public IActionResult ReadExcelAllocation(IFormFile file, int userId)
+        {
+            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))
+            {
+                file.CopyTo(stream);
+            }
+
+            Workbook workbook = new Workbook(filePath);
+            Worksheet worksheet = workbook.Worksheets[0];
+
+            int headerRowIndex = 0; // 第一行为表头
+            int rowCount = worksheet.Cells.MaxDataRow + 1;
+
+            var notids = 0;
+
+            // 第1列(索引0):NewClientDataId(客户数据ID)
+            var clients = new List<Crm_ClientDataAndUser>();
+            for (int row = headerRowIndex + 1; row < rowCount; row++)
+            {
+                var cellVal1 = worksheet.Cells[row, 0].Value;  // 数据id
+
+                int newClientDataId = !string.IsNullOrEmpty(cellVal1?.ToString())
+                    ? int.Parse(cellVal1.ToString())
+                    : 0;
+
+                if (newClientDataId <= 0)
+                {
+                    notids++;
+                    // 跳过无效行
+                    continue;
+                }
+
+                clients.Add(new Crm_ClientDataAndUser
+                {
+                    NewClientDataId = newClientDataId,
+                    usersId = userId,
+                    CreateTime = DateTime.Now,
+                    CreateUserId = 235,
+                    IsDel = 0,
+                });
+            }
+
+            var count = 0;
+            var updateCount = 0;
+            var ids = clients.Select(x => x.NewClientDataId).ToList();
+
+            if (clients.Count > 0)
+            {
+                _sqlSugar.BeginTran();
+
+
+                //清除所有id旧数据
+                updateCount = _sqlSugar.Updateable<Crm_ClientDataAndUser>()
+                   .Where(x => x.IsDel == 0)
+                   .Where(x => ids.Contains(x.NewClientDataId))
+                   .SetColumns(x => new Crm_ClientDataAndUser
+                   {
+                       IsDel = 1,
+                       DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
+                       DeleteUserId = 235
+                   })
+                   .ExecuteCommand();
+
+                //分配新数据
+                count = _sqlSugar.Insertable(clients).ExecuteCommand();
+
+                _sqlSugar.CommitTran();
+            }
+
+            return Ok(new
+            {
+                insertCount = count,
+                updateCount = updateCount,
+                notids
+            });
+        }
+
         [HttpPost]
         public IActionResult SchoolAllocation()
         {