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.go 5.7 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package tech
  2. import (
  3. "code.gitea.io/gitea/routers/response"
  4. techService "code.gitea.io/gitea/services/tech"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "github.com/360EntSecGroup-Skylar/excelize/v2"
  12. )
  13. func GetFilterInfo(ctx *context.APIContext) {
  14. filterType := ctx.QueryInt("type")
  15. limit := ctx.QueryInt("limit")
  16. if filterType == 0 {
  17. projectTypes := models.GetProjectTypes()
  18. allInstitutions := models.GetAllInstitutions()
  19. applyYears, executeYears := models.GetApplyExecuteYears()
  20. ctx.JSON(http.StatusOK, map[string]interface{}{
  21. "type_name": projectTypes,
  22. "institution_name": allInstitutions,
  23. "execute_year": executeYears,
  24. "apply_year": applyYears,
  25. })
  26. return
  27. }
  28. if filterType == 1 {
  29. projectNames := models.GetProjectNames()
  30. topics := models.GetTechRepoTopics(limit)
  31. allInstitutions := models.GetAllInstitutions()
  32. ctx.JSON(http.StatusOK, map[string]interface{}{
  33. "topic": topics,
  34. "institution_name": allInstitutions,
  35. "project_name": projectNames,
  36. })
  37. return
  38. }
  39. ctx.Error(http.StatusBadRequest, "parameter error", "")
  40. }
  41. func SearchRepoInfo(ctx *context.APIContext) {
  42. }
  43. func SearchTechProjectInfo(ctx *context.APIContext) {
  44. }
  45. func ImportBasicInfo(ctx *context.APIContext) {
  46. file, _, err := ctx.GetFile("file")
  47. if err != nil {
  48. log.Error("get file err", err)
  49. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.get_file_fail")))
  50. return
  51. }
  52. defer file.Close()
  53. f, err := excelize.OpenReader(file)
  54. if err != nil {
  55. log.Error("open file err", err)
  56. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.content_type_unsupported")))
  57. return
  58. }
  59. rows, err := f.GetRows(f.GetSheetName(1))
  60. if err != nil {
  61. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.content_type_unsupported")))
  62. return
  63. }
  64. for i, row := range rows {
  65. if i != 0 {
  66. baseInfo := &models.TechConvergeBaseInfo{}
  67. baseInfo.ProjectNumber = strings.TrimSpace(row[2])
  68. tbInDB, err := models.GetTechConvergeBaseInfoByProjectNumber(baseInfo.ProjectNumber)
  69. if err != nil && !models.IsErrTechConvergeBaseInfoNotExist(err) {
  70. log.Error("query err ", i, err)
  71. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.sql_err")))
  72. return
  73. }
  74. if tbInDB != nil {
  75. baseInfo = tbInDB
  76. }
  77. baseInfo.ApplyYear, err = strconv.Atoi(strings.TrimSpace(row[1]))
  78. if err != nil {
  79. log.Warn("base info apply year parse err ", i)
  80. }
  81. baseInfo.ProjectName = strings.TrimSpace(row[3])
  82. baseInfo.Type = strings.TrimSpace(row[16])
  83. baseInfo.Owner = strings.TrimSpace(row[8])
  84. baseInfo.Email = strings.TrimSpace(row[10])
  85. baseInfo.Phone = strings.TrimSpace(row[9])
  86. baseInfo.Recommend = strings.TrimSpace(row[7])
  87. baseInfo.Institution = strings.TrimSpace(row[4])
  88. baseInfo.Category = strings.TrimSpace(row[6])
  89. baseInfo.Province = strings.TrimSpace(row[5])
  90. baseInfo.Contact = strings.TrimSpace(row[11])
  91. baseInfo.ContactPhone = strings.TrimSpace(row[12])
  92. baseInfo.ContactEmail = strings.TrimSpace(row[13])
  93. baseInfo.ExecuteMonth, _ = strconv.Atoi(strings.TrimSpace(row[14]))
  94. baseInfo.ExecutePeriod = strings.TrimSpace(row[15])
  95. baseInfo.ExecuteStartYear, baseInfo.ExecuteEndYear = getBeginEndYear(baseInfo.ExecutePeriod)
  96. baseInfo.StartUp = strings.TrimSpace(row[17])
  97. baseInfo.NumberTopic, _ = strconv.Atoi(strings.TrimSpace(row[30]))
  98. baseInfo.Topic1 = strings.TrimSpace(row[31])
  99. baseInfo.Topic2 = strings.TrimSpace(row[32])
  100. baseInfo.Topic3 = strings.TrimSpace(row[33])
  101. baseInfo.Topic4 = strings.TrimSpace(row[34])
  102. baseInfo.Topic5 = strings.TrimSpace(row[35])
  103. allIns := replaceNewLineWithComma(strings.TrimSpace(row[36]))
  104. if allIns != "" {
  105. allInsArray := strings.Split(allIns, ",")
  106. var allInsValid []string
  107. for _, ins := range allInsArray {
  108. if ins != "" {
  109. allInsValid = append(allInsValid, ins)
  110. }
  111. }
  112. baseInfo.AllInstitution = strings.Join(allInsValid, ",")
  113. }
  114. if baseInfo.AllInstitution == "" {
  115. baseInfo.AllInstitution = baseInfo.Institution
  116. }
  117. err = baseInfo.InsertOrUpdate()
  118. if err != nil {
  119. log.Error("update err", i, err)
  120. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.sql_err")))
  121. return
  122. }
  123. }
  124. }
  125. ctx.JSON(http.StatusOK, models.BaseOKMessageApi)
  126. }
  127. func replaceNewLineWithComma(s string) string {
  128. const replacement = ","
  129. var replacer = strings.NewReplacer(
  130. "\r\n", replacement,
  131. "\r", replacement,
  132. "\n", replacement,
  133. "\v", replacement,
  134. "\f", replacement,
  135. "\u0085", replacement,
  136. "\u2028", replacement,
  137. "\u2029", replacement,
  138. )
  139. return replacer.Replace(s)
  140. }
  141. /**
  142. input:2019.12-2023.12 output: 2019,2023
  143. */
  144. func getBeginEndYear(executePeriod string) (int, int) {
  145. period := strings.Split(executePeriod, "-")
  146. if len(period) == 2 {
  147. start := strings.Split(period[0], ".")
  148. end := strings.Split(period[1], ".")
  149. if len(start) == 2 && len(end) == 2 {
  150. startYear, err := strconv.Atoi(start[0])
  151. endYear, err1 := strconv.Atoi(end[0])
  152. if err == nil && err1 == nil {
  153. return startYear, endYear
  154. }
  155. }
  156. }
  157. return 0, 0
  158. }
  159. func FindTech(ctx *context.APIContext) {
  160. techNo := ctx.Query("no")
  161. name := ctx.Query("name")
  162. institution := ctx.Query("institution")
  163. r, err := techService.FindTech(models.FindTechOpt{TechNo: techNo, ProjectName: name, Institution: institution})
  164. if err != nil {
  165. ctx.JSON(http.StatusOK, response.OuterResponseError(err))
  166. return
  167. }
  168. ctx.JSON(http.StatusOK, response.OuterSuccessWithData(r))
  169. }