HospitalController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\Hospital;
  5. use app\model\HospitalCate;
  6. use app\model\Operatingcity;
  7. class HospitalController extends Base
  8. {
  9. function index()
  10. {
  11. $query = $this->setSearch();
  12. $res = $query->order('sort asc,id desc')
  13. ->paginate(getpage())
  14. ->toArray();
  15. if (!empty($res['data'])) {
  16. foreach ($res['data'] as &$vo) {
  17. $vo = Hospital::conversion($vo);
  18. }
  19. }
  20. $data['data'] = $res;
  21. return $this->json($data);
  22. }
  23. function setSearch()
  24. {
  25. $keyword = trim(input('post.keyword', '', 'serach_in'));
  26. $status = input('post.status', '', 'serach_in');
  27. $weid = weid();
  28. $query = Hospital::where(['weid' => $weid]);
  29. if (!empty($this->ocid)) {
  30. $Operatingcitydata = Operatingcity::find($this->ocid);
  31. if ($Operatingcitydata) {
  32. $Operatingcitydata = $Operatingcitydata->toArray();
  33. if(empty($Operatingcitydata['areatype'])){
  34. $Operatingcitydata['areatype'] = 3;
  35. }
  36. if ($Operatingcitydata['areatype'] == 3) {
  37. $query->where('district_name', $Operatingcitydata['district_name']);
  38. } elseif ($Operatingcitydata['areatype'] == 2) {
  39. $query->where('city_name', $Operatingcitydata['city_name']);
  40. } elseif ($Operatingcitydata['areatype'] == 1) {
  41. $query->where('province_name', $Operatingcitydata['province_name']);
  42. }
  43. }
  44. }
  45. if (!empty($keyword)) {
  46. $query->where('title|tel', 'like', '%' . $keyword . '%');
  47. }
  48. if (!empty($status) || $status === "0") {
  49. $query->where(['status' => $status]);
  50. }
  51. return $query;
  52. }
  53. function listUpdate()
  54. {
  55. $data = only('id,status');
  56. if (!$data['id']) throw new ValidateException('参数错误');
  57. Hospital::update($data);
  58. return $this->json(['msg' => '操作成功']);
  59. }
  60. public function update()
  61. {
  62. $id = $this->request->post('id');
  63. $data = input('post.');
  64. unset($data['create_time']);
  65. if (empty($id)) {
  66. $data['weid'] = weid();
  67. try {
  68. $res = Hospital::create($data);
  69. if ($res->id && empty($data['sort'])) {
  70. Hospital::update(['sort' => $res->id, 'id' => $res->id]);
  71. }
  72. } catch (\Exception $e) {
  73. throw new ValidateException($e->getMessage());
  74. }
  75. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  76. } else {
  77. try {
  78. Hospital::update($data);
  79. } catch (\Exception $e) {
  80. throw new ValidateException($e->getMessage());
  81. }
  82. return $this->json(['msg' => '修改成功']);
  83. }
  84. }
  85. function getInfo()
  86. {
  87. $id = $this->request->post('id', '', 'serach_in');
  88. if (!$id) throw new ValidateException('参数错误');
  89. $data = Hospital::field('*')->find($id)->toArray();
  90. return $this->json(['data' => $data]);
  91. }
  92. function delete()
  93. {
  94. return $this->del(new Hospital());
  95. }
  96. }