StoreVideo.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace addons\Store\common\models;
  3. use addons\Seed\common\enums\SeedEnum;
  4. use common\enums\AddonsEnum;
  5. use common\enums\RedisKeyEnum;
  6. use common\enums\StatusEnum;
  7. use common\enums\UserWalletEnum;
  8. use common\helpers\LockHelper;
  9. use common\helpers\StringHelper;
  10. use common\models\base\BaseActiveRecord;
  11. use common\models\common\CapitalLog;
  12. use common\models\mall\MallAddons;
  13. use services\common\UserWalletService;
  14. use Yii;
  15. use yii\db\Exception;
  16. /**
  17. * This is the model class for table "qimall_addons_store_video".
  18. *
  19. * @property int $id
  20. * @property int $mall_id 商城id
  21. * @property int $store_id 门店id
  22. * @property int $user_id 用户id
  23. * @property string $title 标题
  24. * @property int $content 内容
  25. * @property string $goods_id 门店商品id
  26. * @property float $reward_amount 用户观看后奖励金额
  27. * @property int $viewing_times 观看次数
  28. * @property string $video_url 视频路径
  29. * @property string $first_frame 首帧
  30. * @property int $video_wide 视频宽度
  31. * @property int $video_long 视频长度
  32. * @property string $duration_str 视频时长(时分秒字符串形式),upload_type = video 时才有意义
  33. * @property float $duration_seconds 视频时长(秒为单位浮点数形式),upload_type = video 时才有意义
  34. * @property string $check_remark 审核备注
  35. * @property int $is_on_sale 上架状态【1是 0否】
  36. * @property int $check_status 审核状态:0:未审核 1:审核通过 2:审核不通过
  37. * @property int $check_time 审核时间
  38. * @property int $look_people_num 观看人数
  39. * @property int $look_complete_num 完整观看人数
  40. * @property int $status 状态[-1:删除;0:禁用;1启用]
  41. * @property int $created_at
  42. * @property int $updated_at
  43. */
  44. class StoreVideo extends BaseActiveRecord
  45. {
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public static function tableName()
  50. {
  51. return '{{%addons_store_video}}';
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function rules()
  57. {
  58. return [
  59. [['mall_id', 'store_id', 'user_id'], 'required'],
  60. [['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'],
  61. [['reward_amount', 'duration_seconds'], 'number'],
  62. [['title', 'content', 'video_url', 'first_frame', 'check_remark'], 'string', 'max' => 255],
  63. [['duration_str'], 'string', 'max' => 20],
  64. ];
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function attributeLabels()
  70. {
  71. return [
  72. 'id' => 'ID',
  73. 'mall_id' => 'Mall ID',
  74. 'store_id' => 'Store ID',
  75. 'user_id' => 'User ID',
  76. 'title' => 'Title',
  77. 'content' => 'Content',
  78. 'goods_id' => 'Goods ID',
  79. 'reward_amount' => 'Reward Amount',
  80. 'viewing_times' => 'Viewing Times',
  81. 'video_url' => 'Video Url',
  82. 'first_frame' => 'First Frame',
  83. 'video_wide' => 'Video Wide',
  84. 'video_long' => 'Video Long',
  85. 'duration_str' => 'Duration Str',
  86. 'duration_seconds' => 'Duration Seconds',
  87. 'is_on_sale' => 'is_on_sale',
  88. 'check_remark' => 'Check Remark',
  89. 'check_status' => 'Check Status',
  90. 'check_time' => 'Check Time',
  91. 'look_people_num' => 'Look People Num',
  92. 'look_complete_num' => 'Look Complete Num',
  93. 'status' => 'Status',
  94. 'created_at' => 'Created At',
  95. 'updated_at' => 'Updated At',
  96. ];
  97. }
  98. public static function setData($params)
  99. {
  100. $t = Yii::$app->db->beginTransaction();
  101. try{
  102. $model = new self();
  103. $model->loadDefaultValues();
  104. $store = Store::findOne(['user_id'=>$params['user_id'],'status'=>StatusEnum::ENABLED]);
  105. if(empty($store)) throw new Exception('您没有权限发布视频');
  106. $params['store_id'] = $store['id'];
  107. if (!$model->load($params, '') || !$model->save()) throw new \PHPUnit\Util\Exception($model->getErrorMessage());
  108. $params['video_id'] = $model['id'];
  109. $params['order_no'] = StringHelper::generateOrderNo('v');
  110. $params['pay_money'] = bcmul($params['reward_amount'],$params['viewing_times'],2);
  111. $video_order = StoreVideoOrder::setData($params);
  112. if (!$video_order) throw new Exception(StoreVideoOrder::getStaticError());
  113. $order_trade_info[] = [
  114. 'order_no' =>$params['order_no'] ,
  115. 'amount' => $params['pay_money'],
  116. 'title' => $params['title'],
  117. 'notify_class' => 'addons\Store\common\logic\notify\VideoOrderNotify',
  118. 'order_type' => 'video_order',
  119. ];
  120. $res = \Yii::$app->services->pay->createTrade($params['mall_id'], $params['user_id'], $order_trade_info);
  121. if (!$res) throw new \Exception('生成支付流水失败');
  122. $t->commit();
  123. $res['order_ids'] = $video_order['id'];
  124. $arr['transaction'] = $res;
  125. $arr['pay_success_page'] = Yii::$app->services->setting->mall->get($params['mall_id'], 'order.pay_after_link'); // 支付成功后跳转的地址
  126. $payment_expire_time = Yii::$app->services->setting->mall->get($params['mall_id'], 'order.payment_expire_time');
  127. $arr['surplus_second'] = 30 * 60; //剩余秒数
  128. if ($payment_expire_time) {
  129. $arr['surplus_second'] = intval($payment_expire_time) * 60; //剩余秒数
  130. }
  131. return $arr;
  132. }catch(\Exception $e){
  133. $t->rollBack();
  134. self::$static_error = $e->getMessage();
  135. return false;
  136. }
  137. }
  138. //发放观看视频广告奖励余额
  139. public static function grantReward($params){
  140. $video = StoreVideo::findOne(['id'=>$params['video_id']]);
  141. if(empty($video)) return true;
  142. $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_STORE_VIDEO , $video['id']);
  143. $identification = uniqid();
  144. try {
  145. if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new Exception('当前访问人数过多');
  146. if($video['look_complete_num']>=$video['viewing_times']){
  147. $video->is_on_sale = 0;
  148. }else{
  149. $rate = 0;//广告扣除比例
  150. if(MallAddons::isInstall($video['mall_id'],'Seed')){
  151. $ad_percent = Yii::$app->services->setting->addons->get($video['mall_id'], 'Seed', 'basic.ad_percent');
  152. if(!empty($ad_percent)) $rate = $ad_percent;
  153. }
  154. $rate_money = bcdiv($video['reward_amount']*$rate,100,2) ;
  155. $money = bcsub($video['reward_amount'],$rate_money,2);
  156. if($money>0){
  157. CapitalLog::$capitalType = UserWalletService::WALLET_TYPE_BALANCE;
  158. $condition = [
  159. 'mall_id' => $params['mall_id'],
  160. 'user_id'=>$params['user_id'],
  161. 'source_table'=>StoreVideo::tableName(),
  162. 'source_table_id'=>$params['video_id'],
  163. 'from_type' => UserWalletEnum::REWARD,
  164. 'capital_type' => UserWalletEnum::VIDEO_AD_REWARD
  165. ];
  166. $res = CapitalLog::findOne($condition);
  167. if($res) throw new Exception('已发放过奖励');
  168. $insert_wallet [] = [
  169. 'mall_id' => $params['mall_id'],
  170. 'user_id' => $params['user_id'],
  171. 'is_frozen' => false,
  172. 'wallet_type' =>UserWalletService::WALLET_TYPE_BALANCE,
  173. 'money' => $money,
  174. 'desc' => '观看视频广告获得奖励余额',
  175. 'source_table' => StoreVideo::tableName(),
  176. 'source_table_id' => $params['video_id'],
  177. 'from_type' => UserWalletEnum::REWARD,
  178. 'capital_type' => UserWalletEnum::VIDEO_AD_REWARD,
  179. ];
  180. $res = UserWalletService::batchHandleByQueue($insert_wallet);
  181. if($res==false) throw new Exception('投入资金队列返回错误');
  182. }
  183. }
  184. if($video['look_complete_num']+1>=$video['viewing_times']){
  185. $video->is_on_sale = 0;
  186. }
  187. $video->look_complete_num++;
  188. $video->save();
  189. LockHelper::ulock($lock_key,$identification);
  190. return true;
  191. }catch (\Exception $e){
  192. LockHelper::ulock($lock_key,$identification);
  193. return false;
  194. }
  195. }
  196. public static function browse($params){
  197. $video = StoreVideo::findOne(['id'=>$params['video_id']]);
  198. if(empty($video)) return true;
  199. $video->look_people_num++;
  200. return $video->save();
  201. }
  202. }