'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; } }