'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); } }