|
@@ -0,0 +1,104 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+declare(strict_types=1);
|
|
|
|
|
+
|
|
|
|
|
+namespace SixShop\eav\Controller;
|
|
|
|
|
+
|
|
|
|
|
+use SixShop\Core\Helper;
|
|
|
|
|
+use SixShop\Core\Request;
|
|
|
|
|
+use SixShop\eav\Entity\EvaValueEntity;
|
|
|
|
|
+use SixShop\eav\Model\EvaAttributeModel;
|
|
|
|
|
+use think\Response;
|
|
|
|
|
+
|
|
|
|
|
+class ValueController
|
|
|
|
|
+{
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 查询指定实体的所有属性值
|
|
|
|
|
+ * GET /eav/value?entity_type_id=1&entity_id=123
|
|
|
|
|
+ */
|
|
|
|
|
+ public function index(Request $request, EvaValueEntity $entity): Response
|
|
|
|
|
+ {
|
|
|
|
|
+ $entityTypeId = $request->get('entity_type_id/d');
|
|
|
|
|
+ $entityId = $request->get('entity_id/d');
|
|
|
|
|
+ if (!$entityTypeId || !$entityId) {
|
|
|
|
|
+ return Helper::error_response('参数缺失');
|
|
|
|
|
+ }
|
|
|
|
|
+ // 查询该实体的所有属性值
|
|
|
|
|
+ $list = $entity->where([
|
|
|
|
|
+ 'entity_id' => $entityId
|
|
|
|
|
+ ])->select();
|
|
|
|
|
+ return Helper::success_response($list);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 保存/更新指定实体的属性值
|
|
|
|
|
+ * POST /eav/value
|
|
|
|
|
+ * body: [
|
|
|
|
|
+ * { "entity_id": 123, "entity_type_id": 1, "attribute_id": 2, "value": "红色" }
|
|
|
|
|
+ * ]
|
|
|
|
|
+ */
|
|
|
|
|
+ public function save(Request $request, EvaValueEntity $entity): Response
|
|
|
|
|
+ {
|
|
|
|
|
+ $data = $request->post();
|
|
|
|
|
+ if (empty($data) || !is_array($data)) {
|
|
|
|
|
+ return Helper::error_response('参数错误');
|
|
|
|
|
+ }
|
|
|
|
|
+ // 支持批量写入
|
|
|
|
|
+ foreach ($data as $item) {
|
|
|
|
|
+ if (
|
|
|
|
|
+ empty($item['entity_id']) ||
|
|
|
|
|
+ empty($item['attribute_id']) ||
|
|
|
|
|
+ !isset($item['value'])
|
|
|
|
|
+ ) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 获取属性类型
|
|
|
|
|
+ $attr = EvaAttributeModel::find($item['attribute_id']);
|
|
|
|
|
+ if (!$attr) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ $saveData = [
|
|
|
|
|
+ 'entity_id' => $item['entity_id'],
|
|
|
|
|
+ 'attribute_id' => $item['attribute_id'],
|
|
|
|
|
+ ];
|
|
|
|
|
+ switch ($attr->backend_type) {
|
|
|
|
|
+ case 'int':
|
|
|
|
|
+ $saveData['value_int'] = intval($item['value']);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'decimal':
|
|
|
|
|
+ $saveData['value_decimal'] = floatval($item['value']);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'text':
|
|
|
|
|
+ $saveData['value_text'] = $item['value'];
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'varchar':
|
|
|
|
|
+ default:
|
|
|
|
|
+ $saveData['value_varchar'] = $item['value'];
|
|
|
|
|
+ }
|
|
|
|
|
+ // 先查找是否已存在,存在则更新,否则新增
|
|
|
|
|
+ $exist = $entity->where([
|
|
|
|
|
+ 'entity_id' => $item['entity_id'],
|
|
|
|
|
+ 'attribute_id' => $item['attribute_id']
|
|
|
|
|
+ ])->find();
|
|
|
|
|
+ if ($exist) {
|
|
|
|
|
+ $entity->where('id', $exist['id'])->update($saveData);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $entity->save($saveData);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return Helper::success_response('保存成功');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 删除指定实体的所有属性值
|
|
|
|
|
+ * DELETE /eav/value?entity_id=123
|
|
|
|
|
+ */
|
|
|
|
|
+ public function delete(Request $request, EvaValueEntity $entity): Response
|
|
|
|
|
+ {
|
|
|
|
|
+ $entityId = $request->get('entity_id/d');
|
|
|
|
|
+ if (!$entityId) {
|
|
|
|
|
+ return Helper::error_response('参数缺失');
|
|
|
|
|
+ }
|
|
|
|
|
+ $entity->where(['entity_id' => $entityId])->delete();
|
|
|
|
|
+ return Helper::success_response('删除成功');
|
|
|
|
|
+ }
|
|
|
|
|
+}
|