瀏覽代碼

上传推广码

zmq 2 年之前
父節點
當前提交
311cd674bf

+ 4 - 1
src/platform/ZhonTai.Admin/Core/Db/DbHelper.cs

@@ -196,9 +196,12 @@ public class DbHelper
                     break;
                 case "TenantId":
                     if (e.Value == null || (long)e.Value == default || (long?)e.Value == default)
-                    {
+                    {                        
                         e.Value = user.TenantId;
                     }
+                    if (e.Value != null && (long)e.Value == -1) {
+                        e.Value = "";
+                    }
                     break;
 
             }

+ 1 - 1
src/platform/ZhonTai.Admin/Domain/ProjectLink/ProjectLinkEntity.cs

@@ -51,7 +51,7 @@ namespace ZhonTai.Admin.Domain.ProjectLink
         /// <summary>
         /// 使用时间
         /// </summary>
-        public DateTime UseTime { get; set; }
+        public DateTime? UseTime { get; set; }
         /// <summary>
         /// 业务员姓名
         /// </summary>

+ 1 - 1
src/platform/ZhonTai.Admin/Services/ProjectLink/Dto/ProjectLinkListOutput.cs

@@ -43,7 +43,7 @@ namespace ZhonTai.Admin.Services.ProjectLink.Dto
         /// <summary>
         /// 使用时间
         /// </summary>
-        public DateTime UseTime { get; set; }
+        public DateTime? UseTime { get; set; }
         /// <summary>
         /// 业务员姓名
         /// </summary>

+ 57 - 18
src/platform/ZhonTai.Admin/Services/ProjectLink/ProjectLinkService.cs

@@ -6,6 +6,7 @@ using OnceMi.AspNetCore.OSS;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
+using System.Data;
 using System.IO;
 using System.Linq;
 using System.Text;
@@ -15,6 +16,7 @@ using ZhonTai.Admin.Core.Consts;
 using ZhonTai.Admin.Core.Dto;
 using ZhonTai.Admin.Core.Helpers;
 using ZhonTai.Admin.Domain.File;
+using ZhonTai.Admin.Domain.Project;
 using ZhonTai.Admin.Domain.ProjectLink;
 using ZhonTai.Admin.Domain.Role;
 using ZhonTai.Admin.Repositories;
