151 lines
4.3 KiB
Go
151 lines
4.3 KiB
Go
package service
|
|
|
|
import (
|
|
"SciencesServer/app/api/enterprise/model"
|
|
"SciencesServer/app/basic/controller"
|
|
model2 "SciencesServer/app/common/model"
|
|
"SciencesServer/app/session"
|
|
"errors"
|
|
)
|
|
|
|
type Demand struct {
|
|
*session.Enterprise
|
|
tenantID uint64
|
|
}
|
|
|
|
type (
|
|
// DemandInfo 需求信息
|
|
DemandInfo struct {
|
|
ID string `json:"id"`
|
|
*model2.ServiceDemand
|
|
Kinds []int `json:"kinds"`
|
|
}
|
|
// DemandParams 需求参数信息
|
|
DemandParams struct {
|
|
ID uint64
|
|
Kinds []int
|
|
IsSubmit int
|
|
Title, Name, Mobile, Description string
|
|
}
|
|
)
|
|
|
|
type DemandHandle func(session *session.Enterprise, tenantID uint64) *Demand
|
|
|
|
// List 列表信息
|
|
func (c *Demand) List(title string, status, page, pageSize int) (*controller.ReturnPages, error) {
|
|
mServiceDemand := model.NewServiceDemand()
|
|
|
|
where := []*model2.ModelWhereOrder{
|
|
&model2.ModelWhereOrder{
|
|
Where: model2.NewWhere("tenant_id", c.tenantID),
|
|
Order: model2.NewOrder("id", model2.OrderModeToDesc),
|
|
},
|
|
&model2.ModelWhereOrder{Where: model2.NewWhere("uid", c.UID)},
|
|
&model2.ModelWhereOrder{Where: model2.NewWhere("status", status)},
|
|
}
|
|
|
|
if title != "" {
|
|
where = append(where, &model2.ModelWhereOrder{Where: model2.NewWhereLike("title", title)})
|
|
}
|
|
out := make([]*model2.ServiceDemand, 0)
|
|
|
|
var count int64
|
|
|
|
if err := model2.PagesFields(mServiceDemand.ServiceDemand, &out, []string{"id", "title", "kind", "name", "mobile",
|
|
"status", "created_at"}, page, pageSize, &count, where...); err != nil {
|
|
return nil, err
|
|
}
|
|
list := make([]*DemandInfo, 0)
|
|
|
|
for _, v := range out {
|
|
list = append(list, &DemandInfo{
|
|
ID: v.GetEncodeID(),
|
|
ServiceDemand: v,
|
|
Kinds: v.GetKindAttribute(),
|
|
})
|
|
}
|
|
return &controller.ReturnPages{Data: list, Count: count}, nil
|
|
}
|
|
|
|
// Detail 详细信息
|
|
func (c *Demand) Detail(id uint64) (*DemandInfo, error) {
|
|
mServiceDemand := model.NewServiceDemand()
|
|
mServiceDemand.ID = id
|
|
|
|
isExist, err := model2.First(mServiceDemand.ServiceDemand)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
} else if !isExist {
|
|
return nil, errors.New("操作错误,需求信息不存在或已被删除")
|
|
} else if mServiceDemand.UID != c.UID {
|
|
return nil, errors.New("无权限操作")
|
|
}
|
|
return &DemandInfo{
|
|
ID: mServiceDemand.GetEncodeID(),
|
|
ServiceDemand: mServiceDemand.ServiceDemand,
|
|
Kinds: mServiceDemand.GetKindAttribute(),
|
|
}, nil
|
|
}
|
|
|
|
// Form 数据操作
|
|
func (c *Demand) Form(params *DemandParams) error {
|
|
mServiceDemand := model.NewServiceDemand()
|
|
|
|
if params.ID > 0 {
|
|
mServiceDemand.ID = params.ID
|
|
|
|
if isExist, err := model2.FirstField(mServiceDemand.ServiceDemand, []string{"id", "uid", "status", "created_at"}); err != nil {
|
|
return err
|
|
} else if !isExist {
|
|
return errors.New("操作错误,需求信息不存在或已被删除")
|
|
} else if mServiceDemand.UID != c.UID {
|
|
return errors.New("无权限操作")
|
|
} else if mServiceDemand.Status != model2.ServiceDemandStatusForDraft {
|
|
return errors.New("操作错误,当前需求状态不允许修改")
|
|
}
|
|
}
|
|
mServiceDemand.SetKindAttribute(params.Kinds)
|
|
mServiceDemand.Title = params.Title
|
|
mServiceDemand.Name = params.Name
|
|
mServiceDemand.Mobile = params.Mobile
|
|
mServiceDemand.Description = params.Description
|
|
|
|
if mServiceDemand.ID > 0 {
|
|
mServiceDemand.Status = model2.ServiceDemandStatusForPublish
|
|
|
|
return model2.Updates(mServiceDemand.ServiceDemand, mServiceDemand.ServiceDemand)
|
|
}
|
|
mServiceDemand.TenantID = c.tenantID
|
|
mServiceDemand.UID = c.UID
|
|
|
|
if params.IsSubmit > 0 {
|
|
mServiceDemand.Status = model2.ServiceDemandStatusForPublish
|
|
}
|
|
return model2.Create(mServiceDemand.ServiceDemand)
|
|
}
|
|
|
|
// Delete 删除操作
|
|
func (c *Demand) Delete(id uint64) error {
|
|
mServiceDemand := model.NewServiceDemand()
|
|
mServiceDemand.ID = id
|
|
|
|
if isExist, err := model2.FirstField(mServiceDemand.ServiceDemand, []string{"id", "uid"}); err != nil {
|
|
return err
|
|
} else if !isExist {
|
|
return errors.New("操作错误,需求信息不存在或已被删除")
|
|
} else if mServiceDemand.UID != c.UID {
|
|
return errors.New("无权限操作")
|
|
}
|
|
return model2.Delete(mServiceDemand.ServiceDemand)
|
|
}
|
|
|
|
func NewDemand() DemandHandle {
|
|
return func(session *session.Enterprise, tenantID uint64) *Demand {
|
|
return &Demand{
|
|
Enterprise: session,
|
|
tenantID: tenantID,
|
|
}
|
|
}
|
|
}
|