LuckyGroupVisitLog.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace addons\LuckyGroup\common\models;
  3. use common\helpers\DateHelper;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%addons_lucky_group_visit_log}}".
  8. *
  9. * @property int $id ID
  10. * @property int $mall_id 商城ID
  11. * @property int $visit_user_id 访问者用户ID
  12. * @property int $view_num 浏览次数
  13. * @property int $status 状态[-1:删除;0:禁用;1启用]
  14. * @property int $created_at 创建时间
  15. * @property int $updated_at 更新时间
  16. */
  17. class LuckyGroupVisitLog extends BaseActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return '{{%addons_lucky_group_visit_log}}';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['mall_id'], 'required'],
  33. [['mall_id', 'visit_user_id', 'view_num', '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' => '商城ID',
  44. 'visit_user_id' => '访问者用户ID',
  45. 'view_num' => '浏览次数',
  46. 'status' => '状态[-1:删除;0:禁用;1启用]',
  47. 'created_at' => '创建时间',
  48. 'updated_at' => '更新时间',
  49. ];
  50. }
  51. /**
  52. * 添加访问日志
  53. * @param $params
  54. * @return bool
  55. */
  56. public static function addLog($params)
  57. {
  58. $today = DateHelper::today();
  59. $model = self::find()
  60. ->where(array(
  61. 'visit_user_id' => $params['visit_user_id'],
  62. 'mall_id' => $params['mall_id']
  63. ))->andWhere(array('>=', 'created_at', $today['start']))
  64. ->andWhere(array('<=', 'created_at', $today['end']))
  65. ->one();
  66. if (empty($model)) {
  67. $model = new self();
  68. $model->mall_id = $params['mall_id'];
  69. $model->visit_user_id = isset($params['visit_user_id']) && !empty($params['visit_user_id']) ? $params['visit_user_id'] : '';
  70. $res = $model->save();
  71. if ($res === false) {
  72. self::$static_error = $model->getErrorMessage();
  73. return false;
  74. }
  75. }
  76. $model->view_num += 1;
  77. $res = $model->save();
  78. if ($res === false) {
  79. self::$static_error = $model->getErrorMessage();
  80. return false;
  81. }
  82. }
  83. }