Blame view

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