| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace addons\LuckyGroup\common\models;
- use addons\LuckyGroup\common\enums\LuckyGroupEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_lucky_group_goods".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $goods_id 商品id
- * @property int $activity_id 活动ID
- * @property int $join_num 参团人数
- * @property int $open_times 成团期数,每开一次算一期
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class LuckyGroupGoods extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_lucky_group_goods}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'goods_id', 'activity_id', 'join_num', 'open_times', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'goods_id' => '商品id',
- 'activity_id' => '活动ID',
- 'join_num' => '参团人数',
- 'open_times' => '成团期数,每开一次算一期',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getActivity()
- {
- return $this->hasOne(LuckyGroupActivity::class, ['id' => 'activity_id'])->select('id,attend_user_type,title,virtual_num,attend_award_setting,group_num,random_win_num');
- }
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select('id,goods_name,price,original_price,cover_pic');
- }
- public static function setData(int $mall_id, array $data)
- {
- if (empty($data['id']) && (empty($data['goods_id']) && empty($data['activity_id']))) {
- self::$static_error = '商品 id 和活动 id 不能为空';
- return false;
- }
- if (!empty($data['id'])) {
- $model = self::find()
- ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']])
- ->one();
- if (empty($model)) {
- self::$static_error = '拼团活动商品数据异常';
- return false;
- }
- } elseif (!empty($data['goods_id']) && !empty($data['activity_id'])) {
- $model = self::find()
- ->where(['mall_id' => $mall_id, 'goods_id' => $data['goods_id'], 'activity_id' => $data['activity_id']])
- ->one();
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- $model->status = StatusEnum::ENABLED;
- $model->mall_id = $mall_id;
- $model->join_num = 0;
- $model->open_times = 0;
- unset($data['id']);
- }
- } else {
- $model = new self();
- $model->loadDefaultValues();
- $model->status = StatusEnum::ENABLED;
- $model->mall_id = $mall_id;
- $model->join_num = 0;
- $model->open_times = 0;
- unset($data['id']);
- }
- foreach ($data as $key => $val) {
- $model->$key = $val;
- }
- if (!$model->save()) {
- self::$static_error = '保存数据失败:' . $model->getErrorMessage();
- return false;
- }
- return $model;
- }
- public static function getGoodsList($mall_id,$goods_ids)
- {
- $where[] = ['mall_id' => $mall_id];
- if(!empty($goods_ids)) $where[] = ['goods_id' => $goods_ids];
- $where[] = ['status' => StatusEnum::ENABLED];
- $params['where'] = $where;
- $params['order'] = 'id DESC';
- $params['with'] = ['goods' => function ($query) {
- $query->where(['status' => StatusEnum::ENABLED]);
- }];
- $params['limit'] = 10;
- $params['select'] = 'id,activity_id,goods_id,created_at';
- $list = LuckyGroupGoods::lists($params);
- if (!empty($list)) {
- $activity_id_arr = array_column($list, 'activity_id');
- $activity_arr = LuckyGroupActivity::lists(['where' => [['id' => $activity_id_arr]], 'index' => 'id']);
- foreach ($list as &$item) {
- $item['img'] = $item['goods']['cover_pic'];
- $item['title'] = $item['goods']['goods_name'];
- $item['sales_num'] = $item['goods']['sales_num'] ?? 0;
- $item['price'] = $item['goods']['price'];
- $item['original_price'] = $item['goods']['original_price'];
- $item['tips1'] = '';
- $item['tips2'] = '';
- if (isset($activity_arr[$item['activity_id']])) {
- $item['tips1'] = $activity_arr[$item['activity_id']]['group_num'] . '人拼团,' . $activity_arr[$item['activity_id']]['random_win_num'] . '人拼中';
- $activity_arr[$item['activity_id']]['attend_user_type'];
- $attend_award_setting = json_decode($activity_arr[$item['activity_id']]['attend_award_setting'], true);
- $string = '';
- $balance_name = \Yii::$app->services->setting->mall->get($mall_id, 'balance.balance_name') ?? '余额';
- $score_name = \Yii::$app->services->setting->mall->get($mall_id, 'score.score_name') ?? '积分';
- if (!empty($attend_award_setting['score'])) {
- $string .= $attend_award_setting['score'] . $score_name.',';
- }
- if (!empty($attend_award_setting['balance'])) {
- $string .= $attend_award_setting['balance'] . $balance_name;
- }
- if ($string && !empty($activity_arr[$item['activity_id']]['attend_user_type'])) {
- $string = trim($string, ',');
- $item['tips2'] = LuckyGroupEnum::getAttendMap($activity_arr[$item['activity_id']]['attend_user_type']) . '立得' . $string;
- }
- }
- $item['original_price'] = $item['goods']['original_price'];
- $item['subtitle'] = '';
- unset($item['goods']);
- }
- return $list;
- }
- }
- }
|