123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace app\common\model\lottery;
- use think\Model;
- use traits\model\SoftDelete;
- /**
- * 抽奖参与条件模型
- */
- class LotteryCondition extends Model
- {
- use SoftDelete;
- // 表名
- protected $name = 'shop_lottery_condition';
-
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- // 追加属性
- protected $append = [
- 'type_text',
- 'goods_rule_text',
- 'goods_ids_list'
- ];
- // 条件类型常量
- const TYPE_GOODS = 1; // 购买指定商品
- const TYPE_ORDER_AMOUNT = 2; // 单笔订单消费满额
- const TYPE_RECHARGE = 3; // 单次充值满额
- const TYPE_ACCUMULATE = 4; // 活动期间累计消费满额
- // 商品规则常量
- const GOODS_RULE_INCLUDE = 1; // 指定商品参与
- const GOODS_RULE_EXCLUDE = 2; // 指定商品不可参与
- /**
- * 关联活动
- */
- public function activity()
- {
- return $this->belongsTo('LotteryActivity', 'activity_id');
- }
- /**
- * 获取条件类型文本
- */
- public function getTypeTextAttr($value, $data)
- {
- $types = [
- self::TYPE_GOODS => '购买指定商品',
- self::TYPE_ORDER_AMOUNT => '单笔订单消费满额',
- self::TYPE_RECHARGE => '单次充值满额',
- self::TYPE_ACCUMULATE => '活动期间累计消费满额'
- ];
- return isset($types[$data['type']]) ? $types[$data['type']] : '未知';
- }
- /**
- * 获取商品规则文本
- */
- public function getGoodsRuleTextAttr($value, $data)
- {
- $rules = [
- self::GOODS_RULE_INCLUDE => '指定商品参与',
- self::GOODS_RULE_EXCLUDE => '指定商品不可参与'
- ];
- return isset($rules[$data['goods_rule']]) ? $rules[$data['goods_rule']] : '';
- }
- /**
- * 获取商品ID列表
- */
- public function getGoodsIdsListAttr($value, $data)
- {
- return !empty($data['goods_ids']) ? json_decode($data['goods_ids'], true) : [];
- }
- /**
- * 设置商品ID列表
- */
- public function setGoodsIdsAttr($value)
- {
- return is_array($value) ? json_encode($value) : $value;
- }
- /**
- * 验证订单是否满足条件
- */
- public function validateOrder($orderInfo)
- {
- switch ($this->type) {
- case self::TYPE_GOODS:
- return $this->validateGoodsCondition($orderInfo);
- case self::TYPE_ORDER_AMOUNT:
- return $this->validateOrderAmountCondition($orderInfo);
- case self::TYPE_RECHARGE:
- return $this->validateRechargeCondition($orderInfo);
- default:
- return false;
- }
- }
- /**
- * 验证商品条件
- */
- private function validateGoodsCondition($orderInfo)
- {
- if (empty($orderInfo['goods']) || empty($this->goods_ids_list)) {
- return false;
- }
- $orderGoodsIds = array_column($orderInfo['goods'], 'goods_id');
- $conditionGoodsIds = $this->goods_ids_list;
- $intersection = array_intersect($orderGoodsIds, $conditionGoodsIds);
- if ($this->goods_rule == self::GOODS_RULE_INCLUDE) {
- // 指定商品参与:订单中必须包含指定商品
- return !empty($intersection);
- } else {
- // 指定商品不可参与:订单中不能包含指定商品
- return empty($intersection);
- }
- }
- /**
- * 验证订单金额条件
- */
- private function validateOrderAmountCondition($orderInfo)
- {
- $orderAmount = $orderInfo['total_amount'] ?? 0;
- return $orderAmount >= $this->condition_value;
- }
- /**
- * 验证充值条件
- */
- private function validateRechargeCondition($orderInfo)
- {
- if (($orderInfo['type'] ?? '') !== 'recharge') {
- return false;
- }
-
- $rechargeAmount = $orderInfo['amount'] ?? 0;
- return $rechargeAmount >= $this->condition_value;
- }
- /**
- * 验证累计消费条件
- */
- public function validateAccumulateCondition($userId, $activityId)
- {
- $activity = LotteryActivity::find($activityId);
- if (!$activity) {
- return false;
- }
- // 计算活动期间用户累计消费
- $totalAmount = \app\common\model\Order::where('user_id', $userId)
- ->where('status', 'paid')
- ->where('createtime', '>=', $activity->start_time)
- ->where('createtime', '<=', $activity->end_time)
- ->sum('total_amount');
- return $totalAmount >= $this->condition_value;
- }
- /**
- * 获取活动的有效条件
- */
- public static function getValidConditions($activityId)
- {
- return static::where('activity_id', $activityId)
- ->where('status', 1)
- ->order('id', 'asc')
- ->select();
- }
- /**
- * 检查条件是否可重复获得奖励
- */
- public function canRepeat()
- {
- return $this->is_repeatable == 1;
- }
- /**
- * 获取奖励次数
- */
- public function getRewardTimes()
- {
- return $this->reward_times ?: 1;
- }
- }
|