48 lines
1.6 KiB
Go
48 lines
1.6 KiB
Go
![]() |
package model
|
||
|
|
||
|
import (
|
||
|
"SciencesServer/app/common/model"
|
||
|
"SciencesServer/serve/orm"
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
|
type ActivityInstance struct {
|
||
|
*model.ActivityInstance
|
||
|
}
|
||
|
|
||
|
type ActivityInstanceInfo struct {
|
||
|
*model.ActivityInstance
|
||
|
JoinCount int `json:"join_count"`
|
||
|
model.Area
|
||
|
}
|
||
|
|
||
|
// Activity 活动信息
|
||
|
func (m *ActivityInstance) Activity(page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*ActivityInstanceInfo, error) {
|
||
|
db := orm.GetDB().Table(m.TableName()+" AS a").
|
||
|
Select("a.id", "a.tenant_id", "a.mode", "a.image", "a.title", "a.begin_at", "a.finish_at", "a.join_deadline",
|
||
|
"a.amount", "a.max_number", "a.status", "a.created_at", "j.count AS join_count", "t.province", "t.city").
|
||
|
Joins(fmt.Sprintf("LEFT JOIN (SELECT activity_id, COUNT(id) AS count FROM %s WHERE is_delete = %d AND status = %d GROUP BY activity_id) AS j ON a.id = j.activity_id",
|
||
|
model.NewActivityJoin().TableName(), model.DeleteStatusForNot, model.ActivityJoinStatusForSuccess)).
|
||
|
Joins(fmt.Sprintf("LEFT JOIN %s AS t ON i.tenant_id = t.id", model.NewSysTenant().TableName())).
|
||
|
Where("a.is_deleted = ?", model.DeleteStatusForNot)
|
||
|
|
||
|
if len(where) > 0 {
|
||
|
for _, wo := range where {
|
||
|
db = db.Where(wo.Condition, wo.Value)
|
||
|
}
|
||
|
}
|
||
|
out := make([]*ActivityInstanceInfo, 0)
|
||
|
|
||
|
if err := db.Count(count).Error; err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if err := db.Order("a.id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return out, nil
|
||
|
}
|
||
|
|
||
|
func NewActivityInstance() *ActivityInstance {
|
||
|
return &ActivityInstance{model.NewActivityInstance()}
|
||
|
}
|