2021-12-14 13:34:42 +08:00
|
|
|
|
package controller
|
|
|
|
|
|
2021-12-15 17:36:56 +08:00
|
|
|
|
import (
|
|
|
|
|
"SciencesServer/app/api/website/model"
|
2022-01-18 09:20:18 +08:00
|
|
|
|
config2 "SciencesServer/app/basic/config"
|
2021-12-15 17:36:56 +08:00
|
|
|
|
"SciencesServer/app/basic/controller"
|
|
|
|
|
model2 "SciencesServer/app/common/model"
|
2021-12-16 15:07:53 +08:00
|
|
|
|
"SciencesServer/app/service"
|
2021-12-15 17:36:56 +08:00
|
|
|
|
"SciencesServer/app/session"
|
2021-12-16 15:07:53 +08:00
|
|
|
|
"SciencesServer/config"
|
2021-12-15 17:36:56 +08:00
|
|
|
|
"errors"
|
2022-01-17 16:56:07 +08:00
|
|
|
|
"fmt"
|
2022-01-18 09:20:18 +08:00
|
|
|
|
"strings"
|
2022-01-17 16:56:07 +08:00
|
|
|
|
"time"
|
2021-12-15 17:36:56 +08:00
|
|
|
|
)
|
2021-12-14 13:34:42 +08:00
|
|
|
|
|
2021-12-15 17:36:56 +08:00
|
|
|
|
type Activity struct {
|
|
|
|
|
*session.Enterprise
|
2022-01-14 09:56:55 +08:00
|
|
|
|
tenantID uint64
|
2021-12-15 17:36:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-14 09:56:55 +08:00
|
|
|
|
type ActivityHandle func(session *session.Enterprise, tenantID uint64) *Activity
|
2021-12-15 17:36:56 +08:00
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
|
// ActivityInfo 活动信息
|
|
|
|
|
ActivityInfo struct {
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
*model.ActivityInstanceInfo
|
2022-01-17 16:56:07 +08:00
|
|
|
|
IsJoin bool `json:"is_join"`
|
|
|
|
|
Status int `json:"status"` // 状态(1:未开始,2:进行中,3:已结束)
|
2021-12-15 17:36:56 +08:00
|
|
|
|
}
|
|
|
|
|
// ActivityDetail 详细信息
|
|
|
|
|
ActivityDetail struct {
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
*model.ActivityInstanceDetail
|
2022-01-18 09:20:18 +08:00
|
|
|
|
Industry string `json:"industry"`
|
|
|
|
|
IsJoin bool `json:"is_join"`
|
2021-12-15 17:36:56 +08:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Instance 活动信息
|
2022-01-17 16:56:07 +08:00
|
|
|
|
func (c *Activity) Instance(title, industry string, status, page, pageSize int) (*controller.ReturnPages, error) {
|
2021-12-15 17:36:56 +08:00
|
|
|
|
mActivityInstance := model.NewActivityInstance()
|
|
|
|
|
|
2022-01-17 16:56:07 +08:00
|
|
|
|
now := time.Now()
|
2021-12-15 17:36:56 +08:00
|
|
|
|
|
2022-01-14 09:56:55 +08:00
|
|
|
|
where := []*model2.ModelWhere{model2.NewWhere("a.tenant_id", c.tenantID)}
|
2021-12-14 13:34:42 +08:00
|
|
|
|
|
2021-12-15 17:36:56 +08:00
|
|
|
|
if title != "" {
|
|
|
|
|
where = append(where, model2.NewWhereLike("a.title", title))
|
|
|
|
|
}
|
2022-01-17 16:56:07 +08:00
|
|
|
|
if industry != "" {
|
|
|
|
|
where = append(where, model2.NewWhereCondition("a.industry", "LIKE", "%"+fmt.Sprintf(`"%v`, industry)+"%"))
|
|
|
|
|
}
|
|
|
|
|
if status > 0 {
|
|
|
|
|
if status == 1 {
|
|
|
|
|
where = append(where, model2.NewWhereCondition("a.begin_at", ">", now))
|
|
|
|
|
} else if status == 2 {
|
|
|
|
|
where = append(where, model2.NewWhereCondition("a.begin_at", "<=", now),
|
|
|
|
|
model2.NewWhereCondition("a.finish_at", ">=", now))
|
|
|
|
|
} else {
|
|
|
|
|
where = append(where, model2.NewWhereCondition("a.finish_at", "<", now))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var count int64
|
2022-01-15 11:54:05 +08:00
|
|
|
|
|
|
|
|
|
out, err := mActivityInstance.Activity(c.UID, c.SelectIdentity, page, pageSize, &count, where...)
|
2021-12-15 17:36:56 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list := make([]*ActivityInfo, 0)
|
|
|
|
|
|
|
|
|
|
for _, v := range out {
|
2022-01-18 09:20:18 +08:00
|
|
|
|
v.Image.Image = v.Image.Analysis(config.SystemConfig[config.SysImageDomain])
|
|
|
|
|
|
2022-01-17 16:56:07 +08:00
|
|
|
|
data := &ActivityInfo{
|
|
|
|
|
ID: v.GetEncodeID(), ActivityInstanceInfo: v, IsJoin: v.JoinID > 0, Status: 2,
|
|
|
|
|
}
|
2022-01-18 09:20:18 +08:00
|
|
|
|
if now.Before(v.BeginAt) {
|
2022-01-17 16:56:07 +08:00
|
|
|
|
data.Status = 1
|
|
|
|
|
goto CONTINUE
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 09:20:18 +08:00
|
|
|
|
if now.After(v.FinishAt) {
|
2022-01-17 16:56:07 +08:00
|
|
|
|
data.Status = 3
|
|
|
|
|
goto CONTINUE
|
|
|
|
|
}
|
|
|
|
|
CONTINUE:
|
|
|
|
|
list = append(list, data)
|
2021-12-15 17:36:56 +08:00
|
|
|
|
}
|
|
|
|
|
return &controller.ReturnPages{Data: list, Count: count}, nil
|
2021-12-14 13:34:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 17:36:56 +08:00
|
|
|
|
// Detail 详细信息
|
|
|
|
|
func (c *Activity) Detail(id uint64) (*ActivityDetail, error) {
|
|
|
|
|
mActivityInstance := model.NewActivityInstance()
|
|
|
|
|
|
2022-01-13 17:48:41 +08:00
|
|
|
|
out, err := mActivityInstance.Detail(id, c.UID, c.SelectIdentity)
|
2021-12-15 17:36:56 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-01-18 09:20:18 +08:00
|
|
|
|
out.Image.Image = out.Image.Analysis(config.SystemConfig[config.SysImageDomain])
|
|
|
|
|
|
|
|
|
|
industrys := make([]string, 0)
|
|
|
|
|
|
|
|
|
|
for _, v := range out.GetIndustryAttribute() {
|
2022-01-20 09:43:26 +08:00
|
|
|
|
industrys = append(industrys, config2.GetIndustryInfo(v, "-", "-").Value)
|
2022-01-18 09:20:18 +08:00
|
|
|
|
}
|
2022-01-15 11:54:05 +08:00
|
|
|
|
|
2021-12-15 17:36:56 +08:00
|
|
|
|
return &ActivityDetail{
|
|
|
|
|
ID: out.GetEncodeID(),
|
|
|
|
|
ActivityInstanceDetail: out,
|
2022-01-18 09:20:18 +08:00
|
|
|
|
Industry: strings.Join(industrys, ";"),
|
|
|
|
|
IsJoin: out.JoinID > 0.,
|
2021-12-15 17:36:56 +08:00
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Join 加入活动
|
|
|
|
|
func (c *Activity) Join(id uint64) error {
|
|
|
|
|
mActivityInstance := model.NewActivityInstance()
|
|
|
|
|
|
2022-01-15 11:54:05 +08:00
|
|
|
|
isExist, err := model2.FirstField(mActivityInstance.ActivityInstance, []string{"id", "amount", "join_deadline"},
|
2022-01-14 09:56:55 +08:00
|
|
|
|
model2.NewWhere("id", id), model2.NewWhere("tenant_id", c.tenantID),
|
2021-12-15 17:36:56 +08:00
|
|
|
|
model2.NewWhere("status", model2.ActivityInstanceStatusForShow))
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if !isExist {
|
|
|
|
|
return errors.New("操作错误,活动信息不存在或已被删除")
|
|
|
|
|
}
|
2021-12-16 15:07:53 +08:00
|
|
|
|
|
2021-12-15 17:36:56 +08:00
|
|
|
|
if !mActivityInstance.IsCanJoin() {
|
|
|
|
|
return errors.New("操作错误,当前活动信息不可报名")
|
|
|
|
|
}
|
2022-01-13 17:48:41 +08:00
|
|
|
|
if c.SelectIdentity <= 0 {
|
|
|
|
|
return errors.New("操作错误,当前未选择任何身份信息")
|
|
|
|
|
}
|
2021-12-16 15:07:53 +08:00
|
|
|
|
// 查看当前活动是否报名
|
|
|
|
|
var count int64
|
2021-12-15 17:36:56 +08:00
|
|
|
|
|
2022-01-13 17:48:41 +08:00
|
|
|
|
if err = model2.Count(model.NewActivityJoin().ActivityJoin, &count, model2.NewWhere("id", c.UID),
|
|
|
|
|
model2.NewWhere("identity", c.SelectIdentity),
|
2021-12-16 15:07:53 +08:00
|
|
|
|
model2.NewWhere("activity_id", id), model2.NewWhere("status", model2.ActivityJoinStatusForSuccess)); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if count > 0 {
|
|
|
|
|
return errors.New("操作错误,不可重复报名")
|
|
|
|
|
}
|
2022-01-12 15:05:14 +08:00
|
|
|
|
if mActivityInstance.Amount <= 0 {
|
2022-01-13 17:48:41 +08:00
|
|
|
|
service.Publish(config.EventForActivityJoinProduce, id, c.UID, c.SelectIdentity)
|
2021-12-16 15:07:53 +08:00
|
|
|
|
}
|
2021-12-14 13:34:42 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2021-12-15 17:36:56 +08:00
|
|
|
|
|
|
|
|
|
func NewActivity() ActivityHandle {
|
2022-01-14 09:56:55 +08:00
|
|
|
|
return func(session *session.Enterprise, tenantID uint64) *Activity {
|
|
|
|
|
return &Activity{Enterprise: session, tenantID: tenantID}
|
2021-12-15 17:36:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|