| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Coupon;
- use app\models\Cat;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\ActivityWechatRoom;
- use app\models\ActivityWechatRoomGoods;
- use app\models\ActivityWechatRoomOrder;
- 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 ActivityWechatRoomForm 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 $goods_name;
- public $wechat_room_id;
- public $qr_code;
- public function rules()
- {
- return [
- [['status', 'id'], 'integer'],
- [['start_time', 'end_time', 'ids', 'name'], 'string'],
- [['goods_name', 'store_id', 'goods_ids', 'goods_ext', 'activity_id',
- 'saas_id', 'user_id', 'qr_code', 'wechat_room_id'], 'safe'],
- ];
- }
- public function init() {
- parent::init();
- if(empty($this->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;
- }
-
-
-
-
-
- }
|