| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace addons\Area\common\service;
- use addons\Area\common\models\AreaCommunity;
- use common\enums\StatusEnum;
- class CommunityService
- {
- public function page(int $mall_id, array $params)
- {
- $map["where"][] = ["mall_id" => $mall_id];
- if (!empty($params['address'])) {
- $map['where'][] = ['like', 'fully_address', trim($params['address'])];
- }
- // 这里要使用or
- if (!empty($params['keyword'])) {
- if (is_numeric($params['keyword'])) {
- $map['where'][] = ['like', 'id', trim($params['keyword'])];
- } else {
- $map['where'][] = ['like', 'community_name', trim($params['keyword'])];
- }
- }
- $map['where'][] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $map["page"] = $params["page"];
- $map["limit"] = $params["limit"];
- $map["order"] = "id desc";
- $ret = AreaCommunity::listPage($map);
- if (!$ret) return AreaCommunity::getStaticError();
- if (!empty($ret['list'])) {
- foreach ($ret['list'] as &$item) {
- if (empty($item['town_id'])) $item['town_id'] = '';
- }
- }
- return $ret;
- }
- /**
- * @param int $mall_id
- * @param array $params
- * @param bool $is_api
- * @return mixed|string
- */
- public function list(int $mall_id, array $params, bool $is_api = false)
- {
- $map["where"][] = ["mall_id" => $mall_id];
- $map['where'][] = ['status' => StatusEnum::ENABLED];
- $map['where'][] = ['town_id' => $params['town_id']];
- if (!empty($params['is_district'])) $map['where'][] = ['parent_is_district' => StatusEnum::ENABLED]; // 如果是通过第三级获取
- $map["page"] = $params["page"];
- $map["limit"] = $params["limit"];
- $map["order"] = "id desc";
- $map['select'] = 'id, community_name';
- if ($is_api) {
- $map['select'] = 'id, community_name as name';
- }
- $ret = AreaCommunity::lists($map);
- if (!empty(AreaCommunity::getStaticError())) return AreaCommunity::getStaticError();
- return $ret;
- }
- /**
- * 删除
- * @param int $mall_id
- * @param int $id
- * @return string|true
- */
- public function delete(int $mall_id, int $id)
- {
- $row = AreaCommunity::find()->where(['mall_id' => $mall_id])
- ->andWhere(['id' => $id])->one();
- if (!$row) return '记录不存在';
- $row->status = StatusEnum::DELETE;
- if (!$row->save()) return AreaCommunity::getStaticError();
- return true;
- }
- /**
- * 修改状态
- * @param int $mall_id
- * @param int $id
- * @param int $status
- * @return string|true
- */
- public function status(int $mall_id, int $id, int $status)
- {
- $row = AreaCommunity::find()->where(['mall_id' => $mall_id])
- ->andWhere(['id' => $id])->one();
- if (!$row) return '记录不存在';
- $row->status = $status;
- if (!$row->save()) return AreaCommunity::getStaticError();
- return true;
- }
- /**
- * @param int $mall_id
- * @param array $data
- * @return string|true
- */
- public function create(int $mall_id, array $data)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($data['id'])) {
- $model = AreaCommunity::find()->where(['mall_id' => $mall_id])
- ->where(['id' => $data['id']])->one();
- } else {
- $model = new AreaCommunity();
- $model->mall_id = $mall_id;
- }
- $model->province_id = $data['province_id'];
- $model->city_id = $data['city_id'];
- $model->district_id = $data['district_id'];
- $model->town_id = !empty($data['town_id']) ? $data['town_id'] : $data['district_id'];
- $model->parent_is_district = !empty($data['town_id']) ? StatusEnum::DISABLED : StatusEnum::ENABLED; // 上级是否为区县级别
- $model->lng = $data['lng'];
- $model->lat = $data['lat'];
- $model->community_name = $data['community_name'];
- $model->fully_address = $data['fully_address']; // 完整地址
- $model->street = $data['street']; // 街道地址
- if (!empty($data['addressComponents'])) {
- $town_txt = !empty($data['addressComponents']['town']) ? ",{$data['addressComponents']['town']}" : "";
- $model->area = "{$data['addressComponents']['province']},{$data['addressComponents']['city']},{$data['addressComponents']['district']}{$town_txt}"; // 省市区镇
- }
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- return $e->getMessage();
- }
- return true;
- }
- /**
- * @param int $mall_id
- * @param int $id
- * @return mixed|string
- */
- public function fetch(int $mall_id, int $id)
- {
- $row = AreaCommunity::getOne([
- ['mall_id' => $mall_id],
- ['id' => $id]
- ]);
- if (!$row) return '记录不存在';
- return $row;
- }
- }
|