246 lines
8.6 KiB
Go
246 lines
8.6 KiB
Go
package technology
|
||
|
||
import (
|
||
"SciencesServer/app/api/enterprise/model"
|
||
"SciencesServer/app/basic/controller"
|
||
model2 "SciencesServer/app/common/model"
|
||
"SciencesServer/app/session"
|
||
"SciencesServer/serve/orm"
|
||
"SciencesServer/utils"
|
||
"errors"
|
||
"gorm.io/gorm"
|
||
"time"
|
||
)
|
||
|
||
// Demand 技术需求管理
|
||
type Demand struct {
|
||
*session.Enterprise
|
||
tenantID uint64
|
||
}
|
||
|
||
type DemandHandle func(session *session.Enterprise, tenantID uint64) *Demand
|
||
|
||
type (
|
||
// DemandInfo 需求信息
|
||
DemandInfo struct {
|
||
ID string `json:"id"`
|
||
Title string `json:"title"`
|
||
Name string `json:"name"`
|
||
Mobile string `json:"mobile"`
|
||
Kinds []string `json:"kinds"`
|
||
Budget float64 `json:"budget"`
|
||
BudgetMode model2.TechnologyDemandBudgetMode `json:"budget_mode"`
|
||
Deadline time.Time `json:"deadline"`
|
||
CreatedAt time.Time `json:"created_at"`
|
||
}
|
||
// DemandDetailInfo 需求详细信息
|
||
DemandDetailInfo struct {
|
||
ID string `json:"id"`
|
||
*model2.TechnologyDemand
|
||
Kinds []string `json:"kinds"`
|
||
Industry []string `json:"industry"`
|
||
*model2.TechnologyDemandOther
|
||
}
|
||
// DemandParams 需求参数信息
|
||
DemandParams struct {
|
||
ID uint64
|
||
Title, Introduce, Name, Mobile, Deadline string
|
||
Industry, Kinds []string
|
||
Budget float64
|
||
BudgetMode int
|
||
Expects []string // 期望合作的企业及模式
|
||
Demand struct {
|
||
Basic string // 基础
|
||
Expect string // 预期
|
||
Benefit string // 效益
|
||
} // 需求详细信息
|
||
IsSubmit int
|
||
}
|
||
)
|
||
|
||
// List 列表信息
|
||
func (c *Demand) List(status, page, pageSize int) (*controller.ReturnPages, error) {
|
||
mTechnologyDemand := model.NewTechnologyDemand()
|
||
|
||
where := []*model2.ModelWhereOrder{&model2.ModelWhereOrder{
|
||
Where: model2.NewWhere("uid", c.UID),
|
||
Order: model2.NewOrder("id", model2.OrderModeToDesc),
|
||
}, &model2.ModelWhereOrder{
|
||
Where: model2.NewWhere("status", status),
|
||
}}
|
||
var count int64
|
||
|
||
out := make([]*model2.TechnologyDemand, 0)
|
||
|
||
if err := model2.PagesFields(mTechnologyDemand.TechnologyDemand, &out, []string{"id", "title", "kind", "name",
|
||
"mobile", "budget", "budget_mode", "deadline", "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(), Title: v.Title, Name: v.Name, Mobile: v.Mobile, Budget: v.Budget, BudgetMode: v.BudgetMode,
|
||
Kinds: v.GetKindAttribute(), Deadline: v.Deadline, CreatedAt: v.CreatedAt,
|
||
})
|
||
}
|
||
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(), TechnologyDemand: mTechnologyDemand.TechnologyDemand,
|
||
Kinds: mTechnologyDemand.GetKindAttribute(), Industry: mTechnologyDemand.GetIndustryAttribute(),
|
||
TechnologyDemandOther: mTechnologyDemand.GetOtherAttribute(),
|
||
}, nil
|
||
}
|
||
|
||
// Form 数据操作
|
||
func (c *Demand) Form(params *DemandParams) error {
|
||
mTechnologyDemand := model.NewTechnologyDemand()
|
||
|
||
if params.ID > 0 {
|
||
mTechnologyDemand.ID = params.ID
|
||
|
||
isExist, err := model2.FirstField(mTechnologyDemand.TechnologyDemand, []string{"id", "uid", "status", "created_at"})
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,需求信息不存在或已被删除")
|
||
} else if mTechnologyDemand.UID != c.UID {
|
||
return errors.New("无权限操作")
|
||
} else if mTechnologyDemand.Status != model2.TechnologyStatusKindForRefuse &&
|
||
mTechnologyDemand.Status != model2.TechnologyStatusKindForDraft {
|
||
return errors.New("操作错误,当前状态不允许修改")
|
||
}
|
||
}
|
||
mTechnologyDemand.Title = params.Title
|
||
mTechnologyDemand.Name = params.Name
|
||
mTechnologyDemand.Mobile = params.Mobile
|
||
mTechnologyDemand.Introduce = params.Introduce
|
||
mTechnologyDemand.SetKindAttribute(params.Kinds)
|
||
mTechnologyDemand.SetIndustryAttribute(params.Industry)
|
||
mTechnologyDemand.Budget = params.Budget
|
||
mTechnologyDemand.BudgetMode = model2.TechnologyDemandBudgetMode(params.BudgetMode)
|
||
mTechnologyDemand.Deadline = utils.DataTimeToDate(params.Deadline)
|
||
|
||
mTechnologyDemand.SetOtherAttribute(&model2.TechnologyDemandOther{
|
||
Expect: params.Expects,
|
||
Demand: struct {
|
||
Basic string `json:"basic"`
|
||
Expect string `json:"expect"`
|
||
Benefit string `json:"benefit"`
|
||
}{
|
||
Basic: params.Demand.Basic, Expect: params.Demand.Expect, Benefit: params.Demand.Benefit,
|
||
},
|
||
})
|
||
if mTechnologyDemand.ID > 0 {
|
||
mTechnologyDemand.Status = model2.TechnologyStatusKindForExamining
|
||
return model2.Updates(mTechnologyDemand.TechnologyDemand, mTechnologyDemand.TechnologyDemand)
|
||
}
|
||
mTechnologyDemand.UID = c.UID
|
||
mTechnologyDemand.TenantID = c.tenantID
|
||
|
||
if params.IsSubmit > 0 {
|
||
mTechnologyDemand.Status = model2.TechnologyStatusKindForExamining
|
||
}
|
||
return model2.Create(mTechnologyDemand.TechnologyDemand)
|
||
}
|
||
|
||
// Delete 删除操作
|
||
func (c *Demand) Delete(id uint64) error {
|
||
mTechnologyDemand := model.NewTechnologyDemand()
|
||
mTechnologyDemand.ID = id
|
||
|
||
isExist, err := model2.FirstField(mTechnologyDemand.TechnologyDemand, []string{"id", "m_uid", "status"})
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,数据不存在")
|
||
} else if mTechnologyDemand.UID != c.UID {
|
||
return errors.New("无权限操作")
|
||
}
|
||
if err = model2.Delete(mTechnologyDemand.TechnologyDemand); err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// Progress 进度信息
|
||
func (c *Demand) Progress(demandID uint64) {
|
||
mTechnologyDemandService := model.NewTechnologyDemandService()
|
||
|
||
model2.FirstWhere(mTechnologyDemandService.TechnologyDemandService,
|
||
model2.NewWhere("demand_id", demandID))
|
||
}
|
||
|
||
// ProgressLaunch 进度发起
|
||
func (c *Demand) ProgressLaunch(demandID uint64, status int, config string) error {
|
||
// TODO:缺少发起判断
|
||
// 查询当前进度
|
||
mTechnologyDemandService := model.NewTechnologyDemandService()
|
||
|
||
isExist, err := model2.FirstWhere(mTechnologyDemandService.TechnologyDemandService,
|
||
model2.NewWhere("demand_id", demandID))
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return orm.GetDB().Transaction(func(tx *gorm.DB) error {
|
||
mTechnologyDemandService.UID = c.UID
|
||
mTechnologyDemandService.DemandID = demandID
|
||
mTechnologyDemandService.Progress = model2.TechnologyDemandServiceProgressKindForMemorandum
|
||
|
||
if err = model2.Create(mTechnologyDemandService.TechnologyDemandService, tx); err != nil {
|
||
return err
|
||
}
|
||
mTechnologyDemandServiceProgress := model.NewTechnologyDemandServiceProgress()
|
||
mTechnologyDemandServiceProgress.ServiceID = mTechnologyDemandService.ID
|
||
mTechnologyDemandServiceProgress.Progress = model2.TechnologyDemandServiceProgressKindForMemorandum
|
||
mTechnologyDemandServiceProgress.Config = config
|
||
return model2.Create(mTechnologyDemandServiceProgress.TechnologyDemandServiceProgress, tx)
|
||
})
|
||
}
|
||
if mTechnologyDemandService.Status != model2.TechnologyDemandServiceStatusForOngoing {
|
||
return errors.New("操作错误,当前状态不可处理")
|
||
}
|
||
return orm.GetDB().Transaction(func(tx *gorm.DB) error {
|
||
// 下一流程
|
||
mTechnologyDemandService.Progress = mTechnologyDemandService.NextProgress()
|
||
|
||
if mTechnologyDemandService.Progress == model2.TechnologyDemandServiceProgressKindForComplete {
|
||
if status <= 0 {
|
||
return errors.New("操作错误,请确认结题状态")
|
||
}
|
||
mTechnologyDemandService.Status = model2.TechnologyDemandServiceStatus(status)
|
||
}
|
||
if err = model2.Updates(mTechnologyDemandService.TechnologyDemandService, tx); err != nil {
|
||
return err
|
||
}
|
||
mTechnologyDemandServiceProgress := model.NewTechnologyDemandServiceProgress()
|
||
mTechnologyDemandServiceProgress.ServiceID = mTechnologyDemandService.ID
|
||
mTechnologyDemandServiceProgress.Progress = mTechnologyDemandService.Progress
|
||
mTechnologyDemandServiceProgress.Config = config
|
||
return model2.Create(mTechnologyDemandServiceProgress.TechnologyDemandServiceProgress, tx)
|
||
})
|
||
}
|
||
|
||
func NewDemand() DemandHandle {
|
||
return func(session *session.Enterprise, tenantID uint64) *Demand {
|
||
return &Demand{Enterprise: session, tenantID: tenantID}
|
||
}
|
||
}
|