| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace addons\Store\common\models;
- use addons\Seed\common\enums\SeedEnum;
- use common\enums\AddonsEnum;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\helpers\LockHelper;
- use common\helpers\StringHelper;
- use common\models\base\BaseActiveRecord;
- use common\models\common\CapitalLog;
- use common\models\mall\MallAddons;
- use services\common\UserWalletService;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "qimall_addons_store_video".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $store_id 门店id
- * @property int $user_id 用户id
- * @property string $title 标题
- * @property int $content 内容
- * @property string $goods_id 门店商品id
- * @property float $reward_amount 用户观看后奖励金额
- * @property int $viewing_times 观看次数
- * @property string $video_url 视频路径
- * @property string $first_frame 首帧
- * @property int $video_wide 视频宽度
- * @property int $video_long 视频长度
- * @property string $duration_str 视频时长(时分秒字符串形式),upload_type = video 时才有意义
- * @property float $duration_seconds 视频时长(秒为单位浮点数形式),upload_type = video 时才有意义
- * @property string $check_remark 审核备注
- * @property int $is_on_sale 上架状态【1是 0否】
- * @property int $check_status 审核状态:0:未审核 1:审核通过 2:审核不通过
- * @property int $check_time 审核时间
- * @property int $look_people_num 观看人数
- * @property int $look_complete_num 完整观看人数
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class StoreVideo extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_video}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id', 'user_id'], 'required'],
- [['mall_id', 'store_id', 'goods_id','user_id', 'viewing_times', 'video_wide', 'video_long', 'check_status', 'check_time','is_on_sale', 'look_people_num', 'look_complete_num', 'status', 'created_at', 'updated_at'], 'integer'],
- [['reward_amount', 'duration_seconds'], 'number'],
- [['title', 'content', 'video_url', 'first_frame', 'check_remark'], 'string', 'max' => 255],
- [['duration_str'], 'string', 'max' => 20],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'store_id' => 'Store ID',
- 'user_id' => 'User ID',
- 'title' => 'Title',
- 'content' => 'Content',
- 'goods_id' => 'Goods ID',
- 'reward_amount' => 'Reward Amount',
- 'viewing_times' => 'Viewing Times',
- 'video_url' => 'Video Url',
- 'first_frame' => 'First Frame',
- 'video_wide' => 'Video Wide',
- 'video_long' => 'Video Long',
- 'duration_str' => 'Duration Str',
- 'duration_seconds' => 'Duration Seconds',
- 'is_on_sale' => 'is_on_sale',
- 'check_remark' => 'Check Remark',
- 'check_status' => 'Check Status',
- 'check_time' => 'Check Time',
- 'look_people_num' => 'Look People Num',
- 'look_complete_num' => 'Look Complete Num',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public static function setData($params)
- {
- $t = Yii::$app->db->beginTransaction();
- try{
- $model = new self();
- $model->loadDefaultValues();
- $store = Store::findOne(['user_id'=>$params['user_id'],'status'=>StatusEnum::ENABLED]);
- if(empty($store)) throw new Exception('您没有权限发布视频');
- $params['store_id'] = $store['id'];
- if (!$model->load($params, '') || !$model->save()) throw new \PHPUnit\Util\Exception($model->getErrorMessage());
- $params['video_id'] = $model['id'];
- $params['order_no'] = StringHelper::generateOrderNo('v');
- $params['pay_money'] = bcmul($params['reward_amount'],$params['viewing_times'],2);
- $video_order = StoreVideoOrder::setData($params);
- if (!$video_order) throw new Exception(StoreVideoOrder::getStaticError());
- $order_trade_info[] = [
- 'order_no' =>$params['order_no'] ,
- 'amount' => $params['pay_money'],
- 'title' => $params['title'],
- 'notify_class' => 'addons\Store\common\logic\notify\VideoOrderNotify',
- 'order_type' => 'video_order',
- ];
- $res = \Yii::$app->services->pay->createTrade($params['mall_id'], $params['user_id'], $order_trade_info);
- if (!$res) throw new \Exception('生成支付流水失败');
- $t->commit();
- $res['order_ids'] = $video_order['id'];
- $arr['transaction'] = $res;
- $arr['pay_success_page'] = Yii::$app->services->setting->mall->get($params['mall_id'], 'order.pay_after_link'); // 支付成功后跳转的地址
- $payment_expire_time = Yii::$app->services->setting->mall->get($params['mall_id'], 'order.payment_expire_time');
- $arr['surplus_second'] = 30 * 60; //剩余秒数
- if ($payment_expire_time) {
- $arr['surplus_second'] = intval($payment_expire_time) * 60; //剩余秒数
- }
- return $arr;
- }catch(\Exception $e){
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- //发放观看视频广告奖励余额
- public static function grantReward($params){
- $video = StoreVideo::findOne(['id'=>$params['video_id']]);
- if(empty($video)) return true;
- $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_STORE_VIDEO , $video['id']);
- $identification = uniqid();
- try {
- if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new Exception('当前访问人数过多');
- if($video['look_complete_num']>=$video['viewing_times']){
- $video->is_on_sale = 0;
- }else{
- $rate = 0;//广告扣除比例
- if(MallAddons::isInstall($video['mall_id'],'Seed')){
- $ad_percent = Yii::$app->services->setting->addons->get($video['mall_id'], 'Seed', 'basic.ad_percent');
- if(!empty($ad_percent)) $rate = $ad_percent;
- }
- $rate_money = bcdiv($video['reward_amount']*$rate,100,2) ;
- $money = bcsub($video['reward_amount'],$rate_money,2);
- if($money>0){
- CapitalLog::$capitalType = UserWalletService::WALLET_TYPE_BALANCE;
- $condition = [
- 'mall_id' => $params['mall_id'],
- 'user_id'=>$params['user_id'],
- 'source_table'=>StoreVideo::tableName(),
- 'source_table_id'=>$params['video_id'],
- 'from_type' => UserWalletEnum::REWARD,
- 'capital_type' => UserWalletEnum::VIDEO_AD_REWARD
- ];
- $res = CapitalLog::findOne($condition);
- if($res) throw new Exception('已发放过奖励');
- $insert_wallet [] = [
- 'mall_id' => $params['mall_id'],
- 'user_id' => $params['user_id'],
- 'is_frozen' => false,
- 'wallet_type' =>UserWalletService::WALLET_TYPE_BALANCE,
- 'money' => $money,
- 'desc' => '观看视频广告获得奖励余额',
- 'source_table' => StoreVideo::tableName(),
- 'source_table_id' => $params['video_id'],
- 'from_type' => UserWalletEnum::REWARD,
- 'capital_type' => UserWalletEnum::VIDEO_AD_REWARD,
- ];
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if($res==false) throw new Exception('投入资金队列返回错误');
- }
- }
- if($video['look_complete_num']+1>=$video['viewing_times']){
- $video->is_on_sale = 0;
- }
- $video->look_complete_num++;
- $video->save();
- LockHelper::ulock($lock_key,$identification);
- return true;
- }catch (\Exception $e){
- LockHelper::ulock($lock_key,$identification);
- return false;
- }
- }
- public static function browse($params){
- $video = StoreVideo::findOne(['id'=>$params['video_id']]);
- if(empty($video)) return true;
- $video->look_people_num++;
- return $video->save();
- }
- }
|