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

miaoxing / logistics / 13100244582

02 Feb 2025 03:42PM UTC coverage: 35.456% (+0.2%) from 35.243%
13100244582

push

github

semantic-release-bot
chore(release): publish

See CHANGELOG.md for more details.

24 of 99 branches covered (24.24%)

206 of 581 relevant lines covered (35.46%)

5.57 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\Plugin\BaseModel;
6
use Miaoxing\Plugin\Model\HasAppIdTrait;
7
use Miaoxing\Plugin\Model\ModelTrait;
8
use Miaoxing\Plugin\Model\ReqQueryTrait;
9
use Miaoxing\Plugin\Model\SnowflakeTrait;
10
use Miaoxing\Region\Service\RegionModel;
11
use Wei\Model\SoftDeleteTrait;
12

13
/**
14
 * @property ShippingTplRuleModel|ShippingTplRuleModel[] $rules
15
 * @property string|null $id
16
 * @property string $appId
17
 * @property array $serviceIds 支持的物流服务编号
18
 * @property string $name 名称
19
 * @property int $sendRegionId 发货地址
20
 * @property int $sendTime 发货时间。以小时为单位,-1表示无
21
 * @property bool $isFreeShipping 是否包邮
22
 * @property int $valuationType 计价方式。1:按件;2:按重量
23
 * @property int $sort 顺序
24
 * @property string|null $createdAt
25
 * @property string|null $updatedAt
26
 * @property string $createdBy
27
 * @property string $updatedBy
28
 * @property string|null $deletedAt
29
 * @property string $deletedBy
30
 * @property ShippingTplRuleModel|ShippingTplRuleModel[] $getRulesByCity 根据城市编号或名称,获取匹配的运费规则
31
 */
32
class ShippingTplModel extends BaseModel
33
{
34
    use HasAppIdTrait;
35
    use ModelTrait;
36
    use ReqQueryTrait;
37
    use SnowflakeTrait;
38
    use SoftDeleteTrait;
39

40
    public const VALUATION_TYPE_BY_PIECE = 1;
41

42
    public const VALUATION_TYPE_BY_WEIGHT = 2;
43

44
    protected $columns = [
45
        'serviceIds' => [
46
            'cast' => [
47
                'list',
48
                'type' => 'int',
49
            ],
50
        ],
51
    ];
52

53
    /**
54
     * 特殊城市与所属"市辖区"ID的对应关系
55
     *
56
     * @var array
57
     * @internal
58
     */
59
    protected $cityIds = [
60
        '北京市' => 110100,
61
        '天津市' => 120100,
62
        '上海市' => 310100,
63
        '重庆市' => 500100,
64
    ];
65

66
    /**
67
     * @Relation
68
     */
69
    public function rules(): ShippingTplRuleModel
70
    {
71
        return $this->hasMany(ShippingTplRuleModel::class);
12✔
72
    }
73

74
    /**
75
     * 获取城市和匹配的运费规则
76
     *
77
     * @param string|int $city 如果为空,则按需识别出城市
78
     * @return array 返回数组包含 city 和 rules
79
     */
80
    public function getCityAndRules($city = null): array
81
    {
82
        if ($this->isFreeShipping) {
×
83
            // 包邮无需加载规则
84
            $rules = [];
×
85
        } elseif (1 === $this->rules->count()) {
×
86
            // 只有默认规则,直接返回
87
            $rules = $this->rules;
×
88
        } else {
89
            // 定位城市并根据城市过滤出匹配的规则
90
            $city || $city = wei()->lbs->getIpInfo()['city'];
×
91
            $rules = $this->getRulesByCity($city);
×
92
        }
93

94
        return [
×
95
            'city' => $city,
×
96
            'rules' => $rules,
×
97
        ];
×
98
    }
99

100
    /**
101
     * 根据城市编号或名称,获取匹配的运费规则
102
     *
103
     * @param int|string $cityId 城市编号或名称,如上海市辖区310000,深圳440300
104
     * @return ShippingTplRuleModel|ShippingTplRuleModel[]
105
     */
106
    public function getRulesByCity($cityId = null): ShippingTplRuleModel
107
    {
108
        // 如果不是数字ID,查表转换为数字ID
109
        if ($cityId && !is_numeric($cityId)) {
24✔
110
            // 如果是特殊城市,如上海市,转换为上海市"市辖区"的ID
111
            if (isset($this->cityIds[$cityId])) {
12✔
112
                $cityId = $this->cityIds[$cityId];
6✔
113
            } else {
114
                $region = RegionModel::select('id')->findBy('name', $cityId);
6✔
115
                if ($region) {
6✔
116
                    $cityId = $region['id'];
6✔
117
                }
118
            }
119
        }
120

121
        $serviceRules = [];
24✔
122
        foreach ($this->rules as $rule) {
24✔
123
            $serviceRules[$rule->serviceId][] = $rule;
24✔
124
        }
125

126
        $matches = [];
24✔
127
        $default = null;
24✔
128
        foreach ($serviceRules as $serviceId => $rules) {
24✔
129
            foreach ($rules as $rule) {
24✔
130
                if ($rule->isDefault) {
24✔
131
                    $default = $rule;
24✔
132
                }
133
                if (in_array($cityId, $rule->regionIds, true)) {
24✔
134
                    $matches[$serviceId] = $rule;
18✔
135
                    break;
18✔
136
                }
137
            }
138
            // 没有匹配的地区,使用默认
139
            if (!isset($matches[$serviceId])) {
24✔
140
                $matches[$serviceId] = $default;
24✔
141
            }
142
        }
143

144
        return ShippingTplRuleModel::newColl(array_values($matches));
24✔
145
    }
146
}
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

© 2025 Coveralls, Inc