liaoxujun@qq.com
2024-03-04 c8f9d5977cc950592f0ccfea01237eccde506350
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
/***********************************************************************
 *            Project: CoreCms
 *        ProjectName: 百分兵法管理系统                               
 *                Web: hhtp://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 202403/02   
 *        Description: 暂无
 ***********************************************************************/
 
using System;
using System.Linq;
using System.Threading.Tasks;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.Caching.AutoMate.RedisCache;
using CoreCms.Net.Configuration;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
 
namespace CoreCms.Net.Web.WebApi.Controllers
{
    /// <summary>
    /// 优惠券接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CouponController : ControllerBase
    {
 
        private readonly IHttpContextUser _user;
        private readonly ICoreCmsCouponServices _couponServices;
        private readonly ICoreCmsPromotionServices _promotionServices;
        private readonly IRedisOperationRepository _redisOperationRepository;
        private readonly IUnitOfWork _unionOfWork;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public CouponController(IHttpContextUser user
            , ICoreCmsCouponServices couponServices, ICoreCmsPromotionServices promotionServices, IRedisOperationRepository redisOperationRepository, IUnitOfWork unionOfWork)
        {
            _user = user;
            _couponServices = couponServices;
            _promotionServices = promotionServices;
            _redisOperationRepository = redisOperationRepository;
            _unionOfWork = unionOfWork;
        }
 
        //公共接口====================================================================================================
 
        #region 获取 可领取的优惠券==================================================
        /// <summary>
        /// 获取 可领取的优惠券
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        //[Authorize]
        public async Task<WebApiCallBack> CouponList([FromBody] FMCouponForUserCouponPost entity)
        {
            var jm = new WebApiCallBack() { msg = "获取失败" };
 
            var list = await _promotionServices.GetReceiveCouponList(entity.page, entity.limit);
            jm.status = true;
            jm.data = list;
            jm.msg = "获取成功";
            jm.otherData = new
            {
                totalCount = 0,
                totalPages = 0,
            };
            if (list != null && list.Any())
            {
                jm.data = list;
                jm.otherData = new
                {
                    list.TotalCount,
                    list.TotalPages
                };
            }
            return jm;
        }
        #endregion
 
        //验证接口====================================================================================================
 
        #region 获取优惠券 详情==================================================
        /// <summary>
        /// 获取优惠券 详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<WebApiCallBack> CouponDetail([FromBody] FMIntId entity)
        {
            var jm = new WebApiCallBack() { msg = "获取失败" };
 
            if (entity.id == 0)
            {
                jm.status = false;
                jm.msg = GlobalErrorCodeVars.Code15006;
                return jm;
            }
 
            var promotionModel = await _promotionServices.QueryByClauseAsync(p => p.id == entity.id);
            if (promotionModel != null)
            {
                jm.status = true;
                jm.data = promotionModel;
                jm.msg = "获取成功";
            }
            return jm;
 
        }
        #endregion
 
        #region 获取用户已领取的优惠券==================================================
        /// <summary>
        /// 获取用户已领取的优惠券
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<WebApiCallBack> UserCoupon([FromBody] FMCouponForUserCouponPost entity)
        {
            var jm = await _couponServices.GetMyCoupon(_user.ID, 0, entity.display, entity.page, entity.limit);
            return jm;
        }
        #endregion
 
        #region 用户领取优惠券==================================================
        /// <summary>
        /// 用户领取优惠券
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<WebApiCallBack> GetCoupon([FromBody] FMIntId entity)
        {
            var jm = new WebApiCallBack();
 
            var lockKey = "LOCK_GetCoupon:user_" + entity.id;
            var lockHolder = Guid.NewGuid().ToString("N"); //锁持有者
            var redisUserLock = await _redisOperationRepository.LockTakeAsync(lockKey, lockHolder, TimeSpan.FromSeconds(5));
            if (redisUserLock)
            {
                try
                {
                    if (entity.id == 0)
                    {
                        jm.msg = GlobalErrorCodeVars.Code15006;
                        return jm;
                    }
                    //判断优惠券是否可以领取?
                    var promotionModel = await _promotionServices.ReceiveCoupon(entity.id);
                    if (promotionModel.status == false)
                    {
                        return promotionModel;
                    }
 
                    var promotion = (CoreCmsPromotion)promotionModel.data;
                    if (promotion == null)
                    {
                        jm.msg = GlobalErrorCodeVars.Code15019;
                        return jm;
                    }
 
                    if (promotion.maxNums > 0)
                    {
                        //判断用户是否已领取?领取次数
                        var couponResult = await _couponServices.GetMyCoupon(_user.ID, entity.id, "all", 1, 9999);
                        if (couponResult.status && couponResult.code >= promotion.maxNums)
                        {
                            jm.msg = GlobalErrorCodeVars.Code15018;
                            return jm;
                        }
                    }
                    jm = await _couponServices.AddData(_user.ID, entity.id, promotion);
                    jm.otherData = promotionModel;
                    return jm;
 
 
                }
                catch (Exception e)
                {
                    jm.msg = "数据处理异常";
                    jm.otherData = e;
                }
                finally
                {
                    await _redisOperationRepository.LockReleaseAsync(lockKey, lockHolder);
                }
            }
            else
            {
                jm.msg = "当前请求太频繁_请稍后再试";
            }
            return jm;
        }
        #endregion
 
        #region 用户输入code领取优惠券==================================================
        /// <summary>
        /// 用户输入code领取优惠券
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<WebApiCallBack> GetCouponKey([FromBody] FMCouponForGetCouponKeyPost entity)
        {
            var jm = new WebApiCallBack();
 
            if (string.IsNullOrEmpty(entity.key))
            {
                jm.msg = GlobalErrorCodeVars.Code15006;
                return jm;
            }
 
            var coupon = await _couponServices.QueryByClauseAsync(p => p.couponCode == entity.key);
            if (coupon == null || coupon.promotionId <= 0)
            {
                jm.msg = GlobalErrorCodeVars.Code15009;
                return jm;
            }
 
            try
            {
                _unionOfWork.BeginTran();
 
                //判断优惠券是否可以领取?
                var promotionModel = await _promotionServices.ReceiveCoupon(coupon.promotionId);
                if (promotionModel.status == false)
                {
                    _unionOfWork.RollbackTran();
                    return promotionModel;
                }
 
                var promotion = (CoreCmsPromotion)promotionModel.data;
                if (promotion == null)
                {
                    _unionOfWork.RollbackTran();
                    jm.msg = GlobalErrorCodeVars.Code15019;
                    return jm;
                }
 
                if (promotion.maxNums > 0)
                {
                    //判断用户是否已领取?领取次数
                    var couponResult = await _couponServices.GetMyCoupon(_user.ID, coupon.promotionId, "all", 1, 9999);
                    if (couponResult.status && couponResult.code >= promotion.maxNums)
                    {
                        _unionOfWork.RollbackTran();
                        jm.msg = GlobalErrorCodeVars.Code15018;
                        return jm;
                    }
                }
                jm = await _couponServices.ReceiveCoupon(_user.ID, entity.key);
 
                _unionOfWork.CommitTran();
 
                jm.otherData = promotionModel;
 
            }
            catch (Exception e)
            {
                _unionOfWork.RollbackTran();
                jm.msg = GlobalErrorCodeVars.Code10000;
                jm.data = e;
            }
 
            return jm;
        }
        #endregion
 
    }
}