BusinessCardMaterialsBrowse.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace addons\BusinessCard\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\user\User;
  5. use Exception;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_addons_business_card_material_browse".
  9. *
  10. * @property int $id 名片素材客户浏览统计
  11. * @property int|null $mall_id 商城ID
  12. * @property int|null $user_id 用户ID
  13. * @property int|null $view_material 查看素材数量
  14. * @property int|null $view_times 观看总次数
  15. * @property int|null $view_seconds 观看总时长(秒)
  16. * @property int|null $recent_time 最近观看时间戳
  17. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int|null $created_at 创建时间
  19. * @property int|null $updated_at 更新时间
  20. */
  21. class BusinessCardMaterialsBrowse extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return 'qimall_addons_business_card_material_browse';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'user_id', 'view_material', 'view_times', 'view_seconds', 'recent_time', 'status', 'created_at', 'updated_at'], 'integer'],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => 'Mall ID',
  47. 'user_id' => 'User ID',
  48. 'view_material' => 'View Material',
  49. 'view_times' => 'View Times',
  50. 'view_seconds' => 'View Seconds',
  51. 'recent_time' => 'Recent Time',
  52. 'status' => 'Status',
  53. 'created_at' => 'Created At',
  54. 'updated_at' => 'Updated At',
  55. ];
  56. }
  57. /**
  58. * @return \yii\db\ActiveQuery
  59. */
  60. public function getUser()
  61. {
  62. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,parent_id,username');
  63. }
  64. /**
  65. * 数据设置
  66. */
  67. public static function setData(array $params)
  68. {
  69. try{
  70. if (!isset($params['mall_id']) || empty($params['mall_id'])) throw new Exception('缺少商城ID');
  71. if (isset($params['id']) && !empty($params['id'])) {
  72. $model = self::findOne([
  73. 'id' => $params['id'],
  74. 'mall_id' => $params['mall_id'],
  75. 'status' => StatusEnum::ENABLED
  76. ]);
  77. if(empty($model)) throw new Exception('统计记录不存在或已删除');
  78. } else if (isset($params['user_id']) && !empty($params['user_id'])) {
  79. $model = self::findOne([
  80. 'user_id' => $params['user_id'],
  81. 'mall_id' => $params['mall_id'],
  82. 'status' => StatusEnum::ENABLED
  83. ]);
  84. }
  85. if (!empty($model)) {
  86. if (isset($params['view_material'])) {
  87. $model->view_material = $model->view_material + $params['view_material'];
  88. unset($params['view_material']);
  89. }
  90. if (isset($params['view_times'])) {
  91. $model->view_times = $model->view_times + $params['view_times'];
  92. unset($params['view_times']);
  93. }
  94. if (isset($params['view_seconds'])) {
  95. $model->view_seconds = $model->view_seconds + $params['view_seconds'];
  96. unset($params['view_seconds']);
  97. }
  98. } else {
  99. $model = new self();
  100. $model->loadDefaultValues();
  101. $model->mall_id = $params['mall_id'];
  102. }
  103. if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  104. return $model;
  105. }catch(Exception $e){
  106. self::setStaticError($e->getMessage());
  107. return false;
  108. }
  109. }
  110. }