UserCoupon.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use cores\BaseModel;
  14. use app\common\model\Coupon as CouponModel;
  15. use app\common\library\helper;
  16. use app\common\enum\coupon\ExpireType as ExpireTypeEnum;
  17. use think\model\relation\BelongsTo;
  18. /**
  19. * 用户优惠券模型
  20. * Class UserCoupon
  21. * @package app\common\model
  22. */
  23. class UserCoupon extends BaseModel
  24. {
  25. // 定义表名
  26. protected $name = 'user_coupon';
  27. // 定义主键
  28. protected $pk = 'user_coupon_id';
  29. /**
  30. * 追加字段
  31. * @var array
  32. */
  33. protected $append = ['state'];
  34. /**
  35. * 关联用户表
  36. * @return BelongsTo
  37. */
  38. public function user(): BelongsTo
  39. {
  40. return $this->belongsTo('User');
  41. }
  42. /**
  43. * 优惠券状态
  44. * @param $value
  45. * @param $data
  46. * @return array
  47. */
  48. public function getStateAttr($value, $data): array
  49. {
  50. if ($data['is_use']) {
  51. return ['text' => '已使用', 'value' => 0];
  52. }
  53. if ($data['is_expire']) {
  54. return ['text' => '已过期', 'value' => 0];
  55. }
  56. return ['text' => '正常', 'value' => 1];
  57. }
  58. /**
  59. * 获取器:格式化折扣率
  60. * @param $value
  61. * @return float|int
  62. */
  63. public function getDiscountAttr($value)
  64. {
  65. return $value / 10;
  66. }
  67. /**
  68. * 有效期-开始时间
  69. * @param $value
  70. * @return string
  71. */
  72. public function getStartTimeAttr($value): string
  73. {
  74. return date('Y/m/d', $value);
  75. }
  76. /**
  77. * 有效期-结束时间
  78. * @param $value
  79. * @return string
  80. */
  81. public function getEndTimeAttr($value): string
  82. {
  83. return date('Y/m/d', $value);
  84. }
  85. /**
  86. * 获取器:适用范围配置
  87. * @param $value
  88. * @return mixed
  89. */
  90. public function getApplyRangeConfigAttr($value)
  91. {
  92. return $value ? helper::jsonDecode($value) : [];
  93. }
  94. /**
  95. * 修改器:格式化折扣率
  96. * @param $value
  97. * @return float|int
  98. */
  99. public function setDiscountAttr($value)
  100. {
  101. return $value * 10;
  102. }
  103. /**
  104. * 修改器:适用范围配置
  105. * @param $array
  106. * @return mixed
  107. */
  108. public function setApplyRangeConfigAttr($array)
  109. {
  110. return helper::jsonEncode($array);
  111. }
  112. /**
  113. * 优惠券详情
  114. * @param $couponId
  115. * @return null|static
  116. */
  117. public static function detail($couponId)
  118. {
  119. return static::get($couponId);
  120. }
  121. /**
  122. * 设置优惠券使用状态
  123. * @param int $userCouponId 用户的优惠券ID
  124. * @param bool $isUse 是否已使用
  125. * @return bool|false
  126. */
  127. public static function setIsUse(int $userCouponId, bool $isUse = true): bool
  128. {
  129. return static::updateBase(['is_use' => (int)$isUse], ['user_coupon_id' => $userCouponId]);
  130. }
  131. /**
  132. * 验证指定用户是否已领取优惠券
  133. * @param int $couponId 优惠券ID
  134. * @param int $userId 用户ID
  135. * @return bool
  136. */
  137. public static function checktUserCoupon(int $couponId, int $userId): bool
  138. {
  139. return (bool)(new static)->where('coupon_id', '=', $couponId)
  140. ->where('user_id', '=', $userId)
  141. ->value('user_coupon_id');
  142. }
  143. /**
  144. * 添加领取记录
  145. * @param int $userId 用户ID
  146. * @param CouponModel $couponInfo
  147. * @return bool
  148. */
  149. public function add(int $userId, CouponModel $couponInfo): bool
  150. {
  151. // 计算有效期
  152. if ($couponInfo['expire_type'] == ExpireTypeEnum::RECEIVE) {
  153. $startTime = time();
  154. $endTime = $startTime + ($couponInfo['expire_day'] * 86400);
  155. } else {
  156. $startTime = $couponInfo->getData('start_time');
  157. $endTime = $couponInfo->getData('end_time');
  158. }
  159. // 整理领取记录
  160. $data = [
  161. 'coupon_id' => $couponInfo['coupon_id'],
  162. 'name' => $couponInfo['name'],
  163. 'coupon_type' => $couponInfo['coupon_type'],
  164. 'reduce_price' => $couponInfo['reduce_price'],
  165. 'discount' => $couponInfo['discount'],
  166. 'min_price' => $couponInfo['min_price'],
  167. 'expire_type' => $couponInfo['expire_type'],
  168. 'expire_day' => $couponInfo['expire_day'],
  169. 'start_time' => $startTime,
  170. 'end_time' => $endTime,
  171. 'apply_range' => $couponInfo['apply_range'],
  172. 'apply_range_config' => $couponInfo['apply_range_config'],
  173. 'user_id' => $userId,
  174. 'store_id' => self::$storeId
  175. ];
  176. // 事务处理
  177. return $this->transaction(function () use ($data, $couponInfo) {
  178. // 添加领取记录
  179. $this->save($data);
  180. // 累计优惠券已领取的数量
  181. CouponModel::setIncReceiveNum($couponInfo['coupon_id']);
  182. return true;
  183. });
  184. }
  185. }