Coupon.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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\library\helper;
  15. use app\common\enum\coupon\ExpireType as ExpireTypeEnum;
  16. /**
  17. * 优惠券模型
  18. * Class Coupon
  19. * @package app\common\model
  20. */
  21. class Coupon extends BaseModel
  22. {
  23. // 定义表名
  24. protected $name = 'coupon';
  25. // 定义主键
  26. protected $pk = 'coupon_id';
  27. /**
  28. * 追加字段
  29. * @var array
  30. */
  31. protected $append = ['state'];
  32. /**
  33. * 优惠券状态 (是否可领取)
  34. * @param $value
  35. * @param array $data
  36. * @return array
  37. */
  38. public function getStateAttr($value, array $data): array
  39. {
  40. // mix: is_receive不是数据库字段, 来源于 setIsReceive方法
  41. if (isset($data['is_receive']) && $data['is_receive']) {
  42. return ['text' => '已领取', 'value' => 0];
  43. }
  44. if ($data['total_num'] > -1 && $data['receive_num'] >= $data['total_num']) {
  45. return ['text' => '已抢光', 'value' => 0];
  46. }
  47. if ($data['expire_type'] == ExpireTypeEnum::FIXED_TIME && ($data['end_time'] + 86400) < time()) {
  48. return ['text' => '已过期', 'value' => 0];
  49. }
  50. return ['text' => '正常', 'value' => 1];
  51. }
  52. /**
  53. * 获取器:格式化折扣率
  54. * @param $value
  55. * @return float|int
  56. */
  57. public function getDiscountAttr($value)
  58. {
  59. return $value / 10;
  60. }
  61. /**
  62. * 获取器:开始时间
  63. * @param $value
  64. * @return string
  65. */
  66. public function getStartTimeAttr($value): ?string
  67. {
  68. return $value ? date('Y/m/d', $value) : null;
  69. }
  70. /**
  71. * 获取器:结束时间
  72. * @param $value
  73. * @return string
  74. */
  75. public function getEndTimeAttr($value): ?string
  76. {
  77. return $value ? date('Y/m/d', $value) : null;
  78. }
  79. /**
  80. * 获取器:适用范围配置
  81. * @param $value
  82. * @return mixed
  83. */
  84. public function getApplyRangeConfigAttr($value)
  85. {
  86. return $value ? helper::jsonDecode($value) : [];
  87. }
  88. /**
  89. * 修改器:格式化折扣率
  90. * @param $value
  91. * @return float|int
  92. */
  93. public function setDiscountAttr($value)
  94. {
  95. return $value * 10;
  96. }
  97. /**
  98. * 修改器:适用范围配置
  99. * @param $array
  100. * @return mixed
  101. */
  102. public function setApplyRangeConfigAttr($array)
  103. {
  104. return helper::jsonEncode($array);
  105. }
  106. /**
  107. * 优惠券详情
  108. * @param int $couponId 优惠券ID
  109. * @return static|array|null
  110. */
  111. public static function detail(int $couponId)
  112. {
  113. return self::get($couponId);
  114. }
  115. /**
  116. * 验证优惠券状态是否可领取
  117. * @param $couponInfo
  118. * @return bool
  119. */
  120. public function checkReceive($couponInfo): bool
  121. {
  122. if (empty($couponInfo)) {
  123. $this->error = '很抱歉,当前优惠券不存在';
  124. return false;
  125. }
  126. if ($couponInfo['total_num'] > -1 && $couponInfo['receive_num'] >= $couponInfo['total_num']) {
  127. $this->error = '很抱歉,当前优惠券已发完';
  128. return false;
  129. }
  130. if ($couponInfo['expire_type'] == ExpireTypeEnum::FIXED_TIME && ($couponInfo->getData('end_time') + 86400) < time()) {
  131. $this->error = '很抱歉,当前优惠券已过期';
  132. return false;
  133. }
  134. return true;
  135. }
  136. /**
  137. * 累计优惠券已领取的数量
  138. * @param int $couponId
  139. * @return mixed
  140. */
  141. public static function setIncReceiveNum(int $couponId)
  142. {
  143. return (new static)->setInc($couponId, 'receive_num', 1);
  144. }
  145. }