Doctors.php 16 KB

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