|
- package models
-
- import (
- "fmt"
- "strconv"
-
- "code.gitea.io/gitea/modules/timeutil"
- "xorm.io/builder"
- )
-
- const (
- TechHide = 1
- TechShow = 2
- TechMigrating = 3
- TechMigrateFailed = 4
- )
-
- const DefaultTechStatus = 2
-
- type TechConvergeBaseInfo struct {
- ID int64 `xorm:"pk autoincr"`
- ProjectNumber string `xorm:"UNIQUE NOT NULL"` //项目立项编号
- ProjectName string //科技项目名称
- Institution string //项目承担单位
- ApplyYear int //申报年度
- Province string //所属省(省市)
- Category string //单位性质
- Recommend string //推荐单位
- Owner string //项目负责人
- Phone string //负责人电话
- Email string //负责人邮箱
- Contact string //项目联系人
- ContactPhone string //联系人电话
- ContactEmail string //联系人邮箱
- ExecuteMonth int //执行周期(月)
- ExecuteStartYear int //执行开始年份
- ExecuteEndYear int //执行结束年份
- ExecutePeriod string //执行期限
- Type string //项目类型
- StartUp string //启动会时间
- NumberTopic int
- Topic1 string
- Topic2 string
- Topic3 string
- Topic4 string
- Topic5 string
- Topic6 string
- Topic7 string
- AllInstitution string `xorm:"TEXT"`
- CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
- UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
- }
-
- func (t *TechConvergeBaseInfo) Brief() *TechConvergeBrief {
- return &TechConvergeBrief{
- ProjectNumber: t.ProjectNumber,
- ProjectName: t.ProjectName,
- Institution: t.Institution,
- AllInstitution: t.AllInstitution,
- }
- }
-
- type RepoConvergeInfo struct {
- ID int64 `xorm:"pk autoincr"`
- RepoID int64
- Url string
- BaseInfoID int64
- Institution string
- UID int64
- Status int
- CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
- UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
- User *User `xorm:"-"`
- Repo *Repository `xorm:"-"`
- BaseInfo *TechConvergeBaseInfo `xorm:"-"`
- }
-
- func (r *RepoConvergeInfo) InsertOrUpdate() error {
- if r.ID != 0 {
- _, err := x.ID(r.ID).Update(r)
- return err
- } else {
- _, err := x.InsertOne(r)
- return err
- }
- }
-
- func GetTechConvergeBaseInfoByProjectNumber(projectNumber string) (*TechConvergeBaseInfo, error) {
- tb := &TechConvergeBaseInfo{ProjectNumber: projectNumber}
- return getTechConvergeBaseInfo(tb)
- }
- func (baseInfo *TechConvergeBaseInfo) InsertOrUpdate() error {
- if baseInfo.ID != 0 {
- _, err := x.ID(baseInfo.ID).Update(baseInfo)
- return err
- } else {
- _, err := x.InsertOne(baseInfo)
- return err
- }
- }
-
- type ErrTechConvergeBaseInfoNotExist struct {
- ID string
- }
-
- func (err ErrTechConvergeBaseInfoNotExist) Error() string {
- return fmt.Sprintf("TechConvergeBaseInfo does not exist [id: %s]", err.ID)
- }
-
- func IsErrTechConvergeBaseInfoNotExist(err error) bool {
- _, ok := err.(ErrTechConvergeBaseInfoNotExist)
- return ok
- }
-
- func getTechConvergeBaseInfo(tb *TechConvergeBaseInfo) (*TechConvergeBaseInfo, error) {
- has, err := x.Get(tb)
- if err != nil {
- return nil, err
- } else if !has {
- if tb.ProjectNumber != "" {
- return nil, ErrTechConvergeBaseInfoNotExist{tb.ProjectNumber}
- } else {
- return nil, ErrTechConvergeBaseInfoNotExist{strconv.FormatInt(tb.ID, 10)}
- }
- }
- return tb, nil
- }
-
- type TechConvergeBrief struct {
- ProjectNumber string `json:"no"` //项目立项编号
- ProjectName string `json:"name"` //科技项目名称
- Institution string `json:"institution"` //项目承担单位
- AllInstitution string `json:"all_institution"`
- }
-
- type FindTechOpt struct {
- TechNo string
- ProjectName string
- Institution string
- }
-
- func FindTech(opt FindTechOpt) ([]*TechConvergeBaseInfo, error) {
- var cond builder.Cond
- if opt.TechNo != "" {
- cond = cond.And(builder.Like{"project_number", opt.TechNo})
- }
- if opt.ProjectName != "" {
- cond = cond.And(builder.Like{"project_name", opt.ProjectName})
- }
- if opt.Institution != "" {
- cond = cond.And(builder.Like{"institution", opt.Institution}.Or(builder.Like{"all_institution", opt.Institution}))
- }
-
- r := make([]*TechConvergeBaseInfo, 0)
- err := x.Where(cond).OrderBy("updated_unix desc").Find(&r)
- if err != nil {
- return nil, err
- }
- return r, nil
- }
-
- func GetTechByTechNo(techNo string) (*TechConvergeBaseInfo, error) {
- var tech *TechConvergeBaseInfo
- has, err := x.Where("project_number = ?", techNo).Get(&tech)
- if err != nil {
- return nil, err
- } else if !has {
- return nil, ErrTechConvergeBaseInfoNotExist{}
- }
- return tech, nil
-
- }
|