CourseUserWatch.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace addons\Course\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_course_user_watch".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $user_id
  12. * @property int $duration 观看时长
  13. * @property int $register_time 注册时间
  14. * @property int $status 活动状态,1:启用,0:禁用,-1删除
  15. * @property int $created_at
  16. * @property int $updated_at
  17. */
  18. class CourseUserWatch extends BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%addons_course_user_watch}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['mall_id', 'user_id', 'duration', 'register_time', '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. 'duration' => '观看时长',
  46. 'register_time' => '注册时间',
  47. 'status' => '活动状态,1:启用,0:禁用,-1删除',
  48. 'created_at' => 'Created At',
  49. 'updated_at' => 'Updated At',
  50. ];
  51. }
  52. public static function setData(array $params)
  53. {
  54. try {
  55. $model = self::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'],'status' => [StatusEnum::ENABLED]]);
  56. if (empty($model)) {
  57. $model = new self();
  58. $model->loadDefaultValues();
  59. }
  60. $params['duration'] += $model['duration'];
  61. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  62. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  63. return $model;
  64. } catch (\Exception $e) {
  65. self::$static_error = $e->getMessage();
  66. return false;
  67. }
  68. }
  69. }