You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tech_converge_info.go 15 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. package models
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. "code.gitea.io/gitea/modules/timeutil"
  7. "xorm.io/builder"
  8. )
  9. const (
  10. TechShow = 1
  11. TechHide = 2
  12. TechMigrating = 3
  13. TechMigrateFailed = 4
  14. TechNotExist = 5
  15. )
  16. const DefaultTechApprovedStatus = TechShow
  17. type TechConvergeBaseInfo struct {
  18. ID int64 `xorm:"pk autoincr"`
  19. ProjectNumber string `xorm:"UNIQUE NOT NULL"` //项目立项编号
  20. ProjectName string //科技项目名称
  21. Institution string //项目承担单位
  22. ApplyYear int //申报年度
  23. Province string //所属省(省市)
  24. Category string //单位性质
  25. Recommend string //推荐单位
  26. Owner string //项目负责人
  27. Phone string //负责人电话
  28. Email string //负责人邮箱
  29. Contact string //项目联系人
  30. ContactPhone string //联系人电话
  31. ContactEmail string //联系人邮箱
  32. ExecuteMonth int //执行周期(月)
  33. ExecuteStartYear int //执行开始年份
  34. ExecuteEndYear int //执行结束年份
  35. ExecutePeriod string //执行期限
  36. Type string //项目类型
  37. StartUp string //启动会时间
  38. NumberTopic int
  39. Topic1 string
  40. Topic2 string
  41. Topic3 string
  42. Topic4 string
  43. Topic5 string
  44. Topic6 string
  45. Topic7 string
  46. AllInstitution string `xorm:"TEXT"`
  47. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  48. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  49. }
  50. func (t *TechConvergeBaseInfo) Brief() *TechConvergeBrief {
  51. return &TechConvergeBrief{
  52. ProjectNumber: t.ProjectNumber,
  53. ProjectName: t.ProjectName,
  54. Institution: t.Institution,
  55. AllInstitution: t.AllInstitution,
  56. }
  57. }
  58. func (t *TechConvergeBaseInfo) IsValidInstitution(institution string) bool {
  59. if t.AllInstitution == "" && t.Institution == "" {
  60. return false
  61. }
  62. allInstitution := make([]string, 0)
  63. if t.AllInstitution != "" {
  64. allInstitution = strings.Split(t.AllInstitution, ",")
  65. }
  66. if t.Institution != "" {
  67. allInstitution = append(allInstitution, t.Institution)
  68. }
  69. newInstitution := strings.Split(institution, ",")
  70. total := len(newInstitution)
  71. matched := 0
  72. for _, n := range newInstitution {
  73. for _, s := range allInstitution {
  74. if s == n {
  75. matched++
  76. break
  77. }
  78. }
  79. }
  80. if matched == total {
  81. return true
  82. }
  83. return false
  84. }
  85. type RepoConvergeInfo struct {
  86. ID int64 `xorm:"pk autoincr"`
  87. RepoID int64
  88. Url string
  89. BaseInfoID int64
  90. Institution string
  91. UID int64
  92. Status int
  93. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  94. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  95. User *User `xorm:"-"`
  96. Repo *Repository `xorm:"-"`
  97. BaseInfo *TechConvergeBaseInfo `xorm:"-"`
  98. }
  99. func (r *RepoConvergeInfo) InsertOrUpdate() error {
  100. if r.ID != 0 {
  101. _, err := x.ID(r.ID).Update(r)
  102. return err
  103. } else {
  104. _, err := x.InsertOne(r)
  105. return err
  106. }
  107. }
  108. func GetTechConvergeBaseInfoByProjectNumber(projectNumber string) (*TechConvergeBaseInfo, error) {
  109. tb := &TechConvergeBaseInfo{ProjectNumber: projectNumber}
  110. return getTechConvergeBaseInfo(tb)
  111. }
  112. func (baseInfo *TechConvergeBaseInfo) InsertOrUpdate() error {
  113. if baseInfo.ID != 0 {
  114. _, err := x.ID(baseInfo.ID).Update(baseInfo)
  115. return err
  116. } else {
  117. _, err := x.InsertOne(baseInfo)
  118. return err
  119. }
  120. }
  121. type ErrTechConvergeBaseInfoNotExist struct {
  122. ID string
  123. }
  124. func (err ErrTechConvergeBaseInfoNotExist) Error() string {
  125. return "tech.tech_not_exist"
  126. }
  127. func IsErrTechConvergeBaseInfoNotExist(err error) bool {
  128. _, ok := err.(ErrTechConvergeBaseInfoNotExist)
  129. return ok
  130. }
  131. func getTechConvergeBaseInfo(tb *TechConvergeBaseInfo) (*TechConvergeBaseInfo, error) {
  132. has, err := x.Get(tb)
  133. if err != nil {
  134. return nil, err
  135. } else if !has {
  136. if tb.ProjectNumber != "" {
  137. return nil, ErrTechConvergeBaseInfoNotExist{tb.ProjectNumber}
  138. } else {
  139. return nil, ErrTechConvergeBaseInfoNotExist{strconv.FormatInt(tb.ID, 10)}
  140. }
  141. }
  142. return tb, nil
  143. }
  144. func GetProjectNames() []string {
  145. var names []string
  146. x.Table("tech_converge_base_info").Distinct("project_name").Find(&names)
  147. return names
  148. }
  149. func GetIdByProjectName(name string) []string {
  150. var ids []int64
  151. x.Table("tech_converge_base_info").Cols("id").Where("project_name=?", name).Find(&ids)
  152. idStrs := make([]string, 0, len(ids))
  153. for _, id := range ids {
  154. idStrs = append(idStrs, strconv.FormatInt(id, 10))
  155. }
  156. return idStrs
  157. }
  158. func GetSummitRepoIds() []int64 {
  159. var ids []int64
  160. x.Table("repo_converge_info").Cols("repo_id").Find(&ids)
  161. return ids
  162. }
  163. func GetTechRepoTopics(limit int) []string {
  164. repoIds := GetSummitRepoIds()
  165. if len(repoIds) == 0 {
  166. return []string{}
  167. }
  168. //select name, repo_count from topic a, repo_topic b
  169. //where a.id=b.topic_id and repo_id in (1,3) order by repo_count desc
  170. inCondition := "repo_id in ("
  171. const MaxINItems = 1000
  172. for i := 0; i < len(repoIds); i++ {
  173. if i == len(repoIds)-1 {
  174. inCondition += strconv.FormatInt(repoIds[i], 10)
  175. } else if (i+1)%MaxINItems == 0 {
  176. inCondition += strconv.FormatInt(repoIds[i], 10) + ") or repo_id in ("
  177. } else {
  178. inCondition += strconv.FormatInt(repoIds[i], 10) + ","
  179. }
  180. }
  181. inCondition += ")"
  182. sql := "select name, repo_count from topic a, repo_topic b where a.id=b.topic_id and (" + inCondition + ") order by repo_count desc"
  183. if limit > 0 {
  184. sql += "limit " + strconv.Itoa(limit)
  185. }
  186. result, err := x.QueryString(sql)
  187. if err != nil {
  188. return []string{}
  189. }
  190. var topics []string
  191. for _, record := range result {
  192. topics = append(topics, record["name"])
  193. }
  194. return topics
  195. }
  196. func GetProjectTypes() []string {
  197. sql := "SELECT COUNT(id) AS theCount, type from tech_converge_base_info GROUP BY type ORDER BY theCount DESC"
  198. result, err := x.QueryString(sql)
  199. if err != nil {
  200. return []string{}
  201. }
  202. var projectTypes []string
  203. for _, record := range result {
  204. projectTypes = append(projectTypes, record["type"])
  205. }
  206. return projectTypes
  207. }
  208. func GetApplyExecuteYears() ([]int, []int) {
  209. apply, executeStart, executeEnd := GetYearInfos()
  210. applyEnd := time.Now().Year()
  211. var applyArray []int
  212. var executeArray []int
  213. for i := apply; i <= applyEnd; i++ {
  214. applyArray = append(applyArray, i)
  215. }
  216. for i := executeStart; i <= executeEnd; i++ {
  217. executeArray = append(executeArray, i)
  218. }
  219. return applyArray, executeArray
  220. }
  221. func GetYearInfos() (int, int, int) {
  222. 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"
  223. result, err := x.QueryString(sql)
  224. if err != nil {
  225. return 2018, 2019, 2024
  226. }
  227. for _, record := range result {
  228. apply, _ := strconv.Atoi(record["apply_year"])
  229. executeStart, _ := strconv.Atoi(record["execute_start_year"])
  230. executeEnd, _ := strconv.Atoi(record["execute_end_year"])
  231. return apply, executeStart, executeEnd
  232. }
  233. return 2018, 2019, 2024
  234. }
  235. func GetAllInstitutions() []string {
  236. var names []string
  237. x.Table("tech_converge_base_info").Cols("all_institution").Find(&names)
  238. var allNames []string
  239. for _, name := range names {
  240. singleNames := strings.Split(name, ",")
  241. for _, singleName := range singleNames {
  242. if singleName != "" {
  243. if !contains(allNames, singleName) {
  244. allNames = append(allNames, singleName)
  245. }
  246. }
  247. }
  248. }
  249. return allNames
  250. }
  251. func contains(s []string, e string) bool {
  252. for _, a := range s {
  253. if a == e {
  254. return true
  255. }
  256. }
  257. return false
  258. }
  259. type SearchTechOpt struct {
  260. Q string //科技项目名称
  261. ProjectType string
  262. Institution string
  263. ApplyYear int
  264. ExecuteYear int
  265. OrderBy string
  266. ListOptions
  267. }
  268. type SearchRepoOpt struct {
  269. Q string //项目名称,简介
  270. ProjectName string
  271. Topic string
  272. Institution string
  273. OrderBy string
  274. ListOptions
  275. }
  276. type RepoWithInstitution struct {
  277. ID int64 `json:"id"`
  278. OwnerID int64 `json:"owner_id"`
  279. OwnerName string `json:"owner_name"`
  280. Name string `json:"name"`
  281. Alias string `json:"alias"`
  282. Topics []string `json:"topics"`
  283. Description string `json:"description"`
  284. Institution string `json:"institution"`
  285. UpdatedUnix timeutil.TimeStamp `json:"updated_unix"`
  286. }
  287. type TechRepoInfo struct {
  288. ID int64 `json:"id"`
  289. ProjectName string `json:"project_name"`
  290. Institution string `json:"institution"`
  291. Type string `json:"type"`
  292. ApplyYear int `json:"apply_year"`
  293. ExecutePeriod string `json:"execute_period"`
  294. AllInstitution string `json:"all_institution"`
  295. RepoCount int `json:"repo_numer"`
  296. Repos []*RepoWithInstitution
  297. }
  298. func GetAvailableRepoConvergeInfo(opt *SearchRepoOpt) ([]*RepoConvergeInfo, error) {
  299. repos := make([]*RepoConvergeInfo, 0)
  300. err := x.Table("repo_converge_info").Where(buildRepoFilterCond(opt)).Find(&repos)
  301. return repos, err
  302. }
  303. func buildRepoFilterCond(opt *SearchRepoOpt) string {
  304. sql := ""
  305. if opt.Institution != "" {
  306. sql += getPrefixWithoutWhere(sql) + " (institution like '%" + opt.Institution + ",%'" + " or institution like '%," + opt.Institution + "%'" + " or institution = '" + opt.Institution + "')"
  307. }
  308. if opt.ProjectName != "" {
  309. baseInfoIds := GetIdByProjectName(opt.ProjectName)
  310. if len(baseInfoIds) > 0 {
  311. sql += getPrefixWithoutWhere(sql) + " id in (" + strings.Join(baseInfoIds, ",") + ")"
  312. }
  313. }
  314. return sql
  315. }
  316. func SearchTechRepoInfo(opt *SearchTechOpt) ([]*TechRepoInfo, int64, error) {
  317. sql := `select a.*,COALESCE(b.count,0) as repo_count, COALESCE(b.max,0) as max from tech_converge_base_info a left join
  318. (select base_info_id,count(id),max(updated_unix) from repo_converge_info where status=` + strconv.Itoa(TechShow) + ` GROUP BY base_info_id ) b
  319. on a.id=b.base_info_id`
  320. totalSql := "select count(*) from (" + sql + ") c" + buildTechFilterCond(opt)
  321. total, err := x.SQL(totalSql).Count(new(TechConvergeBaseInfo))
  322. resultList := make([]*TechRepoInfo, 0)
  323. if err != nil {
  324. return resultList, total, err
  325. }
  326. resultSql := "select id,project_name, institution,type,apply_year,execute_period,all_institution,repo_count from (" +
  327. sql + ") c " + buildTechFilterCond(opt) + opt.OrderBy + " offset " + strconv.Itoa((opt.Page-1)*opt.PageSize) + " limit " + strconv.Itoa(opt.PageSize)
  328. resultMap, err := x.QueryInterface(resultSql)
  329. if err == nil {
  330. for _, record := range resultMap {
  331. resultList = append(resultList, &TechRepoInfo{
  332. ID: record["id"].(int64),
  333. ProjectName: record["project_name"].(string),
  334. Institution: record["institution"].(string),
  335. Type: record["type"].(string),
  336. ApplyYear: int(record["apply_year"].(int64)),
  337. ExecutePeriod: record["execute_period"].(string),
  338. AllInstitution: record["all_institution"].(string),
  339. RepoCount: int(record["repo_count"].(int64)),
  340. })
  341. }
  342. }
  343. loadRepoInfoForTech(resultList)
  344. return resultList, total, err
  345. }
  346. func buildTechFilterCond(opt *SearchTechOpt) string {
  347. sql := ""
  348. if opt.Q != "" {
  349. sql += getWherePrefix(sql) + " project_name like '%" + opt.Q + "%'"
  350. }
  351. if opt.ProjectType != "" {
  352. sql += getWherePrefix(sql) + " type ='" + opt.ProjectType + "'"
  353. }
  354. if opt.ApplyYear != 0 {
  355. sql += getWherePrefix(sql) + " apply_year =" + strconv.Itoa(opt.ApplyYear)
  356. }
  357. if opt.Institution != "" {
  358. sql += getWherePrefix(sql) + " (all_institution like '%" + opt.Institution + ",%'" + " or all_institution like '%," + opt.Institution + "%'"
  359. }
  360. if opt.ExecuteYear != 0 {
  361. sql += getWherePrefix(sql) + " execute_start_year <=" + strconv.Itoa(opt.ExecuteYear) + " and execute_end_year >=" + strconv.Itoa(opt.ExecuteYear)
  362. }
  363. return sql
  364. }
  365. func getWherePrefix(sql string) string {
  366. if sql == "" {
  367. return " where "
  368. }
  369. return " and "
  370. }
  371. func getPrefixWithoutWhere(sql string) string {
  372. if sql == "" {
  373. return ""
  374. }
  375. return " and "
  376. }
  377. func loadRepoInfoForTech(list []*TechRepoInfo) {
  378. for _, techRepo := range list {
  379. techRepo.Repos = []*RepoWithInstitution{}
  380. if techRepo.RepoCount > 0 {
  381. var repoIds []int64
  382. x.Table("repo_converge_info").Cols("repo_id").Where("base_info_id=?", techRepo.ID).Desc("updated_unix").Find(&repoIds)
  383. resultMap, err := GetRepositoriesMapByIDs(repoIds)
  384. if err == nil {
  385. for _, repoId := range repoIds {
  386. repo, ok := resultMap[repoId]
  387. if ok {
  388. techRepo.Repos = append(techRepo.Repos, &RepoWithInstitution{
  389. ID: repo.ID,
  390. Institution: techRepo.Institution,
  391. OwnerID: repo.OwnerID,
  392. OwnerName: repo.OwnerName,
  393. Name: repo.Name,
  394. Alias: repo.Alias,
  395. Topics: repo.Topics,
  396. Description: repo.Description,
  397. UpdatedUnix: repo.UpdatedUnix,
  398. })
  399. }
  400. }
  401. }
  402. }
  403. }
  404. }
  405. type TechConvergeBrief struct {
  406. ProjectNumber string `json:"no"` //项目立项编号
  407. ProjectName string `json:"name"` //科技项目名称
  408. Institution string `json:"institution"` //项目承担单位
  409. AllInstitution string `json:"all_institution"`
  410. }
  411. type FindTechOpt struct {
  412. TechNo string
  413. ProjectName string
  414. Institution string
  415. }
  416. func FindTech(opt FindTechOpt) ([]*TechConvergeBaseInfo, error) {
  417. var cond = builder.NewCond()
  418. if opt.TechNo != "" {
  419. cond = cond.And(builder.Like{"project_number", opt.TechNo})
  420. }
  421. if opt.ProjectName != "" {
  422. cond = cond.And(builder.Like{"project_name", opt.ProjectName})
  423. }
  424. if opt.Institution != "" {
  425. cond = cond.And(builder.Like{"institution", opt.Institution}.Or(builder.Like{"all_institution", opt.Institution}))
  426. }
  427. r := make([]*TechConvergeBaseInfo, 0)
  428. err := x.Where(cond).OrderBy("updated_unix desc").Find(&r)
  429. if err != nil {
  430. return nil, err
  431. }
  432. return r, nil
  433. }
  434. func GetTechByTechNo(techNo string) (*TechConvergeBaseInfo, error) {
  435. var tech = &TechConvergeBaseInfo{}
  436. has, err := x.Where("project_number = ?", techNo).Get(tech)
  437. if err != nil {
  438. return nil, err
  439. } else if !has {
  440. return nil, ErrTechConvergeBaseInfoNotExist{}
  441. }
  442. return tech, nil
  443. }
  444. type GetRepoConvergeOpts struct {
  445. RepoId int64
  446. BaseInfoId int64
  447. Status []int
  448. }
  449. func GetRepoConverge(opts GetRepoConvergeOpts) ([]*RepoConvergeInfo, error) {
  450. r := make([]*RepoConvergeInfo, 0)
  451. cond := builder.NewCond()
  452. if opts.RepoId > 0 {
  453. cond = cond.And(builder.Eq{"repo_id": opts.RepoId})
  454. }
  455. if opts.BaseInfoId > 0 {
  456. cond = cond.And(builder.Eq{"base_info_id": opts.BaseInfoId})
  457. }
  458. if len(opts.Status) > 0 {
  459. cond = cond.And(builder.In("status", opts.Status))
  460. }
  461. err := x.Where(cond).Find(&r)
  462. if err != nil {
  463. return nil, err
  464. }
  465. return r, nil
  466. }
  467. func UpdateRepoConvergeStatus(id int64, status int) (int64, error) {
  468. return x.ID(id).Update(&RepoConvergeInfo{
  469. Status: status,
  470. })
  471. }