Переглянути джерело

Merge branch 'feature/task_68_cancelpublish' into test

qinchaoyue 5 місяців тому
батько
коміт
3ada6985ec

+ 24 - 5
src/Hotline.Api/Controllers/OrderController.cs

@@ -382,15 +382,24 @@ public class OrderController : BaseController
     public async Task PublishOrder([FromBody] PublishOrderDto dto)
     {
         //验证订单
-        var order = await _orderRepository.GetAsync(dto.Id, HttpContext.RequestAborted);
-
-
-        if (order is null)
-            throw UserFriendlyException.SameMessage("未找到工单,无法发布");
+        var order = await _orderRepository.GetAsync(dto.Id, HttpContext.RequestAborted)
+            ?? throw UserFriendlyException.SameMessage("未找到工单,无法发布");
 
         if (order.Status != EOrderStatus.Filed)
             throw UserFriendlyException.SameMessage("当前状态无法发布");
 
+        var enabled = _systemSettingCacheManager.CancelPublishOrderEnabled;
+        if (enabled)
+        {
+            // 获取上一次被取消发布的发布信息
+            var publishedDeleted = await _orderPublishRepository.Queryable().Where(m => m.OrderId == dto.Id && m.IsDeleted == true)
+                .OrderByDescending(m => m.CreationTime).FirstAsync(HttpContext.RequestAborted);
+            if (publishedDeleted != null && _sessionContext.RequiredUserId != publishedDeleted.CreatorId)
+            {
+                throw UserFriendlyException.SameMessage($"改工单被取消发布过, 之前的发布人是 [{publishedDeleted.CreatorName}], 您不能发布;");
+            }
+        }
+
         //新增发布工单
         var orderPublish = _mapper.Map<OrderPublish>(dto);
         orderPublish.OrderId = order.Id;
@@ -771,10 +780,20 @@ public class OrderController : BaseController
     {
         var enabled = _systemSettingCacheManager.CancelPublishOrderEnabled;
         if (enabled == false) return "取消发布功能已被关闭";
+
         var publish = await _orderPublishRepository.GetAsync(dto.OrderPublishId)
             ?? throw UserFriendlyException.SameMessage("发布单不存在");
         publish.IsDeleted = true;
+        var order = await _orderRepository.GetAsync(publish.OrderId, HttpContext.RequestAborted) ??
+            throw UserFriendlyException.SameMessage("工单不存在");
+        if (order.Status != EOrderStatus.Published && order.Status != EOrderStatus.Visited)
+            throw UserFriendlyException.SameMessage("工单状态非[已发布]和[已回访], 不可取消发布.");
+
         await _orderPublishRepository.UpdateAsync(publish);
+        order.Status = EOrderStatus.Filed;
+        // 被取消的工单继续由之前发布的用户继续发布;
+        order.WaitForPublisherId = publish.CreatorId;
+        await _orderRepository.UpdateAsync(order);
         return "取消成功";
     }
     #endregion

+ 34 - 3
src/Hotline.Application.Tests/Controller/OrderControllerTest.cs

@@ -15,6 +15,7 @@ using Hotline.Settings.Hotspots;
 using Hotline.Share.Dtos.File;
 using Hotline.Share.Dtos.FlowEngine;
 using Hotline.Share.Dtos.Order;
+using Hotline.Share.Dtos.Order.Publish;
 using Hotline.Share.Dtos.Users;
 using Hotline.Share.Enums.FlowEngine;
 using Hotline.Share.Enums.Order;
@@ -34,6 +35,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using XF.Domain.Authentications;
+using XF.Domain.Exceptions;
 using XF.Domain.Repository;
 
 namespace Hotline.Application.Tests.Controller;
@@ -205,8 +207,37 @@ public class OrderControllerTest : TestBase
         await _orderController.PublishCancelAsync(new PublishCancelInDto { OrderPublishId = publish.Id });
         var visit = await _orderVisitRepository.GetAsync(m => m.OrderId == order.Id);
         var publishCount = await _orderPublishRepository.Queryable().Where(m => m.OrderId == order.Id && m.IsDeleted == false).CountAsync();
-        publishCount.ShouldBe(0);
-        visit.ShouldNotBeNull();
-        visit.VisitState.ShouldBe(EVisitState.Visited);
+        publishCount.ShouldBe(0, "发布工单数据已经被删除, 应该是0条");
+        visit.ShouldNotBeNull("回访信息不存在");
+        visit.VisitState.ShouldBe(EVisitState.Visited, "回访状态应该是已回访");
+
+        orderEntity = await _orderRepository.GetAsync(order.Id);
+        orderEntity.Status.ShouldBe(EOrderStatus.Filed);
+        var publishList = await _orderController.PublishOrderList(new QueryOrderPublishDto 
+        {
+            PageSize = 1000,
+            QuerySelf = true
+        });
+
+        publishList.Items.Any(m => m.Id == order.Id).ShouldBeTrue("工单发布列表中没有取消的工单");
+        SetZuoXi();
+        // 验证是否能非上次发布人员发布
+        try
+        {
+            await _orderController.PublishOrder(new PublishOrderDto { Id = order.Id });
+        }
+        catch (UserFriendlyException e)
+        {
+            e.Message.Contains("您不能发布").ShouldBeTrue();
+        }
+
+        // 验证被取消前的发布人能否继续发布
+        SetPaiDanYuan();
+        var inDto = _fixture.Create<PublishOrderDto>();
+        inDto.Id = order.Id;
+        await _orderController.PublishOrder(inDto);
+        var published = await _orderPublishRepository.Queryable().Where(m => m.OrderId == order.Id && m.IsDeleted == false).FirstAsync();
+        published.ShouldNotBeNull();
+        published.IsDeleted.ShouldBeFalse();
     }
 }

+ 1 - 0
src/Hotline.Application.Tests/Infrastructure/TestSettingConstants.cs

@@ -7,6 +7,7 @@ using System.Threading.Tasks;
 namespace Hotline.Application.Tests.Infrastructure;
 public static class TestSettingConstants
 {
+    public const string ZuoXiAccountName = "UnitTestZuoXi";
     public const string PaiDanYuanAccountName = "UnitTestPDY";
     public const string FirstOrgAccountName = "cs";
     public const string SecondOrgAccountName = "cs21";

+ 1 - 1
src/Hotline.Application.Tests/TestBase.cs

@@ -58,7 +58,7 @@ public class TestBase
 
     public void SetZuoXi()
     {
-        SetOperator("坐席", "市民热线服务中心", "单元测试派单员", "001", "13408389849", EUserType.Seat, TestSettingConstants.PaiDanYuanAccountName);
+        SetOperator("坐席", "市民热线服务中心", "单元测试坐席", "001", "13408389849", EUserType.Seat, TestSettingConstants.ZuoXiAccountName);
     }
 
     private void SetOperator(string displayName, string fullOrgName, string name, string orgId, string phoneNo, EUserType userType, string userName)