HousingestateController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\HousingEstate;
  5. use app\model\Operatingcity;
  6. use app\model\Tuanzhang;
  7. use app\model\Geocoder;
  8. class HousingestateController extends Base
  9. {
  10. function index()
  11. {
  12. $query = $this->setSearch();
  13. $res = $query->order('sort asc,id desc')
  14. ->paginate(getpage())
  15. ->toArray();
  16. if (!empty($res['data'])) {
  17. foreach ($res['data'] as &$vo) {
  18. $vo['region_name'] = $vo['province_name'] . $vo['city_name'] . $vo['district_name'];
  19. $vo['tuanzhang'] = Tuanzhang::getInfobyid($vo['tzid']);
  20. }
  21. }
  22. $data['data'] = $res;
  23. return $this->json($data);
  24. }
  25. function my()
  26. {
  27. $query = $this->setSearch();
  28. $query->where(['tzid' => (int) $this->tzid]);
  29. $res = $query->order('sort asc,id desc')
  30. ->paginate(getpage())
  31. ->toArray();
  32. if (!empty($res['data'])) {
  33. foreach ($res['data'] as &$vo) {
  34. $vo['region_name'] = $vo['province_name'] . $vo['city_name'] . $vo['district_name'];
  35. $vo['tuanzhang'] = Tuanzhang::getInfobyid($vo['tzid']);
  36. }
  37. }
  38. $data['data'] = $res;
  39. return $this->json($data);
  40. }
  41. function setSearch()
  42. {
  43. $keyword = trim(input('post.keyword', '', 'serach_in'));
  44. $status = input('post.status', '', 'serach_in');
  45. $path = input('post.path', '', 'serach_in');
  46. $weid = weid();
  47. if ($path == "/housingestate/audit") {
  48. $status = "0";
  49. }
  50. $query = HousingEstate::where(['weid' => $weid]);
  51. if (!empty($this->ocid)) {
  52. $Operatingcitydata = Operatingcity::find($this->ocid);
  53. if ($Operatingcitydata) {
  54. $Operatingcitydata = $Operatingcitydata->toArray();
  55. if (empty($Operatingcitydata['areatype'])) {
  56. $Operatingcitydata['areatype'] = 3;
  57. }
  58. if ($Operatingcitydata['areatype'] == 3) {
  59. $query->where('district_name', $Operatingcitydata['district_name']);
  60. } elseif ($Operatingcitydata['areatype'] == 2) {
  61. $query->where('city_name', $Operatingcitydata['city_name']);
  62. } elseif ($Operatingcitydata['areatype'] == 1) {
  63. $query->where('province_name', $Operatingcitydata['province_name']);
  64. }
  65. }
  66. }
  67. if (!empty($keyword)) {
  68. $query->where('title|tel', 'like', '%' . $keyword . '%');
  69. }
  70. if (!empty($status) || $status === "0") {
  71. $query->where(['status' => $status]);
  72. }
  73. return $query;
  74. }
  75. function listUpdate()
  76. {
  77. $data = only('id,status');
  78. if (!$data['id']) throw new ValidateException('参数错误');
  79. HousingEstate::update($data);
  80. return $this->json(['msg' => '操作成功']);
  81. }
  82. public function update()
  83. {
  84. $id = $this->request->post('id');
  85. $data = input('post.');
  86. unset($data['create_time']);
  87. if (empty($data['tzid'])) {
  88. throw new ValidateException('请选择所属社区');
  89. }
  90. $areaarray = $data['region_name'];
  91. $data['province_name'] = $areaarray[0];
  92. $data['city_name'] = $areaarray[1];
  93. $data['district_name'] = $areaarray[2];
  94. $dizhi = $data['province_name'];
  95. if ($data['city_name']) {
  96. $dizhi = $dizhi . $data['city_name'];
  97. }
  98. if ($data['district_name']) {
  99. $dizhi = $dizhi . $data['district_name'];
  100. }
  101. $data['area_name'] = $dizhi;
  102. if ($data['house_number']) {
  103. $dizhi = $dizhi . $data['house_number'];
  104. }
  105. if ($dizhi) {
  106. $coder = Geocoder::geocoding($dizhi);
  107. $data['latitude'] = $coder['latitude'];
  108. $data['longitude'] = $coder['longitude'];
  109. $data['region_name'] = $dizhi;
  110. }
  111. if (empty($id)) {
  112. $data['weid'] = weid();
  113. try {
  114. $res = HousingEstate::create($data);
  115. if ($res->id && empty($data['sort'])) {
  116. HousingEstate::update(['sort' => $res->id, 'id' => $res->id]);
  117. }
  118. } catch (\Exception $e) {
  119. throw new ValidateException($e->getMessage());
  120. }
  121. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  122. } else {
  123. try {
  124. HousingEstate::update($data);
  125. } catch (\Exception $e) {
  126. throw new ValidateException($e->getMessage());
  127. }
  128. return $this->json(['msg' => '修改成功']);
  129. }
  130. }
  131. function getInfo()
  132. {
  133. $id = $this->request->post('id', '', 'serach_in');
  134. if (!$id) throw new ValidateException('参数错误');
  135. $data = HousingEstate::find($id);
  136. if ($data) {
  137. $data = $data->toArray();
  138. $data['pic'] = toimg($data['pic']);
  139. $area[0] = areaconversion($data['province_name']);
  140. $area[1] = $data['city_name'];
  141. if (!empty($data['district_name'])) {
  142. $area[2] = $data['district_name'];
  143. }
  144. $data['region_name'] = $area;
  145. }
  146. return $this->json(['data' => $data]);
  147. }
  148. function delete()
  149. {
  150. return $this->del(new HousingEstate());
  151. }
  152. function getField()
  153. {
  154. $weid = weid();
  155. $query = Tuanzhang::where(['weid' => $weid, 'status' => 1]);
  156. if (!empty($this->ocid)) {
  157. $Operatingcitydata = Operatingcity::find($this->ocid);
  158. if ($Operatingcitydata) {
  159. $Operatingcitydata = $Operatingcitydata->toArray();
  160. if (empty($Operatingcitydata['areatype'])) {
  161. $Operatingcitydata['areatype'] = 3;
  162. }
  163. if ($Operatingcitydata['areatype'] == 3) {
  164. $query->where('district_name', $Operatingcitydata['district_name']);
  165. } elseif ($Operatingcitydata['areatype'] == 2) {
  166. $query->where('city_name', $Operatingcitydata['city_name']);
  167. } elseif ($Operatingcitydata['areatype'] == 1) {
  168. $query->where('province_name', $Operatingcitydata['province_name']);
  169. }
  170. }
  171. }
  172. $data = $query->order('sort asc')->select()->toArray();
  173. foreach ($data as $k => $v) {
  174. $array[$k]['val'] = $v['id'];
  175. $array[$k]['key'] = $v['title'];
  176. }
  177. $data['tzidarray'] = $array;
  178. return $this->json(['data' => $data]);
  179. }
  180. }