| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace common\models\backend;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "qimall_admin_addons_views".
- *
- * @property int $id
- * @property int $admin_id 账户id
- * @property string $addons 应用名称
- * @property int $view_times 浏览时长
- * @property int $created_at
- * @property int $updated_at
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- */
- class AdminAddonsViews extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%admin_addons_views}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['admin_id', 'view_times', 'created_at', 'updated_at', 'status'], 'integer'],
- [['addons'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'admin_id' => '账户id',
- 'addons' => '应用名称',
- 'view_times' => '浏览时长',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- ];
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2023/6/21 0021 15:19
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $admin_id
- * @param $name
- * @param $time
- * @return bool
- */
- public static function setData($admin_id, $name, $time)
- {
- try {
- // 获取用户账号
- $admin = Admin::find()->where(['id' => $admin_id, 'source' => 1])->one();
- if (empty($admin)) return true;
- if ($name == 'mall') {
- $admin->view_times += $time;
- } else {
- $admin->addons_view_times += $time;
- }
- if (!$admin->save()) throw new Exception('总统计'.$admin->getErrorMessage());
- // 应用统计
- if ($name != 'mall') {
- $model = self::find()->where(['admin_id' => $admin_id, 'addons' => $name, 'status' => StatusEnum::ENABLED])->one();
- if (empty($model)) {
- $model = new self();
- $model->admin_id = $admin_id;
- $model->addons = $name;
- $model->view_times = $time;
- $model->loadDefaultValues();
- } else {
- $model->view_times += $time;
- }
- if (!$model->save()) throw new Exception('应用统计'.$model->getErrorMessage());
- }
- return true;
- } catch (\Exception $e) {
- self::setStaticError('统计保存失败:' . $e->getMessage());
- return false;
- }
- }
- }
|