移动系统liao
2024-10-15 94da0698c01915b1e340415e080aa03050700d97
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 202403/02   
 *        Description: 暂无
 ***********************************************************************/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.Loging;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.ViewModels.Basics;
using CoreCms.Net.Model.ViewModels.DTO;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Utility.Helper;
using SqlSugar;
 
namespace CoreCms.Net.Repository
{
    /// <summary>
    /// 优惠券码表 接口实现
    /// </summary>
    public class CoreCmsCouponRepository : BaseRepository<CoreCmsCoupon>, ICoreCmsCouponRepository
    {
 
 
        public CoreCmsCouponRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }
 
        #region 根据优惠券编码取优惠券的信息,并判断是否可用
 
        /// <summary>
        /// 根据优惠券编码取优惠券的信息,并判断是否可用
        /// </summary>
        /// <param name="code"></param>
        /// <param name="check"></param>
        public async Task<WebApiCallBack> ToInfo(string[] code, bool check = false)
        {
            var jm = new WebApiCallBack();
            var outData = new List<CoreCmsPromotion>();
 
            foreach (var codeStr in code)
            {
                var model = await DbClient.Queryable<CoreCmsCoupon, CoreCmsPromotion>((coupon, prommotion) => new object[]
                    {
                        JoinType.Inner, coupon.promotionId == prommotion.id
                    }).Where((coupon, prommotion) => coupon.couponCode == codeStr)
                    .Select((coupon, prommotion) => new
                    {
                        coupon,
                        prommotion
                    }).FirstAsync();
                ;
                if (model != null)
                {
                    if (check)
                    {
                        //判断规则是否开启
                        if (model.prommotion.isEnable == false)
                        {
                            jm.data = 15012;
                            jm.msg = GlobalErrorCodeVars.Code15012;
                            return jm;
                        }
                        //判断优惠券规则是否到达开始时间
                        var dt = DateTime.Now;
                        if (model.coupon.startTime > dt)
                        {
                            jm.data = 15010;
                            jm.msg = GlobalErrorCodeVars.Code15010;
                            return jm;
                        }
                        //判断优惠券规则是否已经到结束时间了,也就是是否过期了
                        if (model.coupon.endTime < dt)
                        {
                            jm.data = 15011;
                            jm.msg = GlobalErrorCodeVars.Code15011;
                            return jm;
                        }
                        //判断是否已经使用过了
                        if (model.coupon.isUsed)
                        {
                            jm.data = 15013;
                            jm.msg = GlobalErrorCodeVars.Code15013;
                            return jm;
                        }
                        //判断此类优惠券是否已经使用过,防止一类优惠券使用多张
                        if (outData.Exists(p => p.id == model.coupon.promotionId))
                        {
                            jm.data = 15015;
                            jm.msg = GlobalErrorCodeVars.Code15015;
                            return jm;
                        }
                    }
                    outData.Add(model.prommotion);
                }
                else
                {
                    jm.data = 15009;
                    jm.msg = GlobalErrorCodeVars.Code15009;
                    return jm;
                }
            }
            jm.status = true;
            jm.data = outData;
            return jm;
        }
 
 
        #endregion
 
