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
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. RelAvatarLink string `json:"rel_avatar_link"`
  286. UpdatedUnix timeutil.TimeStamp `json:"updated_unix"`
  287. }
  288. type TechRepoInfo struct {
  289. ID int64 `json:"id"`
  290. ProjectName string `json:"project_name"`
  291. Institution string `json:"institution"`
  292. Type string `json:"type"`
  293. ApplyYear int `json:"apply_year"`
  294. ExecutePeriod string `json:"execute_period"`
  295. AllInstitution string `json:"all_institution"`
  296. RepoCount int `json:"repo_numer"`
  297. Repos []*RepoWithInstitution
  298. }
  299. func GetAvailableRepoConvergeInfo(opt *SearchRepoOpt) ([]*RepoConvergeInfo, error) {
  300. repos := make([]*RepoConvergeInfo, 0)
  301. err := x.Table("repo_converge_info").Where(buildRepoFilterCond(opt)).Find(&repos)
  302. return repos, err
  303. }
  304. func buildRepoFilterCond(opt *SearchRepoOpt) string {
  305. sql := ""
  306. if opt.Institution != "" {
  307. sql += getPrefixWithoutWhere(sql) + " (institution like '%" + opt.Institution + ",%'" + " or institution like '%," + opt.Institution + "%'" + " or institution = '" + opt.Institution + "')"
  308. }
  309. if opt.ProjectName != "" {
  310. baseInfoIds := GetIdByProjectName(opt.ProjectName)
  311. if len(baseInfoIds) > 0 {
  312. sql += getPrefixWithoutWhere(sql) + " id in (" + strings.Join(baseInfoIds, ",") + ")"
  313. }
  314. }
  315. return sql
  316. }
  317. func SearchTechRepoInfo(opt *SearchTechOpt) ([]*TechRepoInfo, int64, error) {
  318. sql := `select a.*,COALESCE(b.count,0) as repo_count, COALESCE(b.max,0) as max from tech_converge_base_info a left join
  319. (select base_info_id,count(id),max(updated_unix) from repo_converge_info where status=` + strconv.Itoa(TechShow) + ` GROUP BY base_info_id ) b
  320. on a.id=b.base_info_id`
  321. totalSql := "select count(*) from (" + sql + ") c" + buildTechFilterCond(opt)
  322. total, err := x.SQL(totalSql).Count(new(TechConvergeBaseInfo))
  323. resultList := make([]*TechRepoInfo, 0)
  324. if err != nil {
  325. return resultList, total, err
  326. }
  327. resultSql := "select id,project_name, institution,type,apply_year,execute_period,all_institution,repo_count from (" +
  328. sql + ") c " + buildTechFilterCond(opt) + opt.OrderBy + " offset " + strconv.Itoa((opt.Page-1)*opt.PageSize) + " limit " + strconv.Itoa(opt.PageSize)
  329. resultMap, err := x.QueryInterface(resultSql)
  330. if err == nil {
  331. for _, record := range resultMap {
  332. resultList = append(resultList, &TechRepoInfo{
  333. ID: record["id"].(int64),
  334. ProjectName: record["project_name"].(string),
  335. Institution: record["institution"].(string),
  336. Type: record["type"].(string),
  337. ApplyYear: int(record["apply_year"].(int64)),
  338. ExecutePeriod: record["execute_period"].(string),
  339. AllInstitution: record["all_institution"].(string),
  340. RepoCount: int(record["repo_count"].(int64)),
  341. })
  342. }
  343. }
  344. loadRepoInfoForTech(resultList)
  345. return resultList, total, err
  346. }
  347. func buildTechFilterCond(opt *SearchTechOpt) string {
  348. sql := ""
  349. if opt.Q != "" {
  350. sql += getWherePrefix(sql) + " project_name like '%" + opt.Q + "%'"
  351. }
  352. if opt.ProjectType != "" {
  353. sql += getWherePrefix(sql) + " type ='" + opt.ProjectType + "'"
  354. }
  355. if opt.ApplyYear != 0 {
  356. sql += getWherePrefix(sql) + " apply_year =" + strconv.Itoa(opt.ApplyYear)
  357. }
  358. if opt.Institution != "" {
  359. sql += getWherePrefix(sql) + " (all_institution like '%" + opt.Institution + ",%'" + " or all_institution like '%," + opt.Institution + "%'" + " or all_institution = '" + opt.Institution + "')"
  360. }
  361. if opt.ExecuteYear != 0 {
  362. sql += getWherePrefix(sql) + " execute_start_year <=" + strconv.Itoa(opt.ExecuteYear) + " and execute_end_year >=" + strconv.Itoa(opt.ExecuteYear)
  363. }
  364. return sql
  365. }
  366. func getWherePrefix(sql string) string {
  367. if sql == "" {
  368. return " where "
  369. }
  370. return " and "
  371. }
  372. func getPrefixWithoutWhere(sql string) string {
  373. if sql == "" {
  374. return ""
  375. }
  376. return " and "
  377. }
  378. func loadRepoInfoForTech(list []*TechRepoInfo) {
  379. for _, techRepo := range list {
  380. techRepo.Repos = []*RepoWithInstitution{}
  381. if techRepo.RepoCount > 0 {
  382. var repoIds []int64
  383. x.Table("repo_converge_info").Cols("repo_id").Where("base_info_id=?", techRepo.ID).Limit(2).Desc("updated_unix").Find(&repoIds)
  384. resultMap, err := GetRepositoriesMapByIDs(repoIds)
  385. if err == nil {
  386. for _, repoId := range repoIds {
  387. repo, ok := resultMap[repoId]
  388. if ok {
  389. techRepo.Repos = append(techRepo.Repos, &RepoWithInstitution{
  390. ID: repo.ID,
  391. Institution: techRepo.Institution,
  392. OwnerID: repo.OwnerID,
  393. OwnerName: repo.OwnerName,
  394. Name: repo.Name,
  395. Alias: repo.Alias,
  396. Topics: repo.Topics,
  397. Description: repo.Description,
  398. RelAvatarLink: repo.RelAvatarLink(),
  399. UpdatedUnix: repo.UpdatedUnix,
  400. })
  401. }
  402. }
  403. }
  404. }
  405. }
  406. }
  407. type TechConvergeBrief struct {
  408. ProjectNumber string `json:"no"` //项目立项编号
  409. ProjectName string `json:"name"` //科技项目名称
  410. Institution string `json:"institution"` //项目承担单位
  411. AllInstitution string `json:"all_institution"`
  412. }
  413. type FindTechOpt struct {
  414. TechNo string
  415. ProjectName string
  416. Institution string
  417. }
  418. func FindTech(opt FindTechOpt) ([]*TechConvergeBaseInfo, error) {
  419. var cond = builder.NewCond()
  420. if opt.TechNo != "" {
  421. cond = cond.And(builder.Like{"project_number", opt.TechNo})
  422. }
  423. if opt.ProjectName != "" {
  424. cond = cond.And(builder.Like{"project_name", opt.ProjectName})
  425. }
  426. if opt.Institution != "" {
  427. cond = cond.And(builder.Like{"institution", opt.Institution}.Or(builder.Like{"all_institution", opt.Institution}))
  428. }
  429. r := make([]*TechConvergeBaseInfo, 0)
  430. err := x.Where(cond).OrderBy("updated_unix desc").Find(&r)
  431. if err != nil {
  432. return nil, err
  433. }
  434. return r, nil
  435. }
  436. func GetTechByTechNo(techNo string) (*TechConvergeBaseInfo, error) {
  437. var tech = &TechConvergeBaseInfo{}
  438. has, err := x.Where("project_number = ?", techNo).Get(tech)
  439. if err != nil {
  440. return nil, err
  441. } else if !has {
  442. return nil, ErrTechConvergeBaseInfoNotExist{}
  443. }
  444. return tech, nil
  445. }
  446. type GetRepoConvergeOpts struct {
  447. RepoId int64
  448. BaseInfoId int64
  449. Status []int
  450. }
  451. func GetRepoConverge(opts GetRepoConvergeOpts) ([]*RepoConvergeInfo, error) {
  452. r := make([]*RepoConvergeInfo, 0)
  453. cond := builder.NewCond()
  454. if opts.RepoId > 0 {
  455. cond = cond.And(builder.Eq{"repo_id": opts.RepoId})
  456. }
  457. if opts.BaseInfoId > 0 {
  458. cond = cond.And(builder.Eq{"base_info_id": opts.BaseInfoId})
  459. }
  460. if len(opts.Status) > 0 {
  461. cond = cond.And(builder.In("status", opts.Status))
  462. }
  463. err := x.Where(cond).Find(&r)
  464. if err != nil {
  465. return nil, err
  466. }
  467. return r, nil
  468. }
  469. func UpdateRepoConvergeStatus(id int64, status int) (int64, error) {
  470. return x.ID(id).Update(&RepoConvergeInfo{
  471. Status: status,
  472. })
  473. }