ShippingTemplateRuleModel.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\ShippingTemplate\Model;
  4. use SixShop\ShippingTemplate\Enum\ShippingTemplateAreaTypeEnum;
  5. use think\Model;
  6. /**
  7. * Class SixShop\ShippingTemplate\Model\ShippingTemplateRuleModel
  8. *
  9. * @property int $id 主键
  10. * @property int $template_id 运费模板ID
  11. * @property float $first 首件/首重/首体积
  12. * @property float $first_price 首件/首重/首体积费用
  13. * @property float $next_price 续件/续重/续体积费用
  14. * @property ShippingTemplateAreaTypeEnum $area_type 区域类型
  15. * @property string $area_name 区域名称
  16. * @property string $regions 地区信息(JSON格式)
  17. * @property string $create_time 创建时间
  18. * @property string $update_time 更新时间
  19. */
  20. class ShippingTemplateRuleModel extends Model
  21. {
  22. protected function getOptions(): array
  23. {
  24. return [
  25. 'name' => 'shipping_template_rules',
  26. 'pk' => 'id',
  27. 'type' => [
  28. 'area_type' => ShippingTemplateAreaTypeEnum::class,
  29. 'regions' => 'array'
  30. ]
  31. ];
  32. }
  33. /**
  34. * 关联运费模板
  35. */
  36. public function template()
  37. {
  38. return $this->belongsTo(ShippingTemplateModel::class, 'template_id');
  39. }
  40. /**
  41. * 获取默认规则
  42. */
  43. public static function getDefaultRule(int $templateId)
  44. {
  45. return self::where('template_id', $templateId)
  46. ->where('area_type', ShippingTemplateAreaTypeEnum::DEFAULT)
  47. ->find();
  48. }
  49. /**
  50. * 获取特殊区域规则
  51. */
  52. public static function getSpecialRules(int $templateId)
  53. {
  54. return self::where('template_id', $templateId)
  55. ->where('area_type', ShippingTemplateAreaTypeEnum::SPECIAL)
  56. ->select();
  57. }
  58. /**
  59. * 检查地区是否在特殊区域规则中
  60. */
  61. public static function checkRegionInSpecialRules(int $templateId, string $regionCode)
  62. {
  63. $specialRules = self::getSpecialRules($templateId);
  64. foreach ($specialRules as $rule) {
  65. $regions = json_decode($rule->regions, true);
  66. if (is_array($regions) && in_array($regionCode, $regions)) {
  67. return $rule;
  68. }
  69. }
  70. return null;
  71. }
  72. }