diff --git a/application/admin/controller/user4s/Import.php b/application/admin/controller/user4s/Import.php index 88e3bae..3349d89 100644 --- a/application/admin/controller/user4s/Import.php +++ b/application/admin/controller/user4s/Import.php @@ -5,6 +5,7 @@ namespace app\admin\controller\user4s; use app\common\controller\Backend; use app\common\model\user4s\Category; use app\common\model\user4s\Level; +use app\common\model\user4s\User; use think\Config; use think\Db; @@ -52,6 +53,7 @@ class Import extends Backend { parent::_initialize(); $prefix = Config::get('database.prefix'); + $this->model = new User(); $this->level_model = new \app\common\model\user4s\Level; $this->all_levels = $this->level_model->where('status', 1)->column('id,name'); @@ -77,14 +79,14 @@ class Import extends Backend { if ($this->request->isPost()) { $params = $this->request->post("row/a"); - $step = $params['step']; + //$step = $params['step']; if ($params) { $params = $this->preExcludeFields($params); if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } $result = false; - Db::startTrans(); + $this->model->startTrans(); try { //是否采用模型验证 // if ($this->modelValidate) { @@ -104,9 +106,9 @@ class Import extends Backend $fileData['params'] = http_build_query($params); $fileData['newtable'] = $params['newtable']; - if (!$step) { - $this->success('匹配到' . $fileData['count'] . '列,开始预览', '', $fileData); - } + // if (!$step) { + // $this->success('匹配到' . $fileData['count'] . '列,开始预览', '', $fileData); + // } $insert = $fileData['insert']; $fieldArr = $fileData['fieldArr']; //是否包含admin_id字段 @@ -156,43 +158,56 @@ class Import extends Backend } $val['level_id'] = $this->all_levels_name[$val['level_id']]; } - var_dump($insert);exit(); + //var_dump($insert); $prefix = Config::get('database.prefix'); $count = 0; - if ($params['update']) { - foreach ($insert as &$val) { - $count += Db::name('user4s_user') - ->where($params['update'], $val['pid']) - ->update($val); + // if ($params['update']) { + // foreach ($insert as &$val) { + // $count += $this->model->name('user4s_user') + // ->where($params['update'], $val['pid']) + // ->update($val); + // } + // } else { + // $res = $this->model->name('user4s_user')->insertAll($insert); + // $count = count($insert); + // } + + $res = $this->model->saveAll($insert); + foreach ($res as $key => $val) { + if ($params['need_visit'] == 1) { + $log_res = $this->model->afterlog($this->auth->id, $val, $val->id, '', true); + } else { + $log_res = $this->model->afterlog($this->auth->id, $val, $val->id, '', false); + } + //var_dump($log_res); + if (!$log_res) { + $this->model->rollback(); } - } else { - $res = Db::name('user4s_user')->insertAll($insert); - $count = count($insert); } - - - Db::commit(); + $count = count($insert); + //var_dump($res);exit(); + $this->model->commit(); } catch (ValidateException $e) { - Db::rollback(); + $this->model->rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { - Db::rollback(); + $this->model->rollback(); $this->error($e->getMessage()); } catch (Exception $e) { - Db::rollback(); + $this->model->rollback(); $this->error($e->getMessage()); } if ($count !== false) { - $params['status'] = 'normal'; - $result = $this->model->allowField(true)->save($params); - $tip = $params['update'] ? '成功更新' : '成功新增'; - $this->success($tip . $count . '条记录', '', array('count' => $count)); + // $params['status'] = 1; + // $result = $this->model->allowField(true)->save($params); + // $tip = $params['update'] ? '成功更新' : '成功新增'; + $this->success('导入成功' . $count . '条记录', '', array('count' => $count)); } else { - $this->error(__('No rows were inserted')); + $this->error(__('没有数据写入')); } } - $this->error(__('Parameter %s can not be empty', '')); + $this->error(__(' %s 不能为空', '')); } $this->view->assign("update", $this->request->request('update')); $this->view->assign("to", $this->request->request('to')); @@ -221,21 +236,21 @@ class Import extends Backend if ($params) { $params = $this->preExcludeFields($params); $result = false; - Db::startTrans(); + $this->model->startTrans(); try { $params['newtable'] = ''; $fileData = $this->fileData($params); $fileData['params'] = http_build_query($params); $this->success('匹配到' . $fileData['count'] . '列,开始预览', '', $fileData); - Db::commit(); + $this->model->commit(); } catch (ValidateException $e) { - Db::rollback(); + $this->model->rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { - Db::rollback(); + $this->model->rollback(); $this->error($e->getMessage()); } catch (Exception $e) { - Db::rollback(); + $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { @@ -341,7 +356,7 @@ class Import extends Backend $notnull = []; if (!$params['newtable']) { - $pk = Db::getTableInfo($table, 'pk'); + $pk = $this->model->getTableInfo($table, 'pk'); $list = db()->query( "SELECT COLUMN_NAME,COLUMN_COMMENT,COLUMN_TYPE,IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database] diff --git a/application/admin/controller/user4s/User.php b/application/admin/controller/user4s/User.php index 00ae73d..d4cd093 100644 --- a/application/admin/controller/user4s/User.php +++ b/application/admin/controller/user4s/User.php @@ -572,134 +572,5 @@ class User extends Backend } return $this->view->fetch(); } - /** - * 导入数据界面 - */ - public function excel() - { - if ($this->request->isPost()) { - $params = $this->request->post("row/a"); - $step = $params['step']; - if ($params) { - $params = $this->preExcludeFields($params); - if ($this->dataLimit && $this->dataLimitFieldAutoFill) { - $params[$this->dataLimitField] = $this->auth->id; - } - $result = false; - Db::startTrans(); - try { - //是否采用模型验证 - if ($this->modelValidate) { - $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); - $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; - $this->model->validateFailException(true)->validate($validate); - } - //新建表导入处理 - if ($params['newtable']) { - $prefix = Config::get('database.prefix'); - $table = $prefix . $params['newtable']; - $check = db()->query("SHOW TABLES LIKE '%{$table}%';"); - if ($check) { - $this->error(__($params['newtable'] . '表已经存在')); - } - } else { - if (!$params['table']) $this->error('未选择目标表'); - } - $fileData = $this->fileData($params); - - - $fileData['params'] = http_build_query($params); - $fileData['newtable'] = $params['newtable']; - if (!$step) { - $this->success('匹配到' . $fileData['count'] . '列,开始预览', '', $fileData); - } - $insert = $fileData['insert']; - $fieldArr = $fileData['fieldArr']; - //是否包含admin_id字段 - $has_admin_id = false; - foreach ($fieldArr as $name => $key) { - if ($key == 'admin_id') { - $has_admin_id = true; - break; - } - } - if ($has_admin_id) { - foreach ($insert as &$val) { - if (!isset($val['admin_id']) || empty($val['admin_id'])) { - $val['admin_id'] = $this->auth->isLogin() ? $this->auth->id : 0; - } - } - } - $prefix = Config::get('database.prefix'); - $count = 0; - if ($params['update']) { - foreach ($insert as &$val) { - $count += Db::name(str_replace($prefix, "", $params['table'])) - ->where($params['update'], $val['pid']) - ->update($val); - } - } else { - if ($params['to']) { - $file = db('attachment')->where('url', $fileData['path'])->find(); - $this->fieldModel = new \app\admin\model\salary\Field; - $fields = $this->fieldModel->where('name', 'not in', ['pid', 'name', 'status', 'create_time', 'update_time', 'deletetime'])->select(); - $toData = []; - // dump($fields); - $insertData = []; - foreach ($insert as $key => $val) { - foreach ($fields as $ke => $field) { - if (isset($val[$field['name']])) { - $toData[$ke]['pid'] = $val['pid']; - $toData[$ke]['name'] = $val['name']; - $toData[$ke]['type'] = $field['name']; - $toData[$ke]['type_name'] = $field['desc']; - $toData[$ke]['field_type'] = $field['type']; - $toData[$ke]['je'] = $val[$field['name']]; - $toData[$ke]['filename'] = $file['filename']; - $toData[$ke]['sha1'] = $file['sha1']; - $toData[$ke]['createtime'] = time(); - } - } - if ($insertData) $insertData = array_merge($insertData, $toData); - else $insertData = $toData; - } - // dump($insertData); - // exit; - Db::name(str_replace($prefix, "", $params['to']))->where('sha1', $file['sha1'])->delete(); - $res = Db::name(str_replace($prefix, "", $params['to']))->insertAll($insertData); - } else { - $res = Db::name(str_replace($prefix, "", $params['table']))->insertAll($insert); - } - $count = count($insert); - } - - - Db::commit(); - } catch (ValidateException $e) { - Db::rollback(); - $this->error($e->getMessage()); - } catch (PDOException $e) { - Db::rollback(); - $this->error($e->getMessage()); - } catch (Exception $e) { - Db::rollback(); - $this->error($e->getMessage()); - } - if ($count !== false) { - $params['status'] = 'normal'; - $result = $this->model->allowField(true)->save($params); - $tip = $params['update'] ? '成功更新' : '成功新增'; - $this->success($tip . $count . '条记录', '', array('count' => $count)); - } else { - $this->error(__('No rows were inserted')); - } - } - - $this->error(__('Parameter %s can not be empty', '')); - } - $this->view->assign("update", $this->request->request('update')); - $this->view->assign("to", $this->request->request('to')); - return $this->view->fetch(); - } } diff --git a/application/admin/view/user4s/import/index.html b/application/admin/view/user4s/import/index.html index 1391c30..9c93675 100644 --- a/application/admin/view/user4s/import/index.html +++ b/application/admin/view/user4s/import/index.html @@ -17,9 +17,10 @@ -->