'shipping_template_rules', 'pk' => 'id', 'type' => [ 'area_type' => ShippingTemplateAreaTypeEnum::class, 'regions' => 'array' ] ]; } /** * 关联运费模板 */ public function template() { return $this->belongsTo(ShippingTemplateModel::class, 'template_id'); } /** * 获取默认规则 */ public static function getDefaultRule(int $templateId) { return self::where('template_id', $templateId) ->where('area_type', ShippingTemplateAreaTypeEnum::DEFAULT) ->find(); } /** * 获取特殊区域规则 */ public static function getSpecialRules(int $templateId) { return self::where('template_id', $templateId) ->where('area_type', ShippingTemplateAreaTypeEnum::SPECIAL) ->select(); } /** * 检查地区是否在特殊区域规则中 */ public static function checkRegionInSpecialRules(int $templateId, string $regionCode) { $specialRules = self::getSpecialRules($templateId); foreach ($specialRules as $rule) { $regions = json_decode($rule->regions, true); if (is_array($regions) && in_array($regionCode, $regions)) { return $rule; } } return null; } }