Depart.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. $list = $res->items();
  57. $list = (json_decode(json_encode($list), true));
  58. $arr = [];
  59. foreach ($list as $k=>$v) {
  60. foreach ($v as $kk => $vv) {
  61. $arr[$k][strtolower($kk)] = $vv;
  62. }
  63. }
  64. $this->success('', [
  65. 'list' => $list,
  66. 'total' => $res->total(),
  67. 'remark' => get_route_remark(),
  68. ]);
  69. }
  70. /**
  71. * 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
  72. */
  73. public function getDepart(): bool|string
  74. {
  75. $id = $this->request->post('id');
  76. $depart = $this->model->where('institution_id',$id)->field(['id','depart_code','depart_name as label','institution_id'])->select()->toArray();
  77. $this->success('success',$depart);
  78. }
  79. public function getDepartUser(): bool|string
  80. {
  81. $id = $this->request->post('id');
  82. $admin = Admin::where('depart_id',$id)->column('nickname as label,depart');
  83. $this->success('success',$admin);
  84. }
  85. public function importDepart(): void
  86. {
  87. set_time_limit(0);
  88. // 获取文件
  89. // $file = $this->request->request('file');
  90. $file = $this->request->file('file');
  91. if (!$file) {
  92. $this->error('上传文件错误');
  93. }
  94. $attachment = '';
  95. try {
  96. $upload = new Upload($file);
  97. $attachment = $upload->upload(null, $this->auth->id);
  98. unset($attachment['create_time'], $attachment['quote']);
  99. } catch (Throwable $e) {
  100. $this->error($e->getMessage());
  101. }
  102. if (!is_file($_SERVER['DOCUMENT_ROOT'].$attachment['url'])) {
  103. $this->error(__('No results were found'));
  104. }
  105. $url = $_SERVER['DOCUMENT_ROOT'].$attachment['url'];
  106. $allInstitution = Institution::cache(300)->column('id','institution_code');
  107. if (!(is_file($url))) {
  108. $this->error('文件获取失败');
  109. }
  110. // 读取内容
  111. $excel_data = read_excel($url);
  112. if (empty($excel_data)) {
  113. $this->error('请填写导入数据');
  114. }
  115. $data = [];
  116. foreach ($excel_data as $key=>$val) {
  117. foreach ($val as &$v) {
  118. $v = str_replace(' ', '', trim($v));
  119. $v = str_replace("\xC2\xA0", '', $v);
  120. }
  121. unset($v);
  122. $institution = $allInstitution[$val[2]] ?? '';
  123. $data[] = [
  124. 'depart_name'=>$val[0], //科室名称
  125. 'depart_code'=>$val[1], //科室编码
  126. 'institution_id'=>$institution, //所属机构
  127. ];
  128. }
  129. Cache::delete('allDepart');
  130. if(!empty($data))
  131. {
  132. $this->model->saveAll($data);
  133. }else{
  134. $this->error('导入失败,未读取到数据');
  135. }
  136. $this->success('导入成功');
  137. }
  138. public function importAdmin(): void
  139. {
  140. set_time_limit(0);
  141. // 获取文件
  142. // $file = $this->request->request('file');
  143. $file = $this->request->file('file');
  144. if (!$file) {
  145. $this->error('上传文件错误');
  146. }
  147. $attachment = '';
  148. try {
  149. $upload = new Upload($file);
  150. $attachment = $upload->upload(null, $this->auth->id);
  151. unset($attachment['create_time'], $attachment['quote']);
  152. } catch (Throwable $e) {
  153. $this->error($e->getMessage());
  154. }
  155. // $filePath = ROOT_PATH . DS . 'public' . DS . $file;
  156. if (!is_file($_SERVER['DOCUMENT_ROOT'].$attachment['url'])) {
  157. $this->error(__('No results were found'));
  158. }
  159. $url = $_SERVER['DOCUMENT_ROOT'].$attachment['url'];
  160. $allInstitution = Institution::cache(300)->column('id','institution_code');
  161. $allDepart = $this->model->cache('allDepart',300)->column('id','depart_code');
  162. // $url = $_SERVER['DOCUMENT_ROOT'].'/storage/default/20240807/user.xlsx';
  163. if (!(is_file($url))) {
  164. $this->error('文件获取失败');
  165. }
  166. // 读取内容
  167. $excel_data = read_excel($url);
  168. if (empty($excel_data)) {
  169. $this->error('请填写导入数据');
  170. }
  171. $data = [];
  172. $admin = new Admin();
  173. foreach ($excel_data as $key=>$val) {
  174. foreach ($val as &$v) {
  175. $v = str_replace(' ', '', trim($v));
  176. $v = str_replace("\xC2\xA0", '', $v);
  177. }
  178. unset($v);
  179. //加密盐
  180. $salt = Random::build('alnum', 16);
  181. //密码
  182. $passwd = encrypt_password($val[1], $salt);
  183. // 获取科室
  184. $depart = $allDepart[$val[3]] ?? '';
  185. // 获取机构
  186. $institution = $allInstitution[$val[4]] ?? '';
  187. if(empty($val[0]))
  188. {
  189. continue;
  190. }
  191. $data = [
  192. 'username'=>$val[0], //账号
  193. 'password'=>$passwd, //密码
  194. 'nickname'=>$val[2], //昵称
  195. 'salt'=>$salt,
  196. 'depart'=>$depart, //所属科室
  197. 'institution'=>$institution, //所属科室
  198. ];
  199. $adminId = $admin->insertGetId($data);
  200. if(!empty($val[5]))
  201. {
  202. $groupId = AdminGroup::where('name',$val[5])->value('id');
  203. Db::name('admin_group_access')->insert(['uid'=>$adminId,'group_id'=>$groupId]);
  204. }
  205. }
  206. $this->success('导入成功');
  207. }
  208. /**
  209. * 添加
  210. */
  211. public function add(): void
  212. {
  213. if ($this->request->isPost()) {
  214. $data = $this->request->post();
  215. if (!$data) {
  216. $this->error(__('Parameter %s can not be empty', ['']));
  217. }
  218. $data = $this->excludeFields($data);
  219. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  220. $data[$this->dataLimitField] = $this->auth->id;
  221. }
  222. $result = false;
  223. $this->model->startTrans();
  224. try {
  225. // 模型验证
  226. if ($this->modelValidate) {
  227. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  228. if (class_exists($validate)) {
  229. $validate = new $validate();
  230. if ($this->modelSceneValidate) $validate->scene('add');
  231. $validate->check($data);
  232. }
  233. }
  234. if(!empty($data['create_user_id']))
  235. {
  236. $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
  237. }
  238. $result = $this->model->save($data);
  239. $this->model->commit();
  240. Cache::delete('allDepart');
  241. } catch (Throwable $e) {
  242. $this->model->rollback();
  243. $this->error($e->getMessage());
  244. }
  245. if ($result !== false) {
  246. $this->success(__('Added successfully'));
  247. } else {
  248. $this->error(__('No rows were added'));
  249. }
  250. }
  251. $this->error(__('Parameter error'));
  252. }
  253. /**
  254. * 编辑
  255. * @throws Throwable
  256. */
  257. public function edit(): void
  258. {
  259. $pk = $this->model->getPk();
  260. $id = $this->request->param($pk);
  261. $row = $this->model->find($id);
  262. if (!$row) {
  263. $this->error(__('Record not found'));
  264. }
  265. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  266. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  267. $this->error(__('You have no permission'));
  268. }
  269. if ($this->request->isPost()) {
  270. $data = $this->request->post();
  271. if (!$data) {
  272. $this->error(__('Parameter %s can not be empty', ['']));
  273. }
  274. $data = $this->excludeFields($data);
  275. $result = false;
  276. $this->model->startTrans();
  277. try {
  278. // 模型验证
  279. if ($this->modelValidate) {
  280. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  281. if (class_exists($validate)) {
  282. $validate = new $validate();
  283. if ($this->modelSceneValidate) $validate->scene('edit');
  284. $data[$pk] = $row[$pk];
  285. $validate->check($data);
  286. }
  287. }
  288. if(!empty($data['create_user_id']))
  289. {
  290. $data['create_user'] = Admin::where('id',$data['create_user_id'])->value('nickname');
  291. }
  292. $result = $row->save($data);
  293. $this->model->commit();
  294. Cache::delete('allDepart');
  295. } catch (Throwable $e) {
  296. $this->model->rollback();
  297. $this->error($e->getMessage());
  298. }
  299. if ($result !== false) {
  300. $this->success(__('Update successful'));
  301. } else {
  302. $this->error(__('No rows updated'));
  303. }
  304. }
  305. $this->success('', [
  306. 'row' => $row
  307. ]);
  308. }
  309. }