UpdateDb.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Services\Interface\Database\Update;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Services\Interface\Database\CommonDb;
  6. class UpdateDb extends CommonDb
  7. {
  8. public function __construct(array $interfaceInfo, array $assembleInfo, array $assembleColumns, array $requestData, array $columnIdToCodes)
  9. {
  10. parent::__construct($interfaceInfo, $assembleInfo, $assembleColumns, $requestData, $columnIdToCodes);
  11. }
  12. public function exec(): ?bool
  13. {
  14. $update_type = $this->interfaceSettingInfo['update_type'] ?? 0;
  15. if (2 === $update_type) { // 批量更新
  16. $result = $this->updateMulti();
  17. } elseif (3 === $update_type) { // 更新单个树型数据
  18. $result = $this->updateOneTree();
  19. } elseif (1 === $update_type) { // 更新单条数据
  20. $result = $this->updateOne();
  21. } else {
  22. $result = $this->updateOne();
  23. }
  24. return $result;
  25. }
  26. /**
  27. * 更新单条数据.
  28. */
  29. public function updateOne()
  30. {
  31. $entityId = $this->requestData['id'] ?? '';
  32. $entityMid = $this->requestData['mid'] ?? '';
  33. if (empty($entityId) && empty($entityMid)) {
  34. abort(508, '参数错误,接口配置有误! updateOne');
  35. }
  36. return $this->updateData($this->requestData, $entityId, $entityMid);
  37. }
  38. /**
  39. * 更新树形数据.
  40. *
  41. * @return bool
  42. *
  43. * @throws \Throwable
  44. */
  45. public function updateOneTree()
  46. {
  47. $entityId = $this->requestData['id'] ?? '';
  48. $entityMid = $this->requestData['mid'] ?? '';
  49. if (empty($entityId) && empty($entityMid)) {
  50. abort(508, '参数错误,接口配置有误!');
  51. }
  52. // 设置树型数据参数
  53. $this->requestData = $this->getTreeParams($this->requestData);
  54. return $this->updateData($this->requestData, $entityId, $entityMid);
  55. }
  56. /**
  57. * 批量更新.
  58. *
  59. * @return bool
  60. *
  61. * @throws \Throwable
  62. */
  63. public function updateMulti()
  64. {
  65. $entityId = $this->requestData['id'] ?? '';
  66. $entityMid = $this->requestData['mid'] ?? '';
  67. if (empty($entityId) && empty($entityMid)) {
  68. abort(508, '参数错误,接口配置有误!');
  69. }
  70. dd('开发中');
  71. }
  72. /**
  73. * 更新数据.
  74. */
  75. public function updateData($requestData, $entityId, $entityMid)
  76. {
  77. // 防止冲突 去掉id
  78. if (isset($requestData['id'])) {
  79. unset($requestData['id']);
  80. }
  81. // 判断是否存在该数据
  82. $find = DB::connection($this->getDbConnection())->table($this->table);
  83. if ($entityId) {
  84. $find->where('id', $entityId);
  85. }
  86. if ($entityMid) {
  87. $find->where('mid', $entityMid);
  88. }
  89. $existInfo = $find->first('id');
  90. if (empty($existInfo)) {
  91. abort(508, '数据不存在!');
  92. } else {
  93. $entityId = $existInfo->id;
  94. }
  95. // 过滤数据
  96. list($tableExtendData, $tableEntityData) = $this->getTableToData('update', $requestData);
  97. if (empty($tableExtendData) && empty($tableEntityData)) {
  98. abort(508, '保存的数据为空,请重试!');
  99. }
  100. $callback = null;
  101. return DB::connection($this->getDbConnection())->transaction(function () use ($callback, $tableEntityData, $entityId) {
  102. // 更新实体
  103. DB::connection($this->getDbConnection())->table($this->table)->where('id', $entityId)->update($tableEntityData);
  104. // 其他闭包操作
  105. if ($callback) {
  106. \call_user_func($callback);
  107. }
  108. return true;
  109. });
  110. }
  111. /**
  112. * 给数组数据,添加entity_id.
  113. */
  114. public function setValueEntityId(array $arr, int $entity_id): array
  115. {
  116. foreach ($arr as $key => $row) {
  117. // object类型 es和数据表存储的方式不同,这里转换一下
  118. if ('object' === $row['type'] || 'json' === $row['type']) {
  119. $arr[$key]['value'] = json_encode($row['value']);
  120. }
  121. unset($arr[$key]['type'], $arr[$key]['column']);
  122. $arr[$key]['entity_id'] = $entity_id;
  123. }
  124. return $arr;
  125. }
  126. /**
  127. * 填充树型数据的父级分类数据.
  128. */
  129. private function getTreeParams(?array $requestData): array
  130. {
  131. if (isset($requestData['parent_id']) && $requestData['parent_id']) {
  132. // 查询父级信息
  133. $parentInfo = DB::table($this->table)->find($requestData['parent_id']);
  134. if (empty($parentInfo)) {
  135. abort(508, '父级数据不存在,请重试!');
  136. }
  137. $requestData['level'] = $parentInfo['level'] + 1;
  138. } else {
  139. $requestData['parent_id'] = 0;
  140. $requestData['level'] = 1;
  141. }
  142. return $requestData;
  143. }
  144. }