2021-11-25 09:58:08 +08:00
|
|
|
package activity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"SciencesServer/app/api/enterprise/model"
|
|
|
|
"SciencesServer/app/basic/controller"
|
|
|
|
model2 "SciencesServer/app/common/model"
|
|
|
|
"SciencesServer/app/session"
|
|
|
|
"SciencesServer/utils"
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Apply struct {
|
|
|
|
*session.Enterprise
|
|
|
|
local string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ApplyHandle func(session *session.Enterprise, local string) *Apply
|
|
|
|
|
|
|
|
type (
|
|
|
|
// ApplyLaunchParams 参数信息
|
|
|
|
ApplyLaunchParams struct {
|
|
|
|
Mode, MaxNUmber int
|
|
|
|
Title, Content string
|
|
|
|
BeginAt, FinishAt, JoinDeadline string
|
2021-12-15 17:36:56 +08:00
|
|
|
Amount float64
|
2021-11-25 09:58:08 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// List 列表信息
|
|
|
|
func (c *Apply) List(title string, page, pageSize int) (*controller.ReturnPages, error) {
|
|
|
|
mActivityApply := model.NewActivityApply()
|
|
|
|
|
|
|
|
out := make([]*model2.ActivityApply, 0)
|
|
|
|
|
|
|
|
where := []*model2.ModelWhereOrder{
|
|
|
|
&model2.ModelWhereOrder{
|
|
|
|
Where: model2.NewWhere("local", c.local),
|
|
|
|
Order: model2.NewOrder("id", model2.OrderModeToDesc),
|
|
|
|
},
|
|
|
|
&model2.ModelWhereOrder{
|
2021-12-07 16:10:12 +08:00
|
|
|
Where: model2.NewWhere("identity_uid", c.IdentityUID),
|
2021-11-25 09:58:08 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if title != "" {
|
|
|
|
where = append(where, &model2.ModelWhereOrder{
|
|
|
|
Where: model2.NewWhereLike("title", title),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
//if status > 0 {
|
|
|
|
// where = append(where, &model2.ModelWhereOrder{
|
|
|
|
// Where: model2.NewWhere("status", status),
|
|
|
|
// })
|
|
|
|
//}
|
|
|
|
var count int64
|
|
|
|
|
|
|
|
if err := model2.Pages(mActivityApply.ActivityApply, &out, page, pageSize, &count, where...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &controller.ReturnPages{Data: out, Count: count}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch 发起操作
|
|
|
|
func (c *Apply) Launch(params *ApplyLaunchParams) error {
|
|
|
|
mActivityApply := model.NewActivityApply()
|
|
|
|
mActivityApply.Local.Local = c.local
|
2021-12-07 16:10:12 +08:00
|
|
|
mActivityApply.IdentityUID = c.IdentityUID
|
2021-11-25 09:58:08 +08:00
|
|
|
mActivityApply.Mode = model2.ActivityInstanceMode(params.Mode)
|
|
|
|
mActivityApply.Title = params.Title
|
2021-12-15 17:36:56 +08:00
|
|
|
mActivityApply.Amount = params.Amount
|
2021-11-25 09:58:08 +08:00
|
|
|
mActivityApply.Content = params.Content
|
|
|
|
mActivityApply.MaxNumber = params.MaxNUmber
|
|
|
|
mActivityApply.BeginAt = utils.DateTimeToTime(params.BeginAt)
|
|
|
|
mActivityApply.FinishAt = utils.DateTimeToTime(params.FinishAt)
|
|
|
|
mActivityApply.JoinDeadline = utils.DateTimeToTime(params.JoinDeadline)
|
|
|
|
return model2.Create(mActivityApply.ActivityApply)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revoke 撤销操作
|
|
|
|
func (c *Apply) Revoke(id uint64) error {
|
|
|
|
mActivityApply := model.NewActivityApply()
|
|
|
|
mActivityApply.ID = id
|
|
|
|
|
2021-12-07 16:10:12 +08:00
|
|
|
isExist, err := model2.FirstField(mActivityApply.ActivityApply, []string{"id", "identity_uid", "status"})
|
2021-11-25 09:58:08 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !isExist {
|
|
|
|
return errors.New("操作错误,活动信息不存在或已被删除")
|
|
|
|
} else if mActivityApply.Status != model2.ActivityApplyStatusForExamining {
|
|
|
|
return errors.New("操作错误,当前活动状态易发生变化,不可撤销")
|
2021-12-07 16:10:12 +08:00
|
|
|
} else if mActivityApply.IdentityUID != c.IdentityUID {
|
|
|
|
return errors.New("无权限操作")
|
2021-11-25 09:58:08 +08:00
|
|
|
}
|
|
|
|
return model2.Updates(mActivityApply.ActivityApply, map[string]interface{}{
|
|
|
|
"status": model2.ActivityApplyStatusForRevoke, "updated_at": time.Now(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete 删除操作
|
|
|
|
func (c *Apply) Delete(id uint64) error {
|
|
|
|
mActivityApply := model.NewActivityApply()
|
|
|
|
mActivityApply.ID = id
|
2021-12-07 16:10:12 +08:00
|
|
|
|
|
|
|
isExist, err := model2.FirstField(mActivityApply.ActivityApply, []string{"id", "identity_uid"})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !isExist {
|
|
|
|
return errors.New("操作错误,活动信息不存在或已被删除")
|
|
|
|
} else if mActivityApply.IdentityUID != c.IdentityUID {
|
|
|
|
return errors.New("无权限操作")
|
|
|
|
}
|
2021-11-25 09:58:08 +08:00
|
|
|
return model2.Delete(mActivityApply.ActivityApply)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewApply() ApplyHandle {
|
|
|
|
return func(session *session.Enterprise, local string) *Apply {
|
|
|
|
return &Apply{
|
|
|
|
Enterprise: session,
|
|
|
|
local: local,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|