123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?php
- namespace app\admin\controller\institution;
- use app\admin\model\Admin;
- use app\admin\model\AdminGroup;
- use app\admin\model\institution\Institution;
- use ba\Random;
- use think\facade\Cache;
- use think\facade\Db;
- use Throwable;
- use app\common\controller\Backend;
- use app\common\library\Upload;
- /**
- * 部门管理
- */
- class Depart extends Backend
- {
- /**
- * Depart模型对象
- * @var object
- * @phpstan-var \app\admin\model\institution\Depart
- */
- protected object $model;
- protected array|string $preExcludeFields = ['id', 'update_time'];
- protected array $withJoinTable = ['institution'];
- protected string|array $quickSearchField = ['id'];
- protected array $noNeedPermission = ['getDepart','getDepartUser'];
- protected array $noNeedLogin = [];
- public function initialize(): void
- {
- parent::initialize();
- $this->model = new \app\admin\model\institution\Depart();
- }
- /**
- * 查看
- * @throws Throwable
- */
- public function index(): void
- {
- // 如果是 select 则转发到 select 方法,若未重写该方法,其实还是继续执行 index
- if ($this->request->param('select')) {
- $this->select();
- }
- /**
- * 1. withJoin 不可使用 alias 方法设置表别名,别名将自动使用关联模型名称(小写下划线命名规则)
- * 2. 以下的别名设置了主表别名,同时便于拼接查询参数等
- * 3. paginate 数据集可使用链式操作 each(function($item, $key) {}) 遍历处理
- */
- list($where, $alias, $limit, $order) = $this->queryBuilder();
- $res = $this->model
- ->withJoin($this->withJoinTable, $this->withJoinType)
- ->alias($alias)
- ->where($where)
- ->order($order)
- ->paginate($limit);
- $res->visible(['institution' => ['name']]);
- $this->success('', [
- 'list' => $res->items(),
- 'total' => $res->total(),
- 'remark' => get_route_remark(),
- ]);
- }
- /**
- * 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
- */
- public function getDepart(): bool|string
- {
- $id = $this->request->post('id');
- $depart = $this->model->where('institution_id',$id)->field(['id','depart_code','depart_name as label','institution_id'])->select()->toArray();
- $this->success('success',$depart);
- }
- public function getDepartUser(): bool|string
- {
- $id = $this->request->post('id');
- $admin = Admin::where('depart_id',$id)->column('nickname as label,depart');
- $this->success('success',$admin);
- }
- public function importDepart(): void
- {
- set_time_limit(0);
- // 获取文件
- // $file = $this->request->request('file');
- $file = $this->request->file('file');
- if (!$file) {
- $this->error('上传文件错误');
- }
- $attachment = '';
- try {
- $upload = new Upload($file);
- $attachment = $upload->upload(null, $this->auth->id);
- unset($attachment['create_time'], $attachment['quote']);
- } catch (Throwable $e) {
- $this->error($e->getMessage());
- }
- if (!is_file($_SERVER['DOCUMENT_ROOT'].$attachment['url'])) {
- $this->error(__('No results were found'));
- }
- $url = $_SERVER['DOCUMENT_ROOT'].$attachment['url'];
- $allInstitution = Institution::cache(300)->column('id','institution_code');
- if (!(is_file($url))) {
- $this->error('文件获取失败');
- }
- // 读取内容
- $excel_data = read_excel($url);
- if (empty($excel_data)) {
- $this->error('请填写导入数据');
- }
- $data = [];
- foreach ($excel_data as $key=>$val) {
- foreach ($val as &$v) {
- $v = str_replace(' ', '', trim($v));
- $v = str_replace("\xC2\xA0", '', $v);
- }
- unset($v);
- $institution = $allInstitution[$val[2]] ?? '';
- $data[] = [
- 'depart_name'=>$val[0], //科室名称
- 'depart_code'=>$val[1], //科室编码
- 'institution_id'=>$institution, //所属机构
- ];
- }
- Cache::delete('allDepart');
- if(!empty($data))
- {
- $this->model->saveAll($data);
- }else{
- $this->error('导入失败,未读取到数据');
- }
- $this->success('导入成功');
- }
- public function importAdmin(): void
- {
- set_time_limit(0);
- // 获取文件
- // $file = $this->request->request('file');
- $file = $this->request->file('file');
- if (!$file) {
- $this->error('上传文件错误');
- }
- $attachment = '';
- try {
- $upload = new Upload($file);
- $attachment = $upload->upload(null, $this->auth->id);
- unset($attachment['create_time'], $attachment['quote']);
- } catch (Throwable $e) {
- $this->error($e->getMessage());
- }
- // $filePath = ROOT_PATH . DS . 'public' . DS . $file;
- if (!is_file($_SERVER['DOCUMENT_ROOT'].$attachment['url'])) {
- $this->error(__('No results were found'));
- }
- $url = $_SERVER['DOCUMENT_ROOT'].$attachment['url'];
- $allInstitution = Institution::cache(300)->column('id','institution_code');
- $allDepart = $this->model->cache('allDepart',300)->column('id','depart_code');
- // $url = $_SERVER['DOCUMENT_ROOT'].'/storage/default/20240807/user.xlsx';
- if (!(is_file($url))) {
- $this->error('文件获取失败');
- }
- // 读取内容
- $excel_data = read_excel($url);
- if (empty($excel_data)) {
- $this->error('请填写导入数据');
- }
- $data = [];
- $admin = new Admin();
- foreach ($excel_data as $key=>$val) {
- foreach ($val as &$v) {
- $v = str_replace(' ', '', trim($v));
- $v = str_replace("\xC2\xA0", '', $v);
- }
- unset($v);
- //加密盐
- $salt = Random::build('alnum', 16);
- //密码
- $passwd = encrypt_password($val[1], $salt);
- // 获取科室
- $depart = $allDepart[$val[3]] ?? '';
- // 获取机构
- $institution = $allInstitution[$val[4]] ?? '';
- if(empty($val[0]))
- {
- continue;
- }
- $data = [
- 'username'=>$val[0], //账号
- 'password'=>$passwd, //密码
- 'nickname'=>$val[2], //昵称
- 'salt'=>$salt,
- 'depart'=>$depart, //所属科室
- 'institution'=>$institution, //所属科室
- ];
- $adminId = $admin->insertGetId($data);
- if(!empty($val[5]))
- {
- $groupId = AdminGroup::where('name',$val[5])->value('id');
- Db::name('admin_group_access')->insert(['uid'=>$adminId,'group_id'=>$groupId]);
- }
- }
- $this->success('导入成功');
- }
- /**
- * 添加
- */
- public function add(): void
- {
- if ($this->request->isPost()) {
- $data = $this->request->post();
- if (!$data) {
- $this->error(__('Parameter %s can not be empty', ['']));
- }
- $data = $this->excludeFields($data);
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $data[$this->dataLimitField] = $this->auth->id;
- }
- $result = false;
- $this->model->startTrans();
- try {
- // 模型验证
- if ($this->modelValidate) {
- $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- if (class_exists($validate)) {
- $validate = new $validate();
- if ($this->modelSceneValidate) $validate->scene('add');
- $validate->check($data);
- }
- }
- if(!empty($data['create_user_id']))
- {
- $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
- }
- $result = $this->model->save($data);
- $this->model->commit();
- Cache::delete('allDepart');
- } catch (Throwable $e) {
- $this->model->rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success(__('Added successfully'));
- } else {
- $this->error(__('No rows were added'));
- }
- }
- $this->error(__('Parameter error'));
- }
- /**
- * 编辑
- * @throws Throwable
- */
- public function edit(): void
- {
- $pk = $this->model->getPk();
- $id = $this->request->param($pk);
- $row = $this->model->find($id);
- if (!$row) {
- $this->error(__('Record not found'));
- }
- $dataLimitAdminIds = $this->getDataLimitAdminIds();
- if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
- $this->error(__('You have no permission'));
- }
- if ($this->request->isPost()) {
- $data = $this->request->post();
- if (!$data) {
- $this->error(__('Parameter %s can not be empty', ['']));
- }
- $data = $this->excludeFields($data);
- $result = false;
- $this->model->startTrans();
- try {
- // 模型验证
- if ($this->modelValidate) {
- $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- if (class_exists($validate)) {
- $validate = new $validate();
- if ($this->modelSceneValidate) $validate->scene('edit');
- $data[$pk] = $row[$pk];
- $validate->check($data);
- }
- }
- if(!empty($data['create_user_id']))
- {
- $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
- }
- $result = $row->save($data);
- $this->model->commit();
- Cache::delete('allDepart');
- } catch (Throwable $e) {
- $this->model->rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success(__('Update successful'));
- } else {
- $this->error(__('No rows updated'));
- }
- }
- $this->success('', [
- 'row' => $row
- ]);
- }
- }
|