123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- namespace app\common\model\lottery;
- use think\Model;
- use traits\model\SoftDelete;
- /**
- * 抽奖奖品模型
- */
- class LotteryPrize extends Model
- {
- use SoftDelete;
- // 表名
- protected $name = 'shop_lottery_prize';
-
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = 'deletetime';
- // 追加属性
- protected $append = [
- 'type_text',
- 'deliver_type_text',
- 'exchange_codes_list',
- 'used_codes_list'
- ];
- // 奖品类型常量
- const TYPE_NO_PRIZE = 1; // 未中奖
- const TYPE_PHYSICAL = 2; // 实物奖品
- const TYPE_COUPON = 3; // 优惠券
- const TYPE_RED_PACKET = 4; // 红包
- const TYPE_EXCHANGE_CODE = 5; // 兑换码
- const TYPE_GOODS = 6; // 商城奖品
- // 发放方式常量
- const DELIVER_AUTO = 1; // 自动发放
- const DELIVER_MANUAL = 2; // 手动发放
- /**
- * 关联活动
- */
- public function activity()
- {
- return $this->belongsTo('LotteryActivity', 'activity_id');
- }
- /**
- * 关联商品
- */
- public function goods()
- {
- return $this->belongsTo('app\common\model\Goods', 'goods_id');
- }
- /**
- * 关联商品SKU
- */
- public function goodsSku()
- {
- return $this->belongsTo('app\common\model\Sku', 'goods_sku_id');
- }
- /**
- * 关联优惠券
- */
- public function coupon()
- {
- return $this->belongsTo('app\common\model\Coupon', 'coupon_id');
- }
- /**
- * 获取奖品类型文本
- */
- public function getTypeTextAttr($value, $data)
- {
- $types = [
- self::TYPE_NO_PRIZE => '未中奖',
- self::TYPE_PHYSICAL => '实物奖品',
- self::TYPE_COUPON => '优惠券',
- self::TYPE_RED_PACKET => '红包',
- self::TYPE_EXCHANGE_CODE => '兑换码',
- self::TYPE_GOODS => '商城奖品'
- ];
- return isset($types[$data['type']]) ? $types[$data['type']] : '未知';
- }
- /**
- * 获取发放方式文本
- */
- public function getDeliverTypeTextAttr($value, $data)
- {
- $types = [
- self::DELIVER_AUTO => '自动发放',
- self::DELIVER_MANUAL => '手动发放'
- ];
- return isset($types[$data['deliver_type']]) ? $types[$data['deliver_type']] : '未知';
- }
- /**
- * 获取兑换码列表
- */
- public function getExchangeCodesListAttr($value, $data)
- {
- return !empty($data['exchange_codes']) ? json_decode($data['exchange_codes'], true) : [];
- }
- /**
- * 设置兑换码列表
- */
- public function setExchangeCodesAttr($value)
- {
- return is_array($value) ? json_encode($value) : $value;
- }
- /**
- * 获取已使用兑换码列表
- */
- public function getUsedCodesListAttr($value, $data)
- {
- return !empty($data['used_codes']) ? json_decode($data['used_codes'], true) : [];
- }
- /**
- * 设置已使用兑换码列表
- */
- public function setUsedCodesAttr($value)
- {
- return is_array($value) ? json_encode($value) : $value;
- }
- /**
- * 检查奖品库存是否充足
- */
- public function hasStock($quantity = 1)
- {
- return $this->remain_stock >= $quantity;
- }
- /**
- * 减少库存
- */
- public function decreaseStock($quantity = 1)
- {
- if (!$this->hasStock($quantity)) {
- return false;
- }
-
- $this->remain_stock -= $quantity;
- $this->win_count += $quantity;
- return $this->save();
- }
- /**
- * 获取可用的兑换码
- */
- public function getAvailableExchangeCode()
- {
- if ($this->type != self::TYPE_EXCHANGE_CODE) {
- return null;
- }
-
- $allCodes = $this->exchange_codes_list;
- $usedCodes = $this->used_codes_list;
-
- $availableCodes = array_diff($allCodes, $usedCodes);
-
- if (empty($availableCodes)) {
- return null;
- }
-
- return array_shift($availableCodes);
- }
- /**
- * 标记兑换码为已使用
- */
- public function markExchangeCodeUsed($code)
- {
- $usedCodes = $this->used_codes_list;
- if (!in_array($code, $usedCodes)) {
- $usedCodes[] = $code;
- $this->used_codes = json_encode($usedCodes);
- return $this->save();
- }
- return true;
- }
- /**
- * 获取有效奖品(库存大于0且状态正常)
- */
- public static function getValidPrizes($activityId)
- {
- return static::where('activity_id', $activityId)
- ->where('status', 1)
- ->where('remain_stock', '>', 0)
- ->order('sort_order', 'asc')
- ->select();
- }
- /**
- * 检查是否已解锁(按人数解锁功能)
- */
- public function isUnlocked($currentPeopleCount)
- {
- if (empty($this->unlock_people_num)) {
- return true;
- }
-
- return $currentPeopleCount >= $this->unlock_people_num;
- }
- }
|