package technology import ( "SciencesServer/app/api/enterprise/model" "SciencesServer/app/basic/config" "SciencesServer/app/basic/controller" model2 "SciencesServer/app/common/model" "SciencesServer/app/session" "errors" "time" ) // Instance 技术管理 type Instance struct { *session.Enterprise local string } type InstanceHandle func(session *session.Enterprise, local string) *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 int Products, Keywords []string IsSubmit int } ) // 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), }, &model2.ModelWhereOrder{ Where: model2.NewWhere("uid", c.UID), }} 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"}) if err != nil { return err } else if !isExist { return errors.New("操作错误,技术信息不存在或已被删除") } else if c.UID != mTechnologyInstance.UID { return errors.New("操作错误,无权限操作") } else if mTechnologyInstance.Status != model2.TechnologyInstanceStatusForDraft && mTechnologyInstance.Status != model2.TechnologyInstanceStatusForRefuse { return errors.New("操作错误,当前状态不允许修改") } } mTechnologyInstance.PatentID = params.PatentID mTechnologyInstance.Title = params.Title mTechnologyInstance.Company = params.Company mTechnologyInstance.Maturity = config.TechnologyMaturity(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 if mTechnologyInstance.Status != model2.TechnologyInstanceStatusForDraft && mTechnologyInstance.Status != model2.TechnologyInstanceStatusForExamining { return errors.New("操作错误,状态参数错误") } if params.ID > 0 { mTechnologyInstance.Status = model2.TechnologyInstanceStatusForExamining return model2.Updates(mTechnologyInstance.TechnologyInstance, mTechnologyInstance.TechnologyInstance) } mTechnologyInstance.UID = c.UID mTechnologyInstance.Local.Local = c.local if params.IsSubmit > 0 { mTechnologyInstance.Status = model2.TechnologyInstanceStatusForExamining } 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.UID != mTechnologyInstance.UID { return errors.New("操作错误,无权限操作") } else if mTechnologyInstance.Status != model2.TechnologyInstanceStatusForAgree { return errors.New("操作错误,当前状态不允许处理上下架") } else if mTechnologyInstance.Shelf.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.UID != mTechnologyInstance.UID { return errors.New("操作错误,无权限操作") } if err = model2.Delete(mTechnologyInstance.TechnologyInstance); err != nil { return err } return nil } func NewInstance() InstanceHandle { return func(session *session.Enterprise, local string) *Instance { return &Instance{Enterprise: session, local: local} } }