LotteryCondition.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace app\common\model\lottery;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. /**
  6. * 抽奖参与条件模型
  7. */
  8. class LotteryCondition extends Model
  9. {
  10. use SoftDelete;
  11. // 表名
  12. protected $name = 'shop_lottery_condition';
  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. 'goods_rule_text',
  23. 'goods_ids_list'
  24. ];
  25. // 条件类型常量
  26. const TYPE_GOODS = 1; // 购买指定商品
  27. const TYPE_ORDER_AMOUNT = 2; // 单笔订单消费满额
  28. const TYPE_RECHARGE = 3; // 单次充值满额
  29. const TYPE_ACCUMULATE = 4; // 活动期间累计消费满额
  30. // 商品规则常量
  31. const GOODS_RULE_INCLUDE = 1; // 指定商品参与
  32. const GOODS_RULE_EXCLUDE = 2; // 指定商品不可参与
  33. /**
  34. * 关联活动
  35. */
  36. public function activity()
  37. {
  38. return $this->belongsTo('LotteryActivity', 'activity_id');
  39. }
  40. /**
  41. * 获取条件类型文本
  42. */
  43. public function getTypeTextAttr($value, $data)
  44. {
  45. $types = [
  46. self::TYPE_GOODS => '购买指定商品',
  47. self::TYPE_ORDER_AMOUNT => '单笔订单消费满额',
  48. self::TYPE_RECHARGE => '单次充值满额',
  49. self::TYPE_ACCUMULATE => '活动期间累计消费满额'
  50. ];
  51. return isset($types[$data['type']]) ? $types[$data['type']] : '未知';
  52. }
  53. /**
  54. * 获取商品规则文本
  55. */
  56. public function getGoodsRuleTextAttr($value, $data)
  57. {
  58. $rules = [
  59. self::GOODS_RULE_INCLUDE => '指定商品参与',
  60. self::GOODS_RULE_EXCLUDE => '指定商品不可参与'
  61. ];
  62. return isset($rules[$data['goods_rule']]) ? $rules[$data['goods_rule']] : '';
  63. }
  64. /**
  65. * 获取商品ID列表
  66. */
  67. public function getGoodsIdsListAttr($value, $data)
  68. {
  69. return !empty($data['goods_ids']) ? json_decode($data['goods_ids'], true) : [];
  70. }
  71. /**
  72. * 设置商品ID列表
  73. */
  74. public function setGoodsIdsAttr($value)
  75. {
  76. return is_array($value) ? json_encode($value) : $value;
  77. }
  78. /**
  79. * 验证订单是否满足条件
  80. */
  81. public function validateOrder($orderInfo)
  82. {
  83. switch ($this->type) {
  84. case self::TYPE_GOODS:
  85. return $this->validateGoodsCondition($orderInfo);
  86. case self::TYPE_ORDER_AMOUNT:
  87. return $this->validateOrderAmountCondition($orderInfo);
  88. case self::TYPE_RECHARGE:
  89. return $this->validateRechargeCondition($orderInfo);
  90. default:
  91. return false;
  92. }
  93. }
  94. /**
  95. * 验证商品条件
  96. */
  97. private function validateGoodsCondition($orderInfo)
  98. {
  99. if (empty($orderInfo['goods']) || empty($this->goods_ids_list)) {
  100. return false;
  101. }
  102. $orderGoodsIds = array_column($orderInfo['goods'], 'goods_id');
  103. $conditionGoodsIds = $this->goods_ids_list;
  104. $intersection = array_intersect($orderGoodsIds, $conditionGoodsIds);
  105. if ($this->goods_rule == self::GOODS_RULE_INCLUDE) {
  106. // 指定商品参与:订单中必须包含指定商品
  107. return !empty($intersection);
  108. } else {
  109. // 指定商品不可参与:订单中不能包含指定商品
  110. return empty($intersection);
  111. }
  112. }
  113. /**
  114. * 验证订单金额条件
  115. */
  116. private function validateOrderAmountCondition($orderInfo)
  117. {
  118. $orderAmount = $orderInfo['total_amount'] ?? 0;
  119. return $orderAmount >= $this->condition_value;
  120. }
  121. /**
  122. * 验证充值条件
  123. */
  124. private function validateRechargeCondition($orderInfo)
  125. {
  126. if (($orderInfo['type'] ?? '') !== 'recharge') {
  127. return false;
  128. }
  129. $rechargeAmount = $orderInfo['amount'] ?? 0;
  130. return $rechargeAmount >= $this->condition_value;
  131. }
  132. /**
  133. * 验证累计消费条件
  134. */
  135. public function validateAccumulateCondition($userId, $activityId)
  136. {
  137. $activity = LotteryActivity::find($activityId);
  138. if (!$activity) {
  139. return false;
  140. }
  141. // 计算活动期间用户累计消费
  142. $totalAmount = \app\common\model\Order::where('user_id', $userId)
  143. ->where('status', 'paid')
  144. ->where('createtime', '>=', $activity->start_time)
  145. ->where('createtime', '<=', $activity->end_time)
  146. ->sum('total_amount');
  147. return $totalAmount >= $this->condition_value;
  148. }
  149. /**
  150. * 获取活动的有效条件
  151. */
  152. public static function getValidConditions($activityId)
  153. {
  154. return static::where('activity_id', $activityId)
  155. ->where('status', 1)
  156. ->order('id', 'asc')
  157. ->select();
  158. }
  159. /**
  160. * 检查条件是否可重复获得奖励
  161. */
  162. public function canRepeat()
  163. {
  164. return $this->is_repeatable == 1;
  165. }
  166. /**
  167. * 获取奖励次数
  168. */
  169. public function getRewardTimes()
  170. {
  171. return $this->reward_times ?: 1;
  172. }
  173. }