store_id)){ $this->store_id = get_store_id(); } } public function search () { try { $query = ActivityWechatRoom::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->orderBy('id DESC'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $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 = Order::find()->where(['activity_wechat_room_id' => $item['id'], 'is_pay' => 1])->groupBy(['saas_id'])->count(); $item['userCount'] = $userCount ?? 0; $orderCount = Order::find()->where(['activity_wechat_room_id' => $item['id'], 'is_pay' => 1])->count(); $item['orderCount'] = $orderCount ?? 0; $orderSum = Order::find()->where(['activity_wechat_room_id' => $item['id'], 'is_pay' => 1])->sum('pay_price'); $item['orderSum'] = $orderSum ?? 0; } 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 = ActivityWechatRoom::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 = ActivityWechatRoom::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']); $activity['wechat_room_id'] = explode(',',$activity['wechat_room_id']); $goods_ext = ActivityWechatRoomGoods::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 = ActivityWechatRoom::findOne($this->id); if (empty($activity)) { $activity = new ActivityWechatRoom(); $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->wechat_room_id = implode(',', json_decode($this->wechat_room_id,true)); $activity->qr_code = $this->qr_code; if (!$activity->save()) { \Yii::error([__METHOD__, $activity->attributes]); 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; } ActivityWechatRoomGoods::saveList($this->goods_ext, $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); ActivityWechatRoom::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 close () { try { if ($this->ids) { $ids = explode(',', $this->ids); ActivityWechatRoom::updateAll(['end_time' => time() - 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; } }