123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- declare(strict_types=1);
- namespace App\Services\Interface\Database\Update;
- use Illuminate\Support\Facades\DB;
- use App\Services\Interface\Database\CommonDb;
- class UpdateDb extends CommonDb
- {
- public function __construct(array $interfaceInfo, array $assembleInfo, array $assembleColumns, array $requestData, array $columnIdToCodes)
- {
- parent::__construct($interfaceInfo, $assembleInfo, $assembleColumns, $requestData, $columnIdToCodes);
- }
- public function exec(): ?bool
- {
- $update_type = $this->interfaceSettingInfo['update_type'] ?? 0;
- if (2 === $update_type) { // 批量更新
- $result = $this->updateMulti();
- } elseif (3 === $update_type) { // 更新单个树型数据
- $result = $this->updateOneTree();
- } elseif (1 === $update_type) { // 更新单条数据
- $result = $this->updateOne();
- } else {
- $result = $this->updateOne();
- }
- return $result;
- }
- /**
- * 更新单条数据.
- */
- public function updateOne()
- {
- $entityId = $this->requestData['id'] ?? '';
- $entityMid = $this->requestData['mid'] ?? '';
- if (empty($entityId) && empty($entityMid)) {
- abort(508, '参数错误,接口配置有误! updateOne');
- }
- return $this->updateData($this->requestData, $entityId, $entityMid);
- }
- /**
- * 更新树形数据.
- *
- * @return bool
- *
- * @throws \Throwable
- */
- public function updateOneTree()
- {
- $entityId = $this->requestData['id'] ?? '';
- $entityMid = $this->requestData['mid'] ?? '';
- if (empty($entityId) && empty($entityMid)) {
- abort(508, '参数错误,接口配置有误!');
- }
- // 设置树型数据参数
- $this->requestData = $this->getTreeParams($this->requestData);
- return $this->updateData($this->requestData, $entityId, $entityMid);
- }
- /**
- * 批量更新.
- *
- * @return bool
- *
- * @throws \Throwable
- */
- public function updateMulti()
- {
- $entityId = $this->requestData['id'] ?? '';
- $entityMid = $this->requestData['mid'] ?? '';
- if (empty($entityId) && empty($entityMid)) {
- abort(508, '参数错误,接口配置有误!');
- }
- dd('开发中');
- }
- /**
- * 更新数据.
- */
- public function updateData($requestData, $entityId, $entityMid)
- {
- // 防止冲突 去掉id
- if (isset($requestData['id'])) {
- unset($requestData['id']);
- }
- // 判断是否存在该数据
- $find = DB::connection($this->getDbConnection())->table($this->table);
- if ($entityId) {
- $find->where('id', $entityId);
- }
- if ($entityMid) {
- $find->where('mid', $entityMid);
- }
- $existInfo = $find->first('id');
- if (empty($existInfo)) {
- abort(508, '数据不存在!');
- } else {
- $entityId = $existInfo->id;
- }
- // 过滤数据
- list($tableExtendData, $tableEntityData) = $this->getTableToData('update', $requestData);
- if (empty($tableExtendData) && empty($tableEntityData)) {
- abort(508, '保存的数据为空,请重试!');
- }
- $callback = null;
- return DB::connection($this->getDbConnection())->transaction(function () use ($callback, $tableEntityData, $entityId) {
- // 更新实体
- DB::connection($this->getDbConnection())->table($this->table)->where('id', $entityId)->update($tableEntityData);
- // 其他闭包操作
- if ($callback) {
- \call_user_func($callback);
- }
- return true;
- });
- }
- /**
- * 给数组数据,添加entity_id.
- */
- public function setValueEntityId(array $arr, int $entity_id): array
- {
- foreach ($arr as $key => $row) {
- // object类型 es和数据表存储的方式不同,这里转换一下
- if ('object' === $row['type'] || 'json' === $row['type']) {
- $arr[$key]['value'] = json_encode($row['value']);
- }
- unset($arr[$key]['type'], $arr[$key]['column']);
- $arr[$key]['entity_id'] = $entity_id;
- }
- return $arr;
- }
- /**
- * 填充树型数据的父级分类数据.
- */
- private function getTreeParams(?array $requestData): array
- {
- if (isset($requestData['parent_id']) && $requestData['parent_id']) {
- // 查询父级信息
- $parentInfo = DB::table($this->table)->find($requestData['parent_id']);
- if (empty($parentInfo)) {
- abort(508, '父级数据不存在,请重试!');
- }
- $requestData['level'] = $parentInfo['level'] + 1;
- } else {
- $requestData['parent_id'] = 0;
- $requestData['level'] = 1;
- }
- return $requestData;
- }
- }
|