CommunityService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace addons\Area\common\service;
  3. use addons\Area\common\models\AreaCommunity;
  4. use common\enums\StatusEnum;
  5. class CommunityService
  6. {
  7. public function page(int $mall_id, array $params)
  8. {
  9. $map["where"][] = ["mall_id" => $mall_id];
  10. if (!empty($params['address'])) {
  11. $map['where'][] = ['like', 'fully_address', trim($params['address'])];
  12. }
  13. // 这里要使用or
  14. if (!empty($params['keyword'])) {
  15. if (is_numeric($params['keyword'])) {
  16. $map['where'][] = ['like', 'id', trim($params['keyword'])];
  17. } else {
  18. $map['where'][] = ['like', 'community_name', trim($params['keyword'])];
  19. }
  20. }
  21. $map['where'][] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  22. $map["page"] = $params["page"];
  23. $map["limit"] = $params["limit"];
  24. $map["order"] = "id desc";
  25. $ret = AreaCommunity::listPage($map);
  26. if (!$ret) return AreaCommunity::getStaticError();
  27. if (!empty($ret['list'])) {
  28. foreach ($ret['list'] as &$item) {
  29. if (empty($item['town_id'])) $item['town_id'] = '';
  30. }
  31. }
  32. return $ret;
  33. }
  34. /**
  35. * @param int $mall_id
  36. * @param array $params
  37. * @param bool $is_api
  38. * @return mixed|string
  39. */
  40. public function list(int $mall_id, array $params, bool $is_api = false)
  41. {
  42. $map["where"][] = ["mall_id" => $mall_id];
  43. $map['where'][] = ['status' => StatusEnum::ENABLED];
  44. $map['where'][] = ['town_id' => $params['town_id']];
  45. if (!empty($params['is_district'])) $map['where'][] = ['parent_is_district' => StatusEnum::ENABLED]; // 如果是通过第三级获取
  46. $map["page"] = $params["page"];
  47. $map["limit"] = $params["limit"];
  48. $map["order"] = "id desc";
  49. $map['select'] = 'id, community_name';
  50. if ($is_api) {
  51. $map['select'] = 'id, community_name as name';
  52. }
  53. $ret = AreaCommunity::lists($map);
  54. if (!empty(AreaCommunity::getStaticError())) return AreaCommunity::getStaticError();
  55. return $ret;
  56. }
  57. /**
  58. * 删除
  59. * @param int $mall_id
  60. * @param int $id
  61. * @return string|true
  62. */
  63. public function delete(int $mall_id, int $id)
  64. {
  65. $row = AreaCommunity::find()->where(['mall_id' => $mall_id])
  66. ->andWhere(['id' => $id])->one();
  67. if (!$row) return '记录不存在';
  68. $row->status = StatusEnum::DELETE;
  69. if (!$row->save()) return AreaCommunity::getStaticError();
  70. return true;
  71. }
  72. /**
  73. * 修改状态
  74. * @param int $mall_id
  75. * @param int $id
  76. * @param int $status
  77. * @return string|true
  78. */
  79. public function status(int $mall_id, int $id, int $status)
  80. {
  81. $row = AreaCommunity::find()->where(['mall_id' => $mall_id])
  82. ->andWhere(['id' => $id])->one();
  83. if (!$row) return '记录不存在';
  84. $row->status = $status;
  85. if (!$row->save()) return AreaCommunity::getStaticError();
  86. return true;
  87. }
  88. /**
  89. * @param int $mall_id
  90. * @param array $data
  91. * @return string|true
  92. */
  93. public function create(int $mall_id, array $data)
  94. {
  95. $t = \Yii::$app->db->beginTransaction();
  96. try {
  97. if (!empty($data['id'])) {
  98. $model = AreaCommunity::find()->where(['mall_id' => $mall_id])
  99. ->where(['id' => $data['id']])->one();
  100. } else {
  101. $model = new AreaCommunity();
  102. $model->mall_id = $mall_id;
  103. }
  104. $model->province_id = $data['province_id'];
  105. $model->city_id = $data['city_id'];
  106. $model->district_id = $data['district_id'];
  107. $model->town_id = !empty($data['town_id']) ? $data['town_id'] : $data['district_id'];
  108. $model->parent_is_district = !empty($data['town_id']) ? StatusEnum::DISABLED : StatusEnum::ENABLED; // 上级是否为区县级别
  109. $model->lng = $data['lng'];
  110. $model->lat = $data['lat'];
  111. $model->community_name = $data['community_name'];
  112. $model->fully_address = $data['fully_address']; // 完整地址
  113. $model->street = $data['street']; // 街道地址
  114. if (!empty($data['addressComponents'])) {
  115. $town_txt = !empty($data['addressComponents']['town']) ? ",{$data['addressComponents']['town']}" : "";
  116. $model->area = "{$data['addressComponents']['province']},{$data['addressComponents']['city']},{$data['addressComponents']['district']}{$town_txt}"; // 省市区镇
  117. }
  118. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  119. $t->commit();
  120. } catch (\Exception $e) {
  121. $t->rollBack();
  122. return $e->getMessage();
  123. }
  124. return true;
  125. }
  126. /**
  127. * @param int $mall_id
  128. * @param int $id
  129. * @return mixed|string
  130. */
  131. public function fetch(int $mall_id, int $id)
  132. {
  133. $row = AreaCommunity::getOne([
  134. ['mall_id' => $mall_id],
  135. ['id' => $id]
  136. ]);
  137. if (!$row) return '记录不存在';
  138. return $row;
  139. }
  140. }