| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- declare(strict_types=1);
- namespace SixShop\ShippingTemplate\Model;
- use SixShop\ShippingTemplate\Enum\ShippingTemplateAreaTypeEnum;
- use think\Model;
- /**
- * Class SixShop\ShippingTemplate\Model\ShippingTemplateRuleModel
- *
- * @property int $id 主键
- * @property int $template_id 运费模板ID
- * @property float $first 首件/首重/首体积
- * @property float $first_price 首件/首重/首体积费用
- * @property float $next_price 续件/续重/续体积费用
- * @property ShippingTemplateAreaTypeEnum $area_type 区域类型
- * @property string $area_name 区域名称
- * @property string $regions 地区信息(JSON格式)
- * @property string $create_time 创建时间
- * @property string $update_time 更新时间
- */
- class ShippingTemplateRuleModel extends Model
- {
- protected function getOptions(): array
- {
- return [
- 'name' => '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;
- }
- }
|