Hospital.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\admin\controller\nhc;
  3. use app\admin\common\AdminController;
  4. use app\common\api\DictApi;
  5. use app\common\api\UploadApi;
  6. use think\facade\Db;
  7. /**
  8. * 医院管理
  9. */
  10. class Hospital extends AdminController {
  11. public function index() {
  12. $where = [];
  13. $where[] = ["status", "<>", 3];
  14. $medicalCommunities = Db::table("nhc_medical_community")->where($where)->order("num asc")->select()->toArray();
  15. return view("", ["medicalCommunities" => $medicalCommunities]);
  16. }
  17. public function list() {
  18. $order = trim($this->request->param("order")) ?: "desc";
  19. //$offset = trim($this->request->param("offset")) ?: 0;
  20. //$limit = trim($this->request->param("limit")) ?: 10;
  21. $name = trim($this->request->param("name"));
  22. $communityId = trim($this->request->param("communityId"));
  23. $where = [];
  24. $where[] = ["h.status", "<>", 3];
  25. if ($name) {
  26. $where[] = ["h.name", "like", "%" . $name . "%"];
  27. }
  28. if ($communityId) {
  29. $where[] = ["h.communityId", "=", $communityId];
  30. }
  31. //$count = Db::table("nhc_hospital")->alias("h")->leftJoin("nhc_medical_community mc", "mc.id=h.communityId")->where($where)->count();
  32. $rows = Db::table("nhc_hospital")->alias("h")->leftJoin("nhc_medical_community mc", "mc.id=h.communityId")
  33. ->field("h.*,mc.name as medicalCommunityName")
  34. ->where($where)->order("mc.num asc,h.num asc,h.createTime " . $order)
  35. ->select()->toArray();
  36. return $rows;
  37. }
  38. public function add() {
  39. $where = [];
  40. $where[] = ["status", "<>", 3];
  41. $medicalCommunities = Db::table("nhc_medical_community")->where($where)->order("num asc")->select()->toArray();
  42. return view("edit", ["medicalCommunities" => $medicalCommunities]);
  43. }
  44. public function edit() {
  45. $id = \StrUtil::getRequestDecodeParam($this->request, 'id');
  46. $info = Db::table("nhc_hospital")->where("id", $id)->find();
  47. $where = [];
  48. $where[] = ["status", "<>", 3];
  49. $medicalCommunities = Db::table("nhc_medical_community")->where($where)->order("num asc")->select()->toArray();
  50. return view("edit", ["row" => $info, "medicalCommunities" => $medicalCommunities]);
  51. }
  52. public function upsert() {
  53. $response_obj = new \StdClass();
  54. $response_obj->code = 500;
  55. $id = \StrUtil::getRequestDecodeParam($this->request, 'id');
  56. $where = [];
  57. if ($id) {
  58. $info = Db::table("nhc_hospital")->where("id", $id)->find();
  59. if (!$info) {
  60. $response_obj->msg = '找不到记录';
  61. return \StrUtil::back($response_obj, 'HospitalInfo.callBack');
  62. }
  63. $where[] = ["id", "<>", $id];
  64. }
  65. $name = trim($this->request['name']);
  66. if (\StrUtil::isEmpOrNull($name)) {
  67. $response_obj->msg = '医院名称不能为空';
  68. return \StrUtil::back($response_obj, 'HospitalInfo.callBack');
  69. }
  70. $where[] = ["name", "=", $name];
  71. $repeat = Db::table("nhc_hospital")->where($where)->find();
  72. if ($repeat) {
  73. $response_obj->msg = '医院名称已经被使用';
  74. return \StrUtil::back($response_obj, 'HospitalInfo.callBack');
  75. }
  76. $communityId = trim($this->request['communityId']);
  77. if (!$communityId) {
  78. $response_obj->msg = '请选择医院所属医共体';
  79. return \StrUtil::back($response_obj, 'HospitalInfo.callBack');
  80. }
  81. $isGeneral = trim($this->request['isGeneral']);
  82. if (!$isGeneral) {
  83. $response_obj->msg = '请选择是否总院(当前医共体)';
  84. return \StrUtil::back($response_obj, 'HospitalInfo.callBack');
  85. }
  86. $num = trim($this->request['num']);
  87. if (\StrUtil::isNotEmpAndNull($num) && !is_numeric($num)) {
  88. $response_obj->msg = '序号只能是数字';
  89. return \StrUtil::back($response_obj, 'HospitalInfo.callBack');
  90. }
  91. $status = trim($this->request['status']);
  92. if (\StrUtil::isEmpOrNull($status)) {
  93. $response_obj->msg = '启用状态不能为空';
  94. return \StrUtil::back($response_obj, 'HospitalInfo.callBack');
  95. }
  96. Db::startTrans();
  97. try {
  98. if ($isGeneral == 1) {
  99. //各医共体下最多仅有一家总院,检查是否有其它设置,如有,变更为2,当前设为1
  100. $where = [];
  101. $where[] = ["communityId", "=", $communityId];
  102. $where[] = ["isGeneral", "=", 1];
  103. if ($id) {
  104. $where[] = ["id", "<>", $id];
  105. }
  106. $updOther["isGeneral"] = 2;
  107. $updOther["updateTime"] = date("Y-m-d H:i:s");
  108. $updOther["updateUser"] = $this->user["uid"];
  109. Db::table("nhc_hospital")->where($where)->save($updOther);
  110. }
  111. $data["name"] = $name;
  112. $data["communityId"] = $communityId;
  113. $data["isGeneral"] = $isGeneral;
  114. $data["status"] = $status;
  115. $data["num"] = $num;
  116. $data["description"] = \StrUtil::getRequestDecodeParam($this->request, "description");
  117. if ($id) {
  118. $data["id"] = $id;
  119. $data["updateTime"] = date("Y-m-d H:i:s");
  120. $data["updateUser"] = $this->user['uid'];
  121. Db::table("nhc_hospital")->save($data);
  122. } else {
  123. $data["createTime"] = date("Y-m-d H:i:s");
  124. $data["createUser"] = $this->user['uid'];
  125. Db::table("nhc_hospital")->insert($data);
  126. }
  127. Db::commit();
  128. $response_obj->code = 200;
  129. $response_obj->msg = '修改成功';
  130. return \StrUtil::back($response_obj, "HospitalInfo.callBack");
  131. } catch (\Exception $e) {
  132. Db::rollback();
  133. $response_obj->msg = $e->getMessage();
  134. return \StrUtil::back($response_obj, "HospitalInfo.callBack");
  135. }
  136. }
  137. public function setGeneral() {
  138. $id = \StrUtil::getRequestDecodeParam($this->request, 'id');
  139. if (\StrUtil::isEmpOrNull($id)) {
  140. return json(['code' => 500, 'msg' => '缺少参数']);
  141. }
  142. $info = Db::table("nhc_hospital")->where("id", $id)->find();
  143. if (!$info) {
  144. return json(['code' => 500, 'msg' => '没有查询到对应医院信息']);
  145. }
  146. $communityId = $info["communityId"];
  147. Db::startTrans();
  148. try {
  149. //各医共体下最多仅有一家总院,检查是否有其它设置,如有,变更为2,当前设为1
  150. $where = [];
  151. $where[] = ["communityId", "=", $communityId];
  152. $where[] = ["isGeneral", "=", 1];
  153. $where[] = ["id", "<>", $id];
  154. $updOther["isGeneral"] = 2;
  155. $updOther["updateTime"] = date("Y-m-d H:i:s");
  156. $updOther["updateUser"] = $this->user["uid"];
  157. Db::table("nhc_hospital")->where($where)->save($updOther);
  158. $updOther["id"] = $id;
  159. $updOther["isGeneral"] = 1;
  160. Db::table("nhc_hospital")->save($updOther);
  161. Db::commit();
  162. return json(['code' => 200, 'msg' => "设置成功"]);
  163. } catch (\Exception $e) {
  164. Db::rollback();
  165. return json(['code' => 500, 'msg' => $e->getMessage()]);
  166. }
  167. }
  168. public function delete() {
  169. $id = \StrUtil::getRequestDecodeParam($this->request, 'id');
  170. if (\StrUtil::isEmpOrNull($id)) {
  171. return json(['code' => 500, 'msg' => '缺少参数']);
  172. }
  173. $data["id"] = $id;
  174. $data["status"] = 3;
  175. $data["updateTime"] = date("Y-m-d H:i:s");
  176. $data["updateUser"] = $this->user["uid"];
  177. try {
  178. Db::table("nhc_hospital")->save($data);
  179. return json(['code' => 200, 'msg' => "删除成功"]);
  180. } catch (\Exception $e) {
  181. return json(['code' => 500, 'msg' => $e->getMessage()]);
  182. }
  183. }
  184. }