123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- declare(strict_types=1);
- namespace App\Services\Interface\Database\Add;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Str;
- use Txj\Elastic\Facades\ES;
- use App\Services\Interface\Database\CommonDb;
- class AddDb extends CommonDb
- {
- public function __construct(array $interfaceInfo, array $assembleInfo, array $assembleColumns, array $requestData, array $columnIdToCodes)
- {
- parent::__construct($interfaceInfo, $assembleInfo, $assembleColumns, $requestData, $columnIdToCodes);
- // $requestData 过滤多余的字段,防止添加的时候造成字段污染
- $columnArr = array_column($this->assembleColumns, 'code');
- if ($requestData) {
- foreach ($requestData as $key => $value) {
- if (!in_array($key, $columnArr)) {
- unset($requestData[$key]);
- }
- }
- $this->requestData = $requestData;
- }
- }
- public function exec()
- {
- $add_type = $this->interfaceSettingInfo['add_type'] ?? 0;
- if (2 === $add_type) { // 批量添加
- $result = $this->addMulti();
- } elseif (3 === $add_type) { // 添加树型数据
- $result = $this->addOneTree();
- } elseif (1 === $add_type) { // 添加单条数据
- $result = $this->addOne();
- } else {
- $result = $this->addOne();
- }
- return $result;
- }
- /**
- * 添加单条数据.
- */
- public function addOne(): int
- {
- $find = DB::connection($this->getDbConnection())->table($this->table);
- // 添加数据 最多允许的数量
- $this->addTotalLimit($find);
- if (!isset($this->requestData['mid'])) {
- $this->requestData['mid'] = Str::random(12);
- }
- if (!isset($this->requestData['created_at'])) {
- $this->requestData['created_at'] = time();
- }
- if (!isset($this->requestData['updated_at'])) {
- $this->requestData['updated_at'] = time();
- }
- return $find->insertGetId($this->requestData);
- }
- /**
- * 添加树型数据.
- *
- * @return int
- */
- public function addOneTree()
- {
- $find = DB::connection($this->getDbConnection())->table($this->table);
- // 添加数据 最多允许的数量
- $this->addTotalLimit($find);
- if (!isset($this->requestData['mid'])) {
- $this->requestData['mid'] = Str::random(12);
- }
- if (!isset($this->requestData['created_at'])) {
- $this->requestData['created_at'] = time();
- }
- if (!isset($this->requestData['updated_at'])) {
- $this->requestData['updated_at'] = time();
- }
- // 设置树型数据参数
- $requestData = $this->getTreeParams($this->requestData);
- return $find->insertGetId($requestData);
- }
- /**
- * 批量添加数据.
- */
- public function addMulti() // // todo 代码不对
- {
- $find = DB::connection($this->getDbConnection())->table($this->table);
- return $find->insert();
- }
- /**
- * 填充树型数据的父级分类数据.
- */
- private function getTreeParams(?array $requestData): array
- {
- if (isset($requestData['parent_id']) && $requestData['parent_id']) {
- // 查询父级信息
- $parentInfo = ES::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;
- }
- /**
- * 添加数据 最多允许的数量.
- */
- private function addTotalLimit($find): void
- {
- // 判断是否超出总数限制
- $totalSetting = $this->interfaceInfo['total_setting'] ?? [];
- if ($totalSetting) {
- $totalSetting = json_decode($totalSetting, true);
- if (2 === $totalSetting['type']) {
- $num = $totalSetting['num'] ?? 1;
- $total = $find->count();
- if ($total > $num) {
- abort(508, '对不起,最多只允许添加' . $num . '条信息!');
- }
- }
- }
- }
- }
|