Browse Source

Merge branch 'feature/snapshot' into dev

qinchaoyue 4 days ago
parent
commit
f804b6e24a

+ 30 - 2
src/Hotline.Api/Controllers/Snapshot/SnapshotUserController.cs

@@ -3,6 +3,7 @@ using Hotline.Repository.SqlSugar.Extensions;
 using Hotline.Share.Dtos;
 using Hotline.Share.Dtos.Snapshot;
 using Hotline.Share.Tools;
+using Hotline.Snapshot.IRepository;
 using Microsoft.AspNetCore.Mvc;
 using System.ComponentModel;
 
@@ -12,10 +13,28 @@ namespace Hotline.Api.Controllers.Snapshot;
 public class SnapshotUserController : BaseController
 {
     private readonly ISnapshotUserApplication _snapshotUserApplication;
+    private readonly ISafetyTypeRepository _safetyTypeRepository;
 
-    public SnapshotUserController(ISnapshotUserApplication snapshotUserApplication)
+    public SnapshotUserController(ISnapshotUserApplication snapshotUserApplication, ISafetyTypeRepository safetyTypeRepository)
     {
         _snapshotUserApplication = snapshotUserApplication;
+        _safetyTypeRepository = safetyTypeRepository;
+    }
+
+    /// <summary>
+    /// 获取添加安全志愿者和安全员分类关系的基础数据
+    /// </summary>
+    /// <returns></returns>
+    [HttpGet("citizen_relation/basedata")]
+    public async Task<Dictionary<string, object>> GetCitizenRelationSafetyTypeBase()
+    {
+        var safetyTypes = await _safetyTypeRepository.Queryable()
+            .Select(m => new { m.Id, m.Name})
+            .ToListAsync(HttpContext.RequestAborted);
+        return new Dictionary<string, object>
+        {
+            { "safetyTypes", safetyTypes }
+        };
     }
 
     /// <summary>
@@ -27,11 +46,20 @@ public class SnapshotUserController : BaseController
         => (await _snapshotUserApplication.GetCitizenRelationSafetyType(dto).ToPagedListAsync(dto)).ToPaged();
 
     /// <summary>
-    /// 添加安全志愿者
+    /// 添加安全志愿者和安全员分类关系
     /// </summary>
     /// <param name="dto"></param>
     /// <returns></returns>
     [HttpPost("citizen_relation")]
     public async Task AddCitizenRelationSafetyTypeInDto([FromBody]AddCitizenRelationSafetyTypeInDto dto)
         => await _snapshotUserApplication.AddCitizenRelationSafetyType(dto, HttpContext.RequestAborted);
+
+    /// <summary>
+    /// 批量删除安全志愿者分类关系
+    /// </summary>
+    /// <param name="dto"></param>
+    /// <returns></returns>
+    [HttpDelete("citizen_relation")]
+    public async Task DeleteCitizenRelationSafetyType([FromBody] DeleteCitizenRelationSafetyTypeInDto dto)
+        => await _snapshotUserApplication.DeleteCitizenRelationSafetyAsync(dto);
 }

+ 13 - 0
src/Hotline.Application/Snapshot/Contracts/ISnapshotUserApplication.cs

@@ -7,5 +7,18 @@ public interface ISnapshotUserApplication
 {
     ISugarQueryable<CitizenRelationSafetyTypeOutDto> GetCitizenRelationSafetyType(CitizenRelationSafetyTypeInDto dto);
 
+    /// <summary>
+    /// 添加安全员类型和市民关系
+    /// </summary>
+    /// <param name="dto"></param>
+    /// <param name="token"></param>
+    /// <returns></returns>
     Task AddCitizenRelationSafetyType(AddCitizenRelationSafetyTypeInDto dto, CancellationToken token);
+
+    /// <summary>
+    /// 删除关系
+    /// </summary>
+    /// <param name="dto"></param>
+    /// <returns></returns>
+    Task DeleteCitizenRelationSafetyAsync(DeleteCitizenRelationSafetyTypeInDto dto);
 }

+ 21 - 1
src/Hotline.Application/Snapshot/SnapshotUserApplication.cs

@@ -49,7 +49,9 @@ public class SnapshotUserApplication : ISnapshotUserApplication, IScopeDependenc
             safeType.Citizens.Add(new Citizen { Id = item });
         }
 
-        _safetyTypeRepository.AddNav(safeType);
+        await _safetyTypeRepository.AddNav(safeType)
+            .Include(m => m.Citizens)
+            .ExecuteCommandAsync();
     }
 
     public ISugarQueryable<CitizenRelationSafetyTypeOutDto> GetCitizenRelationSafetyType(CitizenRelationSafetyTypeInDto dto)
@@ -68,4 +70,22 @@ public class SnapshotUserApplication : ISnapshotUserApplication, IScopeDependenc
             });
         return query;
     }
