LqMdXdbhsjService.cs
8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using NCC.ClayObject;
using NCC.Common.Configuration;
using NCC.Common.Core.Manager;
using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Common.Helper;
using NCC.Common.Model.NPOI;
using NCC.DataEncryption;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.Extend.Entitys.Dto.LqMdXdbhsj;
using NCC.Extend.Entitys.lq_md_xdbhsj;
using NCC.Extend.Interfaces.LqMdXdbhsj;
using NCC.FriendlyException;
using NCC.JsonSerialization;
using SqlSugar;
using Yitter.IdGenerator;
namespace NCC.Extend.LqMdXdbhsj
{
/// <summary>
/// 门店新店保护时间服务
/// </summary>
[ApiDescriptionSettings(Tag = "绿纤门店信息服务", Name = "LqMdXdbhsj", Order = 201)]
[Route("api/Extend/[controller]")]
public class LqMdXdbhsjService : ILqMdXdbhsjService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository<LqMdXdbhsjEntity> _lqMdXdbhsjRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
/// <summary>
/// 初始化一个<see cref="LqMdXdbhsjService"/>类型的新实例
/// </summary>
public LqMdXdbhsjService(ISqlSugarRepository<LqMdXdbhsjEntity> lqMdXdbhsjRepository, IUserManager userManager)
{
_lqMdXdbhsjRepository = lqMdXdbhsjRepository;
_db = _lqMdXdbhsjRepository.Context;
_userManager = userManager;
}
#region 门店新店保护时间信息
/// <summary>
/// 获取门店新店保护时间信息
/// </summary>
/// <param name="id">参数</param>
/// <returns></returns>
[HttpGet("{id}")]
public async Task<dynamic> GetInfo(string id)
{
var entity = await _db.Queryable<LqMdXdbhsjEntity>().FirstAsync(p => p.Id == id);
var output = entity.Adapt<LqMdXdbhsjInfoOutput>();
return output;
}
#endregion
#region 门店新店保护时间列表
/// <summary>
/// 获取门店新店保护时间列表
/// </summary>
/// <param name="input">请求参数</param>
/// <returns></returns>
[HttpGet("")]
public async Task<dynamic> GetList([FromQuery] LqMdXdbhsjListQueryInput input)
{
var sidx = input.sidx == null ? "F_Id" : input.sidx;
var data = await _db.Queryable<LqMdXdbhsjEntity>()
.WhereIF(!string.IsNullOrEmpty(input.mdid), p => p.Mdid.Contains(input.mdid))
.WhereIF(input.bhkssj.HasValue, p => p.Bhkssj >= input.bhkssj)
.WhereIF(input.bhjssj.HasValue, p => p.Bhjssj <= input.bhjssj)
.WhereIF(input.sfqy.HasValue, p => p.Sfqy == input.sfqy)
.Select(it => new LqMdXdbhsjListOutput
{
F_Id = it.Id,
mdid = it.Mdid,
bhkssj = it.Bhkssj,
bhjssj = it.Bhjssj,
sm = it.Sm,
cjsj = it.Cjsj,
sfqy = it.Sfqy,
stage = it.Stage,
})
.MergeTable()
.OrderBy(sidx + " " + input.sort)
.ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult<LqMdXdbhsjListOutput>.SqlSugarPageResult(data);
}
#endregion
#region 新建门店新店保护时间
/// <summary>
/// 新建门店新店保护时间
/// </summary>
/// <param name="input">参数</param>
/// <returns></returns>
[HttpPost("")]
public async Task Create([FromBody] LqMdXdbhsjCrInput input)
{
var userInfo = await _userManager.GetUserInfo();
var entity = input.Adapt<LqMdXdbhsjEntity>();
entity.Id = YitIdHelper.NextId().ToString();
entity.Cjsj = DateTime.Now;
// 验证时间逻辑
if (entity.Bhkssj >= entity.Bhjssj)
{
throw NCCException.Oh("保护开始时间必须早于保护结束时间");
}
// 检查是否存在重叠的保护时间
var exists = await _db.Queryable<LqMdXdbhsjEntity>()
.Where(p => p.Mdid == entity.Mdid && p.Sfqy == 1)
.Where(p => (p.Bhkssj <= entity.Bhkssj && p.Bhjssj > entity.Bhkssj) || (p.Bhkssj < entity.Bhjssj && p.Bhjssj >= entity.Bhjssj) || (p.Bhkssj >= entity.Bhkssj && p.Bhjssj <= entity.Bhjssj))
.AnyAsync();
if (exists)
{
throw NCCException.Oh("该门店在指定时间段内已存在保护时间设置");
}
var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
if (!(isOk > 0))
throw NCCException.Oh(ErrorCode.COM1000);
}
#endregion
#region 更新门店新店保护时间
/// <summary>
/// 更新门店新店保护时间
/// </summary>
/// <param name="id">参数</param>
/// <param name="input">参数</param>
/// <returns></returns>
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] LqMdXdbhsjUpInput input)
{
var entity = input.Adapt<LqMdXdbhsjEntity>();
entity.Id = id;
// 验证时间逻辑
if (entity.Bhkssj >= entity.Bhjssj)
{
throw NCCException.Oh(ErrorCode.COM1000, "保护开始时间必须早于保护结束时间");
}
// 检查是否存在重叠的保护时间(排除当前记录)
var exists = await _db.Queryable<LqMdXdbhsjEntity>()
.Where(p => p.Mdid == entity.Mdid && p.Sfqy == 1 && p.Id != id)
.Where(p =>
(p.Bhkssj <= entity.Bhkssj && p.Bhjssj > entity.Bhkssj) || (p.Bhkssj < entity.Bhjssj && p.Bhjssj >= entity.Bhjssj) || (p.Bhkssj >= entity.Bhkssj && p.Bhjssj <= entity.Bhjssj)
)
.AnyAsync();
if (exists)
{
throw NCCException.Oh(ErrorCode.COM1000, "该门店在指定时间段内已存在保护时间设置");
}
var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0))
throw NCCException.Oh(ErrorCode.COM1001);
}
#endregion
#region 删除门店新店保护时间
/// <summary>
/// 删除门店新店保护时间
/// </summary>
/// <returns></returns>
[HttpDelete("{id}")]
public async Task Delete(string id)
{
var entity = await _db.Queryable<LqMdXdbhsjEntity>().FirstAsync(p => p.Id == id);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
var isOk = await _db.Deleteable<LqMdXdbhsjEntity>().Where(d => d.Id == id).ExecuteCommandAsync();
if (!(isOk > 0))
throw NCCException.Oh(ErrorCode.COM1002);
}
#endregion
#region 获取门店新店保护时间下拉选择数据
/// <summary>
/// 获取门店新店保护时间下拉选择数据
/// </summary>
/// <returns></returns>
[HttpGet("Selector")]
public async Task<dynamic> GetSelector()
{
var list = await _db.Queryable<LqMdXdbhsjEntity>()
.Where(p => p.Sfqy == 1)
.Select(it => new
{
id = it.Id,
fullName = it.Mdid,
enCode = it.Id,
stage = it.Stage,
})
.ToListAsync();
return new { list = list };
}
#endregion
#region 根据门店ID获取当前有效的保护时间
/// <summary>
/// 根据门店ID获取当前有效的保护时间
/// </summary>
/// <param name="mdid">门店ID</param>
/// <returns></returns>
[HttpGet("GetByStoreId/{mdid}")]
public async Task<dynamic> GetByStoreId(string mdid)
{
var now = DateTime.Now;
var entity = await _db.Queryable<LqMdXdbhsjEntity>().Where(p => p.Mdid == mdid && p.Sfqy == 1).Where(p => p.Bhkssj <= now && p.Bhjssj >= now).FirstAsync();
if (entity == null)
{
return new { hasProtection = false, message = "该门店当前无保护时间设置" };
}
var output = entity.Adapt<LqMdXdbhsjInfoOutput>();
return new { hasProtection = true, data = output };
}
#endregion
}
}