Insdoctors.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. namespace app\manage\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use think\Session;
  6. use think\Config;
  7. use think\Cookie;
  8. use think\Request;
  9. use app\common\library\SysLogs;
  10. use app\common\library\UUIDs;
  11. class Insdoctors extends Base {
  12. public function index() {
  13. return $this->fetch();
  14. }
  15. public function datas() {
  16. // 只查本机构的
  17. // 拿到机构ID
  18. $manager = $admin = Session::get('session_manager');
  19. $insId = $manager["institution_id"];
  20. if (empty($insId)) {
  21. echo "[]";
  22. return;
  23. }
  24. $request = Request::instance()->param();
  25. $username = isset($request["username"]) ? $request["username"] : null;
  26. $status = isset($request["status"]) ? $request["status"] : null;
  27. $institution_id = $insId;
  28. $whereArr = array();
  29. if (!empty($username)) {
  30. $whereArr["username"] = array("like", $username . "%");
  31. }
  32. if ($status != null) {
  33. $whereArr["status"] = $status;
  34. }
  35. $whereArr["institution_id"] = $institution_id;
  36. $page = empty($_GET["page"]) ? 1 : $_GET["page"];
  37. $pagesize = empty($_GET["rows"]) ? 1 : $_GET["rows"];
  38. if (empty($page) || $page < 1) {
  39. $page = 1;
  40. }
  41. if (empty($pagesize) || $pagesize < 1) {
  42. $pagesize = 30;
  43. }
  44. $info = DB::table('doctors')->where($whereArr)->page($page, $pagesize)->select();
  45. foreach ($info as $k => $v) {
  46. $iname = DB::table('institution')->where('id', $v['institution_id'])->field('name')->find();
  47. $info[$k]['institution_name'] = $iname['name'];
  48. $dname = DB::table('department')->where('id', $v['department_id'])->field('department_name')->find();
  49. $info[$k]['department_name'] = $dname['department_name'];
  50. }
  51. $num = DB::table('doctors')->where($whereArr)->count();
  52. $data = array();
  53. $data['total'] = $num;
  54. $data['rows'] = $info;
  55. echo json_encode($data);
  56. }
  57. /**
  58. * 编辑窗口
  59. * @return type
  60. */
  61. public function edit() {
  62. // 医院的管理员,只能处理本机构的记录
  63. $manager = $admin = Session::get('session_manager');
  64. $insId = $manager["institution_id"];
  65. if (empty($insId)) {
  66. echo "not login ?";
  67. return;
  68. }
  69. $special = DB::table('special_remote')
  70. ->where('status',1)
  71. ->field('id,name,0 as sc_status')
  72. ->select();
  73. if (isset($_GET["id"])) {
  74. $id = $_GET["id"];
  75. if ($id != null) {
  76. $doctors = Db::table("doctors")->where("id", $id)->find();
  77. if (count($doctors) > 0) {
  78. if (empty($doctors['doctor_role'])) {
  79. $doctors['doctor_role'] = array();
  80. } else {
  81. $doctors['doctor_role'] = explode(',', $doctors['doctor_role']);
  82. }
  83. $this->assign("doctors", $doctors);
  84. $exam_class = explode(',',$doctors['exam_class']);
  85. $this->assign('examcla',$exam_class);
  86. // 查询医生的分类
  87. $doctorsCla = Db::table("doctor_class")->where("doctor_id", $id)->find();
  88. if (!empty($doctorsCla)) {
  89. $doctorClaStr = $doctorsCla["doctor_class"];
  90. if (!empty($doctorClaStr)) {
  91. $dc_arr = explode(",", $doctorClaStr);
  92. $this->assign("doctorcla", $dc_arr);
  93. }
  94. }
  95. // 医生的exam报告类权限
  96. }
  97. }
  98. $special = DB::table('special_remote')
  99. ->alias('sr')
  100. ->join(['special_contact'=>'sc'],'sc.did=\''.$id.'\' and sc.sid=sr.id','left')
  101. ->where('sr.status',1)
  102. ->field('sr.id,sr.name,sc.status as sc_status')
  103. ->select();
  104. }
  105. $this->assign('special', $special);
  106. //
  107. $examCla = DB::table('constant')->where('parent_id','exam_class')->select();
  108. $this->assign('examclass',$examCla);
  109. $doctorClas = Db::table("constant")->where("parent_id", "doctor_class")->order("ordernum", "1")->select();
  110. $this->assign('doctorclas', $doctorClas);
  111. $institution = DB::table('institution')->where("parent_institution", $insId)->whereOr("id", $insId)->select();
  112. $this->assign('institution', $institution);
  113. // 按机构查询科室
  114. $department = DB::table('department')->where("institution_id", $insId)->select();
  115. $this->assign('department', $department);
  116. return $this->fetch('edit');
  117. }
  118. public function save() {
  119. // 医院的管理员,只能处理本机构的记录
  120. $manager = $admin = Session::get('session_manager');
  121. $insId = $manager["institution_id"];
  122. if (empty($insId)) {
  123. echo "fail";
  124. return;
  125. }
  126. $info = $_GET;
  127. $data = $_GET;
  128. //预创建医生id
  129. $id = UUIDs::uuid16();
  130. if(isset($data['id']) && !empty($data['id'])){
  131. $id = $data['id'];
  132. }
  133. unset($info['special_name']);
  134. foreach ($data['special_name'] as $v){
  135. unset($info['special_'.$v]);
  136. $special_contact = DB::table('special_contact')->where('did',$id)->where('sid',$v)->find();
  137. if($data['special_'.$v] == '1'){
  138. //存在医生则查找
  139. if(empty($special_contact)){
  140. $sort = DB::table('special_contact')->max('sort');
  141. //不存在 创建信息
  142. $special = [
  143. 'sid'=>$v,
  144. 'did'=>$id,
  145. 'hid'=>$info['institution_id'],
  146. 'describe'=>'',
  147. 'sort'=>$sort+10,
  148. 'status'=>1,
  149. 'cost'=>0
  150. ];
  151. DB::table('special_contact')->insert($special);
  152. }else{
  153. //存在 修改信息
  154. DB::table('special_contact')->where('did',$info['id'])->where('sid',$v)->update(['status'=>1]);
  155. }
  156. }else{
  157. if(!empty($special_contact)){
  158. DB::table('special_contact')->where('did',$id)->where('sid',$v)->update(['status'=>0]);
  159. }
  160. }
  161. }
  162. unset($info['doctorcla']);
  163. if(isset($_GET['doctor_role']) && !empty($_GET['doctor_role'])){
  164. $info['doctor_role'] = implode(',', $_GET['doctor_role']);
  165. }
  166. // $info['doctor_role'] = implode(',', $_GET['doctor_role']);
  167. $info['password'] = md5($_GET['password']);
  168. $info["institution_id"] = $insId;
  169. if(isset($info['examcla'])){
  170. $info['exam_class'] = implode(',',$info['examcla']);
  171. unset($info['examcla']);
  172. }
  173. if (empty($_GET['id'])) {
  174. unset($_GET['id']);
  175. $info['id'] = $id;
  176. $a = DB::table('doctors')->insert($info);
  177. SysLogs::log("doctors", "C", json_encode($info));
  178. $this->saveDoctorCla($id,$_GET['doctorcla']);
  179. return 'insert_ok;' . $id;
  180. } else {
  181. $a = DB::table('doctors')->where('id', $_GET['id'])->where("institution_id", $insId)->update($info);
  182. SysLogs::log("doctors", "U", $_GET['id'] . " --> " . json_encode($info));
  183. $this->saveDoctorCla($_GET['id'],$_GET['doctorcla']);
  184. return 'success';
  185. }
  186. }
  187. protected function saveDoctorCla($doctor_id, $doctorcla) {
  188. // doctorcla
  189. Db::table("doctor_class")->where("doctor_id", $doctor_id)->delete();
  190. if (isset($doctorcla) && count($doctorcla) > 0) {
  191. $newrow = array();
  192. $newrow["id"] = UUIDs::uuid16();
  193. $newrow["doctor_id"] = $doctor_id;
  194. $doctorclaStr = json_encode($doctorcla);
  195. $doctorclaStr = str_replace('"', '', $doctorclaStr);
  196. $doctorclaStr = str_replace('[', '', $doctorclaStr);
  197. $doctorclaStr = str_replace(']', '', $doctorclaStr);
  198. $doctorclaStr = str_replace(" ", "", $doctorclaStr);
  199. $newrow["doctor_class"] = $doctorclaStr;
  200. $newrow["status"] = "1";
  201. Db::table("doctor_class")->insert($newrow);
  202. SysLogs::log("doctors", "C", $doctor_id . " doctorclass --> " . $doctorclaStr);
  203. }
  204. }
  205. /**
  206. * 软删除记录
  207. */
  208. public function delete() {
  209. // 医院的管理员,只能处理本机构的记录
  210. $manager = $admin = Session::get('session_manager');
  211. $insId = $manager["institution_id"];
  212. if (empty($insId)) {
  213. echo "fail";
  214. return;
  215. }
  216. if (isset($_GET["ids"])) {
  217. $ids = $_GET["ids"];
  218. if (!empty($ids)) {
  219. $idArr = explode(",", $ids);
  220. if (count($idArr) > 0) {
  221. Db::table("doctors")->where("id", "in", $idArr)->where("institution_id", $insId)->update(['status' => 1]);
  222. SysLogs::log("doctors", "U", $ids . " status 修改为 1 ");
  223. }
  224. echo "delete_ok";
  225. return;
  226. }
  227. }
  228. echo "fail";
  229. }
  230. /**
  231. * 显示权限编辑窗口
  232. * @return type
  233. */
  234. public function permissions() {
  235. // 医院的管理员,只能处理本机构的记录
  236. $manager = $admin = Session::get('session_manager');
  237. $insId = $manager["institution_id"];
  238. $id = is_string($_GET["id"]) ? $_GET["id"] : null;
  239. if (empty($insId) || empty($id)) {
  240. // 没有传入管理员ID和医生ID,不能编辑
  241. echo "no login or no permissions";
  242. return;
  243. }
  244. if ($id != null) {
  245. // 得到医生信息
  246. $doctor = Db::table("doctors")->where("id", $id)->find();
  247. if (empty($doctor)) {
  248. echo "no doctor !";
  249. return;
  250. }
  251. $this->assign("doctor", $doctor);
  252. $this->assign("id", $id);
  253. }
  254. // 查找已有权限(菜单)
  255. // 只查询本机构的
  256. $permitsMenus = Db::table("dr_cla_permission")->where("doctor_id", $id)->where("type", "1")->select();
  257. $permitMenuIdArr = array();
  258. if (count($permitsMenus) > 0) {
  259. foreach ($permitsMenus as $key => $val) {
  260. array_push($permitMenuIdArr, $val["pass"]);
  261. }
  262. $this->assign("permitMenuIdArr", json_encode($permitMenuIdArr));
  263. }
  264. // 查找已有权限(写报告)
  265. $permitsReport = Db::table("dr_cla_permission")->where("doctor_id", $id)->where("type", "2")->find();
  266. if (!empty($permitsReport)) {
  267. $this->assign("permitReport", $permitsReport["pass"]);
  268. } else {
  269. $this->assign("permitReport", "0");
  270. }
  271. return $this->fetch("permissions");
  272. }
  273. /**
  274. * 保存菜单权限
  275. */
  276. public function saveMenuPermit() {
  277. $request = Request::instance();
  278. $params = $request->param();
  279. // 医院的管理员,只能处理本机构的记录
  280. $manager = $admin = Session::get('session_manager');
  281. $insId = $manager["institution_id"];
  282. $doctorId = isset($params["id"]) ? $params["id"] : null;
  283. $menuIds = isset($params["ids"]) ? $params["ids"] : null;
  284. if (empty($doctorId) || empty($menuIds) || empty($insId)) {
  285. // 如果医生ID,菜单ID,所在机构ID,某一个为空,直接返回失败
  286. echo "fail";
  287. return;
  288. }
  289. // 查询该医生信息
  290. $doctor = Db::table("doctors")->where("institution_id", $insId)->where("id", $doctorId);
  291. if (empty($doctor)) {
  292. // 该机构下,没有找到该医生
  293. echo "fail";
  294. return;
  295. }
  296. $menuIdArr = explode(",", $menuIds);
  297. $menus = Db::table("menu")->whereIn("id", $menuIdArr)->select();
  298. if (count($menus) < 1) {
  299. // 没有找到菜单
  300. echo "fail";
  301. return;
  302. } else {
  303. // 给用户赋值
  304. // 查看是否已经有了权限
  305. $menuIdArr = array();
  306. foreach ($menus as $key => $val) {
  307. array_push($menuIdArr, $val["id"]);
  308. }
  309. // 将已有权限全部清空
  310. Db::table("dr_cla_permission")->where("doctor_id", $doctorId)->where("type", "1")->delete();
  311. SysLogs::log("dr_cla_permission", "D", "where (doctor_id = " . $doctorId . "type = 1) delete ");
  312. // 如果没有,添加
  313. foreach ($menuIdArr as $key => $val) {
  314. $newRow = array();
  315. $newRow["id"] = UUIDs::uuid16();
  316. $newRow["type"] = "1";
  317. $newRow["pass"] = $val;
  318. $newRow["doctor_id"] = $doctorId;
  319. Db::table("dr_cla_permission")->insert($newRow);
  320. SysLogs::log("dr_cla_permission", "C", json_encode($newRow));
  321. }
  322. }
  323. echo "ok";
  324. }
  325. /**
  326. * 保存报告权限
  327. */
  328. public function saveReportPermit() {
  329. $request = Request::instance();
  330. $params = $request->param();
  331. // 医院的管理员,只能处理本机构的记录
  332. $manager = $admin = Session::get('session_manager');
  333. if (empty($manager) || empty($manager["institution_id"])) {
  334. // 登录超时,或者者不是机构管理员
  335. echo "fail";
  336. return;
  337. }
  338. $insId = $manager["institution_id"];
  339. $doctorId = isset($params["id"]) ? $params["id"] : null;
  340. $permitReport = isset($params["report"]) ? $params["report"] : null;
  341. if ($doctorId == null || $permitReport == null || $insId == null) {
  342. echo "fail";
  343. return;
  344. }
  345. // 查询该医生信息
  346. $doctor = Db::table("doctors")->where("institution_id", $insId)->where("id", $doctorId);
  347. if (empty($doctor)) {
  348. // 该机构下,没有找到该医生
  349. echo "fail";
  350. return;
  351. }
  352. $permitData = Db::table("dr_cla_permission")->where("doctor_id", $doctorId)->where("type", "2")->find();
  353. if (!empty($permitData)) {
  354. // 如果已经有报告权限的配置,
  355. // 只更新就好
  356. $permitData["pass"] = $permitReport;
  357. Db::table("dr_cla_permission")->update($permitData);
  358. SysLogs::log("dr_cla_permission", "U", json_encode($permitData));
  359. } else {
  360. // 新建权限记录
  361. $newRow = array();
  362. $newRow["id"] = UUIDs::uuid16();
  363. $newRow["doctor_id"] = $doctorId;
  364. $newRow["type"] = "2";
  365. $newRow["pass"] = $permitReport;
  366. Db::table("dr_cla_permission")->insert($newRow);
  367. SysLogs::log("dr_cla_permission", "C", json_encode($newRow));
  368. }
  369. echo "ok";
  370. }
  371. /**
  372. * 查询全部菜单
  373. */
  374. public function menudata() {
  375. $rootMenuData = array();
  376. // 准备根节点
  377. $rootMenuData["id"] = "root";
  378. $rootMenuData["pId"] = "0";
  379. $rootMenuData["name"] = "菜单(根节点)";
  380. $rootMenuData["url"] = "";
  381. $rootMenuData["open"] = "true";
  382. // 查询全部数据
  383. $menuData = $info = DB::table('menu')->select();
  384. $jsonarray = array();
  385. if ($menuData != null) {
  386. foreach ($menuData as $k => $val) {
  387. $parent_id = $val["parent_id"];
  388. unset($val["parent_id"]);
  389. // 处理parent_id为pId,为前端菜单上下级关系展示处理
  390. $val['pId'] = $parent_id;
  391. $val['open'] = "true";
  392. array_push($jsonarray, $val);
  393. }
  394. }
  395. // 将根节点添加到树
  396. array_unshift($jsonarray, $rootMenuData);
  397. // 返回JSON数据
  398. echo json_encode($jsonarray);
  399. }
  400. // 医院的统计
  401. public function stats() {
  402. $admin = Session::get('session_manager');
  403. // 得到所在机构ID
  404. $insId = $admin["institution_id"];
  405. if (strpos($insId, ",") !== false) {
  406. // 有多个机构时,只取第一个
  407. $insId = substr($insId, 0, trpos($insId, ","));
  408. }
  409. $dateStr = date("Ym");
  410. $whereArr = array();
  411. $whereArr["role_id"] = $insId;
  412. // 取最近5个月的数据
  413. $whereArr["month"] = array(">=", date("Ym", strtotime("201808 - 180 day")));
  414. $stats = Db::table("operating")->where($whereArr)->order("month", "1")->select();
  415. if (count($stats) > 0) {
  416. $this->assign("stats", json_encode($stats));
  417. } else {
  418. $this->assign("stats", json_encode(array()));
  419. }
  420. return $this->fetch("stats");
  421. }
  422. /**
  423. * 医院科室管理
  424. * @return type
  425. */
  426. public function deptview() {
  427. $admin = Session::get('session_manager');
  428. // 得到所在机构ID
  429. $insId = $admin["institution_id"];
  430. $this->assign("insId", $insId);
  431. return $this->fetch('/institution/dept');
  432. }
  433. }