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 9.5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 GetAdminRepoInfo(ctx *context.APIContext) {
  43. go techService.UpdateTechStatus()
  44. opts := &models.SearchUserRepoOpt{}
  45. page := ctx.QueryInt("page")
  46. if page <= 0 {
  47. page = 1
  48. }
  49. opts.Page = page
  50. pageSize := ctx.QueryInt("pageSize")
  51. if pageSize <= 0 {
  52. pageSize = 15
  53. }
  54. opts.PageSize = pageSize
  55. infos, total, err := techService.GetAdminRepoInfo(opts)
  56. if err != nil {
  57. log.Error("search has error", err)
  58. ctx.JSON(http.StatusOK, map[string]interface {
  59. }{"total": 0, "data": []*models.TechRepoInfoAdmin{}})
  60. } else {
  61. ctx.JSON(http.StatusOK, map[string]interface {
  62. }{"total": total, "data": infos})
  63. }
  64. }
  65. func Action(ctx *context.APIContext, ids ActionIDs) {
  66. if len(ids.ID) == 0 {
  67. ctx.JSON(http.StatusOK, models.BaseOKMessageApi)
  68. }
  69. var err error
  70. switch ctx.Params(":action") {
  71. case "show":
  72. err = models.ShowTechRepo(ids.ID, true)
  73. case "hide":
  74. err = models.ShowTechRepo(ids.ID, false)
  75. }
  76. if err != nil {
  77. ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("tech.show_or_hide_fail", ctx.Params(":action"))))
  78. } else {
  79. ctx.JSON(http.StatusOK, models.BaseOKMessage)
  80. }
  81. }
  82. type ActionIDs struct {
  83. ID []int64 `json:"id"`
  84. }
  85. func GetMyRepoInfo(ctx *context.APIContext) {
  86. go techService.UpdateTechStatus()
  87. opts := &models.SearchUserRepoOpt{}
  88. page := ctx.QueryInt("page")
  89. if page <= 0 {
  90. page = 1
  91. }
  92. opts.Page = page
  93. pageSize := ctx.QueryInt("pageSize")
  94. if pageSize <= 0 {
  95. pageSize = 15
  96. }
  97. opts.PageSize = pageSize
  98. opts.User = ctx.User
  99. infos, total, err := techService.GetMyRepoInfo(opts)
  100. if err != nil {
  101. log.Error("search has error", err)
  102. ctx.JSON(http.StatusOK, map[string]interface {
  103. }{"total": 0, "data": []*models.TechRepoInfoUser{}})
  104. } else {
  105. ctx.JSON(http.StatusOK, map[string]interface {
  106. }{"total": total, "data": infos})
  107. }
  108. }
  109. func SearchRepoInfo(ctx *context.APIContext) {
  110. go techService.UpdateTechStatus()
  111. opts := &models.SearchRepoOpt{}
  112. opts.Q = ctx.Query("name")
  113. opts.ProjectName = ctx.Query("tech_name")
  114. opts.Topic = ctx.Query("topic")
  115. opts.Institution = ctx.Query("institution_name")
  116. page := ctx.QueryInt("page")
  117. if page <= 0 {
  118. page = 1
  119. }
  120. opts.Page = page
  121. pageSize := ctx.QueryInt("pageSize")
  122. if pageSize <= 0 {
  123. pageSize = 30
  124. }
  125. opts.PageSize = pageSize
  126. orderBy := ctx.Query("sort")
  127. opts.OrderBy = orderBy
  128. infos, total, err := techService.SearchRepoInfoWithInstitution(opts)
  129. if err != nil {
  130. log.Error("search has error", err)
  131. ctx.JSON(http.StatusOK, map[string]interface {
  132. }{"total": 0, "data": []*models.RepoWithInstitution{}})
  133. } else {
  134. ctx.JSON(http.StatusOK, map[string]interface {
  135. }{"total": total, "data": infos})
  136. }
  137. }
  138. func SearchTechProjectInfo(ctx *context.APIContext) {
  139. go techService.UpdateTechStatus()
  140. opts := &models.SearchTechOpt{}
  141. opts.Q = ctx.Query("name")
  142. opts.ApplyYear = ctx.QueryInt("apply_year")
  143. opts.ExecuteYear = ctx.QueryInt("execute_year")
  144. opts.Institution = ctx.Query("institution_name")
  145. opts.ProjectType = ctx.Query("type_name")
  146. page := ctx.QueryInt("page")
  147. if page <= 0 {
  148. page = 1
  149. }
  150. opts.Page = page
  151. pageSize := ctx.QueryInt("pageSize")
  152. if pageSize <= 0 {
  153. pageSize = 15
  154. }
  155. opts.PageSize = pageSize
  156. orderBy := ctx.Query("sort")
  157. if orderBy == "" || orderBy == "count" {
  158. opts.OrderBy = "order by repo_count desc, max desc "
  159. } else {
  160. opts.OrderBy = "order by max desc, repo_count desc "
  161. }
  162. infos, total, err := models.SearchTechRepoInfo(opts)
  163. if err != nil {
  164. log.Error("search has error", err)
  165. ctx.JSON(http.StatusOK, map[string]interface {
  166. }{"total": 0, "data": []*models.TechRepoInfo{}})
  167. } else {
  168. ctx.JSON(http.StatusOK, map[string]interface {
  169. }{"total": total, "data": infos})
  170. }
  171. }
  172. func ImportBasicInfo(ctx *context.APIContext) {
  173. file, _, err := ctx.GetFile("file")
  174. if err != nil {
  175. log.Error("get file err", err)
  176. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.get_file_fail")))
  177. return
  178. }
  179. defer file.Close()
  180. f, err := excelize.OpenReader(file)
  181. if err != nil {
  182. log.Error("open file err", err)
  183. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.content_type_unsupported")))
  184. return
  185. }
  186. rows, err := f.GetRows(f.GetSheetName(1))
  187. if err != nil {
  188. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.content_type_unsupported")))
  189. return
  190. }
  191. for i, row := range rows {
  192. if i != 0 {
  193. baseInfo := &models.TechConvergeBaseInfo{}
  194. baseInfo.ProjectNumber = strings.TrimSpace(row[2])
  195. tbInDB, err := models.GetTechConvergeBaseInfoByProjectNumber(baseInfo.ProjectNumber)
  196. if err != nil && !models.IsErrTechConvergeBaseInfoNotExist(err) {
  197. log.Error("query err ", i, err)
  198. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.sql_err")))
  199. return
  200. }
  201. if tbInDB != nil {
  202. baseInfo = tbInDB
  203. }
  204. baseInfo.ApplyYear, err = strconv.Atoi(strings.TrimSpace(row[1]))
  205. if err != nil {
  206. log.Warn("base info apply year parse err ", i)
  207. }
  208. baseInfo.ProjectName = strings.TrimSpace(row[3])
  209. baseInfo.Type = strings.TrimSpace(row[16])
  210. baseInfo.Owner = strings.TrimSpace(row[8])
  211. baseInfo.Email = strings.TrimSpace(row[10])
  212. baseInfo.Phone = strings.TrimSpace(row[9])
  213. baseInfo.Recommend = strings.TrimSpace(row[7])
  214. baseInfo.Institution = strings.TrimSpace(row[4])
  215. baseInfo.Category = strings.TrimSpace(row[6])
  216. baseInfo.Province = strings.TrimSpace(row[5])
  217. baseInfo.Contact = strings.TrimSpace(row[11])
  218. baseInfo.ContactPhone = strings.TrimSpace(row[12])
  219. baseInfo.ContactEmail = strings.TrimSpace(row[13])
  220. baseInfo.ExecuteMonth, _ = strconv.Atoi(strings.TrimSpace(row[14]))
  221. baseInfo.ExecutePeriod = strings.TrimSpace(row[15])
  222. baseInfo.ExecuteStartYear, baseInfo.ExecuteEndYear = getBeginEndYear(baseInfo.ExecutePeriod)
  223. baseInfo.StartUp = strings.TrimSpace(row[17])
  224. baseInfo.NumberTopic, _ = strconv.Atoi(strings.TrimSpace(row[30]))
  225. baseInfo.Topic1 = strings.TrimSpace(row[31])
  226. baseInfo.Topic2 = strings.TrimSpace(row[32])
  227. baseInfo.Topic3 = strings.TrimSpace(row[33])
  228. baseInfo.Topic4 = strings.TrimSpace(row[34])
  229. baseInfo.Topic5 = strings.TrimSpace(row[35])
  230. allIns := replaceNewLineWithComma(strings.TrimSpace(row[36]))
  231. if allIns != "" {
  232. allInsArray := strings.Split(allIns, ",")
  233. var allInsValid []string
  234. for _, ins := range allInsArray {
  235. if ins != "" {
  236. allInsValid = append(allInsValid, ins)
  237. }
  238. }
  239. baseInfo.AllInstitution = strings.Join(allInsValid, ",")
  240. }
  241. if baseInfo.AllInstitution == "" {
  242. baseInfo.AllInstitution = baseInfo.Institution
  243. }
  244. err = baseInfo.InsertOrUpdate()
  245. if err != nil {
  246. log.Error("update err", i, err)
  247. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.sql_err")))
  248. return
  249. }
  250. }
  251. }
  252. ctx.JSON(http.StatusOK, models.BaseOKMessageApi)
  253. }
  254. func replaceNewLineWithComma(s string) string {
  255. const replacement = ","
  256. var replacer = strings.NewReplacer(
  257. "\r\n", replacement,
  258. "\r", replacement,
  259. "\n", replacement,
  260. "\v", replacement,
  261. "\f", replacement,
  262. "\u0085", replacement,
  263. "\u2028", replacement,
  264. "\u2029", replacement,
  265. )
  266. return replacer.Replace(s)
  267. }
  268. /**
  269. input:2019.12-2023.12 output: 2019,2023
  270. */
  271. func getBeginEndYear(executePeriod string) (int, int) {
  272. period := strings.Split(executePeriod, "-")
  273. if len(period) == 2 {
  274. start := strings.Split(period[0], ".")
  275. end := strings.Split(period[1], ".")
  276. if len(start) == 2 && len(end) == 2 {
  277. startYear, err := strconv.Atoi(start[0])
  278. endYear, err1 := strconv.Atoi(end[0])
  279. if err == nil && err1 == nil {
  280. return startYear, endYear
  281. }
  282. }
  283. }
  284. return 0, 0
  285. }
  286. func FindTech(ctx *context.APIContext) {
  287. techNo := ctx.Query("no")
  288. name := ctx.Query("name")
  289. institution := ctx.Query("institution")
  290. r, err := techService.FindTech(models.FindTechOpt{TechNo: techNo, ProjectName: name, Institution: institution})
  291. if err != nil {
  292. ctx.JSON(http.StatusOK, response.OuterResponseError(err))
  293. return
  294. }
  295. ctx.JSON(http.StatusOK, response.OuterSuccessWithData(r))
  296. }
  297. func IsAdmin(ctx *context.APIContext) {
  298. isAdmin := role.UserHasRole(ctx.User.ID, models.TechProgramAdmin)
  299. r := map[string]interface{}{}
  300. r["is_admin"] = isAdmin
  301. ctx.JSON(http.StatusOK, response.OuterSuccessWithData(r))
  302. }