BusinessCardCustomerLog.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace addons\BusinessCard\common\models;
  3. use addons\BusinessCard\common\enums\CardEnums;
  4. use common\enums\StatusEnum;
  5. use common\models\user\User;
  6. use Exception;
  7. /**
  8. * This is the model class for table "qimall_addons_business_card_customer_log".
  9. *
  10. * @property int $id
  11. * @property int $mall_id
  12. * @property int $user_id
  13. * @property int $item_id 对象ID
  14. * @property int $log_type 1:浏览首页,2:查看图文,3:查看商品,4:授权号码,5:转发名片,6:查看名片,7:查看视频,8:保存手机,9:点赞,10:收藏,11:评论,12:查看动态,13:新增客户,14:新增线索,15:下单
  15. * @property int $status 状态[1启用 0禁用 -1删除]
  16. * @property int $created_at 创建时间
  17. * @property int $updated_at 更新事件
  18. * @property int|null $material_log_id 素材浏览ID
  19. * @property int|null $material_id 素材ID
  20. * @property int|null $material_times 次数
  21. */
  22. class BusinessCardCustomerLog extends \common\models\base\BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return 'qimall_addons_business_card_customer_log';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'user_id', 'item_id', 'log_type', 'status', 'created_at', 'updated_at', 'material_log_id', 'material_id', 'material_times'], 'integer'],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => 'Mall ID',
  48. 'user_id' => 'User ID',
  49. 'item_id' => 'Item ID',
  50. 'log_type' => 'Log Type',
  51. 'status' => 'Status',
  52. 'created_at' => 'Created At',
  53. 'updated_at' => 'Updated At',
  54. 'material_log_id' => 'Material Log ID',
  55. 'material_id' => 'Material ID',
  56. 'material_times' => 'Material Times',
  57. ];
  58. }
  59. public function getUser()
  60. {
  61. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id, nickname, username, avatar_url, mobile');
  62. }
  63. public function getCard()
  64. {
  65. return $this->hasOne(BusinessCard::class, ['id' => 'item_id']);
  66. }
  67. /**
  68. * 保存数据
  69. *
  70. * @author hpei
  71. * @return bool|Exception
  72. */
  73. public static function setData($params) {
  74. try {
  75. if (empty($params['id'])) {
  76. $model = new self();
  77. $model->loadDefaultValues();
  78. } else {
  79. $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  80. if (empty($model)) throw new \Exception('数据不存在或者已删除');
  81. }
  82. if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
  83. return $model->id;
  84. } catch (\Exception $e) {
  85. self::$static_error = $e->getMessage();
  86. return false;
  87. }
  88. }
  89. /**
  90. * 最近浏览
  91. * @author hpei
  92. */
  93. public static function latelyBrowse($card_id, $day = 100) {
  94. $start_time = strtotime("-$day day");
  95. $user_list = self::find()->select(['user_id'])
  96. ->where(['item_id' => $card_id, 'log_type' => CardEnums::LOG_TYPE_LIKE])
  97. ->andWhere(['>', 'created_at', $start_time])
  98. ->andWhere(['<', 'created_at', time()])
  99. ->column();
  100. return User::find()->where(['id' => $user_list])->select(['avatar_url'])->column();
  101. }
  102. /**
  103. * 秒数转时分格式
  104. * @param $time int
  105. * @author jack
  106. * @throws string
  107. * @return string
  108. */
  109. public static function sec2Time($time)
  110. {
  111. $value = [
  112. "years" => 0, "days" => 0, "hours" => 0,
  113. "minutes" => 0, "seconds" => 0,
  114. ];
  115. if ($time >= 31556926) {
  116. $value["years"] = floor($time/31556926);
  117. $time = ($time%31556926);
  118. }
  119. if ($time >= 86400) {
  120. $value["days"] = floor($time/86400);
  121. $time = ($time%86400);
  122. }
  123. if ($time >= 3600) {
  124. $value["hours"] = floor($time/3600);
  125. $time = ($time%3600);
  126. }
  127. if ($time >= 60) {
  128. $value["minutes"] = floor($time/60);
  129. $time = ($time%60);
  130. }
  131. $value["seconds"] = floor($time);
  132. $t = '';
  133. if($value["years"]>0) $t .= $value["years"] . "年";
  134. if($value["days"]>0) $t .= $value["days"] . "天";
  135. if($value["hours"]>0) $t .= $value["hours"] . "小时";
  136. if($value["minutes"]>0) $t .= $value["minutes"] . "分";
  137. if($value["seconds"]>0) $t .= $value["seconds"] . "秒";
  138. return $t;
  139. }
  140. }