store_id)){ $this->store_id = get_store_id(); } } public function search () { try { $query = ActivityCutPrice::find()->where(['is_delete' => 0, 'store_id' => get_store_id()]); if ((int)$this->status === 1) {//未开始 $query->andWhere(['>' , 'start_time', time()]); } if ((int)$this->status === 2) {//进行中 $query->andWhere(['AND', ['<' , 'start_time', time()], ['>' , 'end_time', time()]]); } if ((int)$this->status === 3) { //已结束 $query->andWhere(['<' , 'end_time', time()]); } if (!empty($this->name)) { //名称 $query->andWhere(['LIKE' , 'name', $this->name]); } if (!empty($this->start_time)) { $query->andWhere(['>' , 'end_time', strtotime($this->start_time)]); } if (!empty($this->end_time)) { $query->andWhere(['<' , 'start_time', strtotime($this->end_time)]); } $query->andWhere(['is_platform' => $this->is_platform]); $query->orderBy('id DESC'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['is_platform_audit'] = (int)$item['is_platform_audit']; $item['publish'] = $item['status']; //获取活动状态 if ($item['start_time'] > time()) { $item['status'] = 1; } if ($item['start_time'] < time() && $item['end_time'] > time()) { $item['status'] = 2; } if ($item['end_time'] < time()) { $item['status'] = 3; } //格式化时间 $item['start_time'] = date("Y-m-d H:i:s", $item['start_time']); $item['end_time'] = date("Y-m-d H:i:s", $item['end_time']); $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']); $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']); $userCount = ActivityCutPriceOrder::find()->where(['activity_id' => $item['id']])->andWhere(['>', 'used_order_id', 0])->groupBy(['saas_id'])->count(); $item['userCount'] = $userCount; $orderCount = ActivityCutPriceOrder::find()->where(['activity_id' => $item['id']])->andWhere(['>', 'used_order_id', 0])->count(); $item['orderCount'] = $orderCount; $orderSum = ActivityCutPriceOrder::find()->where(['activity_id' => $item['id']])->andWhere(['>', 'used_order_id', 0])->sum('pay_price'); $item['orderSum'] = $orderSum; } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $pagination['list'], 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function listSelect() { $query = ActivityCutPrice::find()->where(['is_delete' => 0]); $this->store_id && $query->andWhere(['store_id' => $this->store_id]); if($this->name){ $query->andWhere(['like', 'name', $this->name]); } if (!empty($this->start_time)) { $query->andWhere(['>' , 'end_time', strtotime($this->start_time)]); } if (!empty($this->end_time)) { $query->andWhere(['<' , 'start_time', strtotime($this->end_time)]); } $query->select('id, name'); $query->orderBy('id desc'); $res = $query->asArray()->all(); return [ 'code' => 0, 'msg' => 'success', 'data' => $res, ]; } public static function sortGoods($goods_ext, $goods){ $res = []; foreach($goods_ext as $eitem){ foreach($goods as $gitem){ if($gitem['id'] == $eitem['goods_id']){ $res[] = $gitem; } } } return $res; } public function getInfo() { try { $activity = ActivityCutPrice::find()->where(['id' => $this->id])->one(); if ($activity) { $activity_goods = Goods::find()->where(['in', 'id', explode(',', $activity->goods_ids)])->andWhere(['is_delete' => 0])->asArray()->all(); $activity['start_time'] = date("Y-m-d H:i:s", $activity['start_time']); $activity['end_time'] = date("Y-m-d H:i:s", $activity['end_time']); $goods_ext = ActivityCutPriceGoods::findAll(['activity_id' => $activity->id, 'is_delete' => 0]); $activity_goods = self::sortGoods($goods_ext, $activity_goods); } return [ 'code' => 0, 'msg' => '获取成功', 'data' => [ 'activity_goods' => $activity_goods ?? [], 'goods_ext' => $goods_ext ?? [], 'activity' => $activity ?: [], ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() . $e->getFile() . $e->getLine() ]; } } public function save () { $t = \Yii::$app->db->beginTransaction(); try { if (!$this->name || !$this->start_time || !$this->end_time) { throw new \Exception("请将参数填充完整"); } $activity = ActivityCutPrice::findOne($this->id); if (empty($activity)) { $activity = new ActivityCutPrice(); $activity->store_id = $this->store_id; } $activity->name = $this->name; $activity->start_time = strtotime($this->start_time); $activity->end_time = strtotime($this->end_time); $activity->goods_ids = $this->goods_ids; $activity->min_price = $this->min_price ?: 0; $activity->limit_time = $this->limit_time; $activity->cut_count = $this->cut_count; $activity->front_num = $this->front_num; $activity->first_money_min = $this->first_money_min; $activity->first_money_max = $this->first_money_max; $activity->last_money_min = $this->last_money_min; $activity->last_money_max = $this->last_money_max; $activity->is_platform = intval($this->is_platform); $activity->buy = $this->buy; if (!$activity->save()) { throw new \Exception(array_shift($activity->getFirstErrors())); } if(!empty($this->goods_ext)){ foreach($this->goods_ext as &$item){ $item['activity_id'] = $activity->id; $item['store_id'] = $this->store_id; } $result = ActivityCutPriceGoods::saveList($this->goods_ext, $activity->id, $is_platform_audit); if ($result['code'] !== 0) { throw new \Exception($result['msg']); } if ($is_platform_audit) { $activity->is_platform_audit = 0; $activity->save(); } } (new DiyCommon)->JobBehaviors($this->store_id, StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE, [$activity->id]); $t->commit(); return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function del () { try { if ($this->ids) { $ids = explode(',', $this->ids); if (count($ids) === 1) { (new DiyCommon)->JobBehaviors($this->store_id, StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE, $ids); } ActivityCutPrice::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]); } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function conf(){ $conf = Option::get(OptionSetting::ACTIVITY_CUT_PRICE_CONF, $this->store_id, 'store')['value']; if($conf){ $conf = json_decode($conf, true); }else{ $conf = ['conf' => [ 'rule' => '砍价活动', ]]; } return [ 'code'=>0, 'msg'=>'ok', 'data' => $conf, ]; } public function confSave($conf){ if(!is_array($conf)){ $conf = json_decode($conf, true); } $data = ['conf' => $conf]; Option::set(OptionSetting::ACTIVITY_CUT_PRICE_CONF, json_encode($data), $this->store_id, 'store'); return [ 'code'=>0, 'msg'=>'保存成功' ]; } public function catSave($id = 0, $name = '', $pic_url = '', $sort = 100, $is_show = 1) { try{ $model = $id ? ActivityCutPriceCat::findOne($id) : new ActivityCutPriceCat(); $model->store_id = $this->store_id; $model->name = $name; $model->pic_url = $pic_url; $model->sort = $sort; $model->is_show = $is_show; $save = $model->save(); if(!$save){ \Yii::error([__METHOD__, $model->attributes]); throw new \Exception('操作失败,' . array_shift($model->getFirstErrors())); } return [ 'code' => 0, 'msg' => '操作成功', ]; }catch(\Exception $e){ \Yii::error([__METHOD__, $e]); return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } public function catList() { $query = ActivityCutPriceCat::find()->where(['is_delete' => 0]); $this->store_id && $query->andWhere(['store_id' => $this->store_id]); if($this->name){ $query->andWhere(['like', 'name', $this->name]); } if($this->status >= 0){ $query->andWhere(['is_show' => $this->status]); } $query->orderBy('sort desc'); $res = pagination_make($query); return [ 'code' => 0, 'msg' => 'success', 'data' => $res, 'q' => $query->createCommand()->getRawSql(), ]; } public function catStatus () { try { if ($this->ids) { $ids = explode(',', $this->ids); if (in_array($this->status, [0, 1])) { ActivityCutPriceCat::updateAll(['is_show' => $this->status], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]); } if ((int)$this->status === 2) { ActivityCutPriceCat::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]); } } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function catInfo($id = 0) { $model = ActivityCutPriceCat::findOne($id); return [ 'code' => 0, 'msg' => '操作成功', 'data' => $model, ]; } public function bannerSave($id = 0, $activity_id = 0, $name = '', $pic_url = '', $url = '', $sort = 100) { try{ $model = $id ? ActivityCutPriceBanner::findOne($id) : new ActivityCutPriceBanner(); $model->store_id = $this->store_id; $model->activity_id = $activity_id; $model->name = $name; $model->pic_url = $pic_url; $model->url = $url; $model->sort = $sort; $save = $model->save(); if(!$save){ \Yii::error([__METHOD__, $model->attributes]); throw new \Exception('操作失败,' . array_shift($model->getFirstErrors())); } return [ 'code' => 0, 'msg' => '操作成功', ]; }catch(\Exception $e){ \Yii::error([__METHOD__, $e]); return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } public function bannerList() { $query = ActivityCutPriceBanner::find()->where(['is_delete' => 0]); $this->store_id && $query->andWhere(['store_id' => $this->store_id]); if($this->activity_id){ $query->andWhere(['activity_id' => $this->activity_id]); } if($this->name){ $query->andWhere(['like', 'name', $this->name]); } $query->orderBy('sort desc'); $res = pagination_make($query); return [ 'code' => 0, 'msg' => 'success', 'data' => $res, ]; } public function bannerDel () { try { if ($this->ids) { $ids = explode(',', $this->ids); ActivityCutPriceBanner::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]); } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function getGoodsAttrItem($goods_ext, $mch_list) { // var_dump($mch_list); $attr_id_list = array_column($mch_list[0]['goods_list'][0]['attr'], 'attr_id'); sort($attr_id_list); $goods = Goods::findOne($goods_ext['goods_id']); if (empty($goods)) { return null; } $attr = $goods['attr']; $attr_rows = json_decode($attr, true); if (empty($attr_rows)) { return null; } foreach ($attr_rows as $i => $attr_row) { $key = []; foreach ($attr_row['attr_list'] as $j => $attr) { $key[] = $attr['attr_id']; } sort($key); if (!array_diff($attr_id_list, $key)) { if (!$attr_rows[$i]['price']) { return null; } return $attr_rows[$i]; } } return null; } public function getGoodsExtAttrItem($goods_ext, $mch_list) { // var_dump($mch_list); $attr_id_list = array_column($mch_list[0]['goods_list'][0]['attr'], 'attr_id'); sort($attr_id_list); $attr = $goods_ext['attr']; $attr_rows = json_decode($attr, true); if (empty($attr_rows)) { return null; } foreach ($attr_rows as $i => $attr_row) { $key = []; foreach ($attr_row['attr_list'] as $j => $attr) { $key[] = $attr['attr_id']; } sort($key); if (!array_diff($attr_id_list, $key)) { if (!$attr_rows[$i]['price']) { return null; } return $attr_rows[$i]; } } return null; } //进行中的订单 public static function getHasOrder($user_id = 0, $saas_id = 0, $activity_id = 0, $goods_id = 0) { $saas_user = SaasUser::findOne($saas_id); $user_id = User::findOne(['binding'=>$saas_user->mobile,'store_id'=>get_store_id(),'is_delete'=>0])->id ?: $user_id; $query = ActivityCutPriceOrder::find()->where(['is_delete' => 0]); $query->andWhere([ 'user_id' => $user_id, 'saas_id' => $saas_id, 'activity_id' => $activity_id, 'goods_id' => $goods_id, ]); $payQuery = Order::find()->select('id')->where(['is_pay' => 0, 'trade_status' => Order::ORDER_FLOW_DEFAULT]); $query->andWhere([ 'or', ['>', 'end_time', time()], [ 'and', ['>', 'used_order_id', 0], ['used_order_id' => $payQuery], ], ]); $query->orderBy('id DESC'); $order = $query->one(); return $order; } public function orderCreate($user_id = 0, $saas_id = 0, $activity_id = 0, $goods_id = 0, $mch_list = '') { try{ $mch_list = json_decode($mch_list, true); $activity = ActivityCutPrice::activityAt($activity_id); if(!$activity){ throw new \Exception('砍价活动不存在'); } $store_id = $activity['store_id'] ?:get_store_id(); if (!$user_id) { $saas_user = SaasUser::findOne($saas_id); if ($saas_user) { $user = User::findOne(['binding' => $saas_user->mobile, 'store_id' => $store_id, 'is_delete' => 0]); $user_id = $user->id ?: 0; } } $goods_ext = ActivityCutPriceGoods::findOne(['activity_id' => $activity_id, 'goods_id' => $goods_id, 'is_delete' => 0]); if(!$goods_ext){ throw new \Exception('砍价商品不存在'); } $mchPrice = $this->getGoodsAttrItem($goods_ext, $mch_list); $mchPriceExt = $this->getGoodsExtAttrItem($goods_ext, $mch_list); // var_dump($mchPrice);die; if(!$mchPriceExt['price']){ \Yii::error([__METHOD__, $activity_id, $goods_id, $mch_list, $mchPriceExt]); throw new \Exception('砍价商品价格获取失败'); } $price = $mchPrice['price']; $min_price = $mchPriceExt['price']; $model = new ActivityCutPriceOrder(); $model->store_id = $store_id; $model->activity_id = $activity_id; $model->user_id = $user_id; $model->saas_id = $saas_id; $model->goods_id = $goods_id; $model->activity_goods_id = $goods_ext->id; $model->goods_id = $goods_id; $model->order_mch_list = json_encode($mch_list); $model->total_price = $price; $model->pay_price = $price; $model->buy = $activity['buy']; $model->min_price = $min_price; $model->is_platform = $this->is_platform ?: 0; $end_time = time() + $activity['limit_time'] * 3600; $model->end_time = $end_time > $activity['end_time'] ? $activity['end_time'] : $end_time; $save = $model->save(); if(!$save){ \Yii::error([__METHOD__, $model->attributes]); throw new \Exception('操作失败,' . array_shift($model->getFirstErrors())); } return [ 'code' => 0, 'msg' => '操作成功', 'data' => $model->id, ]; }catch(\Exception $e){ \Yii::error([__METHOD__, $e]); return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } public function orderList() { $query = ActivityCutPriceOrder::find() ->alias('o') ->leftJoin(['a' => ActivityCutPrice::tableName()], 'o.activity_id = a.id') ->where(['o.is_delete' => 0, 's.is_delete' => 0]) ->leftJoin(['s' => Store::tableName()], 'o.store_id = s.id'); // ->leftJoin(['to' => Order::tableName()],'o.id=to.activity_cut_price_order_id') // ->andWhere(['to.is_delete' => 0, 'to.user_delete' => 0]); $this->saas_id && $query->andWhere(['o.saas_id' => $this->saas_id]); $this->user_id && $query->andWhere(['o.user_id' => $this->user_id]); if($this->activity_id){ $query->andWhere(['o.activity_id' => $this->activity_id]); } if ($this->is_platform) { $query->andWhere(['o.is_platform' => 1]); } else { $query->andWhere(['o.store_id' => get_store_id()]); $this->store_id && $query->andWhere(['o.store_id' => $this->store_id]); } if($this->goods_name){ $goods_query = Goods::find()->select('id')->where(['like', 'name', $this->goods_name]); $query->andWhere(['o.goods_id' => $goods_query]); } if ((int)$this->status === 0) {//进行中 $query->andWhere(['>' , 'o.end_time', time()]); } if ((int)$this->status === 1) { //已结束 $query->andWhere(['<' , 'o.end_time', time()]); } if ((int)$this->status === 2) { //已失败 $query->andWhere(['o.buy' => 0]); $query->andWhere(['<' , 'o.end_time', time()]); $query->andWhere('o.pay_price > o.min_price'); } if ((int)$this->status === 10) { //待付款 $payQuery = Order::find()->select('id')->where(['is_pay' => 0]); $query->andWhere(['>', 'o.used_order_id', 0]); $query->andWhere(['o.used_order_id' => $payQuery]); } if ((int)$this->status === 11) { //已付款 $payQuery = Order::find()->select('id')->where(['is_pay' => 1]); $query->andWhere(['o.used_order_id' => $payQuery]); } $query->select('o.*')->orderBy('o.id desc'); $res = pagination_make($query); foreach($res['list'] as &$item){ $store = Store::findOne($item['store_id']); $list['store_name'] = $store->name; $list['logo'] = $store->logo ?: Option::get(OptionSetting::STORE_LOGO, $list['store_id'], 'store', ''); $list['store_id'] = $store->id; $item['status'] = ActivityCutPriceOrder::getStatus($item); $goods_info = Goods::find()->where(['id' => $item['goods_id']])->select('id,name,cover_pic')->one(); $goods_cat = GoodsCat::find()->alias('gc') ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id=c.id') ->where([ 'gc.goods_id' => $item['goods_id'] ]) ->select(['c.name']) ->asArray() ->all(); $mch_list = json_decode($item['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' => $this->store_id,]) ->asArray()->all(); $saasUser = SaasUser::findOne($item['saas_id']); $activity = ActivityCutPrice::findOne($item['activity_id']); $item['attr_list'] = $attr_list; $item['goods_info'] = $goods_info; $item['goods_cat'] = $goods_cat; $item['saasUser'] = $saasUser; $item['activity'] = $activity; $item['activity_finished'] = $activity->end_time < time() ? 1 : 0; } return [ 'code' => 0, 'msg' => 'success', 'data' => $res, 'q' => $query->createCommand()->getRawSql(), ]; } //判断商城订单自动取消时是否允许取消 public static function orderCanCancel($mall_order) { $res = true; $order = ActivityCutPriceOrder::findOne(['used_order_id' => $mall_order['id']]); if($order){ $activity = ActivityCutPrice::findOne($order->activity_id); if($activity){ //活动没结束不允许取消 if($activity->end_time > time()){ $res = false; } } } return $res; } //生成商城订单时回调 public static function orderUsed($order_id, $mall_order) { try{ $order = ActivityCutPriceOrder::findOne($order_id); if(!$order){ throw new \Exception('砍价订单异常' . $order_id); } if($order['used_order_id']){ throw new \Exception('砍价订单已被使用' . $order['used_order_id']); } if($order['user_id'] != $mall_order['user_id']){ throw new \Exception('砍价订单用户异常' . $order['user_id'] . $order['saas_id'] . $mall_order['user_id'] . $mall_order['saas_id']); } $activity = ActivityCutPrice::findOne($order['activity_id']); if($activity['end_time'] <= time()){ throw new \Exception('已过砍价活动截止时间'); } if ($order['buy'] == 0 && $order['pay_price'] > $order['min_price']) { throw new \Exception('砍价失败,没有砍到最低价'); } $order->used_order_id = $mall_order->id; if($order->end_time > time()){ $order->end_time = time(); } $save = $order->save(); if(!$save){ \Yii::error([__METHOD__, $order->attributes]); throw new \Exception('操作失败,' . array_shift($order->getFirstErrors())); } //减活动库存 $goods_ext = ActivityCutPriceGoods::findOne($order->activity_goods_id); if(!$goods_ext){ \Yii::error([__METHOD__, $order->attributes]); throw new \Exception('砍价商品信息不存在'.$order->activity_goods_id); } $mch_list = json_decode($order->order_mch_list, true); $attr = array_column($mch_list[0]['goods_list'][0]['attr'], 'attr_id'); sort($attr); $ext_attrs = json_decode($goods_ext['attr'], true); foreach ($ext_attrs as &$ext_attr) { $ext_attr_id = array_column($ext_attr['attr_list'], 'attr_id'); if (empty(array_diff($ext_attr_id, $attr))) { if($ext_attr['num'] < 1){ throw new \Exception('砍价商品库存不足'.$ext_attr['num']); } $ext_attr['num']--; } } $goods_ext->attr = json_encode($ext_attrs); $save = $goods_ext->save(); if(!$save){ \Yii::error([__METHOD__, $goods_ext->attributes]); throw new \Exception('操作失败,' . array_shift($goods_ext->getFirstErrors())); } return [ 'code' => 0, 'msg' => '操作成功', ]; }catch(\Exception $e){ \Yii::error([__METHOD__, $e]); return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } public function orderDel () { try { if ($this->ids) { $ids = explode(',', $this->ids); ActivityCutPriceOrder::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]); } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //帮砍 public function logSave($user_id = 0, $saas_id = 0, $order_id = 0) { try{ $has = ActivityCutPriceLog::findOne([ 'order_id' => $order_id, 'saas_id' => $saas_id, ]); if($has){ throw new \Exception('已经砍过'); } $order = ActivityCutPriceOrder::findOne(['id' => $order_id, 'is_delete' => 0]); if(!$order){ throw new \Exception('订单异常'); } if($order['used_order_id']){ throw new \Exception('砍价已转订单'.$order['used_order_id']); } $activity = ActivityCutPrice::findOne($order['activity_id']); if($activity['end_time'] <= time()){ throw new \Exception('已过砍价活动截止时间'); } if($order['end_time'] <= time()){ throw new \Exception('已过砍价商品截止时间'); } if($order['pay_price'] <= $order['min_price']){ throw new \Exception('砍价已到最底价'); } $count = ActivityCutPriceLog::find()->where(['order_id' => $order_id])->count(); if($activity['cut_count'] && $count >= $activity['cut_count']){ throw new \Exception('帮砍人数已达上限'); } $cut_price = 0; if($count < $activity['front_num']){ $cut_price = sprintf('%.2f', $activity['first_money_min'] + mt_rand()/mt_getrandmax() * ($activity['first_money_max']-$activity['first_money_min'])); // \Yii::error([__METHOD__, $cut_price, mt_rand()/mt_getrandmax(), $activity['first_money_min'], $activity['first_money_max']]); }else{ $cut_price = sprintf('%.2f', $activity['last_money_min'] + mt_rand()/mt_getrandmax() * ($activity['last_money_max']-$activity['last_money_min'])); // \Yii::error([__METHOD__, $cut_price, mt_rand()/mt_getrandmax(), $activity['last_money_min'], $activity['last_money_max']]); } if($order['pay_price'] - $cut_price <= $order['min_price']){ $cut_price = $order['pay_price'] - $order['min_price']; $order->end_time = time(); // \Yii::error([__METHOD__, $cut_price]); } $model = new ActivityCutPriceLog(); $model->store_id = $this->store_id; $model->order_id = $order_id; $model->user_id = $user_id; $model->saas_id = $saas_id; $model->cut_price = $cut_price; $save = $model->save(); if(!$save){ \Yii::error([__METHOD__, $model->attributes]); throw new \Exception('操作失败,' . array_shift($model->getFirstErrors())); } $order->pay_price -= $cut_price; $save = $order->save(); if(!$save){ \Yii::error([__METHOD__, $order->attributes]); throw new \Exception('操作失败,' . array_shift($order->getFirstErrors())); } return [ 'code' => 0, 'msg' => '操作成功', 'data' => $model, ]; }catch(\Exception $e){ \Yii::error([__METHOD__, $e]); return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } /** * 审核列表 */ public function auditList() { try { $query = ActivityCutPrice::find()->alias('a')->where(['a.is_delete' => 0, 'a.is_platform' => 1, 's.is_delete' => 0]); $query->leftJoin(['s' => Store::tableName()], 's.id = a.store_id'); if (intval($this->status) === 1) {//未开始 $query->andWhere(['AND', ['>' , 'a.start_time', time()], ['a.status' => 1]]); } if (intval($this->status) === 2) {//进行中 $query->andWhere(['AND', ['<' , 'a.start_time', time()], ['>' , 'a.end_time', time()], ['a.status' => 1]]); } if (intval($this->status) === 3) { //已结束 $query->andWhere(['AND', ['<' , 'a.end_time', time()], ['a.status' => 1]]); } if (intval($this->status) === 4) { //已终止 $query->andWhere(['a.status' => 0]); } if ($this->is_platform_audit > -1 && $this->is_platform_audit !== null) { $query->andWhere(['a.is_platform_audit' => $this->is_platform_audit]); } if (!empty($this->name)) { //名称 $query->andWhere(['LIKE' , 'a.name', $this->name]); } if (!empty($this->store_name)) { //名称 $query->andWhere(['LIKE' , 's.name', $this->store_name]); } //只有平台运营的商城或者单店铺的无独立小程序才展示商盟的营销活动 if (\Yii::$app->prod_is_dandianpu()) { $store_id = Option::find()->where(['group' => 'store', 'name' => 'self_mini'])->select('store_id')->column(); $query->andWhere(['NOT IN', 's.id', $store_id]); } else { $query->andWhere(['s.business_model' => 2]); } if (!empty($this->start_time) && !empty($this->end_time)) { //时间筛选 $query->andWhere(['OR', ['AND', ['<=' , 'a.start_time', strtotime($this->start_time)], ['>=' , 'a.end_time',strtotime($this->end_time)] ], ['AND', ['<=' , 'a.start_time', strtotime($this->start_time)], ['<=' , 'a.end_time',strtotime($this->end_time)], ['>=' , 'a.end_time',strtotime($this->start_time)] ], ['AND', ['>=' , 'a.start_time', strtotime($this->start_time)], ['<=' , 'a.end_time',strtotime($this->end_time)] ], ['AND', ['>=' , 'a.start_time', strtotime($this->start_time)], ['>=' , 'a.end_time',strtotime($this->end_time)], ['<=' , 'a.start_time',strtotime($this->end_time)] ], ]); } $query->select('a.*, s.name store_name, s.logo, s.id store_id')->orderBy('a.created_at desc'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['logo'] = $item['logo'] ?: Option::get(OptionSetting::STORE_LOGO, $item['store_id'], 'store', '')['value']; //获取活动状态 if (intval($item['status']) === 1) { if ($item['start_time'] > time()) { $item['status'] = 1; } if ($item['start_time'] < time() && $item['end_time'] > time()) { $item['status'] = 2; } if ($item['end_time'] < time()) { $item['status'] = 3; } } else { $item['status'] = 4; } $item['is_platform_audit'] = (int)$item['is_platform_audit']; $item['publish'] = $item['status']; //格式化时间 $item['start_time'] = date("Y-m-d H:i:s", $item['start_time']); $item['end_time'] = date("Y-m-d H:i:s", $item['end_time']); $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']); $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']); $userCount = ActivityCutPriceOrder::find()->where(['activity_id' => $item['id']])->andWhere(['>', 'used_order_id', 0])->groupBy(['saas_id'])->count(); $item['userCount'] = $userCount; $orderCount = ActivityCutPriceOrder::find()->where(['activity_id' => $item['id']])->andWhere(['>', 'used_order_id', 0])->count(); $item['orderCount'] = $orderCount; $orderSum = ActivityCutPriceOrder::find()->where(['activity_id' => $item['id']])->andWhere(['>', 'used_order_id', 0])->sum('pay_price'); $item['orderSum'] = $orderSum; } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $pagination['list'], 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 处理审核 */ public function auditHandle() { try { $id = $this->id; $status = $this->status; $cut_activity = ActivityCutPrice::findOne(['id' => $id, 'is_platform' => 1, 'is_delete' => 0]); if (!$cut_activity) { throw new \Exception('活动不存在'); } if (in_array($status, [1, 2])) { if (intval($cut_activity->is_platform_audit) !== 0) { throw new \Exception('活动已经审核'); } $cut_activity->is_platform_audit = $status; } if (intval($status) === 3) { $cut_activity->is_delete = 1; } if (in_array($status, [4, 5])) { $cut_activity->status = intval($status - 4); } if (!$cut_activity->save()) { throw new \Exception(json_encode($cut_activity->errors, JSON_UNESCAPED_UNICODE)); } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }