ShortVideoLike.php 2.5 KB

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