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 4.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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 ImportBasicInfo(ctx *context.APIContext) {
  14. file, _, err := ctx.GetFile("file")
  15. if err != nil {
  16. log.Error("get file err", err)
  17. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.get_file_fail")))
  18. return
  19. }
  20. defer file.Close()
  21. f, err := excelize.OpenReader(file)
  22. if err != nil {
  23. log.Error("open file err", err)
  24. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.content_type_unsupported")))
  25. return
  26. }
  27. rows, err := f.GetRows(f.GetSheetName(1))
  28. if err != nil {
  29. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.content_type_unsupported")))
  30. return
  31. }
  32. for i, row := range rows {
  33. if i != 0 {
  34. baseInfo := &models.TechConvergeBaseInfo{}
  35. baseInfo.ProjectNumber = strings.TrimSpace(row[2])
  36. tbInDB, err := models.GetTechConvergeBaseInfoByProjectNumber(baseInfo.ProjectNumber)
  37. if err != nil && !models.IsErrTechConvergeBaseInfoNotExist(err) {
  38. log.Error("query err ", i, err)
  39. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.sql_err")))
  40. return
  41. }
  42. if tbInDB != nil {
  43. baseInfo = tbInDB
  44. }
  45. baseInfo.ApplyYear, err = strconv.Atoi(strings.TrimSpace(row[1]))
  46. if err != nil {
  47. log.Warn("base info apply year parse err ", i)
  48. }
  49. baseInfo.ProjectName = strings.TrimSpace(row[3])
  50. baseInfo.Type = strings.TrimSpace(row[16])
  51. baseInfo.Owner = strings.TrimSpace(row[8])
  52. baseInfo.Email = strings.TrimSpace(row[10])
  53. baseInfo.Phone = strings.TrimSpace(row[9])
  54. baseInfo.Recommend = strings.TrimSpace(row[7])
  55. baseInfo.Institution = strings.TrimSpace(row[4])
  56. baseInfo.Category = strings.TrimSpace(row[6])
  57. baseInfo.Province = strings.TrimSpace(row[5])
  58. baseInfo.Contact = strings.TrimSpace(row[11])
  59. baseInfo.ContactPhone = strings.TrimSpace(row[12])
  60. baseInfo.ContactEmail = strings.TrimSpace(row[13])
  61. baseInfo.ExecuteMonth, _ = strconv.Atoi(strings.TrimSpace(row[14]))
  62. baseInfo.ExecutePeriod = strings.TrimSpace(row[15])
  63. baseInfo.ExecuteStartYear, baseInfo.ExecuteEndYear = getBeginEndYear(baseInfo.ExecutePeriod)
  64. baseInfo.StartUp = strings.TrimSpace(row[17])
  65. baseInfo.NumberTopic, _ = strconv.Atoi(strings.TrimSpace(row[30]))
  66. baseInfo.Topic1 = strings.TrimSpace(row[31])
  67. baseInfo.Topic2 = strings.TrimSpace(row[32])
  68. baseInfo.Topic3 = strings.TrimSpace(row[33])
  69. baseInfo.Topic4 = strings.TrimSpace(row[34])
  70. baseInfo.Topic5 = strings.TrimSpace(row[35])
  71. allIns := replaceNewLineWithComma(strings.TrimSpace(row[36]))
  72. if allIns != "" {
  73. allInsArray := strings.Split(allIns, ",")
  74. var allInsValid []string
  75. for _, ins := range allInsArray {
  76. if ins != "" {
  77. allInsValid = append(allInsValid, ins)
  78. }
  79. }
  80. baseInfo.AllInstitution = strings.Join(allInsValid, ",")
  81. }
  82. if baseInfo.AllInstitution == "" {
  83. baseInfo.AllInstitution = baseInfo.Institution
  84. }
  85. err = baseInfo.InsertOrUpdate()
  86. if err != nil {
  87. log.Error("update err", i, err)
  88. ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.sql_err")))
  89. return
  90. }
  91. }
  92. }
  93. ctx.JSON(http.StatusOK, models.BaseOKMessageApi)
  94. }
  95. func replaceNewLineWithComma(s string) string {
  96. const replacement = ","
  97. var replacer = strings.NewReplacer(
  98. "\r\n", replacement,
  99. "\r", replacement,
  100. "\n", replacement,
  101. "\v", replacement,
  102. "\f", replacement,
  103. "\u0085", replacement,
  104. "\u2028", replacement,
  105. "\u2029", replacement,
  106. )
  107. return replacer.Replace(s)
  108. }
  109. /**
  110. input:2019.12-2023.12 output: 2019,2023
  111. */
  112. func getBeginEndYear(executePeriod string) (int, int) {
  113. period := strings.Split(executePeriod, "-")
  114. if len(period) == 2 {
  115. start := strings.Split(period[0], ".")
  116. end := strings.Split(period[1], ".")
  117. if len(start) == 2 && len(end) == 2 {
  118. startYear, err := strconv.Atoi(start[0])
  119. endYear, err1 := strconv.Atoi(end[0])
  120. if err == nil && err1 == nil {
  121. return startYear, endYear
  122. }
  123. }
  124. }
  125. return 0, 0
  126. }
  127. func FindTech(ctx *context.APIContext) {
  128. techNo := ctx.Query("no")
  129. name := ctx.Query("name")
  130. institution := ctx.Query("institution")
  131. r, err := techService.FindTech(models.FindTechOpt{TechNo: techNo, ProjectName: name, Institution: institution})
  132. if err != nil {
  133. ctx.JSON(http.StatusOK, response.OuterResponseError(err))
  134. return
  135. }
  136. ctx.JSON(http.StatusOK, response.OuterSuccessWithData(r))
  137. }