| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- namespace addons\GroupBuy\common\models;
- use addons\GroupBuy\common\enums\GroupBuyEnum;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use common\models\goods\GoodsAttr;
- use common\models\goods\GoodsMarketing;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_group_buy_goods".
- *
- * @property int $id 拼团商品活动id
- * @property int $mall_id 商城id
- * @property int $cate_id 拼团分类
- * @property int $goods_id 商品id;对应goods主表
- * @property string $start_at 拼团开始时间
- * @property string $end_at 拼团结束时间
- * @property int $effective_time 拼团有效时间(分钟)
- * @property int $group_size 成团人数
- * @property int $give_score 团长赠送积分
- * @property int $give_balance 团长赠送余额
- * @property int $virtual_group_type 虚拟成团类型【1全部 2拼团人数大于】
- * @property int $virtual_group_size 虚拟成团人数
- * @property int $gt_group_size 拼团人数大于N,则虚拟成团
- * @property int $goods_stock 拼团库存
- * @property int $limit_buy_num 限制购买数量
- * @property string $commander_level_limit 团长等级限制
- * @property string $user_level_limit 团员等级限制
- * @property int $is_virtual 是否开启虚拟成团
- * @property int $group_status 拼团商品状态;0未开始;1开团中;已结束2
- * @property int $total_num 已团件数
- * @property int $total_orders 成团订单数
- * @property int $total_orders_price 成团订单金额
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- * @property int $is_initiator_free 发起人免费开团
- * @property int $virtual_group_sub_type "参团结束开团" 出现的选项
- * @property int $expired_auto_complete_min_num 当参团人数≥ x 人,拼团有效期结束后,自动成团
- * @property int $is_show_participant_nickname 是否显示团长昵称 0否 1是
- * @property int $is_show_virtual_avatar 是否显示虚拟拼团头像 0否 1是
- * @property string title 拼团活动名称
- */
- class GroupBuyActive extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_group_buy_active}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id','cate_id', 'goods_id','title'], 'required'],
- [['mall_id', 'cate_id','goods_id', 'effective_time', 'group_size', 'give_score', 'give_balance', 'virtual_group_type', 'virtual_group_size', 'gt_group_size', 'goods_stock', 'limit_buy_num', 'is_virtual', 'group_status', 'total_num', 'total_orders', 'status', 'created_at', 'updated_at','start_at','end_at', 'is_initiator_free', 'virtual_group_sub_type', 'expired_auto_complete_min_num', 'is_show_participant_nickname', 'is_show_virtual_avatar'], 'integer'],
- [['total_orders_price'], 'number'],
- [['restrict_rule_num'], 'integer'],
- [['commander_level_limit', 'user_level_limit','title'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '拼团商品活动id',
- 'mall_id' => '商城id',
- 'cate_id' => '拼团分类',
- 'goods_id' => '商品id;对应goods主表',
- 'start_at' => '拼团开始时间',
- 'end_at' => '拼团结束时间',
- 'effective_time' => '拼团有效时间(分钟)',
- 'group_size' => '成团人数',
- 'give_score' => '团长赠送积分',
- 'give_balance' => '团长赠送余额',
- 'virtual_group_type' => '虚拟成团类型【1全部 2拼团人数大于】',
- 'virtual_group_size' => '虚拟成团人数',
- 'gt_group_size' => '拼团人数大于N,则虚拟成团',
- 'goods_stock' => '拼团库存',
- 'limit_buy_num' => '限制购买数量',
- 'commander_level_limit' => '团长等级限制',
- 'user_level_limit' => '团员等级限制',
- 'is_virtual' => '是否开启虚拟成团',
- 'group_status' => '拼团商品状态;0未开始;1开团中;已结束2',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Update At',
- 'is_initiator_free' => '发起人是否免费',
- 'virtual_group_sub_type' => 'Virtual Group Sub Type',
- 'expired_auto_complete_min_num' => 'Expired Auto Complete Min Num',
- 'is_show_participant_nickname' => '是否显示参团人员昵称',
- 'is_show_virtual_avatar' => '是否显示虚拟拼团头像',
- 'restrict_rule_num' => '参团限制次数',
- 'title' => '拼团活动名称',
- ];
- }
- // 关联模型
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id']);
- }
- public function getActiveGoodsAttr()
- {
- return $this->hasMany(GroupBuyActiveGoodsAttr::class, ['group_buy_active_id' => 'id'])->andWhere(['status' => [StatusEnum::ENABLED]]);
- }
- public static function setData(array $params)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- $param = $params['group_buy_goods'];
- $param['mall_id'] = $params['mall_id'];
- $model = new self();
- $model->loadDefaultValues();
- if (!empty($param['start_at']))$param['start_at'] = strtotime($param['start_at']);
- if (!empty($param['end_at'])) $param['end_at'] = strtotime($param['end_at']);
- if (!$model->load($param, '')) throw new \Exception($model->getErrorMessage());
- if ($model->start_at < time()) $model->group_status = GroupBuyEnum::OPEN_GROUP;
- if ($model->end_at < time()) $model->group_status = GroupBuyEnum::GROUP_FINISH;
- if($param['is_virtual']){
- if($param['virtual_group_type']==1 || $param['virtual_group_type'] == 3){
- $model->virtual_group_size = $param['group_size']-1;
- }else{
- $model->virtual_group_size = $param['group_size'] - $param['gt_group_size'];
- }
- }
- if($params['form']['is_attr']) $model->goods_stock = array_sum(array_column($params['form']['attr'],'stock'));
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- $params['group_buy_active_id'] = $model->id;
- $res = GroupBuyActiveGoodsAttr::setData($params);
- if ($res == false) throw new \Exception(GroupBuyActiveGoodsAttr::getStaticError());
- $param['marketing_id'] = $model->id;
- $param['marketing_type'] = GroupBuyEnum::ADDONS_NAME;
- $res = GoodsMarketing::setData($param);
- if ($res == false) throw new \Exception(GoodsMarketing::getStaticError());
- $t->commit();
- return $model;
- } catch (\Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function delData($params)
- {
- $t = Yii::$app->db->beginTransaction();
- try {
- $res = GroupBuyOrder::findOne(['group_buy_active_id'=>$params['id'],'order_status'=>[GroupBuyEnum::JOINING],'status'=>StatusEnum::ENABLED]);
- if($res) throw new \Exception('该活动有会员参团');
- $res = self::updateAll(['status' => StatusEnum::DELETE], ['mall_id' => $params['mall_id'], 'id' => $params['id']]);
- if ($res == false) throw new \Exception(GroupBuyActiveGoodsAttr::getStaticError());
- GroupBuyActiveGoodsAttr::updateAll(['status' => StatusEnum::DELETE], ['mall_id' => $params['mall_id'], 'group_buy_active_id' => $params['id']]);
- $params['marketing_id'] = $params['id'];
- $params['marketing_type'] = GroupBuyEnum::ADDONS_NAME;
- $params['status'] = StatusEnum::DELETE;
- $res = GoodsMarketing::setData($params);
- if ($res == false) throw new \Exception(GoodsMarketing::getStaticError());
- $t->commit();
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function getActiveListByDiy($mall_id,$post){
- $where[] = ['mall_id' => $mall_id];
- $where[] = ['status' => StatusEnum::ENABLED];
- $where[] = ['group_status' => [GroupBuyEnum::OPEN_GROUP]];
- if (!empty($post['keyword'])) {
- $goods_list = Goods::lists(['where' => [['mall_id' => $mall_id], ['like', 'goods_name', trim($post['keyword']) . '%', false], ['status' => StatusEnum::ENABLED]], 'select' => 'id']);
- $goods_id_arr = array_column($goods_list, 'id');
- $where[] = ['goods_id' => $goods_id_arr];
- }
- $params['where'] = $where;
- $params['order'] = 'id DESC';
- $params['with'] = ['goods' => function ($query) {
- $query->where(['status' => StatusEnum::ENABLED]);
- }];
- $params['limit'] = 10;
- $params['select']='id,goods_id,created_at,title';
- $list = GroupBuyActive::listPage($params,false,true);
- if(!empty($list['list'])){
- foreach ($list['list'] as &$item){
- $item['img'] = $item['goods']['cover_pic'];
- $item['sales_num'] = $item['goods']['sales_num'];
- $item['price'] = '';
- $item['original_price'] = '';
- $item['subtitle'] = '';
- unset($item['goods']);
- unset($item['goods_id']);
- }
- }
- return $list;
- }
- public static function checkActive($id, $mall_id)
- {
- $active = self::findOne(['mall_id' => $mall_id, 'id' => $id]);
- if ($active->start_at < time()) $active->group_status = GroupBuyEnum::OPEN_GROUP;
- if ($active->end_at < time()) $active->group_status = GroupBuyEnum::GROUP_FINISH;
- $active->save();
- }
- /**
- * @desc:活动列表
- * @Author: hua
- * @Date: 2021/12/22
- * @Time: 18:41
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $where
- * @param false $is_page
- * @return array|mixed|void
- */
- public static function getActiveList($where,$is_page=false){
- $params['where'] = $where;
- $params['order'] = 'id DESC';
- $params['select'] = 'id,goods_id,start_at,end_at,group_size,total_num,group_status,title';
- $params['with'] = [
- 'goods' => function ($query) {
- $query->where(['status' => StatusEnum::ENABLED])->select('id,goods_name,cover_pic');
- },
- 'activeGoodsAttr' => function ($query) {
- $query->where(['status' => StatusEnum::ENABLED])->select('attr_id,group_buy_active_id,group_buy_price,goods_id')
- ->with(['goodsAttr']);
- }
- ];
- $function = function(&$item){
- $item['group_buy_price'] = $item['activeGoodsAttr'][0]['group_buy_price'];
- $item['price'] = $item['activeGoodsAttr'][0]['goodsAttr']['price'];
- $item['sheng'] = $item['price']>$item['group_buy_price']?bcsub($item['price'] , $item['group_buy_price'],2):0;
- $item['goods_name'] = $item['goods']['goods_name'];
- $item['cover_pic'] = $item['goods']['cover_pic'];
- $item['goods_id'] = $item['goods']['id'];
- $item['title'] = empty($item['title']) ? $item['goods_name'] : $item['title'];
- unset($item['activeGoodsAttr']);
- unset($item['goods']);
- return $item;
- };
- if($is_page){
- $list = GroupBuyActive::listPage($params, false);
- if ($list === false) return [];
- if(!empty($list['list'])){
- foreach ($list['list'] as &$item){
- $function($item);
- }
- }
- }else{
- // $params['index']= 'id';
- $list = GroupBuyActive::lists($params);
- if ($list === false) return [];
- if(!empty($list)){
- foreach ($list as &$item){
- $function($item);
- }
- }
- }
- return $list;
- }
- }
|