ShortVideoBrowseAward.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace addons\ShortVideo\common\models;
  3. use addons\ShortVideo\common\enums\AddonsShortVideoEnum;
  4. use common\enums\StatusEnum;
  5. use common\enums\UserWalletEnum;
  6. use common\models\user\User;
  7. use services\common\UserWalletService;
  8. use Yii;
  9. /**
  10. * This is the model class for table "{{%addons_short_video_browse_award}}".
  11. *
  12. * @property int $id
  13. * @property int $mall_id
  14. * @property int $video_id 视频id
  15. * @property int $user_id 用户id
  16. * @property int|null $source_user_id 源头id
  17. * @property int|null $user_type 用户类型:0:本人 1:一级 2:二级
  18. * @property int $award_type 奖励类型 0:积分 1:现金
  19. * @property float $award_integral 奖励积分
  20. * @property float $award_money 奖励金额
  21. * @property int $status 状态[-1:删除;0:禁用;1启用]
  22. * @property int $created_at
  23. * @property int $updated_at
  24. */
  25. class ShortVideoBrowseAward extends \common\models\base\BaseActiveRecord
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%addons_short_video_browse_award}}';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['mall_id', 'video_id', 'user_id'], 'required'],
  41. [['mall_id', 'video_id', 'user_id', 'source_user_id', 'user_type', 'award_type', 'status', 'created_at', 'updated_at'], 'integer'],
  42. [['award_integral', 'award_money'], 'number'],
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'id' => 'ID',
  52. 'mall_id' => '商城ID',
  53. 'video_id' => '视频ID',
  54. 'user_id' => '会员ID',
  55. 'source_user_id' => '来源会员ID',
  56. 'user_type' => '会员类型',
  57. 'award_type' => '奖励类型',
  58. 'award_integral' => '奖励积分',
  59. 'award_money' => '奖励金额',
  60. 'status' => '状态[-1:删除;0:禁用;1启用]',
  61. 'created_at' => 'Created At',
  62. 'updated_at' => 'Updated At',
  63. ];
  64. }
  65. /**
  66. * @name:
  67. * @msg: 发放观看视频奖励和分享视频奖励(分享的只发一级)
  68. * @param {int} $mall_id
  69. * @param {array} $data video_id:视频id;user_id:当前会员id;share_user_id:分享视频的会员id
  70. * @return {*}
  71. */
  72. public static function grantReward(int $mall_id, array $data)
  73. {
  74. $video = ShortVideo::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['video_id'], 'check_status' => AddonsShortVideoEnum::VIDEO_AUDIT_STATUS1, 'is_blacklist' => 0]);
  75. if (empty($video)) {
  76. self::$static_error = '视频不存在';
  77. return false;
  78. }
  79. $transaction = \Yii::$app->db->beginTransaction();
  80. $time = time();
  81. // 奖励记录数据
  82. $browse_reward_log_datas = [];
  83. // 钱包数据
  84. $wallet_datas = [];
  85. // 当前会员id
  86. $user_id_0 = $data['user_id'];
  87. // 上一级分享会员id
  88. $user_id_1 = 0;
  89. $browse = ShortVideoBrowse::find()
  90. ->where(['mall_id' => $mall_id, 'user_id' => $data['user_id'], 'video_id' => $data['video_id']])
  91. ->orderBy('created_at desc')
  92. ->limit(1)
  93. ->one();
  94. if (empty($browse)) {
  95. self::$static_error = '视频浏览记录不存在';
  96. return false;
  97. }
  98. // 更新当前会员浏览记录
  99. $browse->is_complete_look = 1;
  100. $browse->complete_time = $time;
  101. $res_browse = $browse->save();
  102. if (false === $res_browse) {
  103. $transaction->rollBack();
  104. self::$static_error = '完整观看视频回调发放观看奖励出错--更新会员浏览记录出错:' . $browse->getErrors();
  105. return false;
  106. }
  107. $reward_info = ShortVideoBrowseAward::find()
  108. ->select('video_id,user_id')
  109. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'video_id' => $data['video_id'], 'user_id' => $data['user_id'], 'user_type' => 0])
  110. ->asArray()
  111. ->one();
  112. // 如果已经发过观看奖励则不再发
  113. if (!empty($reward_info)) return ['reward_score' => 0, 'reward_balance' => 0];
  114. if (!empty($data['share_user_id']) && $user_id_0 != $data['share_user_id']) {
  115. // 当前视频是分享观看,而且不是自己看自己分享的
  116. $wheres = [
  117. 'mall_id' => $mall_id,
  118. 'status' => StatusEnum::ENABLED,
  119. 'video_id' => $data['video_id'],
  120. 'user_id' => $data['share_user_id'],
  121. 'share_to_user_id' => $data['user_id'],
  122. ];
  123. $share_log = ShortVideoShare::findOne($wheres);
  124. if (!empty($share_log)) {
  125. $user_id_1 = $share_log->user_id;
  126. $user = User::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_id_1]);
  127. if (empty($user)) $user_id_1 = 0;
  128. }
  129. }
  130. // 当前视频可以用于奖励的剩余积分
  131. $score_reward_surplus = $video->surplus_integral;
  132. // 当前视频可以用于奖励的剩余金额
  133. $money_reward_surplus = $video->surplus_money;
  134. // 当前会员获得的积分奖励
  135. $score_user_reward_0 = 0;
  136. // 当前会员获得的余额奖励
  137. $money_user_reward_0 = 0;
  138. if (1 == $video->open_integral_award) {
  139. // 开启积分奖励
  140. // 当前视频可以用于奖励的基准积分
  141. $score_reward_base = $video->watch_integral_sum;
  142. $score_user_reward_0 = bcmul($score_reward_base, $video->watch_integral / 100, 2);
  143. if ($score_user_reward_0 > $score_reward_surplus) {
  144. $score_user_reward_0 = $score_reward_surplus;
  145. $score_reward_surplus = 0;
  146. } else {
  147. $score_reward_surplus -= $score_user_reward_0;
  148. }
  149. // 分享上级获得的积分奖励
  150. if (!empty($user_id_1)) {
  151. // 如果有分享上级才计算上级的奖励
  152. $score_user_reward_1 = bcmul($score_reward_base, $video->watch_integral_one / 100, 2);
  153. if ($score_user_reward_1 > $score_reward_surplus) {
  154. $score_user_reward_1 = $score_reward_surplus;
  155. $score_reward_surplus = 0;
  156. } else {
  157. $score_reward_surplus -= $score_user_reward_1;
  158. }
  159. }
  160. if ($score_user_reward_0 > 0) {
  161. $browse_reward_log_datas[] = [$mall_id, $data['video_id'], $user_id_0, 0, 0, 0, $score_user_reward_0, 0, 1, $time, $time];
  162. $wallet_datas[] = [
  163. 'mall_id' => $mall_id,
  164. 'user_id' => $user_id_0,
  165. 'is_frozen' => false,
  166. 'wallet_type' => UserWalletService::WALLET_TYPE_SCORE,
  167. 'money' => $score_user_reward_0,
  168. 'desc' => '观看短视频获得积分奖励',
  169. 'source_table' => $browse->tableSchema->name,
  170. 'source_table_id' => $browse->id,
  171. 'from_type' => UserWalletEnum::REWARD,
  172. 'capital_type' => UserWalletEnum::SHORT_VIDEO_REWARD,
  173. ];
  174. }
  175. if (!empty($user_id_1) && $score_user_reward_1 > 0) {
  176. $browse_reward_log_datas[] = [$mall_id, $data['video_id'], $user_id_1, 0, 1, 0, $score_user_reward_1, 0, 1, $time, $time];
  177. $wallet_datas[] = [
  178. 'mall_id' => $mall_id,
  179. 'user_id' => $user_id_1,
  180. 'is_frozen' => false,
  181. 'wallet_type' => UserWalletService::WALLET_TYPE_SCORE,
  182. 'money' => $score_user_reward_1,
  183. 'desc' => '分享短视频获得积分奖励',
  184. 'source_table' => $browse->tableSchema->name,
  185. 'source_table_id' => $browse->id,
  186. 'from_type' => UserWalletEnum::REWARD,
  187. 'capital_type' => UserWalletEnum::SHORT_VIDEO_REWARD,
  188. ];
  189. }
  190. }
  191. if (1 == $video->open_money_award) {
  192. // 开启现金奖励
  193. // 当前视频可以用于奖励的基准现金
  194. $money_reward_base = $video->watch_money_sum;
  195. $money_user_reward_0 = bcmul($money_reward_base, $video->watch_money / 100, 2);
  196. if ($money_user_reward_0 > $money_reward_surplus) {
  197. $money_user_reward_0 = $money_reward_surplus;
  198. $money_reward_surplus = 0;
  199. } else {
  200. $money_reward_surplus -= $money_user_reward_0;
  201. }
  202. // 分享上级获得的积分奖励
  203. if (!empty($user_id_1)) {
  204. // 如果有分享上级才计算上级的奖励
  205. $money_user_reward_1 = bcmul($money_reward_base, $video->watch_money_one / 100, 2);
  206. if ($money_user_reward_1 > $money_reward_surplus) {
  207. $money_user_reward_1 = $money_reward_surplus;
  208. $money_reward_surplus = 0;
  209. } else {
  210. $money_reward_surplus -= $money_user_reward_1;
  211. }
  212. }
  213. if ($money_user_reward_0 > 0) {
  214. $browse_reward_log_datas[] = [$mall_id, $data['video_id'], $user_id_0, 0, 0, 1, 0, $money_user_reward_0, 1, $time, $time];
  215. $wallet_datas[] = [
  216. 'mall_id' => $mall_id,
  217. 'user_id' => $user_id_0,
  218. 'is_frozen' => false,
  219. 'wallet_type' => UserWalletService::WALLET_TYPE_BALANCE,
  220. 'money' => $money_user_reward_0,
  221. 'desc' => '观看短视频获余额奖励',
  222. 'source_table' => $browse->tableSchema->name,
  223. 'source_table_id' => $browse->id,
  224. 'from_type' => UserWalletEnum::REWARD,
  225. 'capital_type' => UserWalletEnum::SHORT_VIDEO_REWARD,
  226. ];
  227. }
  228. if (!empty($user_id_1) && $money_user_reward_1 > 0) {
  229. $browse_reward_log_datas[] = [$mall_id, $data['video_id'], $user_id_1, 0, 1, 1, 0, $money_user_reward_1, 1, $time, $time];
  230. $wallet_datas[] = [
  231. 'mall_id' => $mall_id,
  232. 'user_id' => $user_id_1,
  233. 'is_frozen' => false,
  234. 'wallet_type' => UserWalletService::WALLET_TYPE_BALANCE,
  235. 'money' => $money_user_reward_1,
  236. 'desc' => '分享短视频获得余额奖励',
  237. 'source_table' => $browse->tableSchema->name,
  238. 'source_table_id' => $browse->id,
  239. 'from_type' => UserWalletEnum::REWARD,
  240. 'capital_type' => UserWalletEnum::SHORT_VIDEO_REWARD,
  241. ];
  242. }
  243. }
  244. // 更新视频剩余奖励、完整观看人数
  245. $params_video = [
  246. 'id' => $video->id,
  247. 'surplus_money' => $money_reward_surplus,
  248. 'surplus_integral' => $score_reward_surplus,
  249. 'look_complete_num' => $video->look_complete_num + 1,
  250. ];
  251. $res_video = ShortVideo::setData($mall_id, $params_video);
  252. if (false === $res_video) {
  253. $transaction->rollBack();
  254. self::$static_error = ShortVideo::getStaticError();
  255. return false;
  256. }
  257. // 添加观看奖励记录
  258. if (!empty($browse_reward_log_datas)) {
  259. $field = ['mall_id', 'video_id', 'user_id', 'source_user_id', 'user_type', 'award_type', 'award_integral', 'award_money', 'status', 'created_at', 'updated_at'];
  260. $res_reward = Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $browse_reward_log_datas)->execute();
  261. if (false === $res_reward) {
  262. $transaction->rollBack();
  263. self::$static_error = '完整观看视频回调发放观看奖励出错--插入奖励记录出错';
  264. return false;
  265. }
  266. }
  267. if (!empty($wallet_datas)) {
  268. $res = UserWalletService::batchHandleByQueue($wallet_datas);
  269. if ($res === false) {
  270. $transaction->rollBack();
  271. self::$static_error = '完整观看视频回调发放观看奖励出错--奖励奖励发放到会员账号出错:' . UserWalletService::getStaticError();
  272. return false;
  273. }
  274. }
  275. $transaction->commit();
  276. return ['reward_score' => $score_user_reward_0, 'reward_balance' => $money_user_reward_0];
  277. }
  278. }