package tech import ( "code.gitea.io/gitea/routers/response" techService "code.gitea.io/gitea/services/tech" "net/http" "strconv" "strings" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "github.com/360EntSecGroup-Skylar/excelize/v2" ) func ImportBasicInfo(ctx *context.APIContext) { file, _, err := ctx.GetFile("file") if err != nil { log.Error("get file err", err) ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.get_file_fail"))) return } defer file.Close() f, err := excelize.OpenReader(file) if err != nil { log.Error("open file err", err) ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.content_type_unsupported"))) return } rows, err := f.GetRows(f.GetSheetName(1)) if err != nil { ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.content_type_unsupported"))) return } for i, row := range rows { if i != 0 { baseInfo := &models.TechConvergeBaseInfo{} baseInfo.ProjectNumber = strings.TrimSpace(row[2]) tbInDB, err := models.GetTechConvergeBaseInfoByProjectNumber(baseInfo.ProjectNumber) if err != nil && !models.IsErrTechConvergeBaseInfoNotExist(err) { log.Error("query err ", i, err) ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.sql_err"))) return } if tbInDB != nil { baseInfo = tbInDB } baseInfo.ApplyYear, err = strconv.Atoi(strings.TrimSpace(row[1])) if err != nil { log.Warn("base info apply year parse err ", i) } baseInfo.ProjectName = strings.TrimSpace(row[3]) baseInfo.Type = strings.TrimSpace(row[16]) baseInfo.Owner = strings.TrimSpace(row[8]) baseInfo.Email = strings.TrimSpace(row[10]) baseInfo.Phone = strings.TrimSpace(row[9]) baseInfo.Recommend = strings.TrimSpace(row[7]) baseInfo.Institution = strings.TrimSpace(row[4]) baseInfo.Category = strings.TrimSpace(row[6]) baseInfo.Province = strings.TrimSpace(row[5]) baseInfo.Contact = strings.TrimSpace(row[11]) baseInfo.ContactPhone = strings.TrimSpace(row[12]) baseInfo.ContactEmail = strings.TrimSpace(row[13]) baseInfo.ExecuteMonth, _ = strconv.Atoi(strings.TrimSpace(row[14])) baseInfo.ExecutePeriod = strings.TrimSpace(row[15]) baseInfo.ExecuteStartYear, baseInfo.ExecuteEndYear = getBeginEndYear(baseInfo.ExecutePeriod) baseInfo.StartUp = strings.TrimSpace(row[17]) baseInfo.NumberTopic, _ = strconv.Atoi(strings.TrimSpace(row[30])) baseInfo.Topic1 = strings.TrimSpace(row[31]) baseInfo.Topic2 = strings.TrimSpace(row[32]) baseInfo.Topic3 = strings.TrimSpace(row[33]) baseInfo.Topic4 = strings.TrimSpace(row[34]) baseInfo.Topic5 = strings.TrimSpace(row[35]) allIns := replaceNewLineWithComma(strings.TrimSpace(row[36])) if allIns != "" { allInsArray := strings.Split(allIns, ",") var allInsValid []string for _, ins := range allInsArray { if ins != "" { allInsValid = append(allInsValid, ins) } } baseInfo.AllInstitution = strings.Join(allInsValid, ",") } if baseInfo.AllInstitution == "" { baseInfo.AllInstitution = baseInfo.Institution } err = baseInfo.InsertOrUpdate() if err != nil { log.Error("update err", i, err) ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("tech.sql_err"))) return } } } ctx.JSON(http.StatusOK, models.BaseOKMessageApi) } func replaceNewLineWithComma(s string) string { const replacement = "," var replacer = strings.NewReplacer( "\r\n", replacement, "\r", replacement, "\n", replacement, "\v", replacement, "\f", replacement, "\u0085", replacement, "\u2028", replacement, "\u2029", replacement, ) return replacer.Replace(s) } /** input:2019.12-2023.12 output: 2019,2023 */ func getBeginEndYear(executePeriod string) (int, int) { period := strings.Split(executePeriod, "-") if len(period) == 2 { start := strings.Split(period[0], ".") end := strings.Split(period[1], ".") if len(start) == 2 && len(end) == 2 { startYear, err := strconv.Atoi(start[0]) endYear, err1 := strconv.Atoi(end[0]) if err == nil && err1 == nil { return startYear, endYear } } } return 0, 0 } func FindTech(ctx *context.APIContext) { techNo := ctx.Query("no") name := ctx.Query("name") institution := ctx.Query("institution") r, err := techService.FindTech(models.FindTechOpt{TechNo: techNo, ProjectName: name, Institution: institution}) if err != nil { ctx.JSON(http.StatusOK, response.OuterResponseError(err)) return } ctx.JSON(http.StatusOK, response.OuterSuccessWithData(r)) }