Browse Source

feat(shipping-template): 新增运费模板关联功能

- 创建运费模板关联表迁移文件
- 添加关联实体类 ShippingTemplateRelationEntity- 实现根据业务类型和 ID 获取/设置/移除模板关联的方法
- 提供通过模板 ID 查询关联业务实体的功能- 在模型中定义与运费模板的关联关系- 增加静态方法便于直接通过模型操作关联数据
runphp 5 months ago
parent
commit
b8312eedb3

+ 22 - 0
database/migrations/20251010100200_create_shipping_template_relations_table.php

@@ -0,0 +1,22 @@
+<?php
+
+use think\migration\Migrator;
+use think\migration\db\Column;
+
+class CreateShippingTemplateRelationsTable extends Migrator
+{
+    public function change()
+    {
+        $table = $this->table('shipping_template_relations');
+        $table->setId('id')
+            ->addColumn('template_id', 'integer', ['comment' => '运费模板ID'])
+            ->addColumn('biz_type', 'string', ['limit' => 50, 'comment' => '业务类型: goods(商品), category(商品分类), shop(店铺)等'])
+            ->addColumn('biz_id', 'integer', ['comment' => '业务实体ID'])
+            ->addTimestamps('create_time', 'update_time')
+            ->addIndex(['template_id'])
+            ->addIndex(['biz_type'])
+            ->addIndex(['biz_id'])
+            ->addIndex(['template_id', 'biz_type', 'biz_id'], ['unique' => true])
+            ->create();
+    }
+}

+ 77 - 0
src/Entity/ShippingTemplateRelationEntity.php

@@ -0,0 +1,77 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\ShippingTemplate\Entity;
+
+use SixShop\Core\Entity\BaseEntity;
+use SixShop\ShippingTemplate\Model\ShippingTemplateRelationModel;
+
+/**
+ * @mixin ShippingTemplateRelationModel
+ */
+class ShippingTemplateRelationEntity extends BaseEntity
+{
+    /**
+     * 根据业务类型和业务ID获取运费模板ID
+     */
+    public function getTemplateIdByBiz(string $bizType, int $bizId): ?int
+    {
+        $relation = $this->where('biz_type', $bizType)
+            ->where('biz_id', $bizId)
+            ->find();
+            
+        return $relation ? $relation->template_id : null;
+    }
+
+    /**
+     * 为业务实体设置运费模板
+     */
+    public function setTemplateForBiz(int $templateId, string $bizType, int $bizId): bool
+    {
+        $relation = $this->where('biz_type', $bizType)
+            ->where('biz_id', $bizId)
+            ->find();
+            
+        if ($relation) {
+            // 更新已存在的关联
+            return $relation->save([
+                'template_id' => $templateId,
+                'update_time' => date('Y-m-d H:i:s')
+            ]);
+        } else {
+            // 创建新的关联
+            return $this->save([
+                'template_id' => $templateId,
+                'biz_type' => $bizType,
+                'biz_id' => $bizId,
+                'create_time' => date('Y-m-d H:i:s'),
+                'update_time' => date('Y-m-d H:i:s')
+            ]);
+        }
+    }
+
+    /**
+     * 移除业务实体的运费模板关联
+     */
+    public function removeTemplateForBiz(string $bizType, int $bizId): bool
+    {
+        $relation = $this->where('biz_type', $bizType)
+            ->where('biz_id', $bizId)
+            ->find();
+            
+        if ($relation) {
+            return $relation->delete();
+        }
+        
+        return true;
+    }
+
+    /**
+     * 根据运费模板ID获取所有关联的业务实体
+     */
+    public function getBizEntitiesByTemplateId(int $templateId): iterable
+    {
+        return $this->where('template_id', $templateId)
+            ->select();
+    }
+}

+ 63 - 0
src/Model/ShippingTemplateRelationModel.php

@@ -0,0 +1,63 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\ShippingTemplate\Model;
+
+use think\Model;
+
+/**
+ * Class SixShop\ShippingTemplate\Model\ShippingTemplateRelationModel
+ *
+ * @property int $id 主键
+ * @property int $template_id 运费模板ID
+ * @property string $biz_type 业务类型
+ * @property int $biz_id 业务实体ID
+ * @property string $create_time 创建时间
+ * @property string $update_time 更新时间
+ */
+class ShippingTemplateRelationModel extends Model
+{
+    protected function getOptions(): array
+    {
+        return [
+            'name' => 'shipping_template_relations',
+            'pk' => 'id',
+        ];
+    }
+
+    /**
+     * 关联运费模板
+     */
+    public function template()
+    {
+        return $this->belongsTo(ShippingTemplateModel::class, 'template_id');
+    }
+
+    /**
+     * 根据业务类型和业务ID获取关联记录
+     */
+    public static function getByBiz(string $bizType, int $bizId)
+    {
+        return self::where('biz_type', $bizType)
+            ->where('biz_id', $bizId)
+            ->find();
+    }
+
+    /**
+     * 根据运费模板ID获取关联记录
+     */
+    public static function getByTemplateId(int $templateId)
+    {
+        return self::where('template_id', $templateId)
+            ->select();
+    }
+
+    /**
+     * 根据业务类型和业务ID获取运费模板ID
+     */
+    public static function getTemplateIdByBiz(string $bizType, int $bizId): ?int
+    {
+        $relation = self::getByBiz($bizType, $bizId);
+        return $relation ? $relation->template_id : null;
+    }
+}