AdminAddonsViews.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace common\models\backend;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "qimall_admin_addons_views".
  9. *
  10. * @property int $id
  11. * @property int $admin_id 账户id
  12. * @property string $addons 应用名称
  13. * @property int $view_times 浏览时长
  14. * @property int $created_at
  15. * @property int $updated_at
  16. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  17. */
  18. class AdminAddonsViews extends BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%admin_addons_views}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['admin_id', 'view_times', 'created_at', 'updated_at', 'status'], 'integer'],
  34. [['addons'], 'string', 'max' => 255],
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'id' => 'ID',
  44. 'admin_id' => '账户id',
  45. 'addons' => '应用名称',
  46. 'view_times' => '浏览时长',
  47. 'created_at' => 'Created At',
  48. 'updated_at' => 'Updated At',
  49. 'status' => '状态[-1:删除;0:禁用;1启用]',
  50. ];
  51. }
  52. /**
  53. * 保存数据
  54. * @Author: lun
  55. * @DateTime: 2023/6/21 0021 15:19
  56. * @Copyright: copyright (c) 2021 广东七件事集团
  57. * @param $admin_id
  58. * @param $name
  59. * @param $time
  60. * @return bool
  61. */
  62. public static function setData($admin_id, $name, $time)
  63. {
  64. try {
  65. // 获取用户账号
  66. $admin = Admin::find()->where(['id' => $admin_id, 'source' => 1])->one();
  67. if (empty($admin)) return true;
  68. if ($name == 'mall') {
  69. $admin->view_times += $time;
  70. } else {
  71. $admin->addons_view_times += $time;
  72. }
  73. if (!$admin->save()) throw new Exception('总统计'.$admin->getErrorMessage());
  74. // 应用统计
  75. if ($name != 'mall') {
  76. $model = self::find()->where(['admin_id' => $admin_id, 'addons' => $name, 'status' => StatusEnum::ENABLED])->one();
  77. if (empty($model)) {
  78. $model = new self();
  79. $model->admin_id = $admin_id;
  80. $model->addons = $name;
  81. $model->view_times = $time;
  82. $model->loadDefaultValues();
  83. } else {
  84. $model->view_times += $time;
  85. }
  86. if (!$model->save()) throw new Exception('应用统计'.$model->getErrorMessage());
  87. }
  88. return true;
  89. } catch (\Exception $e) {
  90. self::setStaticError('统计保存失败:' . $e->getMessage());
  91. return false;
  92. }
  93. }
  94. }