ShortVideoUserWatch.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace addons\ShortVideo\common\models;
  3. use common\enums\StatusEnum;
  4. use Exception;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_short_video_user_watch".
  8. *
  9. * @property int $id id
  10. * @property int $mall_id 商城id
  11. * @property int $user_id 用户id
  12. * @property int $video_id 视频id
  13. * @property int $status 状态(0:未观看,1:已观看)
  14. * @property int|null $created_at 添加时间
  15. * @property int|null $updated_at 修改时间
  16. */
  17. class ShortVideoUserWatch extends \common\models\base\BaseActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return 'qimall_addons_short_video_user_watch';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['mall_id', 'user_id', 'video_id'], 'required'],
  33. [['mall_id', 'user_id', 'video_id', 'status', 'created_at', 'updated_at'], 'integer'],
  34. ];
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function attributeLabels()
  40. {
  41. return [
  42. 'id' => 'ID',
  43. 'mall_id' => 'Mall ID',
  44. 'user_id' => 'User ID',
  45. 'video_id' => 'Video ID',
  46. 'status' => 'Status',
  47. 'created_at' => 'Created At',
  48. 'updated_at' => 'Updated At',
  49. ];
  50. }
  51. public static function setData(array $params)
  52. {
  53. try {
  54. $model = self::findOne(['user_id' => $params['user_id'], 'mall_id' => $params['mall_id'], 'video_id' => $params['video_id']]);
  55. if (empty($model)) {
  56. $model = new self();
  57. $model->loadDefaultValues();
  58. }
  59. if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
  60. return $model;
  61. } catch (\Exception $e) {
  62. self::setStaticError($e->getMessage());
  63. return false;
  64. }
  65. }
  66. //更改观看记录状态
  67. public static function changeWatchStatus($params)
  68. {
  69. try {
  70. $res = ShortVideoUserWatch::updateAll(['status' => StatusEnum::DISABLED], ['mall_id' => $params['mall_id'], 'user_id' => $params['user_id']]);
  71. if (!$res) throw new Exception(ShortVideoUserWatch::$static_error);
  72. } catch (\Exception $e) {
  73. throw new Exception($e->getMessage());
  74. }
  75. }
  76. }