        #region 获取 我的优惠券
        /// <summary>
        /// 获取 我的优惠券
        /// </summary>
        /// <param name="userId">用户序列</param>
        /// <param name="promotionId">促销序列</param>
        /// <param name="display">优惠券状态编码</param>
        /// <param name="page">页码</param>
        /// <param name="limit">数量</param>
        public async Task<WebApiCallBack> GetMyCoupon(int userId, int promotionId = 0, string display = "", int page = 1, int limit = 10)
        {
            var jm = new WebApiCallBack();
            jm.code = 0;
 
            RefAsync<int> totalCount = 0;
            var dt = DateTime.Now;
            var listData = await DbClient.Queryable<CoreCmsCoupon, CoreCmsPromotion>((coupon, promotion) => new object[]
                 {
                        JoinType.Inner, coupon.promotionId == promotion.id
                 })
                .Where((coupon, promotion) => coupon.userId == userId)
                .Where((coupon, promotion) => promotion.isDel == false)
                .Where((coupon, promotion) => promotion.type == (int)GlobalEnumVars.PromotionType.Coupon)
                .WhereIF(display == GlobalEnumVars.CouponIsUsedStatusText.noUsed.ToString(), (coupon, promotion) => coupon.isUsed == false && coupon.endTime >= dt)
                .WhereIF(display == GlobalEnumVars.CouponIsUsedStatusText.yesUsed.ToString(), (coupon, promotion) => coupon.isUsed == true)
                .WhereIF(display == GlobalEnumVars.CouponIsUsedStatusText.invalid.ToString(), (coupon, promotion) => coupon.endTime < dt && coupon.isUsed == false)
                .WhereIF(promotionId > 0, (coupon, promotion) => coupon.promotionId == promotionId)
                .WhereIF(promotionId == 0, (coupon, promotion) => promotion.isEnable == true)
                .Select((coupon, promotion) => new CoreCmsCoupon
                {
                    couponCode = coupon.couponCode,
                    promotionId = coupon.promotionId,
                    isUsed = coupon.isUsed,
                    userId = coupon.userId,
                    usedId = coupon.usedId,
                    createTime = coupon.createTime,
                    updateTime = coupon.updateTime,
                    couponName = promotion.name,
                    startTime = coupon.startTime,
                    endTime = coupon.endTime,
 
                })
                .With(SqlWith.Null)
                .MergeTable()
                .Mapper(p => p.conditions, p => p.promotionId)
                .Mapper(p => p.results, p => p.promotionId)
                .OrderBy(p => p.createTime, OrderByType.Desc)
 
                .ToPageListAsync(page, limit, totalCount);
 
            var totalPages = totalCount.Value / limit;
            if (totalPages == 0) { totalPages++; }
            if (totalPages % limit > 0)
                totalPages++;
 
            var resutlList = new List<GetMyCouponResultDto>();
            if (listData != null && listData.Any())
            {
                foreach (var item in listData)
                {
                    //var pcondition = await DbClient.Queryable<CoreCmsPromotionCondition>().Where(p => p.promotionId == item.promotionId).ToListAsync();
                    //var presult = await DbClient.Queryable<CoreCmsPromotionResult>().Where(p => p.promotionId == item.promotionId).ToListAsync();
                    var expression1 = string.Empty;
                    var expression2 = string.Empty;
 
                    var dto = new GetMyCouponResultDto();
 
                    foreach (var condition in item.conditions)
                    {
                        var str = PromotionHelper.GetConditionMsg(condition.code, condition.parameters);
                        expression1 += str;
                        dto.conditions.Add(str);
                    }
                    foreach (var result in item.results)
                    {
                        var str = PromotionHelper.GetResultMsg(result.code, result.parameters);
                        expression2 += str;
                        dto.results.Add(str);
                    }
 
                    dto.couponCode = item.couponCode;
                    dto.promotionId = item.promotionId;
                    dto.isUsed = item.isUsed;
                    dto.userId = item.userId;
                    dto.usedId = item.usedId;
                    dto.createTime = item.createTime;
                    dto.updateTime = item.updateTime;
                    dto.couponName = item.couponName;
 
                    dto.expression1 = expression1;
                    dto.expression2 = expression2;
 
                    dto.isExpire = dt > item.endTime;
                    dto.startTime = item.startTime;
                    dto.endTime = item.endTime;
                    dto.stime = item.startTime.ToString("yyyy-MM-dd");
                    dto.etime = item.endTime.ToString("yyyy-MM-dd");
 
                    resutlList.Add(dto);
                }
            }
            jm.status = true;
            jm.msg = "获取成功";
            jm.data = new
            {
                list = resutlList,
                count = totalCount.Value,
                display,
                page = totalPages
            };
            jm.code = totalCount.Value;
 
            return jm;
        }
        #endregion
 
        #region 根据条件查询分页数据及导航数据
 
