2022-01-11 17:24:38 +08:00
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" ) .
2022-01-13 10:14:02 +08:00
Select ( "a.id" , "a.tenant_id" , "a.image" , "a.title" , "a.contact" , "a.industry" , "a.contact_mobile" , "a.begin_at" ,
2022-01-12 15:05:14 +08:00
"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" ) .
2022-01-12 17:29:05 +08:00
Joins ( fmt . Sprintf ( "LEFT JOIN (SELECT activity_id, COUNT(id) AS count FROM %s WHERE is_deleted = %d AND status = %d GROUP BY activity_id) AS j ON a.id = j.activity_id" ,
2022-01-11 17:24:38 +08:00
model . NewActivityJoin ( ) . TableName ( ) , model . DeleteStatusForNot , model . ActivityJoinStatusForSuccess ) ) .
2022-01-12 17:29:05 +08:00
Joins ( fmt . Sprintf ( "LEFT JOIN %s AS t ON a.tenant_id = t.id" , model . NewSysTenant ( ) . TableName ( ) ) ) .
2022-01-11 17:24:38 +08:00
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 ( ) }
}