LotteryPrize.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace app\common\model\lottery;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. /**
  6. * 抽奖奖品模型
  7. */
  8. class LotteryPrize extends Model
  9. {
  10. use SoftDelete;
  11. // 表名
  12. protected $name = 'shop_lottery_prize';
  13. // 开启自动写入时间戳字段
  14. protected $autoWriteTimestamp = 'int';
  15. // 定义时间戳字段名
  16. protected $createTime = 'createtime';
  17. protected $updateTime = 'updatetime';
  18. protected $deleteTime = 'deletetime';
  19. // 追加属性
  20. protected $append = [
  21. 'type_text',
  22. 'deliver_type_text',
  23. 'exchange_codes_list',
  24. 'used_codes_list'
  25. ];
  26. // 奖品类型常量
  27. const TYPE_NO_PRIZE = 1; // 未中奖
  28. const TYPE_PHYSICAL = 2; // 实物奖品
  29. const TYPE_COUPON = 3; // 优惠券
  30. const TYPE_RED_PACKET = 4; // 红包
  31. const TYPE_EXCHANGE_CODE = 5; // 兑换码
  32. const TYPE_GOODS = 6; // 商城奖品
  33. // 发放方式常量
  34. const DELIVER_AUTO = 1; // 自动发放
  35. const DELIVER_MANUAL = 2; // 手动发放
  36. /**
  37. * 关联活动
  38. */
  39. public function activity()
  40. {
  41. return $this->belongsTo('LotteryActivity', 'activity_id');
  42. }
  43. /**
  44. * 关联商品
  45. */
  46. public function goods()
  47. {
  48. return $this->belongsTo('app\common\model\Goods', 'goods_id');
  49. }
  50. /**
  51. * 关联商品SKU
  52. */
  53. public function goodsSku()
  54. {
  55. return $this->belongsTo('app\common\model\Sku', 'goods_sku_id');
  56. }
  57. /**
  58. * 关联优惠券
  59. */
  60. public function coupon()
  61. {
  62. return $this->belongsTo('app\common\model\Coupon', 'coupon_id');
  63. }
  64. /**
  65. * 获取奖品类型文本
  66. */
  67. public function getTypeTextAttr($value, $data)
  68. {
  69. $types = [
  70. self::TYPE_NO_PRIZE => '未中奖',
  71. self::TYPE_PHYSICAL => '实物奖品',
  72. self::TYPE_COUPON => '优惠券',
  73. self::TYPE_RED_PACKET => '红包',
  74. self::TYPE_EXCHANGE_CODE => '兑换码',
  75. self::TYPE_GOODS => '商城奖品'
  76. ];
  77. return isset($types[$data['type']]) ? $types[$data['type']] : '未知';
  78. }
  79. /**
  80. * 获取发放方式文本
  81. */
  82. public function getDeliverTypeTextAttr($value, $data)
  83. {
  84. $types = [
  85. self::DELIVER_AUTO => '自动发放',
  86. self::DELIVER_MANUAL => '手动发放'
  87. ];
  88. return isset($types[$data['deliver_type']]) ? $types[$data['deliver_type']] : '未知';
  89. }
  90. /**
  91. * 获取兑换码列表
  92. */
  93. public function getExchangeCodesListAttr($value, $data)
  94. {
  95. return !empty($data['exchange_codes']) ? json_decode($data['exchange_codes'], true) : [];
  96. }
  97. /**
  98. * 设置兑换码列表
  99. */
  100. public function setExchangeCodesAttr($value)
  101. {
  102. return is_array($value) ? json_encode($value) : $value;
  103. }
  104. /**
  105. * 获取已使用兑换码列表
  106. */
  107. public function getUsedCodesListAttr($value, $data)
  108. {
  109. return !empty($data['used_codes']) ? json_decode($data['used_codes'], true) : [];
  110. }
  111. /**
  112. * 设置已使用兑换码列表
  113. */
  114. public function setUsedCodesAttr($value)
  115. {
  116. return is_array($value) ? json_encode($value) : $value;
  117. }
  118. /**
  119. * 检查奖品库存是否充足
  120. */
  121. public function hasStock($quantity = 1)
  122. {
  123. return $this->remain_stock >= $quantity;
  124. }
  125. /**
  126. * 减少库存
  127. */
  128. public function decreaseStock($quantity = 1)
  129. {
  130. if (!$this->hasStock($quantity)) {
  131. return false;
  132. }
  133. $this->remain_stock -= $quantity;
  134. $this->win_count += $quantity;
  135. return $this->save();
  136. }
  137. /**
  138. * 获取可用的兑换码
  139. */
  140. public function getAvailableExchangeCode()
  141. {
  142. if ($this->type != self::TYPE_EXCHANGE_CODE) {
  143. return null;
  144. }
  145. $allCodes = $this->exchange_codes_list;
  146. $usedCodes = $this->used_codes_list;
  147. $availableCodes = array_diff($allCodes, $usedCodes);
  148. if (empty($availableCodes)) {
  149. return null;
  150. }
  151. return array_shift($availableCodes);
  152. }
  153. /**
  154. * 标记兑换码为已使用
  155. */
  156. public function markExchangeCodeUsed($code)
  157. {
  158. $usedCodes = $this->used_codes_list;
  159. if (!in_array($code, $usedCodes)) {
  160. $usedCodes[] = $code;
  161. $this->used_codes = json_encode($usedCodes);
  162. return $this->save();
  163. }
  164. return true;
  165. }
  166. /**
  167. * 获取有效奖品(库存大于0且状态正常)
  168. */
  169. public static function getValidPrizes($activityId)
  170. {
  171. return static::where('activity_id', $activityId)
  172. ->where('status', 1)
  173. ->where('remain_stock', '>', 0)
  174. ->order('sort_order', 'asc')
  175. ->select();
  176. }
  177. /**
  178. * 检查是否已解锁(按人数解锁功能)
  179. */
  180. public function isUnlocked($currentPeopleCount)
  181. {
  182. if (empty($this->unlock_people_num)) {
  183. return true;
  184. }
  185. return $currentPeopleCount >= $this->unlock_people_num;
  186. }
  187. }