Doctors.php 14 KB

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