Doctors.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. use PHPExcel_IOFactory;
  12. use PHPExcel;
  13. class Doctors extends Base {
  14. public function index() {
  15. return $this->fetch();
  16. }
  17. public function datas() {
  18. $request = Request::instance()->param();
  19. $username = isset($request["username"]) ? $request["username"] : null;
  20. $status = isset($request["status"]) ? $request["status"] : null;
  21. $institution_id = isset($request["insId"]) ? $request["insId"] : null;
  22. $whereArr = array();
  23. if (!empty($username)) {
  24. $whereArr["username"] = array("like", $username . "%");
  25. }
  26. if ($status != null) {
  27. $whereArr["status"] = $status;
  28. }
  29. if (!empty($institution_id)) {
  30. $whereArr["institution_id"] = $institution_id;
  31. }
  32. $page = empty($_GET["page"]) ? 1 : $_GET["page"];
  33. $pagesize = empty($_GET["rows"]) ? 1 : $_GET["rows"];
  34. if (empty($page) || $page < 1) {
  35. $page = 1;
  36. }
  37. if (empty($pagesize) || $pagesize < 1) {
  38. $pagesize = 30;
  39. }
  40. $info = DB::table('doctors')->where($whereArr)->page($page, $pagesize)->select();
  41. foreach ($info as $k => $v) {
  42. $iname = DB::table('institution')->where('id', $v['institution_id'])->field('name')->find();
  43. $info[$k]['institution_name'] = $iname['name'] ?? null;
  44. $dname = DB::table('department')->where('id', $v['department_id'])->field('department_name')->find();
  45. $info[$k]['department_name'] = $dname['department_name'] ?? null;
  46. }
  47. $num = DB::table('doctors')->where($whereArr)->count();
  48. $data = array();
  49. $data['total'] = $num;
  50. $data['rows'] = $info;
  51. echo json_encode($data);
  52. }
  53. /**
  54. * 编辑窗口
  55. * @return type
  56. */
  57. public function edit() {
  58. $doctors = null;
  59. $special = DB::table('special_remote')
  60. ->where('status',1)
  61. ->field('id,name,0 as sc_status')
  62. ->select();
  63. if (isset($_GET["id"])) {
  64. $id = $_GET["id"];
  65. if ($id != null) {
  66. $doctors = Db::table("doctors")->where("id", $id)->find();
  67. if (count($doctors) > 0) {
  68. if (empty($doctors['doctor_role'])) {
  69. $doctors['doctor_role'] = array();
  70. } else {
  71. $doctors['doctor_role'] = explode(',', $doctors['doctor_role']);
  72. }
  73. $this->assign("doctors", $doctors);
  74. $exam_class = explode(',',$doctors['exam_class']);
  75. $this->assign('examcla',$exam_class);
  76. // 查询医生的分类
  77. $doctorsCla = Db::table("doctor_class")->where("doctor_id", $id)->find();
  78. if (!empty($doctorsCla)) {
  79. $doctorClaStr = $doctorsCla["doctor_class"];
  80. if (!empty($doctorClaStr)) {
  81. $dc_arr = explode(",", $doctorClaStr);
  82. $this->assign("doctorcla", $dc_arr);
  83. }
  84. }
  85. }
  86. $special = DB::table('special_remote')
  87. ->alias('sr')
  88. ->join(['special_contact'=>'sc'],'sc.did=\''.$id.'\' and sc.sid=sr.id','left')
  89. ->where('sr.status',1)
  90. ->field('sr.id,sr.name,sc.status as sc_status')
  91. ->select();
  92. }
  93. }
  94. $this->assign('special', $special);
  95. $doctorClas = Db::table("constant")->where("parent_id", "doctor_class")->order("ordernum", "1")->select();
  96. $this->assign('doctorclas', $doctorClas);
  97. $examCla = DB::table('constant')->where('parent_id','exam_class')->select();
  98. $this->assign('examclass',$examCla);
  99. $institution = DB::table('institution')->select();
  100. $this->assign('institution', $institution);
  101. $depWheres = array();
  102. if ($doctors != null) {
  103. $depWheres["institution_id"] = $doctors["institution_id"];
  104. }
  105. $department = DB::table('department')->where($depWheres)->select();
  106. $this->assign('department', $department);
  107. return $this->fetch('edit');
  108. }
  109. public function save() {
  110. $request = Request::instance();
  111. $params = $request->param();
  112. $data = $params;
  113. //预创建医生id
  114. $id = UUIDs::uuid16();
  115. if(isset($params['id']) && !empty($params['id'])){
  116. $id = $params['id'];
  117. }
  118. if(isset($params['special_name'])){
  119. unset($params['special_name']);
  120. foreach ($data['special_name'] as $v){
  121. unset($params['special_'.$v]);
  122. $special_contact = DB::table('special_contact')->where('did',$id)->where('sid',$v)->find();
  123. if($data['special_'.$v] == '1'){
  124. //存在医生则查找
  125. if(empty($special_contact)){
  126. $sort = DB::table('special_contact')->max('sort');
  127. //不存在 创建信息
  128. $special = [
  129. 'sid'=>$v,
  130. 'did'=>$id,
  131. 'hid'=>$params['institution_id'],
  132. 'describe'=>'',
  133. 'sort'=>$sort+10,
  134. 'status'=>1,
  135. 'cost'=>0
  136. ];
  137. DB::table('special_contact')->insert($special);
  138. }else{
  139. //存在 修改信息
  140. DB::table('special_contact')->where('did',$params['id'])->where('sid',$v)->update(['status'=>1]);
  141. }
  142. }else{
  143. if(!empty($special_contact)){
  144. DB::table('special_contact')->where('did',$id)->where('sid',$v)->update(['status'=>0]);
  145. }
  146. }
  147. }
  148. }
  149. if (!isset($params["doctorcla"])) {
  150. echo "fail:doctorcla";
  151. return;
  152. }
  153. $doctorcla = $params["doctorcla"];
  154. unset($params["doctorcla"]);
  155. if(isset($params['doctor_role']) && !empty($params['doctor_role'])){
  156. $params['doctor_role'] = implode(',', $params['doctor_role']);
  157. }
  158. $password = $params['password'];
  159. if(isset($params['examcla'])){
  160. $params['exam_class'] = implode(',',$params['examcla']);
  161. unset($params['examcla']);
  162. }
  163. if (!empty($password) && strlen($password) < 30) {
  164. $params['password'] = md5($password);
  165. } else {
  166. unset($params['password']);
  167. }
  168. if (empty($params['id'])) {
  169. unset($params['id']);
  170. $params['id'] = $id;
  171. $a = DB::table('doctors')->insert($params);
  172. if (isset($params["department_id"])) {
  173. $this->saveDoctorCla($id, $doctorcla, $params["department_id"]);
  174. SysLogs::log("doctors", "C", json_encode($params));
  175. }
  176. return 'insert_ok;' . $id;
  177. } else {
  178. $a = DB::table('doctors')->where('id', $params['id'])->update($params);
  179. if (isset($params["department_id"])) {
  180. $this->saveDoctorCla($params["id"], $doctorcla, $params["department_id"]);
  181. }
  182. SysLogs::log("doctors", "U", $params['id'] . " --> " . json_encode($params));
  183. return 'success';
  184. }
  185. }
  186. protected function saveDoctorCla($doctor_id, $doctorcla, $depid) {
  187. // doctorcla
  188. Db::table("doctor_class")->where("doctor_id", $doctor_id)->delete();
  189. if (isset($doctorcla) && count($doctorcla) > 0) {
  190. $newrow = array();
  191. $newrow["id"] = UUIDs::uuid16();
  192. $newrow["doctor_id"] = $doctor_id;
  193. $newrow["department_id"] = $depid;
  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. if (isset($_GET["ids"])) {
  210. $ids = $_GET["ids"];
  211. if (!empty($ids)) {
  212. $idArr = explode(",", $ids);
  213. if (count($idArr) > 0) {
  214. Db::table("doctors")->where("id", "in", $idArr)->update(['status' => 1]);
  215. SysLogs::log("doctors", "U", $ids . " status 修改为 1 ");
  216. }
  217. echo "delete_ok";
  218. return;
  219. }
  220. }
  221. echo "fail";
  222. }
  223. /**
  224. * 显示权限编辑窗口
  225. * @return type
  226. */
  227. public function permissions() {
  228. $id = is_string($_GET["id"]) ? $_GET["id"] : null;
  229. if ($id != null) {
  230. // 得到医生信息
  231. $doctor = Db::table("doctors")->where("id", $id)->find();
  232. if (empty($doctor)) {
  233. echo "no doctor !";
  234. return;
  235. }
  236. $this->assign("doctor", $doctor);
  237. $this->assign("id", $id);
  238. }
  239. // 查找已有权限(菜单)
  240. $permitsMenus = Db::table("dr_cla_permission")->where("doctor_id", $id)->where("type", "1")->select();
  241. $permitMenuIdArr = array();
  242. if (count($permitsMenus) > 0) {
  243. foreach ($permitsMenus as $key => $val) {
  244. array_push($permitMenuIdArr, $val["pass"]);
  245. }
  246. $this->assign("permitMenuIdArr", json_encode($permitMenuIdArr));
  247. }
  248. // 查找已有权限(写报告)
  249. $permitsReport = Db::table("dr_cla_permission")->where("doctor_id", $id)->where("type", "2")->find();
  250. if (!empty($permitsReport)) {
  251. $this->assign("permitReport", $permitsReport["pass"]);
  252. } else {
  253. $this->assign("permitReport", "0");
  254. }
  255. return $this->fetch("permissions");
  256. }
  257. /**
  258. * 保存菜单权限
  259. */
  260. public function saveMenuPermit() {
  261. $doctorId = empty($_GET["id"]) ? null : $_GET["id"];
  262. $menuIds = empty($_GET["ids"]) ? null : $_GET["ids"];
  263. if (empty($doctorId) || empty($menuIds)) {
  264. echo "fail, empty doctorId or menuId";
  265. return;
  266. }
  267. $menuIdArr = explode(",", $menuIds);
  268. $menus = Db::table("menu")->whereIn("id", $menuIdArr)->select();
  269. if (count($menus) < 1) {
  270. // 没有找到菜单
  271. echo "fail, no menu found ";
  272. return;
  273. } else {
  274. // 给用户赋值
  275. // 查看是否已经有了权限
  276. $menuIdArr = array();
  277. foreach ($menus as $key => $val) {
  278. array_push($menuIdArr, $val["id"]);
  279. }
  280. // 将已有权限全部清空
  281. Db::table("dr_cla_permission")->where("doctor_id", $doctorId)->where("type", "1")->delete();
  282. SysLogs::log("dr_cla_permission", "D", "where (doctor_id = " . $doctorId . "type = 1) delete ");
  283. // 如果没有,添加
  284. foreach ($menuIdArr as $key => $val) {
  285. $newRow = array();
  286. $newRow["id"] = UUIDs::uuid16();
  287. $newRow["type"] = "1";
  288. $newRow["pass"] = $val;
  289. $newRow["doctor_id"] = $doctorId;
  290. Db::table("dr_cla_permission")->insert($newRow);
  291. SysLogs::log("dr_cla_permission", "C", json_encode($newRow));
  292. }
  293. }
  294. echo "ok";
  295. }
  296. /**
  297. * 保存报告权限
  298. */
  299. public function saveReportPermit() {
  300. $doctorId = isset($_GET["id"]) ? $_GET["id"] : null;
  301. $permitReport = isset($_GET["report"]) ? $_GET["report"] : null;
  302. if ($doctorId == null || $permitReport == null) {
  303. echo "fail";
  304. return;
  305. }
  306. $permitData = Db::table("dr_cla_permission")->where("doctor_id", $doctorId)->where("type", "2")->find();
  307. if (!empty($permitData)) {
  308. // 如果已经有报告权限的配置,
  309. // 只更新就好
  310. $permitData["pass"] = $permitReport;
  311. Db::table("dr_cla_permission")->update($permitData);
  312. SysLogs::log("dr_cla_permission", "U", json_encode($permitData));
  313. } else {
  314. // 新建权限记录
  315. $newRow = array();
  316. $newRow["id"] = UUIDs::uuid16();
  317. $newRow["doctor_id"] = $doctorId;
  318. $newRow["type"] = "2";
  319. $newRow["pass"] = $permitReport;
  320. Db::table("dr_cla_permission")->insert($newRow);
  321. SysLogs::log("dr_cla_permission", "C", json_encode($newRow));
  322. }
  323. echo "ok";
  324. }
  325. /**
  326. * 查询全部菜单
  327. */
  328. public function menudata() {
  329. $rootMenuData = array();
  330. // 准备根节点
  331. $rootMenuData["id"] = "root";
  332. $rootMenuData["pId"] = "0";
  333. $rootMenuData["name"] = "菜单(根节点)";
  334. $rootMenuData["url"] = "";
  335. $rootMenuData["open"] = "true";
  336. // 查询全部数据
  337. $menuData = $info = DB::table('menu')->select();
  338. $jsonarray = array();
  339. if ($menuData != null) {
  340. foreach ($menuData as $k => $val) {
  341. $parent_id = $val["parent_id"];
  342. unset($val["parent_id"]);
  343. // 处理parent_id为pId,为前端菜单上下级关系展示处理
  344. $val['pId'] = $parent_id;
  345. $val['open '] = "true";
  346. array_push($jsonarray, $val);
  347. }
  348. }
  349. // 将根节点添加到树
  350. array_unshift($jsonarray, $rootMenuData);
  351. // 返回JSON数据
  352. echo json_encode($jsonarray);
  353. }
  354. public function import(){
  355. $objPHPExcel = new PHPExcel();
  356. //获取表单上传文件
  357. $file = request()->file('excel');
  358. $info = $file->validate(['size'=>99999,'ext'=>'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'excel');
  359. if($info){
  360. $exclePath = $info->getSaveName(); //获取文件名
  361. $file_name = ROOT_PATH . 'public' . DS . 'excel' . DS . $exclePath; //上传文件的地址
  362. $objReader = new \PHPExcel_Reader_Excel2007();
  363. if(!$objReader->canRead($file_name)){
  364. $objReader = PHPExcel_IOFactory::createReader('Excel5');
  365. }
  366. $obj_PHPExcel =$objReader->load($file_name); //加载文件内容,编码utf-8
  367. echo "<pre>";
  368. $excel_array=$obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式
  369. array_shift($excel_array); //删除第一个数组(标题);
  370. $data = [];
  371. $dj = array();
  372. $yc = array();
  373. $bgqx = array();
  374. $bg = array();
  375. foreach($excel_array as $k=>$v) {
  376. if(empty($v[2])){
  377. continue;
  378. }
  379. $doctor_info = DB::table('doctors')->where('username',$v[2])->find();
  380. if($doctor_info){
  381. continue;
  382. }
  383. $data[$k]['id'] = UUIDs::uuid16();
  384. $data[$k]['realname'] = $v[0];
  385. $data[$k]['doctor_title'] = isset($v[1]) ? $v[1] : '';
  386. $data[$k]['phone'] = $v[2];
  387. $data[$k]['username'] = $v[2];
  388. $data[$k]['department_id'] = $this->getdid(isset($v[3]) ? $v[3] : '',$v[4]);
  389. $data[$k]['institution_id'] = $v[4];
  390. $data[$k]['is_report'] = 0;
  391. $data[$k]['password'] = md5('123456');
  392. // 报告权限
  393. $bgqx[$k]['doctor_id'] = $data[$k]['id'];
  394. $bgqx[$k]['department_id'] = $data[$k]['department_id'];
  395. $bgqx[$k]['doctor_class'] = $v[5];
  396. $bgqx[$k]['id'] = UUIDs::uuid16();
  397. if(!empty($v[6])){
  398. // 本地报告权限
  399. $bg[$k]['id'] = UUIDs::uuid16();
  400. $bg[$k]['doctor_id'] = $data[$k]['id'];
  401. $bg[$k]['pass'] = '80ddb7d09ebc44b4';
  402. $bg[$k]['type'] = 1;
  403. }
  404. if(!empty($v[7])){
  405. // 登记列表权限
  406. $dj[$k]['id'] = UUIDs::uuid16();
  407. $dj[$k]['doctor_id'] = $data[$k]['id'];
  408. $dj[$k]['pass'] = '0eac42ef01de23ff';
  409. $dj[$k]['type'] = 1;
  410. }
  411. if(!empty($v[8])){
  412. // 远程列表权限
  413. $yc[$k]['id'] = UUIDs::uuid16();
  414. $yc[$k]['doctor_id'] = $data[$k]['id'];
  415. $yc[$k]['pass'] = '0aa9d7b2fdf7268e';
  416. $yc[$k]['type'] = 1;
  417. }
  418. }
  419. $success=Db::name('doctors')->insertAll($data); //批量插入数据
  420. $pbg = DB::table('doctor_class')->insertAll($bgqx); //批评插入报告权限
  421. $pyc = DB::table('dr_cla_permission')->insertAll($yc); //批评插入远程列表权限
  422. $pdj = DB::table('dr_cla_permission')->insertAll($dj); //批评插入登记列表权限
  423. $pbgl = DB::table('dr_cla_permission')->insertAll($bg); //批评插入报告列表权限
  424. SysLogs::log("doctors", "c", '导入医生名单并分配权限,');
  425. $this->success($success.'条插入成功');
  426. // $error=$i-$success;
  427. // echo "总{$i}条,成功{$success}条,失败{$error}条。";
  428. }else{
  429. // 上传失败获取错误信息
  430. echo $file->getError();
  431. }
  432. }
  433. // $depa_name 科室名称 $ins_id 医疗机构id
  434. public function getdid($depa_name,$ins_id){
  435. if(empty($depa_name)){
  436. return '';
  437. }
  438. $info = DB::table('department')->where('institution_id',$ins_id)->where('department_name',$depa_name)->find();
  439. if($info){
  440. return $info['id'];
  441. }else{
  442. $dinfo = array();
  443. $dinfo['id'] = UUIDs::uuid16();
  444. $dinfo['department_name'] = $depa_name;
  445. $dinfo['institution_id'] = $ins_id;
  446. $dinfo['parent_id'] = 'root';
  447. $dinfo['is_report'] = 0;
  448. DB::table('department')->insert($dinfo);
  449. return $dinfo['id'];
  450. }
  451. }
  452. }