Depart.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. namespace app\admin\controller\institution;
  3. use app\admin\model\Admin;
  4. use app\admin\model\AdminGroup;
  5. use app\admin\model\institution\Institution;
  6. use ba\Random;
  7. use think\facade\Cache;
  8. use think\facade\Db;
  9. use Throwable;
  10. use app\common\controller\Backend;
  11. use app\common\library\Upload;
  12. /**
  13. * 部门管理
  14. */
  15. class Depart extends Backend
  16. {
  17. /**
  18. * Depart模型对象
  19. * @var object
  20. * @phpstan-var \app\admin\model\institution\Depart
  21. */
  22. protected object $model;
  23. protected array|string $preExcludeFields = ['id', 'update_time'];
  24. protected array $withJoinTable = ['institution'];
  25. protected string|array $quickSearchField = ['id'];
  26. protected array $noNeedPermission = ['getDepart','getDepartUser'];
  27. protected array $noNeedLogin = [];
  28. public function initialize(): void
  29. {
  30. parent::initialize();
  31. $this->model = new \app\admin\model\institution\Depart();
  32. }
  33. /**
  34. * 查看
  35. * @throws Throwable
  36. */
  37. public function index(): void
  38. {
  39. // 如果是 select 则转发到 select 方法,若未重写该方法,其实还是继续执行 index
  40. if ($this->request->param('select')) {
  41. $this->select();
  42. }
  43. /**
  44. * 1. withJoin 不可使用 alias 方法设置表别名,别名将自动使用关联模型名称(小写下划线命名规则)
  45. * 2. 以下的别名设置了主表别名,同时便于拼接查询参数等
  46. * 3. paginate 数据集可使用链式操作 each(function($item, $key) {}) 遍历处理
  47. */
  48. list($where, $alias, $limit, $order) = $this->queryBuilder();
  49. $res = $this->model
  50. ->withJoin($this->withJoinTable, $this->withJoinType)
  51. ->alias($alias)
  52. ->where($where)
  53. ->order($order)
  54. ->paginate($limit);
  55. $res->visible(['institution' => ['name']]);
  56. $this->success('', [
  57. 'list' => $res->items(),
  58. 'total' => $res->total(),
  59. 'remark' => get_route_remark(),
  60. ]);
  61. }
  62. /**
  63. * 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
  64. */
  65. public function getDepart(): bool|string
  66. {
  67. $id = $this->request->post('id');
  68. $depart = $this->model->where('institution_id',$id)->field(['id','depart_code','depart_name as label','institution_id'])->select()->toArray();
  69. $this->success('success',$depart);
  70. }
  71. public function getDepartUser(): bool|string
  72. {
  73. $id = $this->request->post('id');
  74. $admin = Admin::where('depart_id',$id)->column('nickname as label,depart');
  75. $this->success('success',$admin);
  76. }
  77. public function importDepart(): void
  78. {
  79. set_time_limit(0);
  80. // 获取文件
  81. // $file = $this->request->request('file');
  82. $file = $this->request->file('file');
  83. if (!$file) {
  84. $this->error('上传文件错误');
  85. }
  86. $attachment = '';
  87. try {
  88. $upload = new Upload($file);
  89. $attachment = $upload->upload(null, $this->auth->id);
  90. unset($attachment['create_time'], $attachment['quote']);
  91. } catch (Throwable $e) {
  92. $this->error($e->getMessage());
  93. }
  94. if (!is_file($_SERVER['DOCUMENT_ROOT'].$attachment['url'])) {
  95. $this->error(__('No results were found'));
  96. }
  97. $url = $_SERVER['DOCUMENT_ROOT'].$attachment['url'];
  98. $allInstitution = Institution::cache(300)->column('id','institution_code');
  99. if (!(is_file($url))) {
  100. $this->error('文件获取失败');
  101. }
  102. // 读取内容
  103. $excel_data = read_excel($url);
  104. if (empty($excel_data)) {
  105. $this->error('请填写导入数据');
  106. }
  107. $data = [];
  108. foreach ($excel_data as $key=>$val) {
  109. foreach ($val as &$v) {
  110. $v = str_replace(' ', '', trim($v));
  111. $v = str_replace("\xC2\xA0", '', $v);
  112. }
  113. unset($v);
  114. $institution = $allInstitution[$val[2]] ?? '';
  115. $data[] = [
  116. 'depart_name'=>$val[0], //科室名称
  117. 'depart_code'=>$val[1], //科室编码
  118. 'institution_id'=>$institution, //所属机构
  119. ];
  120. }
  121. Cache::delete('allDepart');
  122. if(!empty($data))
  123. {
  124. $this->model->saveAll($data);
  125. }else{
  126. $this->error('导入失败,未读取到数据');
  127. }
  128. $this->success('导入成功');
  129. }
  130. public function importAdmin(): void
  131. {
  132. set_time_limit(0);
  133. // 获取文件
  134. // $file = $this->request->request('file');
  135. $file = $this->request->file('file');
  136. if (!$file) {
  137. $this->error('上传文件错误');
  138. }
  139. $attachment = '';
  140. try {
  141. $upload = new Upload($file);
  142. $attachment = $upload->upload(null, $this->auth->id);
  143. unset($attachment['create_time'], $attachment['quote']);
  144. } catch (Throwable $e) {
  145. $this->error($e->getMessage());
  146. }
  147. // $filePath = ROOT_PATH . DS . 'public' . DS . $file;
  148. if (!is_file($_SERVER['DOCUMENT_ROOT'].$attachment['url'])) {
  149. $this->error(__('No results were found'));
  150. }
  151. $url = $_SERVER['DOCUMENT_ROOT'].$attachment['url'];
  152. $allInstitution = Institution::cache(300)->column('id','institution_code');
  153. $allDepart = $this->model->cache('allDepart',300)->column('id','depart_code');
  154. // $url = $_SERVER['DOCUMENT_ROOT'].'/storage/default/20240807/user.xlsx';
  155. if (!(is_file($url))) {
  156. $this->error('文件获取失败');
  157. }
  158. // 读取内容
  159. $excel_data = read_excel($url);
  160. if (empty($excel_data)) {
  161. $this->error('请填写导入数据');
  162. }
  163. $data = [];
  164. $admin = new Admin();
  165. foreach ($excel_data as $key=>$val) {
  166. foreach ($val as &$v) {
  167. $v = str_replace(' ', '', trim($v));
  168. $v = str_replace("\xC2\xA0", '', $v);
  169. }
  170. unset($v);
  171. //加密盐
  172. $salt = Random::build('alnum', 16);
  173. //密码
  174. $passwd = encrypt_password($val[1], $salt);
  175. // 获取科室
  176. $depart = $allDepart[$val[3]] ?? '';
  177. // 获取机构
  178. $institution = $allInstitution[$val[4]] ?? '';
  179. if(empty($val[0]))
  180. {
  181. continue;
  182. }
  183. $data = [
  184. 'username'=>$val[0], //账号
  185. 'password'=>$passwd, //密码
  186. 'nickname'=>$val[2], //昵称
  187. 'salt'=>$salt,
  188. 'depart'=>$depart, //所属科室
  189. 'institution'=>$institution, //所属科室
  190. ];
  191. $adminId = $admin->insertGetId($data);
  192. if(!empty($val[5]))
  193. {
  194. $groupId = AdminGroup::where('name',$val[5])->value('id');
  195. Db::name('admin_group_access')->insert(['uid'=>$adminId,'group_id'=>$groupId]);
  196. }
  197. }
  198. $this->success('导入成功');
  199. }
  200. /**
  201. * 添加
  202. */
  203. public function add(): void
  204. {
  205. if ($this->request->isPost()) {
  206. $data = $this->request->post();
  207. if (!$data) {
  208. $this->error(__('Parameter %s can not be empty', ['']));
  209. }
  210. $data = $this->excludeFields($data);
  211. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  212. $data[$this->dataLimitField] = $this->auth->id;
  213. }
  214. $result = false;
  215. $this->model->startTrans();
  216. try {
  217. // 模型验证
  218. if ($this->modelValidate) {
  219. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  220. if (class_exists($validate)) {
  221. $validate = new $validate();
  222. if ($this->modelSceneValidate) $validate->scene('add');
  223. $validate->check($data);
  224. }
  225. }
  226. if(!empty($data['create_user_id']))
  227. {
  228. $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
  229. }
  230. $result = $this->model->save($data);
  231. $this->model->commit();
  232. Cache::delete('allDepart');
  233. } catch (Throwable $e) {
  234. $this->model->rollback();
  235. $this->error($e->getMessage());
  236. }
  237. if ($result !== false) {
  238. $this->success(__('Added successfully'));
  239. } else {
  240. $this->error(__('No rows were added'));
  241. }
  242. }
  243. $this->error(__('Parameter error'));
  244. }
  245. /**
  246. * 编辑
  247. * @throws Throwable
  248. */
  249. public function edit(): void
  250. {
  251. $pk = $this->model->getPk();
  252. $id = $this->request->param($pk);
  253. $row = $this->model->find($id);
  254. if (!$row) {
  255. $this->error(__('Record not found'));
  256. }
  257. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  258. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  259. $this->error(__('You have no permission'));
  260. }
  261. if ($this->request->isPost()) {
  262. $data = $this->request->post();
  263. if (!$data) {
  264. $this->error(__('Parameter %s can not be empty', ['']));
  265. }
  266. $data = $this->excludeFields($data);
  267. $result = false;
  268. $this->model->startTrans();
  269. try {
  270. // 模型验证
  271. if ($this->modelValidate) {
  272. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  273. if (class_exists($validate)) {
  274. $validate = new $validate();
  275. if ($this->modelSceneValidate) $validate->scene('edit');
  276. $data[$pk] = $row[$pk];
  277. $validate->check($data);
  278. }
  279. }
  280. if(!empty($data['create_user_id']))
  281. {
  282. $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
  283. }
  284. $result = $row->save($data);
  285. $this->model->commit();
  286. Cache::delete('allDepart');
  287. } catch (Throwable $e) {
  288. $this->model->rollback();
  289. $this->error($e->getMessage());
  290. }
  291. if ($result !== false) {
  292. $this->success(__('Update successful'));
  293. } else {
  294. $this->error(__('No rows updated'));
  295. }
  296. }
  297. $this->success('', [
  298. 'row' => $row
  299. ]);
  300. }
  301. }