Doctors.php 14 KB

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