ShortVideoShare.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace addons\ShortVideo\common\models;
  3. use addons\ShortVideo\common\enums\AddonsShortVideoEnum;
  4. use common\enums\StatusEnum;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%addons_short_video_share}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $user_id 分享视频的会员的id
  12. * @property int $share_to_user_id 观看分享的会员的id
  13. * @property int $video_id 视频id
  14. * @property int $status 状态[-1:删除;0:禁用;1启用]
  15. * @property int $created_at
  16. * @property int $updated_at
  17. */
  18. class ShortVideoShare extends \common\models\base\BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%addons_short_video_share}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['mall_id', 'user_id', 'video_id'], 'required'],
  34. [['mall_id', 'user_id', 'share_to_user_id', 'video_id', 'status', 'created_at', 'updated_at'], 'integer'],
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'id' => 'ID',
  44. 'mall_id' => 'Mall ID',
  45. 'user_id' => 'User ID',
  46. 'video_id' => 'Video ID',
  47. 'share_to_user_id' => 'Share To User Id',
  48. 'status' => 'Status',
  49. 'created_at' => 'Created At',
  50. 'updated_at' => 'Updated At',
  51. ];
  52. }
  53. public static function setData(int $mall_id, array $data)
  54. {
  55. $video = ShortVideo::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['video_id'], 'check_status' => AddonsShortVideoEnum::VIDEO_AUDIT_STATUS1, 'is_blacklist' => 0]);
  56. if (empty($video)) {
  57. self::$static_error = '视频不存在';
  58. return false;
  59. }
  60. $transaction = \Yii::$app->db->beginTransaction();
  61. $model = new self();
  62. $model->loadDefaultValues();
  63. $model->mall_id = $mall_id;
  64. foreach ($data as $key => $val) {
  65. $model->$key = $val;
  66. }
  67. // dd($model);
  68. $res = $model->save();
  69. if (false === $res) {
  70. $transaction->rollBack();
  71. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  72. return false;
  73. }
  74. $video->share_num += 1;
  75. $res_video = $video->save();
  76. if (false === $res_video) {
  77. $transaction->rollBack();
  78. self::$static_error = ShortVideo::getStaticError();
  79. return false;
  80. }
  81. $transaction->commit();
  82. return $model;
  83. }
  84. }