Files

161 lines
4.6 KiB
Go

package technology
import (
"SciencesServer/app/api/admin/model"
"SciencesServer/app/basic/controller"
model2 "SciencesServer/app/common/model"
"SciencesServer/app/session"
"errors"
)
type Demand struct {
*session.Admin
}
type DemandHandle func(session *session.Admin) *Demand
type (
// DemandInfo 需求信息
DemandInfo struct {
ID string `json:"id"`
*model.TechnologyDemandInfo
Area string `json:"area"`
}
// DemandDetailInfo 需求详细信息
DemandDetailInfo struct {
ID string `json:"id"`
TenantID string `json:"tenant_id"`
*model2.TechnologyDemand
}
)
// Instance 首页信息
func (c *Demand) Instance(tenantID uint64, title string, kind, page, pageSize int) (*controller.ReturnPages, error) {
mTechnologyDemand := model.NewTechnologyDemand()
where := make([]*model2.ModelWhere, 0)
if c.TenantID > 0 {
where = append(where, model2.NewWhere("d.tenant_id", c.TenantID))
}
if tenantID > 0 {
where = append(where, model2.NewWhere("d.tenant_id", tenantID))
}
if title != "" {
where = append(where, model2.NewWhereLike("d.title", title))
}
if kind > 0 {
where = append(where, model2.NewWhere("d.kind", kind))
}
var count int64
out, err := mTechnologyDemand.Demand(page, pageSize, &count, where...)
if err != nil {
return nil, err
}
list := make([]*DemandInfo, 0)
for _, v := range out {
list = append(list, &DemandInfo{
ID: v.GetEncodeID(),
TechnologyDemandInfo: v,
Area: v.FormatBasic(),
})
}
return &controller.ReturnPages{Data: list, Count: count}, nil
}
// Detail 详细信息
func (c *Demand) Detail(id uint64) (*DemandDetailInfo, error) {
mTechnologyDemand := model.NewTechnologyDemand()
mTechnologyDemand.ID = id
isExist, err := model2.First(mTechnologyDemand.TechnologyDemand)
if err != nil {
return nil, err
} else if !isExist {
return nil, errors.New("操作错误,需求信息不存在或已被删除")
}
return &DemandDetailInfo{
ID: mTechnologyDemand.GetEncodeID(),
TenantID: mTechnologyDemand.GetEncodeTenantID(),
TechnologyDemand: mTechnologyDemand.TechnologyDemand,
}, err
}
// Examine 审核操作
func (c *Demand) Examine(id uint64, status int, remark string) error {
mTechnologyDemand := model.NewTechnologyDemand()
mTechnologyDemand.ID = id
isExist, err := model2.FirstField(mTechnologyDemand.TechnologyDemand, []string{"id", "tenant_id", "status"})
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误,需求信息不存在或已被删除")
}
if c.TenantID > 0 && mTechnologyDemand.TenantID != c.TenantID {
return errors.New("操作错误,无权限操作")
}
if mTechnologyDemand.Status != model2.TechnologyStatusKindForExamining {
return errors.New("操作错误,当前需求状态不允许审核")
}
return handleExamine(mTechnologyDemand.TechnologyDemand, c.UID, model2.SysUserExamineLogKindForDemandTechnology, status, remark)
}
// Delete 删除操作
func (c *Demand) Delete(id uint64) error {
mTechnologyDemand := model.NewTechnologyDemand()
mTechnologyDemand.ID = id
isExist, err := model2.FirstField(mTechnologyDemand.TechnologyDemand, []string{"id", "tenant_id"})
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误,需求信息不存在或已被删除")
} else if c.TenantID > 0 && mTechnologyDemand.TenantID != c.TenantID {
return errors.New("操作错误,无权限操作")
}
return model2.Delete(mTechnologyDemand.TechnologyDemand)
}
// Assign 指派经纪人
func (c *Demand) Assign(id, agentID uint64) error {
mTechnologyDemand := model.NewTechnologyDemand()
mTechnologyDemand.ID = id
isExist, err := model2.FirstField(mTechnologyDemand.TechnologyDemand, []string{"id", "tenant_id"})
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误,需求信息不存在或已被删除")
} else if c.TenantID > 0 && mTechnologyDemand.TenantID != c.TenantID {
return errors.New("操作错误,无权限操作")
}
// 查询该需求是否已经指派
mTechnologyDemandService := model.NewTechnologyDemandService()
var count int64
if err = model2.Count(mTechnologyDemandService.TechnologyDemandService, &count, model2.NewWhere("demand_id", id)); err != nil {
return err
} else if count > 0 {
return errors.New("操作错误,该需求信息已指派,请勿重复指派")
}
mTechnologyDemandService.DemandID = id
mTechnologyDemandService.AgentID = agentID
return model2.Create(mTechnologyDemandService.TechnologyDemandService)
}
func NewDemand() DemandHandle {
return func(session *session.Admin) *Demand {
return &Demand{session}
}
}