|
|
@@ -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();
|
|
|
+ }
|
|
|
+}
|