| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace addons\UpgradeGift\common\models;
- use Exception;
- use RuntimeException;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_upgrade_gift_goods".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $user_id 用户id
- * @property int $level 触发领取的等级
- * @property int $status -1已失效(目前只有配置取消才会已失效) 0未领取 1已领取 2放弃领取
- * @property int $receive_time 领取时间
- * @property int $order_id 下单的订单id
- * @property int $goods_id 领取的商品id
- * @property int $created_at
- * @property int $updated_at
- */
- class UpgradeGiftGoods extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_addons_upgrade_gift_goods';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'level', 'status', 'receive_time', 'order_id', 'goods_id', 'attr_id', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'level' => 'Level',
- 'status' => 'Status',
- 'receive_time' => 'Receive Time',
- 'order_id' => 'Order ID',
- 'goods_id' => 'Goods ID',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * @param array $params
- *
- * @return bool|self
- */
- public static function setData(array $params)
- {
- try {
- if (!empty($params['id'])) {
- $model = self::findOne(['id' => $params['id']]);
- if (!$model) {
- throw new RuntimeException('数据不存在');
- }
- if (isset($params['mall_id']) && $model->mall_id != $params['mall_id']) {
- throw new RuntimeException('不允许操作此数据');
- }
- } else {
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '')) {
- throw new RuntimeException($model->getErrorMessage());
- }
- if (!$model->save()) {
- throw new RuntimeException($model->getErrorMessage());
- }
- return $model;
- } catch (Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 获取未领取的礼品记录
- *
- * @param int $userId
- * @param string $field
- *
- * @return array
- */
- public static function getUnclaimedGift(int $userId, string $field = 'level'): array
- {
- return self::find()
- ->where([
- 'user_id' => $userId,
- 'status' => 0
- ])
- ->select($field)
- ->asArray()
- ->all() ?: [];
- }
- /**
- * 返回用户某个等级的礼品是否还未领取
- *
- * @param int $userId
- * @param int $level
- *
- * @return int
- */
- public static function getUnclaimedGiftIdByLevel(int $userId, int $level): int
- {
- return self::find()
- ->where([
- 'user_id' => $userId,
- 'level' => $level,
- 'status' => 0
- ])
- ->select('id')
- ->scalar() ?: 0;
- }
- }
|