| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\controllers;
- use app\models\ActivityNewUser;
- use app\models\ActivityNewUserGoods;
- use app\models\ActivityCutPrice;
- use app\models\ActivityCutPriceGoods;
- use app\models\ActivityCutPriceBanner;
- use app\models\ActivityCutPriceCat;
- use app\models\ActivityCutPriceOrder;
- use app\models\ActivityCutPriceLog;
- use app\models\User;
- use app\modules\admin\models\ActivityCutPriceForm;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\Coupon;
- use app\models\CouponAutoSend;
- use app\models\UserCoupon;
- use app\models\Option;
- use app\constants\OptionSetting;
- use yii\db\Query;
- use app\models\GoodsCat;
- use app\models\Goods;
- use app\models\SaasUser;
- class ActivityController extends BaseController
- {
- public function actionNewUser() {
- $query = ActivityNewUser::find();
- $query->andWhere([
- 'and',
- ['is_delete' => 0, 'status' => 1, 'store_id' => get_store_id(), 'is_platform' => 0],
- ['<', 'start_time', time()],
- ['>', 'end_time', time()],
- ]);
- $user = get_user();
- $activityNewUserList = $query->select('coupon_ids, id, goods_ids')->asArray()->all();
- $coupons = [];
- $goods_ids = [];
- foreach ($activityNewUserList as $newUserItem) {
- $coupons_ = explode(',', $newUserItem['coupon_ids']);
- $coupons = array_merge($coupons, $coupons_);
- $goods_ids_ = explode(',', $newUserItem['goods_ids']);
- $goods_ids = array_merge($goods_ids, $goods_ids_);
- }
- if(!empty($coupons)){
- $coupon_list = Coupon::find()->where(['in', 'id', $coupons])->andWhere(['is_delete' => 0])->asArray()->all();
- $auto_sends = CouponAutoSend::find()->where(['store_id'=>get_store_id(), 'is_delete' => 0, 'status' => 1])->asArray()->all();
- if($auto_sends){
- foreach($coupon_list as &$item){
- $item['auto_send'] = [];
- foreach ($auto_sends as $k){
- $cids = json_decode($k['coupon_id'], true);
- if(in_array($item['id'], $cids)){
- $item['auto_send'] = $k;
- break;
- }
- }
- }
- }
- if($user){
- $user_coupons = UserCoupon::find()->where(['store_id'=>get_store_id(), 'user_id' => $user->id])->asArray()->all();
- foreach($coupon_list as &$item){
- foreach ($user_coupons as $k) {
- if($item['id'] == $k['coupon_id']){
- $item['user_coupon'] = $k;
- break;
- }
- }
- }
- }
- }
- $activityNewUserIds = array_column($activityNewUserList, 'id');
- $goods_ext = ActivityNewUserGoods::find()->where(['activity_id' => $activityNewUserIds, 'is_delete' => 0])->all();
- // var_dump($goods_ext, $activityNewUserIds);die;
- return $this->asJson([
- 'code' => 0,
- 'data' => [
- 'goods_ids' => implode(',', $goods_ids ?: [])
- ],
- 'coupon_list' => $coupon_list ?? [],
- 'goods_ext' => $goods_ext,
- ]);
- }
- public function actionNewUserGoodsList() {
- $activity_id = input_params('activity_id', 0);
- $goods_ext = ActivityNewUserGoods::findAll(['activity_id' => $activity_id, 'is_delete' => 0]);
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'goods_ext' => $goods_ext ?? [],
- ]
- ];
- }
- public function actionNewUserGoodsInfo() {
- $activity_id = input_params('activity_id', 0);
- $goods_id = input_params('goods_id', 0);
- $goods_ext = ActivityNewUserGoods::findOne(['activity_id' => $activity_id, 'goods_id' => $goods_id, 'is_delete' => 0]);
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'goods_ext' => $goods_ext ?? [],
- ]
- ];
- }
-
- public function actionNewUserConf(){
- $conf = Option::get(OptionSetting::ACTIVITY_NEW_USER_CONF, get_store_id(), 'store')['value'];
- if($conf){
- $conf = json_decode($conf, true);
- }else{
- $conf = ['conf' => [
- 'head' => ['bg_color' => '#f00', 'bg_img' => ''],
- 'body' => ['bg_color' => '#f00', 'bg_img' => ''],
- ]];
- }
- return $this->asJson([
- 'code'=>0,
- 'msg'=>'ok',
- 'data' => $conf,
- ]);
- }
-
- public function actionNewUserPushGoods(){
- //{'conf':[{'tab':'推荐',goods_ids:[1,2,3],cat_ids:[5,6,7]}]}
- $conf = Option::get(OptionSetting::ACTIVITY_NEW_USER_GOODS_CONF, get_store_id(), 'store')['value'];
- if($conf){
- $conf = json_decode($conf, true);
- }else{
- $conf = ['conf' => []];
- }
- $gname = input_params('goods_name', '');
- if($gname){
- $gids = [];
- $cids = [];
- foreach($conf['conf'] as $item){
- $item['goods_ids'] && $gids = array_merge($gids, (array)$item['goods_ids']);
- $item['cat_ids'] && $cids = array_merge($cids, (array)$item['cat_ids']);
- }
- if($cids){
- $cids = \app\models\Cat::getCatListByPids(get_store_id(), $cids);
- }
- $query = Goods::find();
- $query->andWhere([
- 'and',
- ['is_delete' => 0, 'status' => 1, 'store_id' => get_store_id()],
- ['like', 'name', $gname],
- ]);
- $idwhere = ['or', 0];
- if($gids){
- $idwhere[] = ['id' => $gids];
- }
- if($cids){
- $cquery = (new Query())->select('goods_id')->from(GoodsCat::tableName())->where(['cat_id' => $cids]);
- $idwhere[] = ['id' => $cquery];
- }
- $query->andWhere($idwhere);
- $gres = $query->select(['id'])->asArray()->column();
- }
- return $this->asJson([
- 'code'=>0,
- 'msg'=>'ok',
- 'data' => $conf,
- 'search_goods_ids' => $gres,
- 'q' => $query ? $query->createCommand()->getRawSql() : '',
- ]);
- }
-
-
-
-
- public function actionCutPriceConf(){
- $form = new ActivityCutPriceForm();
- $form->store_id = get_store_id();
- $res = $form->conf();
- return $this->asJson($res);
- }
- public function actionCutPriceInfo() {
- $store_id = get_store_id();
- $activity_id = intval(input_params('activity_id', 0));
- if($activity_id){
- $info = ActivityCutPrice::activityAt($activity_id);
- if(!$info){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '活动不存在',
- ]);
- }
- }else{
- $alist = ActivityCutPrice::activityAtList($store_id, true);
- $activity_id = array_column($alist, 'id');
- }
- $gcatQuery = ActivityCutPriceGoods::find()->select('cat_id')->where(['activity_id' => $activity_id, 'is_delete' => 0]);
- $cat = ActivityCutPriceCat::find()->where(['id' => $gcatQuery, 'is_delete' => 0, 'is_show' => 1])
- ->select('id,name')->orderBy('sort DESC')->all();
- $banner = ActivityCutPriceBanner::find()->where(['is_delete' => 0])
- ->select('id,name,url,pic_url')->orderBy('sort DESC')->all();
- return $this->asJson([
- 'code' => 0,
- 'data' => $info,
- 'cat' => $cat,
- 'banner' => $banner,
- ]);
- }
-
- public function actionCutPriceGoodsList() {
- $store_id = get_store_id();
- $activity_id = intval(input_params('activity_id', 0));
- if(!$activity_id){
- $alist = ActivityCutPrice::activityAtList($store_id, true);
- $activity_id = array_column($alist, 'id');
- }
- $cat_id = input_params('cat_id', 0);
- $goods_name = input_params('goods_name', '');
- $goods_ext_query = ActivityCutPriceGoods::find()->where(['activity_id' => $activity_id, 'cat_id' => $cat_id, 'is_delete' => 0]);
- if($goods_name){
- $goods_query = Goods::find()->select('id')->where(['like', 'name', $goods_name]);
- $goods_ext_query->andWhere(['goods_id' => $goods_query]);
- }
- $goods_ext = pagination_make($goods_ext_query);
- $goods_ids = array_column($goods_ext['list'], 'goods_id');
- foreach($goods_ext['list'] as &$item){
- $userCount = ActivityCutPriceOrder::find()->where(['activity_id' => $item['activity_id'], 'goods_id' => $item['goods_id']])->groupBy(['saas_id'])->count();
- $item['userCount'] = $userCount;
- }
- $goods_list = Goods::find()->where(['id' => $goods_ids, 'status' => 1, 'is_delete' => 0])
- ->select('id,name,cover_pic,price')->all();
- $goods_list = ActivityCutPriceForm::sortGoods($goods_ext['list'], $goods_list);
- return $this->asJson([
- 'code' => 0,
- 'goods_ids' => $goods_ids,
- 'goods_ext' => $goods_ext,
- 'goods_list' => $goods_list,
- // 'q' => $goods_ext_query->createCommand()->getRawSql(),
- ]);
- }
-
-
-
- public function actionCutPriceOrderByGoodsId() {
- $activity_id = input_params('activity_id', 0);
- $goods_id = input_params('goods_id', 0);
- $user_id = get_user_id();
- $cutOrder = ActivityCutPriceForm::getHasOrder($user_id, get_saas_user_id(), $activity_id, $goods_id);
- return $this->asJson([
- 'code' => 0,
- 'cutOrder' => $cutOrder,
- ]);
- }
- public function actionCutPriceOrderCreate() {
- $activity_id = input_params('activity_id', 0);
- $goods_id = input_params('goods_id', 0);
- $mch_list = input_params('mch_list', '');
- $form = new ActivityCutPriceForm();
- $form->store_id = get_store_id();
- $form->is_platform = 1;
- $user_id = get_user_id();
- $saas_id = get_saas_user_id();
- $res = $form->orderCreate($user_id, $saas_id, $activity_id, $goods_id, $mch_list);
- return $this->asJson($res);
- }
- public function actionCutPriceOrderList() {
- $form = new ActivityCutPriceForm();
- $form->store_id = get_store_id();
- $form->saas_id = get_saas_user_id();
- $form->user_id = get_user_id();
- $form->status = input_params('status', -1);
- $form->is_platform = 1;
- $res = $form->orderList();
- return $this->asJson($res);
- }
-
- public function actionCutPriceOrderInfo() {
- $order_id = input_params('order_id', 0);
- $order = ActivityCutPriceOrder::findOne(['id' => $order_id, 'is_delete' => 0]);
- if(!$order){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '砍价订单不存在',
- ]);
- }
- $store_id = $order->store_id;
- //帮砍
- $form = new ActivityCutPriceForm();
- $form->store_id = $store_id;
- $user_id = get_user_id() ?: 0;
- $saas_id = get_saas_user_id();
- $user = User::findOne(['is_delete' => 0, 'binding' => get_saas_user()->mobile, 'store_id' => $store_id]);
- if ($user) {
- $user_id = $user->id;
- }
- $logSave = $form->logSave($user_id, $saas_id, $order_id);
-
- $log = ActivityCutPriceLog::findOne([
- 'order_id' => $order_id,
- 'saas_id' => $saas_id,
- ]);
- $order = ActivityCutPriceOrder::find()->where(['id' => $order_id, 'is_delete' => 0])->one();
- $status = ActivityCutPriceOrder::getStatus($order);
- $activity = ActivityCutPrice::findOne($order['activity_id']);
- $goods_ext = ActivityCutPriceGoods::findOne(['activity_id' => $order['activity_id'], 'goods_id' => $order['goods_id'], 'is_delete' => 0]);
- $goods_info = Goods::find()->where(['id' => $order['goods_id']])->one();
- if ($goods_info) {
- $pic = $goods_info['cover_pic'];
- $index = strstr($goods_info['cover_pic'], 'http');
- if (!$index) {
- $pic = 'https:' . $goods_info['cover_pic'];
- }
- $goods_info['cover_pic'] = $pic;
- }
- $saasUser = SaasUser::find()->where(['id' => $order['saas_id']])->select('name,avatar')->asArray()->one();
- $mch_list = json_decode($order['order_mch_list'], true);
- $attr_id_list = array_column($mch_list[0]['goods_list'][0]['attr'], 'attr_id');
- sort($attr_id_list);
- $attr_list = Attr::find()->alias('a')
- ->select('ag.id AS attr_group_id,ag.attr_group_name,a.id AS attr_id,a.attr_name')
- ->leftJoin(['ag' => AttrGroup::tableName()], 'a.attr_group_id=ag.id')
- ->where(['a.id' => $attr_id_list, 'ag.store_id' => $store_id])
- ->asArray()->all();
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $order,
- 'status' => $status,
- 'attr_list' => $attr_list,
- 'activity' => $activity,
- 'activity_finished' => $activity->end_time < time() ? 1 : 0,
- 'goods_ext' => $goods_ext,
- 'goods_info' => $goods_info,
- 'saasUser' => $saasUser,
- 'logSave' => $logSave,
- 'log' => $log,
- 'is_self_order' => $user_id == $order->user_id ? 1 : 0,
- ]);
- }
-
- public function actionCutPriceLogList() {
- $order_id = input_params('order_id', 0);
- $query = ActivityCutPriceLog::find()->alias('acp')
- ->leftJoin(['su' => SaasUser::tableName()], 'acp.saas_id = su.id')
- ->where(['order_id' => $order_id, 'acp.is_delete' => 0])
- ->select('acp.user_id,acp.saas_id,acp.created_at,acp.cut_price,su.name,su.avatar');
- $list = $query->asArray()->all();
- $sum = array_sum(array_column($list, 'cut_price'));
- return $this->asJson([
- 'code' => 0,
- 'data' => $list,
- 'count' => count($list),
- 'sum' => $sum,
- // 'q' => $query->createCommand()->getRawSql(),
- ]);
- }
- }
|