Files
2022-01-21 11:42:58 +08:00

155 lines
4.7 KiB
Go

package technology
import (
"SciencesServer/app/api/enterprise/model"
"SciencesServer/app/basic/controller"
model2 "SciencesServer/app/common/model"
"SciencesServer/app/session"
"SciencesServer/utils"
"errors"
"time"
)
type Topic struct {
*session.Enterprise
tenantID uint64
}
type TopicHandle func(session *session.Enterprise, tenantID uint64) *Topic
type (
// TopicInfo 课题信息
TopicInfo struct {
ID string `json:"id"`
Title string `json:"title"`
Kind model2.TechnologyTopicKind `json:"kind"`
Keywords []string `json:"keywords"`
CreatedAt time.Time `json:"created_at"`
}
// TopicDetailInfo 课题详细信息
TopicDetailInfo struct {
ID string `json:"id"`
*model2.TechnologyTopic
Keywords []string `json:"keywords"`
}
// TopicParams 课题参数信息
TopicParams struct {
ID uint64
Title, Emcee, Mechanism, Code, BeginAt, FinishAt string
Amount float64
Source, Kind int
Keywords []string
}
)
// List 列表信息
func (c *Topic) List(status int, page, pageSize int) (*controller.ReturnPages, error) {
mTechnologyTopic := model.NewTechnologyTopic()
where := []*model2.ModelWhereOrder{&model2.ModelWhereOrder{
Where: model2.NewWhere("status", status),
Order: model2.NewOrder("id", model2.OrderModeToDesc),
}, &model2.ModelWhereOrder{Where: model2.NewWhere("uid", c.UID)}}
var count int64
out := make([]*model2.TechnologyTopic, 0)
if err := model2.PagesFields(mTechnologyTopic.TechnologyTopic, &out, []string{"id", "title", "kind", "keyword", "created_at"},
page, pageSize, &count, where...); err != nil {
return nil, err
}
list := make([]*TopicInfo, 0)
for _, v := range out {
list = append(list, &TopicInfo{
ID: v.GetEncodeID(), Title: v.Title, Kind: v.Kind, Keywords: v.GetKeywordAttribute(),
CreatedAt: v.CreatedAt,
})
}
return &controller.ReturnPages{Data: list, Count: count}, nil
}
// Detail 详细信息
func (c *Topic) Detail(id uint64) (*TopicDetailInfo, error) {
mTechnologyTopic := model.NewTechnologyTopic()
mTechnologyTopic.ID = id
isExist, err := model2.First(mTechnologyTopic.TechnologyTopic)
if err != nil {
return nil, err
} else if !isExist {
return nil, errors.New("操作错误,课题信息不存在")
}
return &TopicDetailInfo{
ID: mTechnologyTopic.GetEncodeID(), TechnologyTopic: mTechnologyTopic.TechnologyTopic,
Keywords: mTechnologyTopic.GetKeywordAttribute(),
}, nil
}
// Form 数据处理
func (c *Topic) Form(params *TopicParams) error {
mTechnologyTopic := model.NewTechnologyTopic()
if params.ID > 0 {
mTechnologyTopic.ID = params.ID
isExist, err := model2.FirstField(mTechnologyTopic.TechnologyTopic, []string{"id", "uid", "status"})
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误,课题信息不存在")
} else if mTechnologyTopic.UID != c.UID {
return errors.New("无权限操作")
} else if mTechnologyTopic.Status != model2.TechnologyStatusKindForDraft &&
mTechnologyTopic.Status != model2.TechnologyStatusKindForRefuse {
return errors.New("操作错误,当前状态不允许修改")
}
}
mTechnologyTopic.Title = params.Title
mTechnologyTopic.Emcee = params.Emcee
mTechnologyTopic.Mechanism = params.Mechanism
mTechnologyTopic.Code = params.Code
mTechnologyTopic.Amount = params.Amount
mTechnologyTopic.Source = model2.TechnologyTopicSource(params.Source)
mTechnologyTopic.Kind = model2.TechnologyTopicKind(params.Kind)
mTechnologyTopic.SetKeywordAttribute(params.Keywords)
mTechnologyTopic.BeginAt = utils.DataTimeToDate(params.BeginAt)
mTechnologyTopic.FinishAt = utils.DataTimeToDate(params.FinishAt)
if mTechnologyTopic.ID > 0 {
mTechnologyTopic.UpdatedAt = time.Now()
return model2.Updates(mTechnologyTopic.TechnologyTopic, mTechnologyTopic.TechnologyTopic)
}
mTechnologyTopic.TenantID = c.tenantID
mTechnologyTopic.UID = c.UID
return model2.Create(mTechnologyTopic.TechnologyTopic)
}
// Delete 删除处理
func (c *Topic) Delete(id uint64) error {
mTechnologyTopic := model.NewTechnologyTopic()
mTechnologyTopic.ID = id
isExist, err := model2.FirstField(mTechnologyTopic.TechnologyTopic, []string{"id", "m_uid", "status"})
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误,课题信息不存在")
} else if mTechnologyTopic.UID != c.UID {
return errors.New("无权限操作")
}
return model2.Delete(mTechnologyTopic.TechnologyTopic)
}
func NewTopic() TopicHandle {
return func(session *session.Enterprise, tenantID uint64) *Topic {
return &Topic{
Enterprise: session,
tenantID: tenantID,
}
}
}