| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- namespace Longyi\RewardPoints\Models;
- use Illuminate\Database\Eloquent\Model;
- use Webkul\Customer\Models\Customer;
- class RewardActiveRule extends Model
- {
- // 定义交易类型常量
- const TYPE_SIGN_IN = 1;
- const TYPE_REGISTRATION = 2;
- const TYPE_ORDER = 3;
- const TYPE_REVIEW = 4;
- const TYPE_REFERRAL = 5;
- const TYPE_BIRTHDAY = 6;
- const TYPE_SHARE = 7;
- const TYPE_SUBSCRIBE = 8;
- const TYPE_LOGIN = 9;
- protected $table = 'mw_reward_active_rules';
- protected $primaryKey = 'rule_id';
- public $timestamps = false;
- protected $fillable = [
- 'rule_name', 'type_of_transaction', 'store_view', 'customer_group_ids',
- 'enable_different_points_by_group', 'default_expired', 'expired_day',
- 'date_event', 'comment', 'coupon_code', 'reward_point', 'status',
- 'group_points' // 添加群组积分配置字段
- ];
- protected $casts = [
- 'type_of_transaction' => 'integer',
- 'status' => 'boolean',
- 'expired_day' => 'integer',
- 'enable_different_points_by_group' => 'boolean',
- 'group_points' => 'array', // 自动 JSON 转换
- ];
- /**
- * 获取客户群组积分配置
- * 假设数据库中 group_points 字段存储 JSON,格式:{"1": 100, "2": 150}
- */
- public function getCustomerGroupPointsAttribute(): array
- {
- // 如果使用 group_points 字段(推荐)
- if (isset($this->attributes['group_points'])) {
- $value = $this->attributes['group_points'];
- if (is_string($value)) {
- return json_decode($value, true) ?: [];
- }
- return is_array($value) ? $value : [];
- }
- // 兼容其他可能的字段名
- if (isset($this->attributes['customer_group_points'])) {
- $value = $this->attributes['customer_group_points'];
- if (is_string($value)) {
- return json_decode($value, true) ?: [];
- }
- return is_array($value) ? $value : [];
- }
- return [];
- }
- /**
- * 【核心方法】统一获取客户应得的积分值
- * 整合了所有积分获取逻辑,避免重复代码
- */
- public function getPointsForCustomer($customer): int
- {
- // 获取客户群组ID
- $customerGroupId = $this->extractCustomerGroupId($customer);
- // 如果无法获取群组ID,返回0(注册、订阅等场景应该单独处理)
- if ($customerGroupId === null) {
- return 0;
- }
- // 启用群组差异化:从配置中获取该群组的积分
- if ($this->enable_different_points_by_group) {
- $groupPoints = $this->getCustomerGroupPointsAttribute();
- return (int)($groupPoints[$customerGroupId] ?? 0);
- }
- // 不启用差异化:检查群组是否在允许列表中
- if ($this->isGroupAllowed($customerGroupId)) {
- return (int)$this->reward_point;
- }
- return 0;
- }
- /**
- * 获取特定客户群组的积分值(保留向后兼容)
- */
- public function getRewardPointForCustomerGroup($customerGroupId): int
- {
- if ($this->enable_different_points_by_group) {
- $groupPoints = $this->getCustomerGroupPointsAttribute();
- return (int)($groupPoints[$customerGroupId] ?? 0);
- }
- if ($this->isGroupAllowed($customerGroupId)) {
- return (int)$this->reward_point;
- }
- return 0;
- }
- /**
- * 检查规则是否适用于指定客户
- */
- public function isApplicableToCustomer($customer): bool
- {
- // 检查 store_view
- if (!$this->isStoreViewMatched()) {
- return false;
- }
- $customerGroupId = $this->extractCustomerGroupId($customer);
- if ($customerGroupId === null) {
- return false;
- }
- // 启用群组差异化:检查是否有为该群组配置积分
- if ($this->enable_different_points_by_group) {
- $groupPoints = $this->getCustomerGroupPointsAttribute();
- return !empty($groupPoints) && isset($groupPoints[$customerGroupId]);
- }
- // 不启用差异化:检查群组是否在允许列表中
- return $this->isGroupAllowed($customerGroupId);
- }
- /**
- * 检查 store_view 是否匹配
- */
- protected function isStoreViewMatched(): bool
- {
- if (empty($this->store_view) || $this->store_view === '0') {
- return true;
- }
- $storeViews = explode(',', $this->store_view);
- $currentChannelCode = core()->getCurrentChannelCode();
- return in_array($currentChannelCode, $storeViews);
- }
- /**
- * 检查群组是否允许
- */
- protected function isGroupAllowed($customerGroupId): bool
- {
- if (empty($this->customer_group_ids) || trim($this->customer_group_ids) === '') {
- return true;
- }
- $allowedGroups = array_map('trim', explode(',', $this->customer_group_ids));
- return in_array((string)$customerGroupId, $allowedGroups);
- }
- /**
- * 提取客户群组ID
- */
- protected function extractCustomerGroupId($customer): ?string
- {
- if ($customer instanceof Customer) {
- return $customer->customer_group_id;
- }
- if (is_object($customer) && property_exists($customer, 'customer_group_id')) {
- return $customer->customer_group_id;
- }
- return null;
- }
- /**
- * 获取交易类型文本
- */
- public function getTransactionTypeTextAttribute(): string
- {
- $types = [
- self::TYPE_ORDER => 'Order',
- self::TYPE_REGISTRATION => 'Registration',
- self::TYPE_REVIEW => 'Product Review',
- self::TYPE_LOGIN => 'Login',
- self::TYPE_REFERRAL => 'Referral',
- self::TYPE_BIRTHDAY => 'Birthday',
- self::TYPE_SHARE => 'Share',
- self::TYPE_SUBSCRIBE => 'Subscription',
- self::TYPE_SIGN_IN => 'Sign In',
- ];
- return $types[$this->type_of_transaction] ?? 'Unknown';
- }
- /**
- * 作用域:获取启用的规则
- */
- public function scopeActive($query)
- {
- return $query->where('status', 1);
- }
- /**
- * 作用域:按交易类型查询
- */
- public function scopeOfType($query, $type)
- {
- return $query->where('type_of_transaction', $type);
- }
- }
|