Doctors.php 14 KB

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