Authored by xiaofeng.yao@yoho.cn

新增主播发红包接口

<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "{{%redbag_type}}".
*
* @property string $type_id
* @property string $name
* @property integer $create_time
* @property integer $update_time
*/
class RedbagType extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%redbag_type}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name'], 'required'],
[['create_time', 'update_time'], 'integer'],
[['name'], 'string', 'max' => 100],
[['name'], 'unique'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'type_id' => '类型ID',
'name' => '类型名称',
'create_time' => '创建时间',
'update_time' => '更新时间',
];
}
public function behaviors() {
return [
[
'class' => \yii\behaviors\TimestampBehavior::className(),
'createdAtAttribute' => 'create_time',
'updatedAtAttribute' => 'update_time',
]
];
}
/**
* 返回所有主播[id=>id.' '.name,...]的列表数组
* @return string
*/
public static function getId2name(){
$list = self::find()->all();
$ret = [];
foreach ($list as $model){
$ret[$model->type_id] = $model->name;
}
return $ret;
}
}
... ...
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "{{%room_redbag}}".
*
* @property string $id
* @property string $room_id
* @property string $coupon_id
* @property integer $type
* @property string $coupon_name
* @property integer $stock
* @property integer $effect_time
* @property integer $invalid_time
* @property integer $create_time
* @property integer $update_time
*/
class RoomRedbag extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%room_redbag}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['room_id', 'coupon_id', 'type_id', 'stock', 'effect_time', 'invalid_time', 'create_time', 'update_time'], 'integer'],
[['coupon_name'], 'string', 'max' => 100],
[['room_id', 'coupon_id'], 'unique', 'targetAttribute' => ['room_id', 'coupon_id'], 'message' => 'The combination of 房间号 and 优惠券ID has already been taken.'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'room_id' => '房间号',
'coupon_id' => '优惠券ID',
'type_id' => '红包类型',
'coupon_name' => '优惠券名称',
'stock' => '优惠券剩余库存',
'effect_time' => '优惠券生效时间',
'invalid_time' => '优惠券失效时间',
'create_time' => '创建时间',
'update_time' => '更新时间',
];
}
public function behaviors() {
return [
[
'class' => \yii\behaviors\TimestampBehavior::className(),
'createdAtAttribute' => 'create_time',
'updatedAtAttribute' => 'update_time',
]
];
}
/**
* 关联表-类型
* @return type
*/
public function getType()
{
return $this->hasOne(RedbagType::className(), ['type_id'=>'type_id']);
}
}
... ...
... ... @@ -4,6 +4,9 @@ namespace soa\controllers\v1;
use Yii;
use soa\controllers\BaseController;
use common\models\UserRedbag;
use common\models\RoomRedbag;
use common\models\RedbagType;
/**
* Site controller
*/
... ... @@ -66,5 +69,58 @@ class RedbagController extends BaseController
$this->renderJson(Yii::$app->params['success_code'],'',$ret);
}
/**
* 推流端发红包
* @author yaoxiaofeng 2016.10.26
*/
public function actionPushbaglist()
{
$room_id = $this->requests('room_id');
if (!$room_id){
$this->renderJson(Yii::$app->params['failed_code'],'缺少id');
}
$key = __CLASS__.__FUNCTION__.'room_id'.$room_id;
if (!$result = $this->cache->get($key)) {
//TODO 内部接口没加缓存
$model = RoomRedbag::find()->where(['room_id' => $room_id]);
$list = $model
->with(['type'])
->orderBy(['create_time' => SORT_DESC])
->indexBy('coupon_id')
->all();
//红包类型
$redBagTypeArray = RedbagType::getId2name();
$couponId = [];
$couponId_by_type = [];
if ($list) {
foreach ($list as $model) {
$couponId[] = $model->coupon_id;
//按类型分组,供页面发红包随机用
if (empty($couponId_by_type[$model->type_id])) {
$couponId_by_type[$model->type_id] = [];
}
if (!in_array($model->coupon_id, $couponId_by_type[$model->type_id])) {
$couponId_by_type[$model->type_id][] = $model->coupon_id;
}
}
foreach ($couponId_by_type as $typeId => $coups) {
$result[] = [
'type_id' => $typeId,
'type_name' => $redBagTypeArray[$typeId],
'coupon_ids' => $coups
];
}
}
$dependency = new \yii\caching\DbDependency(['sql' => Yii::$app->db
->createCommand('SELECT MAX(update_time),count(*) FROM {{%room_redbag}} WHERE room_id=:room_id')
->bindValue(':room_id', $room_id)
->getRawSql()]);
$this->cache->set($key, $result, Yii::$app->params['defaultCacheExpire'], $dependency);
}
$this->renderJson(Yii::$app->params['success_code'],'',$result);
}
}
... ...