• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miaoxing / logistics / 10654302660

01 Sep 2024 01:03PM UTC coverage: 35.354% (-1.2%) from 36.522%
10654302660

push

github

semantic-release-bot
chore(release): publish

See CHANGELOG.md for more details.

24 of 103 branches covered (23.3%)

210 of 594 relevant lines covered (35.35%)

2.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.75
/src/Service/ShippingTplModel.php
1
<?php
2

3
namespace Miaoxing\Logistics\Service;
4

5
use Miaoxing\Logistics\Metadata\ShippingTplTrait;
6
use Miaoxing\Plugin\BaseModel;
7
use Miaoxing\Plugin\Model\HasAppIdTrait;
8
use Miaoxing\Plugin\Model\ModelTrait;
9
use Miaoxing\Plugin\Model\ReqQueryTrait;
10
use Miaoxing\Plugin\Model\SnowflakeTrait;
11
use Miaoxing\Region\Service\RegionModel;
12
use Wei\Model\SoftDeleteTrait;
13

14
/**
15
 * @property ShippingTplRuleModel|ShippingTplRuleModel[] $rules
16
 */
17
class ShippingTplModel extends BaseModel
18
{
19
    use HasAppIdTrait;
20
    use ModelTrait;
21
    use ReqQueryTrait;
22
    use ShippingTplTrait;
23
    use SnowflakeTrait;
24
    use SoftDeleteTrait;
25

26
    public const VALUATION_TYPE_BY_PIECE = 1;
27

28
    public const VALUATION_TYPE_BY_WEIGHT = 2;
29

30
    protected $columns = [
31
        'serviceIds' => [
32
            'cast' => [
33
                'list',
34
                'type' => 'int',
35
            ],
36
        ],
37
    ];
38

39
    /**
40
     * 特殊城市与所属"市辖区"ID的对应关系
41
     *
42
     * @var array
43
     * @internal
44
     */
45
    protected $cityIds = [
46
        '北京市' => 110100,
47
        '天津市' => 120100,
48
        '上海市' => 310100,
49
        '重庆市' => 500100,
50
    ];
51

52
    /**
53
     * @Relation
54
     */
55
    public function rules(): ShippingTplRuleModel
56
    {
57
        return $this->hasMany(ShippingTplRuleModel::class);
2✔
58
    }
59

60
    /**
61
     * 获取城市和匹配的运费规则
62
     *
63
     * @param string|int $city 如果为空,则按需识别出城市
64
     * @return array 返回数组包含 city 和 rules
65
     */
66
    public function getCityAndRules($city = null): array
67
    {
68
        if ($this->isFreeShipping) {
×
69
            // 包邮无需加载规则
70
            $rules = [];
×
71
        } elseif (1 === $this->rules->count()) {
×
72
            // 只有默认规则,直接返回
73
            $rules = $this->rules;
×
74
        } else {
75
            // 定位城市并根据城市过滤出匹配的规则
76
            $city || $city = wei()->lbs->getIpInfo()['city'];
×
77
            $rules = $this->getRulesByCity($city);
×
78
        }
79

80
        return [
×
81
            'city' => $city,
×
82
            'rules' => $rules,
×
83
        ];
×
84
    }
85

86
    /**
87
     * 根据城市编号或名称,获取匹配的运费规则
88
     *
89
     * @param int|string $cityId 城市编号或名称,如上海市辖区310000,深圳440300
90
     * @return ShippingTplRuleModel|ShippingTplRuleModel[]
91
     */
92
    public function getRulesByCity($cityId = null): ShippingTplRuleModel
93
    {
94
        // 如果不是数字ID,查表转换为数字ID
95
        if ($cityId && !is_numeric($cityId)) {
4✔
96
            // 如果是特殊城市,如上海市,转换为上海市"市辖区"的ID
97
            if (isset($this->cityIds[$cityId])) {
2✔
98
                $cityId = $this->cityIds[$cityId];
1✔
99
            } else {
100
                $region = RegionModel::select('id')->findBy('name', $cityId);
1✔
101
                if ($region) {
1✔
102
                    $cityId = $region['id'];
1✔
103
                }
104
            }
105
        }
106

107
        $serviceRules = [];
4✔
108
        foreach ($this->rules as $rule) {
4✔
109
            $serviceRules[$rule->serviceId][] = $rule;
4✔
110
        }
111

112
        $matches = [];
4✔
113
        $default = null;
4✔
114
        foreach ($serviceRules as $serviceId => $rules) {
4✔
115
            foreach ($rules as $rule) {
4✔
116
                if ($rule->isDefault) {
4✔
117
                    $default = $rule;
4✔
118
                }
119
                if (in_array($cityId, $rule->regionIds, true)) {
4✔
120
                    $matches[$serviceId] = $rule;
3✔
121
                    break;
3✔
122
                }
123
            }
124
            // 没有匹配的地区,使用默认
125
            if (!isset($matches[$serviceId])) {
4✔
126
                $matches[$serviceId] = $default;
4✔
127
            }
128
        }
129

130
        return ShippingTplRuleModel::newColl(array_values($matches));
4✔
131
    }
132
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc