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