Institution.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. /*
  12. *
  13. */
  14. class Institution extends Base {
  15. public function index() {
  16. // 查询出全部的机构,显示上级机构使用
  17. $rs = Db::table("institution")->select();
  18. $this->assign("institutions", $rs);
  19. $agents = Db::table("manager")->where("role_id", "2")->field("id,username as 'text'")->select();
  20. $this->assign("agents", $agents);
  21. return $this->fetch('index');
  22. }
  23. /**
  24. * 列表数据
  25. */
  26. public function datas() {
  27. $request = Request::instance();
  28. $params = $request->param();
  29. $pid = isset($params["pid"]) ? $params["pid"] : null;
  30. $status = isset($params["status"]) ? $params["status"] : null;
  31. $name = isset($params["name"]) ? $params["name"] : null;
  32. $whereArr = array();
  33. if ($pid != null) {
  34. $whereArr["parent_institution"] = array("like", "%" . $pid . "%");
  35. }
  36. if ($status != null) {
  37. $whereArr["status"] = $status;
  38. }
  39. if ($name != null) {
  40. $whereArr["name"] = array("like", "%" . $name . "%");
  41. }
  42. $rs = Db::table("institution")->where($whereArr)->select();
  43. echo json_encode($rs);
  44. }
  45. /**
  46. * 更新或创建机构信息
  47. */
  48. public function save() {
  49. $request = Request::instance();
  50. $parms = $request->param();
  51. $id = $parms["id"];
  52. $data = array();
  53. $data["name"] = $parms["name"];
  54. $data["address"] = $parms["address"];
  55. $data["remark"] = $parms["remark"];
  56. $data["status"] = $parms["status"];
  57. $data["institution_level"] = $parms["institution_level"];
  58. $data["parent_institution"] = $parms["parent_institution"];
  59. $data["agent_id"] = isset($parms["agent_id"]) ? $parms["agent_id"] : "";
  60. if (is_array($data["parent_institution"])) {
  61. $pids = "";
  62. foreach ($data["parent_institution"] as $key => $val) {
  63. $pids = $pids . "," . $val;
  64. }
  65. if (strlen($pids) > 0) {
  66. $data["parent_institution"] = substr($pids, 1);
  67. } else {
  68. $data["parent_institution"] = "";
  69. }
  70. }
  71. $data["charge_mode"] = $parms["charge_mode"];
  72. $data["local_domain"] = $parms["local_domain"];
  73. // ID有值,认为是更新
  74. if (empty($id)) {
  75. // 无值,认为是添加
  76. if(empty($parms['yid'])){
  77. $data["id"] = UUIDs::uuid16();
  78. }else{
  79. $data["id"] = $parms['yid'];
  80. }
  81. $data["createdAt"] = date("Y-m-d H:i:s");
  82. $id = Db::table("institution")->insertGetId($data);
  83. $this->saveContact($data["parent_institution"],$id);
  84. echo "insert_ok;" . $data["id"];
  85. SysLogs::log("institution", "C", json_encode($data));
  86. } else {
  87. if(!empty($parms['yid'])){
  88. $data["id"] = $parms['yid'];
  89. }
  90. $data["updatedAt"] = date("Y-m-d H:i:s");
  91. // 更新
  92. Db::table("institution")->where("id", $id)->update($data);
  93. $this->saveContact($data["parent_institution"],$id);
  94. SysLogs::log("institution", "U", "id = " . $id . " --> " . json_encode($data));
  95. echo "update_ok";
  96. }
  97. }
  98. public function saveContact($parent,$id)
  99. {
  100. if(empty($parent)){
  101. return '';
  102. }
  103. $local_name = DB::table('institution')->where('id',$id)->value('name');
  104. foreach ($parent as $k=>$v){
  105. $data = DB::table('remote_contact')->where('hospital_id',$id)->where('super_hospital_id',$v)->find();
  106. if(!empty($data)){
  107. return '';
  108. }
  109. $parent_name = DB::table('institution')->where('id',$v)->value('name');
  110. $info = [
  111. 'hospital_id'=>$id,
  112. 'super_hospital_id'=>$v,
  113. 'timestamp'=>time(),
  114. 'hospital_name'=>$local_name,
  115. 'super_hospital_name'=>$parent_name
  116. ];
  117. DB::table('remote_contact')->insert($info);
  118. }
  119. }
  120. /**
  121. * 编辑窗口
  122. * @return type
  123. */
  124. public function edit() {
  125. // 查询出全部的机构,显示上级机构使用
  126. $insRs = Db::table("institution")->field("id,name as 'text'")->select();
  127. $this->assign("insJson", json_encode($insRs));
  128. if (isset($_GET["id"])) {
  129. $id = $_GET["id"];
  130. if ($id != null) {
  131. $institutions = Db::table("institution")->where("id", $id)->find();
  132. if (count($institutions) > 0) {
  133. $this->assign("institution", $institutions);
  134. }
  135. }
  136. }
  137. return $this->fetch('edit');
  138. }
  139. /**
  140. * 删除记录
  141. */
  142. public function delete() {
  143. if (isset($_GET["ids"])) {
  144. $ids = $_GET["ids"];
  145. if (!empty($ids)) {
  146. $idArr = explode(",", $ids);
  147. if (count($idArr) > 0) {
  148. Db::table("institution")->where("id", "in", $idArr)->delete();
  149. }
  150. SysLogs::log("institution", "D", $ids);
  151. echo "delete_ok";
  152. return;
  153. }
  154. }
  155. echo "fail";
  156. }
  157. /**
  158. * 代理商树
  159. */
  160. public function agents() {
  161. $treeData = array();
  162. $rows = Db::table("manager")->where("role_id", "2")->field("id,username as 'text'")->select();
  163. $parentTree = array();
  164. $parentTree["id"] = "";
  165. $parentTree["text"] = "请选择";
  166. array_push($treeData, $parentTree);
  167. if (!empty($rows)) {
  168. foreach ($rows as $key => $val) {
  169. array_push($treeData, $val);
  170. }
  171. }
  172. echo json_encode($treeData);
  173. }
  174. public function deptview() {
  175. $request = Request::instance();
  176. $params = $request->param();
  177. if (isset($params["insId"])) {
  178. $this->assign("insId", $params["insId"]);
  179. } else {
  180. echo "parameter insId error!";
  181. return;
  182. }
  183. return $this->fetch('dept');
  184. }
  185. /**
  186. * 获取医院科室数据
  187. * @return type
  188. */
  189. public function deptdatas() {
  190. $request = Request::instance();
  191. $params = $request->param();
  192. if (!isset($params["insId"])) {
  193. echo "please change a institution!";
  194. return;
  195. }
  196. $ins = Db::table("institution")->where("id", $params["insId"])->find();
  197. if (empty($ins)) {
  198. echo "please change a institution(2)!";
  199. return;
  200. }
  201. $whereArr = array();
  202. $whereArr["institution_id"] = $params["insId"];
  203. $rootMenuData = array();
  204. // 准备根节点
  205. $rootMenuData["id"] = "root";
  206. $rootMenuData["pId"] = "0";
  207. $rootMenuData["name"] = "科室管理(" . $ins["name"] . ")";
  208. $rootMenuData["url"] = "";
  209. $rootMenuData["open"] = "true";
  210. // 查询全部数据
  211. $menuData = $info = DB::table('department')->where($whereArr)->select();
  212. $jsonarray = array();
  213. if ($menuData != null) {
  214. foreach ($menuData as $k => $val) {
  215. $data = array();
  216. $data["id"] = $val["id"];
  217. $data['pId'] = $val["parent_id"];
  218. $data['name'] = $val["department_name"];
  219. $data['institutionid'] = $val["institution_id"];
  220. $data['isreport'] = $val["is_report"];
  221. $data['ordernum'] = $val["order_num"];
  222. array_push($jsonarray, $data);
  223. }
  224. }
  225. // 将根节点添加到树
  226. array_unshift($jsonarray, $rootMenuData);
  227. // 返回JSON数据
  228. echo json_encode($jsonarray);
  229. }
  230. /**
  231. * 添加或修改
  232. */
  233. public function deptupd() {
  234. $id = $_GET["id"];
  235. // id=&pid=root&name=%E5%A4%96%E7%A7%91&institutionid=&=0&ordernum=11
  236. // ID有值,认为是更新
  237. if (empty($id)) {
  238. $data = array();
  239. // 无值,认为是添加
  240. $data["id"] = UUIDs::uuid16();
  241. $data["parent_id"] = $_GET["pid"];
  242. $data["department_name"] = $_GET["name"];
  243. $data["institution_id"] = $_GET["institutionid"];
  244. $data["order_num"] = $_GET["ordernum"];
  245. $data["is_report"] = $_GET["isreport"];
  246. Db::table("department")->insert($data);
  247. echo "insert_ok";
  248. SysLogs::log("constant", "C", json_encode($data));
  249. } else {
  250. // 更新
  251. $data["id"] = $_GET["id"];
  252. $data["parent_id"] = $_GET["pid"];
  253. $data["department_name"] = $_GET["name"];
  254. $data["institution_id"] = $_GET["institutionid"];
  255. $data["order_num"] = $_GET["ordernum"];
  256. $data["is_report"] = $_GET["isreport"];
  257. Db::table("department")->where("id", $id)->update(["parent_id" => $data["parent_id"], "department_name" => $data["department_name"], "institution_id" => $data["institution_id"], "is_report" => $data["is_report"], "order_num" => $data["order_num"]]);
  258. echo "update_ok";
  259. SysLogs::log("constant", "U", $id . " --> " . json_encode($data));
  260. }
  261. }
  262. public function deptdel() {
  263. $id = $_GET["id"];
  264. $result = Db::table("department")->delete($id);
  265. if ($result) {
  266. echo "delete_ok";
  267. SysLogs::log("department", "D", $id . " --> delete_ok");
  268. } else {
  269. echo "delete_fail";
  270. SysLogs::log("department", "D", $id . " --> delete_fail");
  271. }
  272. }
  273. }