| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- <?php
- namespace addons\Community\api\modules\v1\controllers;
- use addons\Community\common\models\CommunityGoods;
- use addons\Community\common\models\CommunityGoodsSetting;
- use addons\Community\common\models\CommunityUser;
- use addons\Community\common\service\SettingService;
- use api\controllers\OnAuthController;
- use common\enums\StatusEnum;
- use common\helpers\LocationHelper;
- use common\models\goods\GoodsAttr;
- use Yii;
- class CommunityUserController extends OnAuthController
- {
- public $modelClass = '';
- protected $authOptional = ['get-user-community-list', 'get-nearest-one'];
- /**
- * @Description: 选择自提点
- * @Author: zsan
- * @DateTime 2023-02-15 15:34:38
- * @return yii\web\Response
- */
- public function actionCheckCommunityId()
- {
- $mall_id = $this->mall_id;
- $user_id = $this->user_id;
- $data = Yii::$app->request->post();
- $valid = Yii::$app->services->validate->validate($data, [
- [['community_id'], 'integer'],
- [['community_id'], 'required', 'message' => '请选择自提点'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $res = CommunityUser::setData([
- 'mall_id' => $mall_id, 'user_id' => $user_id, 'community_id' => $data['community_id'], 'is_checked' => StatusEnum::ENABLED
- ]);
- if ($res === false) return $this->error(CommunityUser::getStaticError());
- return $this->success('success');
- }
- public function actionGetCommunityList()
- {
- $params = Yii::$app->request->get();
- $valid = Yii::$app->services->validate->validate($params, [
- [['latitude', 'longitude', 'keyword', 'type'], 'string'],
- [['page', 'nearby', 'limit'], 'integer'],
- [['attr_ids'], 'safe'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $longitude = $params['longitude'] ?? 0; //经度
- $latitude = $params['latitude'] ?? 0; //纬度
- if ($longitude && $latitude) {
- $page = $params['page'] ?? 1;
- $limit = $params['limit'] ?? 10;
- $sort = 'distance';
- $mall_id = $this->mall_id;
- $head_string = '';
- $is_has = false;
- $nearby = $params['nearby'] ?? 0;
- if (isset($params['attr_ids']) && !empty($params['attr_ids'])) {
- $params['attr_ids'] = explode(',', $params['attr_ids']);
- $is_has = true;
- $goods_attr = GoodsAttr::lists([
- 'where' => [
- ['status' => StatusEnum::ENABLED], ['id' => $params['attr_ids']]
- ],
- 'select' => 'goods_id'
- ]);
- $goods_id = array_column($goods_attr, 'goods_id') ?? [];
- $goods_setting = CommunityGoodsSetting::lists([
- 'where' => [['status' => StatusEnum::ENABLED], ['goods_id' => $goods_id], ['enable' => StatusEnum::ENABLED]],
- 'select' => 'goods_id'
- ]);
- $goods_id = array_column($goods_setting, 'goods_id') ?? [];
- $community_goods = CommunityGoods::lists([
- 'where' => [['status' => StatusEnum::ENABLED], ['goods_id' => $goods_id]],
- 'select' => 'community_id',
- ]);
- $head_arr = array_column($community_goods, 'community_id');
- $head_string = implode(",", $head_arr);
- }
- $status = StatusEnum::ENABLED;
- $setting_service = new SettingService();
- $max_distance = $setting_service->getFieldSetting($this->mall_id, 'position_range');
- $sql = " SELECT id,`name`,`address`,latitude, longitude, logo, city_id,
- round(
- 2 * 6378.137 * ASIN(
- SQRT(
- POW(
- SIN(
- PI() * ({$longitude} - longitude) / 360
- ),
- 2
- ) + COS(PI() * {$latitude} / 180) * COS(latitude * PI() / 180) * POW(
- SIN(
- PI() * ({$latitude} - latitude) / 360
- ),
- 2
- )
- )
- )
- ,1) AS distance
- FROM `qimall_addons_community_head` WHERE (`mall_id`={$mall_id}) AND (`status`={$status}) ";
- if ($is_has) {
- if (!$head_string) {
- $sql .= "AND FALSE ";
- } else {
- $sql .= "AND id IN ($head_string) ";
- }
- }
- if ($nearby) $sql .= " HAVING (`distance`<={$max_distance})";
- $count_sql = " SELECT id, latitude, longitude, `name`, `address`, city_id,
- round(
- 2 * 6378.137 * ASIN(
- SQRT(
- POW(
- SIN(
- PI() * ({$longitude} - longitude) / 360
- ),
- 2
- ) + COS(PI() * {$latitude} / 180) * COS(latitude * PI() / 180) * POW(
- SIN(
- PI() * ({$latitude} - latitude) / 360
- ),
- 2
- )
- )
- )
- ,1) AS distance
- FROM `qimall_addons_community_head` WHERE (`mall_id`={$mall_id}) AND (`status`={$status}) ";
- if ($is_has) {
- if (!$head_string) {
- $count_sql .= " AND FALSE ";
- } else {
- $count_sql .= " AND id IN ($head_string) ";
- }
- }
- if (!empty($params['keyword'])) {
- if ($params['type'] == 'address') {
- $count_sql .= " AND `address` LIKE '%" . $params['keyword'] . "%'";
- $sql .= " AND `address` LIKE '%" . $params['keyword'] . "%'";
- } else {
- $count_sql .= " AND `name` LIKE '%" . $params['keyword'] . "%'";
- $sql .= " AND `name` LIKE '%" . $params['keyword'] . "%'";
- }
- }
- if ($nearby) $count_sql .= " HAVING (`distance`<={$max_distance})";
- $sql .= " ORDER BY {$sort} asc";
- $total_data = \Yii::$app->db->createCommand($count_sql)->queryAll();
- $count = count($total_data);
- $sql .= " limit " . ($page - 1) * $limit . ",{$limit}";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- if (!empty($list)) {
- $location_obj = new LocationHelper();
- foreach ($list as &$item) {
- $distance = $location_obj->getDistanceByGeo($item['latitude'], $item['longitude'], $latitude, $longitude);
- $item['distance'] = bcmul(bcmul($distance, 1000, 4), 1, 1);
- }
- }
- unset($item);
- $list_page['list'] = $list;
- $list_page['page'] = $page;
- $list_page['page_size'] = $limit;
- $list_page['count'] = $count;
- $list_page['page_count'] = ceil($count / $limit);
- } else {
- $list_page['list'] = [];
- $list_page['page'] = 0;
- $list_page['page_size'] = 10;
- $list_page['count'] = 0;
- $list_page['page_count'] = 0;
- }
- return $this->success('成功', $list_page);
- }
- /**
- * @Description: 获取用户自提点选择记录
- * @Author: zsan
- * @DateTime 2023-02-16 19:04:07
- * @return yii\web\Response
- */
- public function actionGetUserCommunityList()
- {
- $params = Yii::$app->request->get();
- $valid = Yii::$app->services->validate->validate($params, [
- [['latitude', 'longitude'], 'string'],
- [['attr_ids'], 'safe']
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $mall_id = $this->mall_id;
- $user_id = $this->user_id;
- if(!$user_id){
- return $this->success('成功', ['current'=>[], 'history'=>[]]);
- }
- $latitude = $params['latitude'] ?? 0;
- $longitude = $params['longitude'] ?? 0;
- $list = CommunityUser::lists(
- [
- 'where' => [['mall_id' => $mall_id], ['user_id' => $user_id], ['status' => StatusEnum::ENABLED]],
- 'select' => 'id, user_id, community_id',
- 'order' => 'is_checked DESC',
- 'with' => [
- 'community' => function ($query) {
- return $query->select('id, longitude, latitude, name, logo, address')
- ->andWhere(['status' => StatusEnum::ENABLED]);
- }
- ]
- ]
- );
- $head_arr = [];
- $is_has = false;
- if (isset($params['attr_ids']) && !empty($params['attr_ids'])) {
- $params['attr_ids'] = explode(',', $params['attr_ids']);
- $is_has = true;
- $goods_attr = GoodsAttr::lists([
- 'where' => [
- ['status' => StatusEnum::ENABLED], ['id' => $params['attr_ids']]
- ],
- 'select' => 'goods_id'
- ]);
- $goods_id = array_column($goods_attr, 'goods_id') ?? [];
- $goods_setting = CommunityGoodsSetting::lists([
- 'where' => [['status' => StatusEnum::ENABLED], ['goods_id' => $goods_id], ['enable' => StatusEnum::ENABLED]],
- 'select' => 'goods_id'
- ]);
- $goods_id = array_column($goods_setting, 'goods_id') ?? [];
- $community_goods = CommunityGoods::lists([
- 'where' => [['status' => StatusEnum::ENABLED], ['goods_id' => $goods_id]],
- 'select' => 'community_id',
- ]);
- $head_arr = array_column($community_goods, 'community_id');
- }
- $current = [];
- $history = [];
- $location_obj = new LocationHelper();
- $community_info = CommunityGoods::getLatestCommunityOne($mall_id, $user_id);
- if(isset($community_info['community'])){
- $current = $community_info['community'];
- $distance = $location_obj->getDistanceByGeo($current['latitude'], $current['longitude'], $latitude, $longitude);
- $current['distance'] = bcmul(bcmul($distance, 1000, 4), 1, 1);
- }
- foreach ($list as $k => $v) {
- if (empty($v['community'])) {
- unset($list[$k]);
- continue;
- }
- $distance = $location_obj->getDistanceByGeo($v['community']['latitude'], $v['community']['longitude'], $latitude, $longitude);
- $v['community']['distance'] = bcmul(bcmul($distance, 1000, 4), 1, 1);
- if ($k > 0) {
- if ($is_has) {
- if (!in_array($v['community']['id'], $head_arr)) continue;
- }
- $history[] = $v['community'];
- }
- }
- return $this->success('成功', compact('current', 'history'));
- }
- public function actionGetNearestOne()
- {
- $params = Yii::$app->request->get();
- $valid = Yii::$app->services->validate->validate($params, [
- [['latitude', 'longitude'], 'string'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $longitude = $params['longitude'] ?? 0; //经度
- $latitude = $params['latitude'] ?? 0; //纬度
- $mall_id = $this->mall_id;
- $user_id = $this->user_id;
- $list_page = [];
- $list_page['list'] = [];
- if(!$user_id){
- return $this->success('成功', $list_page);
- }
- $community_info = CommunityGoods::getLatestCommunityOne($mall_id, $user_id);
- if(!$params['latitude'] || !$params['longitude']) return $this->success('成功', $list_page);
- $location_obj = new LocationHelper();
- if ($community_info) {
- $list_page['list'] = array_merge($list_page['list'], $community_info['community'] ?? []);
- $distance = $location_obj->getDistanceByGeo($list_page['list']['latitude'], $list_page['list']['longitude'], $latitude, $longitude);
- $distance = bcmul(bcmul($distance, 1000, 4), 1, 1);
- $list_page['distance'] = $distance;
- } else {
- $page = 1;
- $limit = 1;
- $status = StatusEnum::ENABLED;
- $sql = " SELECT id,`name`,`address`,latitude, longitude, logo, city_id,
- round(
- 2 * 6378.137 * ASIN(
- SQRT(
- POW(
- SIN(
- PI() * ({$longitude} - longitude) / 360
- ),
- 2
- ) + COS(PI() * {$latitude} / 180) * COS(latitude * PI() / 180) * POW(
- SIN(
- PI() * ({$latitude} - latitude) / 360
- ),
- 2
- )
- )
- )
- ,1) AS distance
- FROM `qimall_addons_community_head` WHERE (`mall_id`={$mall_id}) AND (`status`={$status}) ";
- $sql .= " ORDER BY distance asc";
- $sql .= " limit " . ($page - 1) * $limit . ",{$limit}";
- $list = \Yii::$app->db->createCommand($sql)->queryAll();
- $data = [];
- if (!empty($list)) {
- foreach ($list as $item) {
- $distance = $location_obj->getDistanceByGeo($item['latitude'], $item['longitude'], $latitude, $longitude);
- $item['distance'] = bcmul(bcmul($distance, 1000, 4), 1, 1);
- $data = $item;
- break;
- }
- }
- if ($data) {
- CommunityUser::setData([
- 'mall_id' => $mall_id, 'user_id' => $user_id, 'community_id' => $data['id'], 'is_checked' => StatusEnum::ENABLED
- ]);
- }
- $list_page['list'] = $data;
- }
- return $this->success('成功', $list_page);
- }
- }
|