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 6.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. opts := models.SearchTechOpt{}
  45. opts.Q = ctx.Query("name")
  46. opts.ApplyYear = ctx.QueryInt("apply_year")
  47. opts.ExecuteYear = ctx.QueryInt("execute_year")
  48. opts.Institution = ctx.Query("institution_name")
  49. opts.ProjectType = ctx.Query("type_name")
  50. page := ctx.QueryInt("page")
  51. if page <= 0 {
  52. page = 1
  53. }
  54. opts.Page = page
  55. pageSize := ctx.QueryInt("pageSize")
  56. if pageSize <= 0 {
  57. pageSize = 15
  58. }
  59. opts.PageSize = pageSize
  60. orderBy := ctx.Query("orderBy")
  61. if orderBy == "" || orderBy == "count" {
  62. opts.OrderBy = "order by repo_count desc, max desc "
  63. } else {
  64. opts.OrderBy = "order by max desc, repo_count desc "
  65. }
  66. infos, total, err := models.SearchTechRepoInfo(opts)
  67. if err != nil {
  68. log.Error("search has error", err)
  69. ctx.JSON(http.StatusOK, map[string]interface {
  70. }{"total": 0, "data": []*models.TechRepoInfo{}})
  71. } else {
  72. ctx.JSON(http.StatusOK, map[string]interface {
  73. }{"total": total, "data": infos})
  74. }
  75. }
  76. func ImportBasicInfo(ctx *context.APIContext) {
  77. file, _, err := ctx.GetFile("file")
  78. if err != nil {
  79. log.Error("get file err", err)
  80. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.get_file_fail")))
  81. return
  82. }
  83. defer file.Close()
  84. f, err := excelize.OpenReader(file)
  85. if err != nil {
  86. log.Error("open file err", err)
  87. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.content_type_unsupported")))
  88. return
  89. }
  90. rows, err := f.GetRows(f.GetSheetName(1))
  91. if err != nil {
  92. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.content_type_unsupported")))
  93. return
  94. }
  95. for i, row := range rows {
  96. if i != 0 {
  97. baseInfo := &models.TechConvergeBaseInfo{}
  98. baseInfo.ProjectNumber = strings.TrimSpace(row[2])
  99. tbInDB, err := models.GetTechConvergeBaseInfoByProjectNumber(baseInfo.ProjectNumber)
  100. if err != nil && !models.IsErrTechConvergeBaseInfoNotExist(err) {
  101. log.Error("query err ", i, err)
  102. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.sql_err")))
  103. return
  104. }
  105. if tbInDB != nil {
  106. baseInfo = tbInDB
  107. }
  108. baseInfo.ApplyYear, err = strconv.Atoi(strings.TrimSpace(row[1]))
  109. if err != nil {
  110. log.Warn("base info apply year parse err ", i)
  111. }
  112. baseInfo.ProjectName = strings.TrimSpace(row[3])
  113. baseInfo.Type = strings.TrimSpace(row[16])
  114. baseInfo.Owner = strings.TrimSpace(row[8])
  115. baseInfo.Email = strings.TrimSpace(row[10])
  116. baseInfo.Phone = strings.TrimSpace(row[9])
  117. baseInfo.Recommend = strings.TrimSpace(row[7])
  118. baseInfo.Institution = strings.TrimSpace(row[4])
  119. baseInfo.Category = strings.TrimSpace(row[6])
  120. baseInfo.Province = strings.TrimSpace(row[5])
  121. baseInfo.Contact = strings.TrimSpace(row[11])
  122. baseInfo.ContactPhone = strings.TrimSpace(row[12])
  123. baseInfo.ContactEmail = strings.TrimSpace(row[13])
  124. baseInfo.ExecuteMonth, _ = strconv.Atoi(strings.TrimSpace(row[14]))
  125. baseInfo.ExecutePeriod = strings.TrimSpace(row[15])
  126. baseInfo.ExecuteStartYear, baseInfo.ExecuteEndYear = getBeginEndYear(baseInfo.ExecutePeriod)
  127. baseInfo.StartUp = strings.TrimSpace(row[17])
  128. baseInfo.NumberTopic, _ = strconv.Atoi(strings.TrimSpace(row[30]))
  129. baseInfo.Topic1 = strings.TrimSpace(row[31])
  130. baseInfo.Topic2 = strings.TrimSpace(row[32])
  131. baseInfo.Topic3 = strings.TrimSpace(row[33])
  132. baseInfo.Topic4 = strings.TrimSpace(row[34])
  133. baseInfo.Topic5 = strings.TrimSpace(row[35])
  134. allIns := replaceNewLineWithComma(strings.TrimSpace(row[36]))
  135. if allIns != "" {
  136. allInsArray := strings.Split(allIns, ",")
  137. var allInsValid []string
  138. for _, ins := range allInsArray {
  139. if ins != "" {
  140. allInsValid = append(allInsValid, ins)
  141. }
  142. }
  143. baseInfo.AllInstitution = strings.Join(allInsValid, ",")
  144. }
  145. if baseInfo.AllInstitution == "" {
  146. baseInfo.AllInstitution = baseInfo.Institution
  147. }
  148. err = baseInfo.InsertOrUpdate()
  149. if err != nil {
  150. log.Error("update err", i, err)
  151. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.sql_err")))
  152. return
  153. }
  154. }
  155. }
  156. ctx.JSON(http.StatusOK, models.BaseOKMessageApi)
  157. }
  158. func replaceNewLineWithComma(s string) string {
  159. const replacement = ","
  160. var replacer = strings.NewReplacer(
  161. "\r\n", replacement,
  162. "\r", replacement,
  163. "\n", replacement,
  164. "\v", replacement,
  165. "\f", replacement,
  166. "\u0085", replacement,
  167. "\u2028", replacement,
  168. "\u2029", replacement,
  169. )
  170. return replacer.Replace(s)
  171. }
  172. /**
  173. input:2019.12-2023.12 output: 2019,2023
  174. */
  175. func getBeginEndYear(executePeriod string) (int, int) {
  176. period := strings.Split(executePeriod, "-")
  177. if len(period) == 2 {
  178. start := strings.Split(period[0], ".")
  179. end := strings.Split(period[1], ".")
  180. if len(start) == 2 && len(end) == 2 {
  181. startYear, err := strconv.Atoi(start[0])
  182. endYear, err1 := strconv.Atoi(end[0])
  183. if err == nil && err1 == nil {
  184. return startYear, endYear
  185. }
  186. }
  187. }
  188. return 0, 0
  189. }
  190. func FindTech(ctx *context.APIContext) {
  191. techNo := ctx.Query("no")
  192. name := ctx.Query("name")
  193. institution := ctx.Query("institution")
  194. r, err := techService.FindTech(models.FindTechOpt{TechNo: techNo, ProjectName: name, Institution: institution})
  195. if err != nil {
  196. ctx.JSON(http.StatusOK, response.OuterResponseError(err))
  197. return
  198. }
  199. ctx.JSON(http.StatusOK, response.OuterSuccessWithData(r))
  200. }