+
+    public async Task DeleteCitizenRelationSafetyAsync(DeleteCitizenRelationSafetyTypeInDto dto)
+    {
+        foreach (var item in dto.Items)
+        {
+            var citizen = await _citizenRepository.Queryable()
+                .Includes(m => m.SafetyTypes)
+                .Where(m => m.Id == item.CitizenId)
+                .FirstAsync();
+            var deleteSafetyType = citizen.SafetyTypes.FirstOrDefault(m => m.Id == item.SafetyTypeId);
+            if (deleteSafetyType == null) continue;
+
+            citizen.SafetyTypes.Remove(deleteSafetyType);
+            _citizenRepository.UpdateNav(citizen)
+                .Include(m => m.SafetyTypes)
+                .ExecuteCommand();
+        }
+    }
 }

+ 11 - 1
src/Hotline.Share/Dtos/Snapshot/SnapshotBulletinDto.cs

@@ -255,6 +255,16 @@ public class AddSnapshotBulletinInDto
     /// </summary>
     public bool IsPopup { get; set; }
 
+    /// <summary>
+    /// 是否随手拍
+    /// </summary>
+    public bool? IsSnapshot { get; set; }
+
+    /// <summary>
+    /// 志愿者类型Id
+    /// </summary>
+    public string? SafetyTypeId { get; set; }
+
     /// <summary>
     /// 时间
     /// </summary>
@@ -268,5 +278,5 @@ public class AddSnapshotBulletinInDto
     /// <summary>
     /// 视频地址
     /// </summary>
-    public string VideoPath { get; set; }
+    public string? VideoPath { get; set; }
 }

+ 22 - 0
src/Hotline.Share/Dtos/Snapshot/SnapshotUserInfoDto.cs

@@ -102,3 +102,25 @@ public class AddCitizenRelationSafetyTypeInDto
     public IList<string> CitizenIds { get; set; }
 }
 
+
+public class DeleteCitizenRelationSafetyTypeInDto
+{
+    /// <summary>
+    /// 市民和志愿者类型集合
+    /// </summary>
+    public IList<CitizenIdRelationSafetyTypeId> Items { get; set; }
+}
+
+public class CitizenIdRelationSafetyTypeId
+{
+    /// <summary>
+    /// 市民Id
+    /// </summary>
+    [Required]
+    public string CitizenId { get; set; }
+
+    /// <summary>
+    /// 志愿者类型Id
+    /// </summary>
+    public string SafetyTypeId { get; set; }
+}

+ 40 - 1
test/Hotline.Tests/Application/SnapshotUserApplicationTest.cs

@@ -46,7 +46,7 @@ public class SnapshotUserApplicationTest : TestBase
 
         var citizen = await _citizenRepository.Queryable()
             .LeftJoin<CitizenRelationSafetyType>((citizen, relation) => relation.CitizenId == citizen.Id)
-            .Where((citizen, relation) => relation.Id == null)
+            .Where((citizen, relation) => relation.CitizenId == null)
             .FirstAsync();
         var addDto = new AddCitizenRelationSafetyTypeInDto
         {
@@ -67,5 +67,44 @@ public class SnapshotUserApplicationTest : TestBase
         item.SafetyTypeId.ShouldBe(safetyType.Id);
         item.CitizenId.ShouldBe(citizen.Id);
         item.CitizenName.ShouldBe(citizen.Name);
+
+        var newSafetyTypeXuanChuan = new SafetyType { Name = "宣传员" };
+        var safetyTypeXuanChuan = await _safetyTypeRepository.Queryable()
+            .Where(m => m.Name == newSafetyTypeXuanChuan.Name).FirstAsync();
+        safetyTypeXuanChuan ??= new SafetyType
+            {
+                Id = await _safetyTypeRepository.AddAsync(newSafetyTypeXuanChuan)
+            };
+        addDto = new AddCitizenRelationSafetyTypeInDto
+        {
+            CitizenIds = [citizen.Id],
+            SafetyTypeId = safetyTypeXuanChuan.Id,
+        };
+        await _snapshotUserApplication.AddCitizenRelationSafetyType(addDto, CancellationToken.None);
+        var deleteInDto = new DeleteCitizenRelationSafetyTypeInDto
+        {
+            Items =
+            [
+                new() {
+                    CitizenId = citizen.Id,
+                    SafetyTypeId = safetyType.Id
+                }
+            ]
+        };
+        await _snapshotUserApplication.DeleteCitizenRelationSafetyAsync(deleteInDto);
+
+        items = await _snapshotUserApplication.GetCitizenRelationSafetyType(inDto).ToListAsync();
+        item = items.FirstOrDefault(m => m.CitizenId == citizen.Id && m.SafetyTypeId == safetyType.Id);
+        item.ShouldBeNull();
+        inDto = new CitizenRelationSafetyTypeInDto
+        {
+            SafetyTypeId = safetyTypeXuanChuan.Id
+        };
+        items = await _snapshotUserApplication.GetCitizenRelationSafetyType(inDto).ToListAsync();
+        item = items.FirstOrDefault(m => m.CitizenId == citizen.Id && m.SafetyTypeId == safetyTypeXuanChuan.Id);
+        item.SafetyTypeName.ShouldBe(safetyTypeXuanChuan.Name);
+        item.SafetyTypeId.ShouldBe(safetyTypeXuanChuan.Id);
+        item.CitizenId.ShouldBe(citizen.Id);
+        item.CitizenName.ShouldBe(citizen.Name);
     }
 }