$this->mall_id], ['user_id' => $this->user_id], ['status' => StatusEnum::ENABLED] ]; switch ($coupon_status) { case 1: $common_where[] = ['is_use' => 1]; break; case 2: $common_where[] = ['coupon_status' => 2]; $common_where[] = ['is_use' => 0]; break; case 3: $common_where[] = ['coupon_status' => [3, 4]]; $common_where[] = ['is_giving' => 0]; $common_where[] = ['is_use' => 0]; break; case 4: $common_where[] = ['is_giving' => 1]; $common_where[] = ['giving_status' => 2]; break; } return $common_where; } public function actionMyCouponSummary() { $coupon_status = \Yii::$app->request->get('coupon_status', 1); $common_where = $this->getCommonWhere($coupon_status); $data = [ 'platform_coupon_nums' => 0, 'store_coupon_nums' => 0, 'mch_coupon_nums' => 0, ]; $platform_where = $common_where; $platform_where[] = ['mch_id' => '']; $platform_where[] = ['status' => StatusEnum::ENABLED]; $platform_counts = AddonsCouponUserRelate::getOne($platform_where, true, [], false, 'count(*) as counts'); if (!empty($platform_counts)) $data['platform_coupon_nums'] = $platform_counts['counts']; $mch_where = $common_where; $mch_where[] = ['<>', 'mch_id', '']; $mch_counts = AddonsCouponUserRelate::getOne($mch_where, true, [], false, 'count(*) as counts'); if (!empty($mch_counts)) $data['mch_coupon_nums'] = $mch_counts['counts']; if (MallAddons::isInstall($this->mall_id, 'Store')) { $store_counts = StoreCouponUserRelate::getOne($common_where, true, [], false, 'count(*) as counts'); if (!empty($mch_counts)) $data['store_coupon_nums'] = $store_counts['counts']; } return $this->success('操作成功', $data); } public function actionMyCouponList() { $params = \Yii::$app->request->get(); $res = Yii::$app->services->validate->validate($params, [ [['from_type', 'coupon_status'], 'required'], [['page', 'limit'], 'safe'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $coupon_status = $params['coupon_status'] ?? 1; $from_type = $params['from_type'] ?? 1; $common_where = $this->getCommonWhere($coupon_status); $page_list = []; switch ($from_type) { case 1: case 3: $where = $common_where; $where[] = ['status' => StatusEnum::ENABLED]; $select = 'id,coupon_id,start_time,end_time,is_use,is_giving,giving_status,mch_id,updated_at'; if ($from_type == 1) { $where[] = ['mch_id' => '']; } else { $where[] = ['<>', 'mch_id', '']; } $params['where'] = $where; $params['order'] = 'id desc'; $params['select'] = $select; $params['with'] = ['coupon' => function ($query) { $query->select('id,name,min_price,sub_price,appoint_type,is_giving,type,discount,is_giving_use, coupon_status') ->with(['goodsRelate', 'cateRelate']); }]; $page_list = AddonsCouponUserRelate::listPage($params); if (!empty($page_list['list'])) { $mch_ids = array_column($page_list['list'], 'mch_id'); $mch_list = []; if(MallAddons::isInstall($this->mall_id, 'Mch')) { $mch_list = Mch::lists(['where' => [['id' => $mch_ids]], 'index' => 'id']); } if ($params['coupon_status'] == 4) { $user_coupon_ids = array_column($page_list['list'], 'id'); $share_logs = AddonsCouponShareLog::lists(['where' => [['user_coupon_id' => $user_coupon_ids], ['giving_status' => 2]], 'index' => 'user_coupon_id', 'select' => 'user_coupon_id,receive_user_id']); $receive_user_ids = array_column($share_logs, 'receive_user_id'); $receive_user_list = User::lists(['where' => [['id' => $receive_user_ids]], 'index' => 'id', 'select' => 'id,mobile,nickname']); } // 通过转增得到的ids $giftedIds = AddonsCoupon::getGiftedList($this->mall_id, $page_list['list'], 1); foreach ($page_list['list'] as &$item) { $item['mch_name'] = ''; $item['logo_img'] = ''; $item['store_id'] = ''; $item['give_name'] = ''; $item['give_mobile'] = ''; $item['from_type'] = $from_type; $item['coupon']['goods_ids'] = ''; $item['coupon']['cate_ids'] = ''; $item['use_time'] = $item['updated_at']; if (isset($mch_list[$item['mch_id']])) { $item['mch_name'] = $mch_list[$item['mch_id']]['store_name']; $item['logo_img'] = $mch_list[$item['mch_id']]['logo_img']; } $item['coupon']['is_goods_single'] = 0; if (count($item['coupon']['goodsRelate']) == 1) $item['coupon']['is_goods_single'] = 1; foreach ($item['coupon']['goodsRelate'] as $goodsRelate) { $item['coupon']['goods_ids'] .= $goodsRelate['goods_id'] . ','; } foreach ($item['coupon']['cateRelate'] as $cateRelate) { $item['coupon']['cate_ids'] .= $cateRelate['cate_id'] . ','; } $item['coupon']['goods_ids'] = trim($item['coupon']['goods_ids'], ','); $item['coupon']['cate_ids'] = trim($item['coupon']['cate_ids'], ','); unset($item['coupon']['goodsRelate']); unset($item['coupon']['cateRelate']); if ($params['coupon_status'] == 4) { if ($item['is_giving']) { $item['give_time'] = $item['updated_at']; } if (isset($share_logs[$item['id']])) { $receive_user_id = $share_logs[$item['id']]['receive_user_id']; if (isset($receive_user_list) && isset($receive_user_list[$receive_user_id])) { $item['give_name'] = $receive_user_list[$receive_user_id]['nickname']; $item['give_mobile'] = $receive_user_list[$receive_user_id]['mobile']; } } } $item['is_show_use_btn'] = true; // 返回是否显示使用按钮 if (!empty($item['coupon']['is_giving']) && !empty($item['coupon']['is_giving_use']) && !in_array($item['id'], $giftedIds)) { $item['is_show_use_btn'] = false; } } } break; case 2: $params['where'] = $common_where; $params['order'] = 'id desc'; $select = 'id,coupon_id,start_time,end_time,is_use,is_giving,giving_status,store_id,from_type,updated_at, coupon_status'; $params['select'] = $select; $page_list = StoreCouponUserRelate::listPage($params); if (!empty($page_list['list'])) { $platform_coupon_ids = $store_coupon_ids = []; foreach ($page_list['list'] as &$item) { switch ($item['from_type']) { case 1: $platform_coupon_ids[] = $item['coupon_id']; break; case 2: $store_coupon_ids[] = $item['coupon_id']; break; } } $platform_coupon_list = AddonsCoupon::lists(['where' => [['id' => $platform_coupon_ids]], 'index' => 'id', 'select' => 'id,name,min_price,sub_price,appoint_type,is_giving,type,discount,is_giving_use', 'with' => ['storeGoodsRelate']]); $store_coupon_list = StoreCoupon::lists(['where' => [['id' => $store_coupon_ids]], 'with' => ['goodsRelate', 'cateRelate'], 'index' => 'id', 'select' => 'id,name,min_price,sub_price,appoint_type,is_giving,type,discount,exchange_voucher_type']); $store_ids = array_column($page_list['list'], 'store_id'); $store_list = Store::lists(['where' => [['id' => $store_ids]], 'index' => 'id']); if ($params['coupon_status'] == 4) { $user_coupon_ids = array_column($page_list['list'], 'id'); $share_logs = StoreCouponShareLog::lists(['where' => [['user_coupon_id' => $user_coupon_ids]], 'index' => 'user_coupon_id', 'select' => 'user_coupon_id,receive_user_id']); $receive_user_ids = array_column($share_logs, 'receive_user_id'); $receive_user_list = User::lists(['where' => [['id' => $receive_user_ids]], 'index' => 'id', 'select' => 'id,mobile,nickname']); } foreach ($page_list['list'] as &$item) { $item['store_name'] = ''; $item['logo_img'] = ''; $item['give_name'] = ''; $item['give_mobile'] = ''; $item['use_time'] = $item['updated_at']; if (isset($store_list[$item['store_id']])) { $item['store_name'] = $store_list[$item['store_id']]['store_name']; $item['logo_img'] = $store_list[$item['store_id']]['logo_img']; } if ($params['coupon_status'] == 4) { if ($item['is_giving']) { $item['give_time'] = $item['updated_at']; } if (isset($share_logs[$item['id']])) { $receive_user_id = $share_logs[$item['id']]['receive_user_id']; if (isset($receive_user_list) && isset($receive_user_list[$receive_user_id])) { $item['give_name'] = $receive_user_list[$receive_user_id]['nickname']; $item['give_mobile'] = $receive_user_list[$receive_user_id]['mobile']; } } } $item['is_show_use_btn'] = true; $item['coupon'] = []; switch ($item['from_type']) { case 1: if (isset($platform_coupon_list[$item['coupon_id']])) { $item['coupon'] = $platform_coupon_list[$item['coupon_id']]; $item['coupon']['is_goods_single'] = 0; if (count($item['coupon']['storeGoodsRelate']) == 1) $item['coupon']['is_goods_single'] = 1; foreach ($item['coupon']['storeGoodsRelate'] as $goodsRelate) { $item['coupon']['goods_ids'] .= $goodsRelate['goods_id'] . ','; } $item['coupon']['goods_ids'] = trim($item['coupon']['goods_ids'], ','); unset($item['coupon']['storeGoodsRelate']); // 存储平台优惠券,用于查询这个优惠券是否允许显示 $platformCouponUserRelate[] = &$item; } !isset($item['coupon']['goods_ids']) && $item['coupon']['goods_ids'] = ''; $item['coupon']['cate_ids'] = ''; break; case 2: if (isset($store_coupon_list[$item['coupon_id']])) { $item['coupon'] = $store_coupon_list[$item['coupon_id']]; } foreach ($item['coupon']['goodsRelate'] as $goodsRelate) { $item['coupon']['goods_ids'] .= $goodsRelate['goods_id'] . ','; } foreach ($item['coupon']['cateRelate'] as $cateRelate) { $item['coupon']['cate_ids'] .= $cateRelate['cate_id'] . ','; } $item['coupon']['goods_ids'] = trim($item['coupon']['goods_ids'], ','); $item['coupon']['cate_ids'] = trim($item['coupon']['cate_ids'], ','); unset($item['coupon']['goodsRelate']); unset($item['coupon']['cateRelate']); break; } } // 通过转增得到的ids if (!empty($platformCouponUserRelate)) { $giftedIds = AddonsCoupon::getGiftedList($this->mall_id, $platformCouponUserRelate, 2); array_walk($platformCouponUserRelate, function (&$item) use ($giftedIds) { // 返回是否显示使用按钮 if (!empty($item['coupon']['is_giving']) && !empty($item['coupon']['is_giving_use']) && !in_array($item['id'], $giftedIds)) { $item['is_show_use_btn'] = false; } }); } } break; } $settingService = new SettingService(['mall_id' => $this->mall_id]); $config = $settingService->getSetting(); $page_list = array_merge($page_list, $config); return $this->success('操作成功', $page_list); } public function actionDetail() { $params = \Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['from_type', 'coupon_id'], 'required'], [['store_id', 'user_coupon_id'], 'safe'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $select = 'id,name,min_price,sub_price,appoint_type,rule,expire_type,expire_day,effect_days,begin_at ,end_at'; switch ($params['from_type']) { case 1: case 3: $coupon = AddonsCoupon::getOne([['id' => $params['coupon_id']], 'mall_id' => $this->mall_id], true, false, $select); if (!empty($params['user_coupon_id'])) { if (!empty($params['store_id'])) { $coupon_user_relate = StoreCouponUserRelate::findOne(['id' => $params['user_coupon_id']]); } else { $coupon_user_relate = AddonsCouponUserRelate::findOne(['id' => $params['user_coupon_id']]); } } break; case 2: $coupon = StoreCoupon::getOne([['id' => $params['coupon_id']], 'mall_id' => $this->mall_id], true, false, $select); if (!empty($params['user_coupon_id'])) $coupon_user_relate = StoreCouponUserRelate::findOne(['id' => $params['user_coupon_id']]); break; } $coupon['start_time'] = $coupon['begin_at']; $coupon['end_time'] = $coupon['end_at']; if (!empty($coupon_user_relate)) { $coupon['start_time'] = $coupon_user_relate['start_time']; $coupon['end_time'] = $coupon_user_relate['end_time']; } return $this->success('操作成功', $coupon); } public function actionDetailList() { $params = \Yii::$app->request->get(); $res = Yii::$app->services->validate->validate($params, [ [['from_type', 'coupon_id'], 'required'], [['longitude', 'latitude', 'page', 'limit'], 'safe'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $select = 'id,name,min_price,sub_price,appoint_type,rule,type'; $coupon = []; $goods_page_list = []; $store_page_list = []; $mch_page_list = []; $isInstallStore = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE); switch ($params['from_type']) { case 1: case 3: $select .= ',mch_id,store_id'; $with = ['goodsRelate', 'cateRelate']; if ($isInstallStore) { $with[] = 'storeGoodsRelate'; } $coupon = AddonsCoupon::getOne([['id' => $params['coupon_id']], 'mall_id' => $this->mall_id], true, $with, false, $select); switch ($coupon['appoint_type']) { case 1: $cate_ids = []; foreach ($coupon['cateRelate'] as $cateRelate) { $cate_ids[] = $cateRelate['cate_id']; } $goods_cate_relate_list = GoodsCateRelate::lists(['where' => [['cate_id' => $cate_ids]]]); $goods_ids = array_column($goods_cate_relate_list, 'goods_id'); $params['where'][] = ['id' => $goods_ids, 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1]; $goods_page_list = Goods::listPage($params); break; case 2: $goods_ids = []; foreach ($coupon['goodsRelate'] as $goodsRelate) { $goods_ids[] = $goodsRelate['goods_id']; } $params['where'][] = ['id' => $goods_ids, 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1]; $params['select'] = 'id,goods_name,cover_pic,price,original_price'; $goods_page_list = Goods::listPage($params); break; case 4: $store_ids = explode(',', $coupon['store_id']); $params['where'][] = ['id' => $store_ids]; $params['select'] = 'id,store_name,logo_img,longitude,latitude,sales_volume,praise'; $store_page_list = Store::listPage($params); if (!empty($store_page_list['list'])) { foreach ($store_page_list['list'] as &$value) { $distance = (new LocationHelper())->getDistanceByGeo($params['latitude'], $params['longitude'], $value['latitude'], $value['longitude']); $value['distance'] = $distance; } } break; case 5: $mch_id = explode(',', $coupon['mch_id']); $params['where'][] = ['id' => $mch_id]; $params['select'] = 'id,store_name,logo_img,sales_volume,praise'; $mch_page_list = Mch::listPage($params); break; case AddonsCouponEnum::APPOINT_TYPE6: if (!$isInstallStore) { break; } $params['where'][] = ['id' => array_column($coupon['storeGoodsRelate'] ?? [], 'goods_id'), 'status' => StatusEnum::ENABLED, 'check_status' => 1, 'is_on_sale' => 1]; $params['select'] = 'id,store_id,goods_name,cover_pic,price,original_price'; $goods_page_list = StoreGoods::listPage($params); if ($coupon['type'] == AddonsCouponEnum::EXCHANGE) { foreach ($goods_page_list['list'] as &$value) { $value['price'] = 0; } } break; } break; case 2: $select .= ',store_id, exchange_voucher_type'; $coupon = StoreCoupon::getOne([['id' => $params['coupon_id']], 'mall_id' => $this->mall_id], true, ['goodsRelate', 'cateRelate'], false, $select); switch ($coupon['appoint_type']) { case 1: $cate_ids = []; foreach ($coupon['cateRelate'] as $cateRelate) { $cate_ids[] = $cateRelate['cate_id']; } $goods_cate_relate_list = StoreGoodsCateRelate::lists(['where' => [['cate_id' => $cate_ids]]]); $goods_ids = array_column($goods_cate_relate_list, 'goods_id'); $params['where'][] = ['id' => $goods_ids, 'status' => StatusEnum::ENABLED, 'check_status' => 1, 'is_on_sale' => 1]; $params['select'] = 'id,store_id,goods_name,cover_pic,price,original_price'; $goods_page_list = StoreGoods::listPage($params); break; case 2: $goods_ids = []; foreach ($coupon['goodsRelate'] as $goodsRelate) { $goods_ids[] = $goodsRelate['goods_id']; } $params['where'][] = ['id' => $goods_ids, 'status' => StatusEnum::ENABLED, 'check_status' => 1, 'is_on_sale' => 1]; $params['select'] = 'id,store_id,goods_name,cover_pic,price,original_price'; $goods_page_list = StoreGoods::listPage($params); if ($coupon['type'] == StoreEnum::EXCHANGE) { $goods_attr_relates = StoreGoodsAttr::find()->where(['goods_id' => $goods_ids, 'status' => StatusEnum::ENABLED])->select('goods_id, id')->indexBy('goods_id')->asArray()->all(); foreach ($goods_page_list['list'] as &$value) { if ($coupon['exchange_voucher_type'] == StoreEnum::ALL_DEDUCTION) { $value['price'] = 0; } else { $value['price'] = bcsub($value['price'], $coupon['sub_price'], 2); if ($value['price'] < 0) { $value['price'] = 0; } } $value['attr_id'] = $goods_attr_relates[$value['id']]['id'] ?? 0; } } break; case 3: $params['where'][] = ['id' => $coupon['store_id']]; $params['select'] = 'id,store_name,logo_img,longitude,latitude,sales_volume,praise'; $store_page_list = Store::listPage($params); if (!empty($store_page_list['list'])) { foreach ($store_page_list['list'] as &$value) { $distance = (new LocationHelper())->getDistanceByGeo($params['latitude'], $params['longitude'], $value['latitude'], $value['longitude']); $value['distance'] = bcdiv($distance, 1000, 1); } } break; } break; } unset($coupon['goodsRelate']); unset($coupon['cateRelate']); unset($coupon['storeGoodsRelate']); return $this->success('操作成功', ['coupon' => $coupon, 'goods_page_list' => $goods_page_list, 'store_page_list' => $store_page_list, 'mch_page_list' => $mch_page_list]); } public function actionCenter() { $params = \Yii::$app->request->get(); $res = Yii::$app->services->validate->validate($params, [ [['from_type',], 'required'], [['page', 'limit', 'appoint_type'], 'safe'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $where = [ ['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED], ['is_public_receive' => StatusEnum::ENABLED], ['coupon_status' => AddonsCouponEnum::STATUS_RECEIVING], ]; $params['order'] = 'id desc'; switch ($params['from_type']) { case 1: $where[] = ['mch_id' => '']; $class = AddonsCoupon::class; break; case 2: $class = StoreCoupon::class; break; case 3: $where[] = ['<>', 'mch_id', '']; $class = AddonsCoupon::class; break; } $params['where'] = $where; $list = $class::listPage($params, false, true); return $this->success('操作成功', $list); } /** * @return mixed|void|null */ public function actionJumpCheck() { $params = Yii::$app->request->get(); $res = Yii::$app->services->validate->validate($params, [ [['user_coupon_id', 'goods_id', 'from_type'], 'required'], [['user_coupon_id', 'goods_id', 'from_type'], 'integer'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $params['from_type'] = $params['from_type'] == 2 ? 2 : 1; if (!AddonsCoupon::jumpCheck($this->getCommonWhere(2), $this->mall_id, $this->user_id, $params)) { $this->error(AddonsCoupon::getStaticError()); return; } $this->success(); } }