        /// <summary>
        ///     根据条件查询分页数据及导航数据
        /// </summary>
        /// <param name="predicate">判断集合</param>
        /// <param name="orderByType">排序方式</param>
        /// <param name="isToPage">是否分页</param>
        /// <param name="pageIndex">当前页面索引</param>
        /// <param name="pageSize">分布大小</param>
        /// <param name="orderByExpression"></param>
        /// <returns></returns>
        public async Task<IPageList<CoreCmsCoupon>> QueryPageMapperAsync(Expression<Func<CoreCmsCoupon, bool>> predicate,
            Expression<Func<CoreCmsCoupon, object>> orderByExpression, OrderByType orderByType, bool isToPage = false, int pageIndex = 1,
            int pageSize = 20)
        {
            RefAsync<int> totalCount = 0;
            List<CoreCmsCoupon> page;
            if (isToPage)
            {
                page = await DbClient.Queryable<CoreCmsCoupon>()
                    .OrderByIF(orderByExpression != null, orderByExpression, orderByType)
                    .WhereIF(predicate != null, predicate).Select(p => new CoreCmsCoupon
                    {
                        id = p.id,
                        couponCode = p.couponCode,
                        promotionId = p.promotionId,
                        isUsed = p.isUsed,
                        userId = p.userId,
                        usedId = p.usedId,
                        createTime = p.createTime,
                        updateTime = p.updateTime,
                        startTime = p.startTime,
                        endTime = p.endTime,
                    })
                    .Mapper(p => p.promotion, p => p.promotionId)
                    .Mapper(p => p.conditions, p => p.conditions.First().promotionId)
                    .Mapper(p => p.results, p => p.results.First().promotionId)
                    .ToPageListAsync(pageIndex, pageSize, totalCount);
            }
            else
            {
                page = await DbClient.Queryable<CoreCmsCoupon>()
                    .OrderByIF(orderByExpression != null, orderByExpression, orderByType)
                    .WhereIF(predicate != null, predicate)
                    .Take(pageSize)
                    .Select(p => new CoreCmsCoupon
                    {
                        id = p.id,
                        couponCode = p.couponCode,
                        promotionId = p.promotionId,
                        isUsed = p.isUsed,
                        userId = p.userId,
                        usedId = p.usedId,
                        createTime = p.createTime,
                        updateTime = p.updateTime,
                        startTime = p.startTime,
                        endTime = p.endTime,
                    })
                    .Mapper(p => p.promotion, p => p.promotionId)
                    .Mapper(p => p.conditions, p => p.conditions.First().promotionId)
                    .Mapper(p => p.results, p => p.results.First().promotionId)
                    .ToListAsync();
            }
            var list = new PageList<CoreCmsCoupon>(page, pageIndex, pageSize, totalCount);
            return list;
        }
 
        #endregion
 
        #region 重写数据并获取相关
        /// <summary>
        ///     重写数据并获取相关
        /// </summary>
        /// <param name="predicate">判断集合</param>
        /// <returns></returns>
        public async Task<List<CoreCmsCoupon>> QueryWithAboutAsync(Expression<Func<CoreCmsCoupon, bool>> predicate)
        {
            var page = await DbClient.Queryable<CoreCmsCoupon, CoreCmsUser, CoreCmsPromotion>((p, sUser, sPromotion) => new JoinQueryInfos(
                    JoinType.Left, p.userId == sUser.id,
                    JoinType.Left, p.promotionId == sPromotion.id))
                .Select((p, sUser, sPromotion) => new CoreCmsCoupon
                {
                    id = p.id,
                    couponCode = p.couponCode,
                    promotionId = p.promotionId,
                    isUsed = p.isUsed,
                    userId = p.userId,
                    usedId = p.usedId,
                    createTime = p.createTime,
                    updateTime = p.updateTime,
                    userNickName = sUser.nickName,
                    couponName = sPromotion.name,
                    startTime = p.startTime,
                    endTime = p.endTime,
                })
                .MergeTable()
                .WhereIF(predicate != null, predicate)
                .ToListAsync();
            return page;
        }
 
