package technology import ( "SciencesServer/app/api/enterprise/model" "SciencesServer/app/api/manage/controller" model2 "SciencesServer/app/common/model" "SciencesServer/app/session" "SciencesServer/utils" "errors" "time" ) // Demand 技术需求管理 type Demand struct { *session.Enterprise local string } type DemandHandle func(session *session.Enterprise, local string) *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.TechnologyDemandStatusForRefuse && mTechnologyDemand.Status != model2.TechnologyDemandStatusForDraft { 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.TechnologyDemandStatusForExamining return model2.Updates(mTechnologyDemand.TechnologyDemand, mTechnologyDemand.TechnologyDemand) } mTechnologyDemand.UID = c.UID mTechnologyDemand.Local.Local = c.local if params.IsSubmit > 0 { mTechnologyDemand.Status = model2.TechnologyDemandStatusForExamining } 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 } func NewDemand() DemandHandle { return func(session *session.Enterprise, local string) *Demand { return &Demand{Enterprise: session, local: local} } }