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