Institution.php 10 KB

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