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