158 lines
5.4 KiB
Go
158 lines
5.4 KiB
Go
package technology
|
|
|
|
import (
|
|
"SciencesServer/app/api/controller"
|
|
model2 "SciencesServer/app/common/model"
|
|
"SciencesServer/app/enterprise/model"
|
|
"SciencesServer/app/service"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
// Instance 技术管理
|
|
type Instance struct{ *service.SessionEnterprise }
|
|
|
|
type InstanceHandle func(enterprise *service.SessionEnterprise) *Instance
|
|
|
|
type (
|
|
// InstanceInfo 详细信息
|
|
InstanceInfo struct {
|
|
ID string `json:"id"`
|
|
*model2.TechnologyInstance
|
|
Products []string `json:"products"`
|
|
Keywords []string `json:"keywords"`
|
|
}
|
|
// InstanceParams 参数信息
|
|
InstanceParams struct {
|
|
ID, PatentID, Territory uint64
|
|
Title, Company, Images, ProveImages, Introduce, Purpose, Remark string
|
|
Maturity, Prototype, Source, Transaction, Status int
|
|
Products, Keywords []string
|
|
}
|
|
)
|
|
|
|
// List 列表信息
|
|
func (c *Instance) List(status, page, pageSize int) (*controller.ReturnPages, error) {
|
|
mTechnologyInstance := model.NewTechnologyInstance()
|
|
out := make([]*model2.TechnologyInstance, 0)
|
|
|
|
where := []*model2.ModelWhereOrder{&model2.ModelWhereOrder{
|
|
Where: model2.NewWhere("status", status),
|
|
Order: model2.NewOrder("id", model2.OrderModeToDesc),
|
|
}}
|
|
var count int64
|
|
|
|
if err := model2.Pages(mTechnologyInstance.TechnologyInstance, &out, page, pageSize, &count, where...); err != nil {
|
|
return nil, err
|
|
}
|
|
list := make([]*InstanceInfo, 0)
|
|
|
|
for _, v := range out {
|
|
list = append(list, &InstanceInfo{
|
|
ID: v.GetEncodeID(), TechnologyInstance: v,
|
|
Products: v.GetProductAttribute(), Keywords: v.GetKeywordAttribute(),
|
|
})
|
|
}
|
|
return &controller.ReturnPages{Data: list, Count: count}, nil
|
|
}
|
|
|
|
// Form 数据操作
|
|
func (c *Instance) Form(params *InstanceParams) error {
|
|
mTechnologyInstance := model.NewTechnologyInstance()
|
|
|
|
if params.ID > 0 {
|
|
mTechnologyInstance.ID = params.ID
|
|
|
|
isExist, err := model2.FirstField(mTechnologyInstance.TechnologyInstance, []string{"id", "uid",
|
|
"status", "created_at", "updated_at"})
|
|
|
|
if err != nil {
|
|
return err
|
|
} else if !isExist {
|
|
return errors.New("信息不存在")
|
|
} else if c.TenantUID != mTechnologyInstance.UID {
|
|
return errors.New("操作错误,无权限操作")
|
|
} else if mTechnologyInstance.Status != model2.TechnologyInstanceStatusForDraft &&
|
|
mTechnologyInstance.Status != model2.TechnologyInstanceStatusForRefuse {
|
|
return errors.New("操作错误,当前状态不允许修改")
|
|
}
|
|
}
|
|
mTechnologyInstance.UID = c.TenantUID
|
|
mTechnologyInstance.PatentID = params.PatentID
|
|
mTechnologyInstance.Title = params.Title
|
|
mTechnologyInstance.Company = params.Company
|
|
mTechnologyInstance.Maturity = model2.TechnologyInstanceMaturity(params.Maturity)
|
|
mTechnologyInstance.Prototype = params.Prototype
|
|
mTechnologyInstance.SetProductAttribute(params.Products)
|
|
mTechnologyInstance.Source = model2.TechnologyInstanceSource(params.Source)
|
|
mTechnologyInstance.SetKeywordAttribute(params.Keywords)
|
|
mTechnologyInstance.Territory = params.Territory
|
|
mTechnologyInstance.Transaction = params.Transaction
|
|
mTechnologyInstance.Images = model2.Images{Images: params.Images}
|
|
mTechnologyInstance.ProveImages = params.ProveImages
|
|
mTechnologyInstance.Introduce = params.Introduce
|
|
mTechnologyInstance.Purpose = params.Purpose
|
|
mTechnologyInstance.Remark = params.Remark
|
|
mTechnologyInstance.Status = model2.TechnologyInstanceStatus(params.Status)
|
|
|
|
if mTechnologyInstance.Status != model2.TechnologyInstanceStatusForDraft &&
|
|
mTechnologyInstance.Status != model2.TechnologyInstanceStatusForExamining {
|
|
return errors.New("操作错误,状态参数错误")
|
|
}
|
|
if params.ID > 0 {
|
|
mTechnologyInstance.UpdatedAt = time.Now()
|
|
return model2.Updates(mTechnologyInstance.TechnologyInstance, mTechnologyInstance.TechnologyInstance)
|
|
}
|
|
return model2.Create(mTechnologyInstance.TechnologyInstance)
|
|
}
|
|
|
|
// Shelf 上下架操作
|
|
func (c *Instance) Shelf(id uint64, status int) error {
|
|
mTechnologyInstance := model.NewTechnologyInstance()
|
|
mTechnologyInstance.ID = id
|
|
isExist, err := model2.FirstField(mTechnologyInstance.TechnologyInstance, []string{"id", "uid", "shelf_status", "status"})
|
|
|
|
if err != nil {
|
|
return err
|
|
} else if !isExist {
|
|
return errors.New("信息不存在")
|
|
} else if c.TenantUID != mTechnologyInstance.UID {
|
|
return errors.New("操作错误,无权限操作")
|
|
} else if mTechnologyInstance.Status != model2.TechnologyInstanceStatusForAgree {
|
|
return errors.New("操作错误,当前状态不允许处理上下架")
|
|
} else if mTechnologyInstance.ShelfStatus.ShelfStatus == model2.ShelfStatusKind(status) {
|
|
return errors.New("操作错误,状态异常")
|
|
}
|
|
if err = model2.Updates(mTechnologyInstance.TechnologyInstance, map[string]interface{}{
|
|
"shelf_status": status, "updated_at": time.Now(),
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Delete 删除操作
|
|
func (c *Instance) Delete(id uint64) error {
|
|
mTechnologyInstance := model.NewTechnologyInstance()
|
|
mTechnologyInstance.ID = id
|
|
isExist, err := model2.FirstField(mTechnologyInstance.TechnologyInstance, []string{"id", "uid"})
|
|
|
|
if err != nil {
|
|
return err
|
|
} else if !isExist {
|
|
return errors.New("信息不存在")
|
|
} else if c.TenantUID != mTechnologyInstance.UID {
|
|
return errors.New("操作错误,无权限操作")
|
|
}
|
|
if err = model2.Delete(mTechnologyInstance.TechnologyInstance); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func NewInstance() InstanceHandle {
|
|
return func(enterprise *service.SessionEnterprise) *Instance {
|
|
return &Instance{enterprise}
|
|
}
|
|
}
|