@@ -35,6 +37,7 @@ namespace ZhonTai.Admin.Services.ProjectLink
     public class ProjectLinkService : BaseService, IProjectLinkService, IDynamicApi
     {
         private IProjectLinkRepository _ProjectLinkRepository => LazyGetRequiredService<IProjectLinkRepository>();
+        private IProjectRepository _projectRepository => LazyGetRequiredService<IProjectRepository>();
         private OSSConfig _oSSConfig => LazyGetRequiredService<IOptions<OSSConfig>>().Value;
         private IHttpContextAccessor _httpContextAccessor => LazyGetRequiredService<IHttpContextAccessor>();
 
@@ -102,7 +105,7 @@ namespace ZhonTai.Admin.Services.ProjectLink
         /// <param name="ProjectId">文件目录</param>
         /// <param name="Company">文件重命名</param>
         /// <returns></returns>
-        public async Task UploadLinkAsync([Required] IFormFile file,[Required] long ProjectId, [Required] string Company)
+        public async Task UploadLinkAsync([Required] IFormFile file, [Required] long ProjectId, [Required] string Company)
         {
             var localUploadConfig = _oSSConfig.LocalUploadConfig;
 
@@ -111,26 +114,21 @@ namespace ZhonTai.Admin.Services.ProjectLink
             {
                 throw new Exception($"请上传excel格式文件");
             }
-            var hasIncludeExtension = localUploadConfig.IncludeExtension?.Length > 0;
-            if (hasIncludeExtension && !localUploadConfig.IncludeExtension.Contains(extention))
-            {
-                throw new Exception($"不允许上传{extention}文件格式");
-            }
-            var hasExcludeExtension = localUploadConfig.ExcludeExtension?.Length > 0;
-            if (hasExcludeExtension && localUploadConfig.ExcludeExtension.Contains(extention))
+            var fileLenth = file.Length;
+            if (fileLenth <= 0)
             {
-                throw new Exception($"不允许上传{extention}文件格式");
+                throw new Exception("文件不能为空");
             }
-
-            var fileLenth = file.Length;
             if (fileLenth > localUploadConfig.MaxSize)
             {
                 throw new Exception($"文件大小不能超过{new FileSize(localUploadConfig.MaxSize)}");
             }
-            if (ProjectId <= 0) { 
-
+            if (ProjectId <= 0)
+            {
+                throw new Exception($"请输入有效项目Id");
             }
 
+
             string fileDirectory = "link";
 
             string SaveFileName = FreeUtil.NewMongodbId().ToString();
@@ -146,9 +144,50 @@ namespace ZhonTai.Admin.Services.ProjectLink
             }
             filePath = Path.Combine(env.WebRootPath, filePath).ToPath();
             await uploadHelper.SaveAsync(file, filePath);
-            
 
-             NOPIHelper.ReadExcel(filePath);
+            //导入数据
+            var datatable = NOPIHelper.ReadExcel(filePath);
+            if (datatable == null || datatable.Rows.Count <= 0)
+            {
+                throw new Exception($"文件不能为空");
+            }
+            if (datatable.Columns["编号"] == null || datatable.Columns["口令"] == null || datatable.Columns["短链"] == null || datatable.Columns["二维码"] == null || datatable.Columns["查单链接"] == null)
+            {
+                throw new Exception($"导入格式不正确");
+            }
+            var projectAny = await _projectRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => m.Id == ProjectId).AnyAsync();
+            if (!projectAny)
+            {
+                throw new Exception($"项目不存在");
+            }
+            var ListCompany = _ProjectLinkRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => m.ProjectId == ProjectId)
+                 .GroupBy(m => m.Company).Select(m => m.Key).ToList();
+            if (ListCompany.Count > 0 && !ListCompany.Contains(Company))
+            {
+                throw new Exception($"一个项目只能上传一个推广码来源");
+            }
+            List<string> listNum = (from d in datatable.AsEnumerable() select d.Field<string>("编号")).ToList();
+            var count = await _ProjectLinkRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => listNum.Contains(m.Num)).CountAsync();
+            if (count > 0)
+            {
+                throw new Exception($"编码重复");
+            }
+            List<ProjectLinkEntity> list = new List<ProjectLinkEntity>();
+            foreach (DataRow row in datatable.Rows)
+            {
+                list.Add(new ProjectLinkEntity()
+                {
+                    Num = row["编号"]?.ToString(),
+                    ShareCommand = row["口令"]?.ToString(),
+                    ShortUrl = row["短链"]?.ToString(),
+                    QrcodeUrl = row["二维码"]?.ToString(),
+                    QueryUrl = row["查单链接"]?.ToString(),
+                    ProjectId = ProjectId,
+                    Company = Company,
+                    TenantId=-1
+                });
+            }
+            await _ProjectLinkRepository.InsertAsync(list);            
         }
         /// <summary>
         /// 下载推广码模板
@@ -157,9 +196,9 @@ namespace ZhonTai.Admin.Services.ProjectLink
         [HttpGet]
         public string DownLinkCodeAsync()
         {
-            string fileDirectory = "link";            
+            string fileDirectory = "link";
             var filePath = Path.Combine(fileDirectory, "linkcode.xlsx").ToPath();
-            
+
             var env = LazyGetRequiredService<IWebHostEnvironment>();
             var file = Path.Combine(env.WebRootPath, filePath).ToPath();
             if (!System.IO.File.Exists(file))
@@ -168,7 +207,7 @@ namespace ZhonTai.Admin.Services.ProjectLink
             }
 
             var linkUrl = $"{_httpContextAccessor.HttpContext.Request.Scheme}://{_httpContextAccessor.HttpContext.Request.Host.Value}/{filePath}";
-           
+
             return linkUrl;
         }