        #endregion
 
        #region 重写根据条件查询分页数据
        /// <summary>
        ///     重写根据条件查询分页数据
        /// </summary>
        /// <param name="predicate">判断集合</param>
        /// <param name="orderByType">排序方式</param>
        /// <param name="pageIndex">当前页面索引</param>
        /// <param name="pageSize">分布大小</param>
        /// <param name="orderByExpression"></param>
        /// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
        /// <returns></returns>
        public async Task<IPageList<CoreCmsCoupon>> QueryPageAsync(Expression<Func<CoreCmsCoupon, bool>> predicate,
            Expression<Func<CoreCmsCoupon, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
            int pageSize = 20, bool blUseNoLock = false)
        {
            RefAsync<int> totalCount = 0;
            var page = blUseNoLock
                ? await DbClient.Queryable<CoreCmsCoupon, CoreCmsUser, CoreCmsPromotion>((p, sUser, sPromotion) => new JoinQueryInfos(
                         JoinType.Left, p.userId == sUser.id,
                         JoinType.Left, p.promotionId == sPromotion.id))
                .Select((p, sUser, sPromotion) => new CoreCmsCoupon
                {
                    id = p.id,
                    couponCode = p.couponCode,
                    promotionId = p.promotionId,
                    isUsed = p.isUsed,
                    userId = p.userId,
                    usedId = p.usedId,
                    createTime = p.createTime,
                    updateTime = p.updateTime,
                    userNickName = sUser.nickName,
                    couponName = sPromotion.name,
                    startTime = p.startTime,
                    endTime = p.endTime,
                })
                .With(SqlWith.NoLock)
                .MergeTable()
                .OrderByIF(orderByExpression != null, orderByExpression, orderByType)
                .WhereIF(predicate != null, predicate)
                .ToPageListAsync(pageIndex, pageSize, totalCount)
                :
                await DbClient.Queryable<CoreCmsCoupon, CoreCmsUser, CoreCmsPromotion>((p, sUser, sPromotion) => new JoinQueryInfos(
                        JoinType.Left, p.userId == sUser.id,
                        JoinType.Left, p.promotionId == sPromotion.id))
                    .Select((p, sUser, sPromotion) => new CoreCmsCoupon
                    {
                        id = p.id,
                        couponCode = p.couponCode,
                        promotionId = p.promotionId,
                        isUsed = p.isUsed,
                        userId = p.userId,
                        usedId = p.usedId,
                        createTime = p.createTime,
                        updateTime = p.updateTime,
                        userNickName = sUser.nickName,
                        couponName = sPromotion.name,
                        startTime = p.startTime,
                        endTime = p.endTime,
                    })
                .MergeTable()
                .OrderByIF(orderByExpression != null, orderByExpression, orderByType)
                .WhereIF(predicate != null, predicate)
                .ToPageListAsync(pageIndex, pageSize, totalCount);
            var list = new PageList<CoreCmsCoupon>(page, pageIndex, pageSize, totalCount);
            return list;
        }
 
        #endregion
 
        #region 获取 我的优惠券可用数量
        /// <summary>
        /// 获取 我的优惠券可用数量
        /// </summary>
        /// <param name="userId">用户序列</param>
        public async Task<int> GetMyCouponCount(int userId)
        {
            var jm = new WebApiCallBack();
            jm.code = 0;
 
            var dt = DateTime.Now;
            var count = await DbClient.Queryable<CoreCmsCoupon, CoreCmsPromotion>((coupon, promotion) => new object[]
                 {
                        JoinType.Inner, coupon.promotionId == promotion.id
                 })
                .Where((coupon, promotion) => coupon.userId == userId)
                .Where((coupon, promotion) => promotion.isDel == false)
                .Where((coupon, promotion) => coupon.isUsed == false)
                .Where((coupon, promotion) => promotion.type == (int)GlobalEnumVars.PromotionType.Coupon)
                .Where((coupon, promotion) => coupon.endTime > dt)
                .CountAsync();
            return count;
        }
        #endregion
 
    }
}