qinchaoyue 4 mesiacov pred
rodič
commit
71d45208f6

+ 9 - 0
src/Hotline.Api/Controllers/Snapshot/IndustryController.cs

@@ -46,4 +46,13 @@ public class IndustryController : BaseController
     [HttpGet("industry")]
     public async Task<PagedDto<IndustryItemsOutDto>> GetIndustryAsync([FromQuery] IndustryListInDto dto)
         => (await _industryApplication.GetIndustres(dto).ToPagedListAsync(dto, HttpContext.RequestAborted)).ToPaged();
+
+    /// <summary>
+    /// 修改行业
+    /// </summary>
+    /// <param name="dto"></param>
+    /// <returns></returns>
+    [HttpPut]
+    public async Task UpdateIndustry([FromBody] UpdateIndustryInDto dto)
+        => await _industryApplication.UpdateIndustryAsync(dto, HttpContext.RequestAborted);
 }

+ 57 - 0
src/Hotline.Application.Tests/Application/IndustryApplicationTest.cs

@@ -0,0 +1,57 @@
+using DocumentFormat.OpenXml.Wordprocessing;
+using Hotline.Api.Controllers;
+using Hotline.Application.Snapshot;
+using Hotline.Identity.Accounts;
+using Hotline.Identity.Roles;
+using Hotline.Share.Dtos.Snapshot;
+using Hotline.Share.Tools;
+using Hotline.Snapshot;
+using Hotline.Snapshot.Interfaces;
+using Hotline.Users;
+using Mapster;
+using Microsoft.AspNetCore.Http;
+using Microsoft.Extensions.DependencyInjection;
+using Shouldly;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using XF.Domain.Repository;
+
+namespace Hotline.Application.Tests.Application;
+public class IndustryApplicationTest : TestBase
+{
+    private readonly IIndustryApplication _industryApplication;
+    private readonly IIndustryRepository _industryRepository;
+
+    public IndustryApplicationTest(IAccountRepository accountRepository, IRepository<Role> roleRepository, UserController userController, IServiceScopeFactory scopeFactory, IRepository<User> userRepository, IHttpContextAccessor httpContextAccessor, IThirdIdentiyService thirdIdentiyService, IThirdAccountRepository thirdAccountRepository, IIndustryApplication industryApplication, IIndustryRepository industryRepository) : base(accountRepository, roleRepository, userController, scopeFactory, userRepository, httpContextAccessor, thirdIdentiyService, thirdAccountRepository)
+    {
+        _industryApplication = industryApplication;
+        _industryRepository = industryRepository;
+    }
+
+    [Fact]
+    public async Task UpdateIndustry_Test()
+    {
+        var industryItems = _industryApplication.GetIndustres(new IndustryListInDto(null, null)).ToList();
+        industryItems.NotNullOrEmpty().ShouldBeTrue("行业集合为空");
+        var industry = industryItems.First().Adapt<UpdateIndustryInDto>();
+        var item = await _industryRepository.GetAsync(industry.Id);
+        industry.ForeachClassProperties(async (industry, property, name, value) =>
+        {
+            if (name.ToUpper() == "ID") return true;
+            if (value is String)
+                industry.GetType().GetProperty(name).SetValue(industry, value + DateTime.Now.ToString("ss"));
+            return true;
+        });
+        await _industryApplication.UpdateIndustryAsync(industry, CancellationToken.None);
+        var updateIndustry = await _industryApplication.GetIndustryDetailAsync(item.Id);
+        updateIndustry.ForeachClassProperties(async (industry, property, name, value) =>
+        {
+            industry.GetType().GetProperty(name).GetValue(industry).ShouldBe(value);
+            return true;
+        });
+        await _industryApplication.UpdateIndustryAsync(item.Adapt<UpdateIndustryInDto>(), CancellationToken.None);
+    }
+}

+ 8 - 0
src/Hotline.Application/Snapshot/IIndustryApplication.cs

@@ -28,4 +28,12 @@ public interface IIndustryApplication
     /// <param name="id"></param>
     /// <returns></returns>
     Task<IndustryDetailOutDto> GetIndustryDetailAsync(string id);
+
+    /// <summary>
+    /// 修改行业
+    /// </summary>
+    /// <param name="dto"></param>
+    /// <param name="requestAborted"></param>
+    /// <returns></returns>
+    Task UpdateIndustryAsync(UpdateIndustryInDto dto, CancellationToken requestAborted);
 }

+ 27 - 1
src/Hotline.Application/Snapshot/IndustryApplication.cs

@@ -1,4 +1,5 @@
-using Hotline.Caching.Interfaces;
+using DocumentFormat.OpenXml.Office2010.Excel;
+using Hotline.Caching.Interfaces;
 using Hotline.File;
 using Hotline.Repository.SqlSugar.Extensions;
 using Hotline.Share.Dtos;
@@ -6,14 +7,17 @@ using Hotline.Share.Dtos.Snapshot;
 using Hotline.Share.Tools;
 using Hotline.Snapshot;
 using Hotline.Snapshot.Interfaces;
+using Hotline.Tools;
 using Mapster;
 using SqlSugar;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
 using XF.Domain.Dependency;
+using XF.Domain.Exceptions;
 
 namespace Hotline.Application.Snapshot;
 public class IndustryApplication : IIndustryApplication, IScopeDependency
@@ -77,4 +81,26 @@ public class IndustryApplication : IIndustryApplication, IScopeDependency
             outDto.CellImgUrl = fileDownloadApi + outDto.CellImgUrl;
         return outDto;
     }
+
+    /// <summary>
+    /// 修改行业
+    /// </summary>
+    /// <param name="dto"></param>
+    /// <param name="requestAborted"></param>
+    /// <returns></returns>
+    /// <exception cref="NotImplementedException"></exception>
+    public async Task UpdateIndustryAsync(UpdateIndustryInDto dto, CancellationToken requestAborted)
+    {
+        dto.ValidateObject();
+        var entity = await _industryRepository.GetAsync(dto.Id) ?? throw UserFriendlyException.SameMessage($"行业不存在 {dto.Id}");
+        if (dto.Files.NotNullOrEmpty())
+        {
+            var fileEntities = dto.Files.Adapt<List<File.File>>();
+            fileEntities.ForEach(m => m.Key = entity.Id);
+            await _fileRepository.Removeable().Where(m => m.Key == entity.Id).ExecuteCommandAsync(requestAborted);
+            await _fileRepository.AddRangeAsync(fileEntities, requestAborted);
+        }
+        dto.Adapt(entity);
+        await _industryRepository.UpdateAsync(entity, requestAborted);
+    }
 }

+ 10 - 0
src/Hotline.Share/Dtos/Snapshot/IndustryDto.cs

@@ -119,6 +119,7 @@ public class IndustryOutDto
 /// <param name="departmentName">审批部门</param>
 public record IndustryListInDto(string? Name, string? ApproveOrgName) : PagedRequest;
 
+
 public class IndustryBaseOutDto
 {
     /// <summary>
@@ -162,6 +163,15 @@ public class IndustryBaseOutDto
     public IReadOnlyList<SystemDicDataOutDto> BusinessUnitType { get; set; }
 }
 
+public class UpdateIndustryInDto : AddIndustryDto
+{
+    /// <summary>
+    /// Id
+    /// </summary>
+    [Required]
+    public string Id { get; set; }
+}
+
 public class AddIndustryDto
 {
     /// <summary>