52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package model
|
|
|
|
import (
|
|
"SciencesServer/app/common/model"
|
|
"SciencesServer/serve/orm"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type UserVisit struct {
|
|
*model.UserVisit
|
|
}
|
|
|
|
type (
|
|
ProductVisitInfo struct {
|
|
model.Model
|
|
VisitAt time.Time `json:"visit_at"`
|
|
model.ManageCompanyBasic
|
|
}
|
|
)
|
|
|
|
func (m *UserVisit) Product(page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*ProductVisitInfo, error) {
|
|
db := orm.GetDB().Table(m.TableName()+" AS v").
|
|
Select("v.id", "v.visit_at", "c.id AS company_id", "c.name AS company_name", "c.image AS company_image",
|
|
"c.kind AS company_kind", "c.url AS company_url").
|
|
Joins(fmt.Sprintf("LEFT JOIN %s AS u_c ON v.uid = c.uid AND u_c.invalid_status = %d AND u_c.is_deleted = %d",
|
|
model.NewUserCompany().TableName(), model.InvalidStatusForNot, model.DeleteStatusForNot)).
|
|
Joins(fmt.Sprintf("LEFT JOIN %s AS c ON u_c.company_id = c.id", model.NewManageCompany().TableName())).
|
|
Where("v.is_deleted = ?", model.DeleteStatusForNot).
|
|
Where("v.kind = ?", model.UserVisitKindForTechnologyProduct).
|
|
Where("u_c.id > ?", 0)
|
|
|
|
out := make([]*ProductVisitInfo, 0)
|
|
|
|
if len(where) > 0 {
|
|
for _, v := range where {
|
|
db = db.Where(v.Condition, v.Value)
|
|
}
|
|
}
|
|
if err := db.Count(count).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := db.Order("v.id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func NewUserVisit() *UserVisit {
|
|
return &UserVisit{model.NewUserVisit()}
|
|
}
|