| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\jobs\storeSync\DiyCommon;
- use app\models\Coupon;
- use app\models\Cat;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\ActivityCutPrice;
- use app\models\ActivityCutPriceBanner;
- use app\models\ActivityCutPriceCat;
- use app\models\ActivityCutPriceGoods;
- use app\models\ActivityCutPriceOrder;
- use app\models\ActivityCutPriceLog;
- use app\models\Store;
- use app\models\StoreSyncExtLog;
- use app\models\User;
- use yii\base\Model;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\models\SaasUser;
- use app\models\Order;
- use app\modules\client\models\v1\order\OrderSubmitPreviewForm;
- use app\models\Attr;
- use app\models\AttrGroup;
- class ActivityCutPriceForm extends Model
- {
- public $store_id;
- public $saas_id;
- public $user_id;
- public $id;
- public $activity_id;
- public $ids;
- public $name;
- public $start_time;
- public $end_time;
- public $status;
- public $goods_ext;
- public $goods_ids;
- public $coupon_ids;
- public $goods_name;
- public $coupon_name;
- public $min_price;
- public $limit_time;
- public $cut_count;
- public $front_num;
- public $first_money_min;
- public $first_money_max;
- public $last_money_min;
- public $last_money_max;
- public $buy;
- public $is_platform = 0;
- public $is_platform_audit = 0;
- public $store_name;
- public function rules()
- {
- return [
- [['status', 'id', 'is_platform', 'is_platform_audit'], 'integer'],
- [['start_time', 'end_time', 'ids', 'name', 'store_name'], 'string'],
- [['goods_name', 'coupon_name', 'store_id', 'goods_ids', 'goods_ext', 'coupon_ids', 'buy_limit', 'activity_id',
- 'min_price', 'limit_time', 'cut_count', 'front_num', 'first_money_min', 'first_money_max',
- 'last_money_min', 'last_money_max', 'saas_id', 'user_id', 'buy'], 'safe'],
- ];
- }
- public function init() {
- parent::init();
- if(empty($this->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()
- ];
- }
- }
-
- }
|