|
- package models
-
- import (
- "strconv"
- "strings"
- "time"
-
- "code.gitea.io/gitea/modules/timeutil"
- "xorm.io/builder"
- )
-
- const (
- TechHide = 1
- TechShow = 2
- TechMigrating = 3
- TechMigrateFailed = 4
- )
-
- const DefaultTechApprovedStatus = TechShow
-
- 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,
- }
- }
- func (t *TechConvergeBaseInfo) IsValidInstitution(institution string) bool {
- if t.AllInstitution == "" && t.Institution == "" {
- return false
- }
-
- allInstitution := make([]string, 0)
- if t.AllInstitution != "" {
- allInstitution = strings.Split(t.AllInstitution, ",")
- }
- if t.Institution != "" {
- allInstitution = append(allInstitution, t.Institution)
- }
-
- newInstitution := strings.Split(institution, ",")
- total := len(newInstitution)
- matched := 0
- for _, n := range newInstitution {
- for _, s := range allInstitution {
- if s == n {
- matched++
- break
- }
- }
- }
- if matched == total {
- return true
- }
- return false
- }
-
- 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 "tech.tech_not_exist"
- }
-
- 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
- }
-
- func GetProjectNames() []string {
- var names []string
- x.Table("tech_converge_base_info").Distinct("project_name").Find(&names)
- return names
- }
- func GetRepoIds() []int64 {
- var ids []int64
- x.Table("repo_converge_info").Cols("repo_id").Find(&ids)
- return ids
- }
-
- func GetTechRepoTopics(limit int) []string {
-
- repoIds := GetRepoIds()
- if len(repoIds) == 0 {
- return []string{}
- }
-
- //select name, repo_count from topic a, repo_topic b
- //where a.id=b.topic_id and repo_id in (1,3) order by repo_count desc
- incondition := "repo_id in ("
-
- const MaxINItems = 1000
- for i := 0; i < len(repoIds); i++ {
- if i == len(repoIds)-1 {
- incondition += strconv.FormatInt(repoIds[i], 10)
- } else if (i+1)%MaxINItems == 0 {
- incondition += strconv.FormatInt(repoIds[i], 10) + ") or repo_id in ("
- } else {
- incondition += strconv.FormatInt(repoIds[i], 10) + ","
- }
-
- }
- incondition += ")"
-
- sql := "select name, repo_count from topic a, repo_topic b where a.id=b.topic_id and (" + incondition + ") order by repo_count desc"
- if limit > 0 {
- sql += "limit " + strconv.Itoa(limit)
- }
-
- result, err := x.QueryString(sql)
- if err != nil {
- return []string{}
- }
- var topics []string
- for _, record := range result {
- topics = append(topics, record["name"])
- }
- return topics
-
- }
-
- func GetProjectTypes() []string {
-
- sql := "SELECT COUNT(id) AS theCount, type from tech_converge_base_info GROUP BY type ORDER BY theCount DESC"
- result, err := x.QueryString(sql)
- if err != nil {
- return []string{}
- }
- var projectTypes []string
- for _, record := range result {
- projectTypes = append(projectTypes, record["type"])
- }
- return projectTypes
-
- }
- func GetApplyExecuteYears() ([]int, []int) {
- apply, executeStart, executeEnd := GetYearInfos()
- applyEnd := time.Now().Year()
-
- var applyArray []int
- var executeArray []int
-
- for i := apply; i <= applyEnd; i++ {
- applyArray = append(applyArray, i)
-
- }
- for i := executeStart; i <= executeEnd; i++ {
- executeArray = append(executeArray, i)
-
- }
- return applyArray, executeArray
- }
- func GetYearInfos() (int, int, int) {
-
- sql := "select min(apply_year) as apply_year,min(CASE WHEN execute_start_year != 0 THEN execute_start_year END) as execute_start_year,max(execute_end_year) as execute_end_year from tech_converge_base_info"
- result, err := x.QueryString(sql)
- if err != nil {
- return 2018, 2019, 2024
- }
-
- for _, record := range result {
- apply, _ := strconv.Atoi(record["apply_year"])
- executeStart, _ := strconv.Atoi(record["execute_start_year"])
- executeEnd, _ := strconv.Atoi(record["execute_end_year"])
- return apply, executeStart, executeEnd
- }
- return 2018, 2019, 2024
- }
-
- func GetAllInstitutions() []string {
- var names []string
- x.Table("tech_converge_base_info").Cols("all_institution").Find(&names)
- var allNames []string
- for _, name := range names {
- singleNames := strings.Split(name, ",")
- for _, singleName := range singleNames {
- if singleName != "" {
- if !contains(allNames, singleName) {
- allNames = append(allNames, singleName)
- }
- }
- }
-
- }
- return allNames
- }
-
- func contains(s []string, e string) bool {
- for _, a := range s {
- if a == e {
- return true
- }
- }
- return false
- }
-
- 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.NewCond()
- 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
-
- }
-
- type GetRepoConvergeOpts struct {
- RepoId int64
- BaseInfoId int64
- Status []int
- }
-
- func GetRepoConverge(opts GetRepoConvergeOpts) ([]*RepoConvergeInfo, error) {
- r := make([]*RepoConvergeInfo, 0)
- cond := builder.NewCond()
- if opts.RepoId > 0 {
- cond = cond.And(builder.Eq{"repo_id": opts.RepoId})
- }
- if opts.BaseInfoId > 0 {
- cond = cond.And(builder.Eq{"base_info_id": opts.BaseInfoId})
- }
- if len(opts.Status) > 0 {
- cond = cond.And(builder.In("status", opts.Status))
- }
- err := x.Where(cond).Find(&r)
- if err != nil {
- return nil, err
- }
- return r, nil
-
- }
-
- func UpdateRepoConvergeStatus(id int64, status int) (int64, error) {
- return x.ID(id).Update(&RepoConvergeInfo{
- Status: status,
- })
- }
|