Blame view

美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/LocationSupportAppService.cs 5.56 KB
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
1
  using FoodLabeling.Application.Contracts;
957e20a0   李曜臣   门店支持
2
3
  using FoodLabeling.Application.Contracts.Dtos.LocationSupport;
  using FoodLabeling.Application.Contracts.IServices;
540ac0e3   杨鑫   前端修改bug
4
  using FoodLabeling.Application.Helpers;
957e20a0   李曜臣   门店支持
5
  using FoodLabeling.Application.Services.DbModels;
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
6
  using Microsoft.AspNetCore.Authorization;
957e20a0   李曜臣   门店支持
7
8
9
10
11
12
13
14
15
  using Volo.Abp;
  using Volo.Abp.Application.Services;
  using Volo.Abp.Guids;
  using Volo.Abp.Uow;
  using Yi.Framework.SqlSugarCore.Abstractions;
  
  namespace FoodLabeling.Application.Services;
  
  /// <summary>
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
16
  /// 全局 Support 联系方式(全门店共用;Web 可增改查,App JWT 仅可读)
957e20a0   李曜臣   门店支持
17
  /// </summary>
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
18
  [Authorize]
957e20a0   李曜臣   门店支持
19
20
21
22
23
24
25
26
27
28
29
30
  public class LocationSupportAppService : ApplicationService, ILocationSupportAppService
  {
      private readonly ISqlSugarDbContext _dbContext;
      private readonly IGuidGenerator _guidGenerator;
  
      public LocationSupportAppService(ISqlSugarDbContext dbContext, IGuidGenerator guidGenerator)
      {
          _dbContext = dbContext;
          _guidGenerator = guidGenerator;
      }
  
      /// <inheritdoc />
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
31
      public async Task<LocationSupportGetOutputDto?> GetSupportAsync()
957e20a0   李曜臣   门店支持
32
      {
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
33
34
35
36
37
          var rows = await _dbContext.SqlSugarClient.Queryable<FlLocationSupportDbEntity>()
              .Where(x => !x.IsDeleted)
              .ToListAsync();
          var entity = rows.FirstOrDefault();
          return MapOutput(entity);
957e20a0   李曜臣   门店支持
38
39
40
41
42
43
      }
  
      /// <inheritdoc />
      [UnitOfWork]
      public async Task<LocationSupportGetOutputDto> CreateAsync(LocationSupportCreateInputVo input)
      {
540ac0e3   杨鑫   前端修改bug
44
          EnsureWebAdminCanMutate();
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
45
  
957e20a0   李曜臣   门店支持
46
47
          if (input is null)
          {
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
48
              throw new UserFriendlyException("Request body is required.");
957e20a0   李曜臣   门店支持
49
50
          }
  
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
51
52
          var phone = NormalizeRequired(input.SupportPhone, "Support phone is required.");
          var email = NormalizeRequired(input.SupportEmail, "Support email is required.");
957e20a0   李曜臣   门店支持
53
54
          EnsureEmailFormat(email);
  
957e20a0   李曜臣   门店支持
55
          var existed = await _dbContext.SqlSugarClient.Queryable<FlLocationSupportDbEntity>()
87313aec   李曜臣   门店支持;登陆接口优化
56
              .AnyAsync(x => !x.IsDeleted);
957e20a0   李曜臣   门店支持
57
58
          if (existed)
          {
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
59
60
              throw new UserFriendlyException(
                  "Global support contact already exists. Use update instead.");
957e20a0   李曜臣   门店支持
61
62
63
64
65
66
67
68
69
70
71
          }
  
          var now = Clock.Now;
          var entity = new FlLocationSupportDbEntity
          {
              Id = _guidGenerator.Create().ToString(),
              IsDeleted = false,
              CreationTime = now,
              CreatorId = CurrentUser?.Id?.ToString(),
              LastModificationTime = now,
              LastModifierId = CurrentUser?.Id?.ToString(),
957e20a0   李曜臣   门店支持
72
73
74
75
76
              SupportPhone = phone,
              SupportEmail = email
          };
  
          await _dbContext.SqlSugarClient.Insertable(entity).ExecuteCommandAsync();
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
77
          return MapOutput(entity)!;
957e20a0   李曜臣   门店支持
78
79
80
81
82
83
      }
  
      /// <inheritdoc />
      [UnitOfWork]
      public async Task<LocationSupportGetOutputDto> UpdateAsync(string id, LocationSupportUpdateInputVo input)
      {
540ac0e3   杨鑫   前端修改bug
84
          EnsureWebAdminCanMutate();
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
85
  
957e20a0   李曜臣   门店支持
86
87
88
          var supportId = id?.Trim();
          if (string.IsNullOrWhiteSpace(supportId))
          {
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
89
              throw new UserFriendlyException("Support record id is required.");
957e20a0   李曜臣   门店支持
90
91
92
93
          }
  
          if (input is null)
          {
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
94
              throw new UserFriendlyException("Request body is required.");
957e20a0   李曜臣   门店支持
95
96
          }
  
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
97
98
99
100
          var rows = await _dbContext.SqlSugarClient.Queryable<FlLocationSupportDbEntity>()
              .Where(x => !x.IsDeleted && x.Id == supportId)
              .ToListAsync();
          var entity = rows.FirstOrDefault();
957e20a0   李曜臣   门店支持
101
102
          if (entity is null)
          {
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
103
              throw new UserFriendlyException("Support record not found.");
957e20a0   李曜臣   门店支持
104
105
          }
  
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
106
107
          var phone = NormalizeRequired(input.SupportPhone, "Support phone is required.");
          var email = NormalizeRequired(input.SupportEmail, "Support email is required.");
957e20a0   李曜臣   门店支持
108
          EnsureEmailFormat(email);
957e20a0   李曜臣   门店支持
109
  
957e20a0   李曜臣   门店支持
110
111
112
113
114
115
          entity.SupportPhone = phone;
          entity.SupportEmail = email;
          entity.LastModificationTime = Clock.Now;
          entity.LastModifierId = CurrentUser?.Id?.ToString();
  
          await _dbContext.SqlSugarClient.Updateable(entity).ExecuteCommandAsync();
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
116
          return MapOutput(entity)!;
957e20a0   李曜臣   门店支持
117
118
      }
  
540ac0e3   杨鑫   前端修改bug
119
120
121
122
123
124
125
126
127
128
      private void EnsureWebAdminCanMutate()
      {
          EnsureNotUsAppClient();
          if (!ReportsRoleHelper.IsAdminRole(CurrentUser))
          {
              throw new UserFriendlyException(
                  "Only administrators can create or update global support contact settings.");
          }
      }
  
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
129
      private void EnsureNotUsAppClient()
957e20a0   李曜臣   门店支持
130
      {
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
131
          if (CurrentUser.FindClaim(UsAppJwtClaims.ClientKind)?.Value == UsAppJwtClaims.ClientKindUsApp)
957e20a0   李曜臣   门店支持
132
          {
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
133
134
              throw new UserFriendlyException(
                  "The mobile app can only view support contacts. Please use the web console to edit.");
957e20a0   李曜臣   门店支持
135
          }
957e20a0   李曜臣   门店支持
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
      }
  
      private static string NormalizeRequired(string? value, string message)
      {
          var normalized = value?.Trim();
          if (string.IsNullOrWhiteSpace(normalized))
          {
              throw new UserFriendlyException(message);
          }
  
          return normalized;
      }
  
      private static void EnsureEmailFormat(string email)
      {
          if (!email.Contains("@", StringComparison.Ordinal) || email.StartsWith("@", StringComparison.Ordinal) ||
              email.EndsWith("@", StringComparison.Ordinal))
          {
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
154
              throw new UserFriendlyException("Support email format is invalid.");
957e20a0   李曜臣   门店支持
155
156
157
          }
      }
  
ecb291fd   李曜臣   门店支持优化;标签,产品组件优化
158
      private static LocationSupportGetOutputDto? MapOutput(FlLocationSupportDbEntity? entity)
957e20a0   李曜臣   门店支持
159
160
161
162
163
164
      {
          if (entity is null)
          {
              return null;
          }
  
957e20a0   李曜臣   门店支持
165
166
167
          return new LocationSupportGetOutputDto
          {
              Id = entity.Id,
957e20a0   李曜臣   门店支持
168
169
170
171
172
              SupportPhone = entity.SupportPhone,
              SupportEmail = entity.SupportEmail
          };
      }
  }