|
@@ -3,7 +3,10 @@
|
|
|
namespace app\admin\controller\auto;
|
|
|
|
|
|
use app\common\controller\Backend;
|
|
|
+use think\Db;
|
|
|
use think\exception\HttpResponseException;
|
|
|
+use think\exception\PDOException;
|
|
|
+use think\exception\ValidateException;
|
|
|
use think\Response;
|
|
|
|
|
|
/**
|
|
@@ -37,13 +40,12 @@ class Commands extends Backend
|
|
|
public function getTypes()
|
|
|
{
|
|
|
$confList = config('site.general_config_cmd_types') ?? [];
|
|
|
- foreach ($confList as $key => $value)
|
|
|
- {
|
|
|
+ foreach ($confList as $key => $value) {
|
|
|
$list[] = ['id' => $key, 'name' => $value];
|
|
|
}
|
|
|
$result = [
|
|
|
- 'list' => $list ?? [],
|
|
|
- 'total' => count($list),
|
|
|
+ 'list' => $list ?? [],
|
|
|
+ 'total' => count($list),
|
|
|
];
|
|
|
$response = Response::create($result, 'json', 200);
|
|
|
throw new HttpResponseException($response);
|
|
@@ -61,4 +63,72 @@ class Commands extends Backend
|
|
|
$response = Response::create($result, 'json', 200);
|
|
|
throw new HttpResponseException($response);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加
|
|
|
+ *
|
|
|
+ * @return string
|
|
|
+ * @throws \think\Exception
|
|
|
+ */
|
|
|
+ public function addmulti()
|
|
|
+ {
|
|
|
+ if (false === $this->request->isPost()) {
|
|
|
+ return $this->view->fetch();
|
|
|
+ }
|
|
|
+ $params = $this->request->post('row/a');
|
|
|
+ if (empty($params)) {
|
|
|
+ $this->error(__('Parameter %s can not be empty', ''));
|
|
|
+ }
|
|
|
+ $params = $this->preExcludeFields($params);
|
|
|
+
|
|
|
+ if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
|
|
|
+ $params[$this->dataLimitField] = $this->auth->id;
|
|
|
+ }
|
|
|
+ $result = false;
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ $commands = trim($params['command']);
|
|
|
+ if (empty($commands)) {
|
|
|
+ throw new \Exception('命令不能为空');
|
|
|
+ }
|
|
|
+ $commandList = explode("\r\n", $commands);
|
|
|
+ $dataList = [];
|
|
|
+ foreach ($commandList as $key => $commend) {
|
|
|
+ $colonPos = strpos($commend, ':');
|
|
|
+ $hashPos = strrpos($commend, '#');
|
|
|
+ $subPos = strrpos($commend, '-');
|
|
|
+ $type_id = trim(substr($commend, 0, $colonPos));
|
|
|
+ $command = trim(substr($commend, $colonPos + 1, $hashPos - $colonPos - 1));
|
|
|
+ $note = trim(substr($commend, $hashPos + 1, $subPos - $hashPos - 1));
|
|
|
+ $cmd_tags = trim(substr($commend, $subPos + 1));
|
|
|
+ $dataList[] = [
|
|
|
+ 'type_id' => $type_id,
|
|
|
+ 'command' => $command,
|
|
|
+ 'note' => $note,
|
|
|
+ 'cmd_tags' => $cmd_tags,
|
|
|
+ 'full_text' => $commend,
|
|
|
+ 'weigh' => $params['weigh'],
|
|
|
+ 'status' => $params['status'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $commandArr = array_column($dataList, 'command');
|
|
|
+ $existsData = $this->model->where('command', 'in', $commandArr)->column('command');
|
|
|
+ $newData = array_filter($dataList, function ($item) use ($existsData) {
|
|
|
+ return !in_array($item['command'], $existsData);
|
|
|
+ });
|
|
|
+ if (!empty($newData)) {
|
|
|
+ $result = $this->model->saveAll($newData);
|
|
|
+ } else {
|
|
|
+ throw new \Exception('数据已存在,无需添加');
|
|
|
+ }
|
|
|
+ Db::commit();
|
|
|
+ } catch (ValidateException|PDOException|\Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage());
|
|
|
+ }
|
|
|
+ if ($result === false) {
|
|
|
+ $this->error(__('No rows were inserted'));
|
|
|
+ }
|
|
|
+ $this->success();
|
|
|
+ }
|
|
|
}
|