diff --git a/models/tech_converge_info.go b/models/tech_converge_info.go index d921daada..cd45ba0ef 100644 --- a/models/tech_converge_info.go +++ b/models/tech_converge_info.go @@ -1,12 +1,15 @@ package models import ( + "fmt" + "strconv" + "code.gitea.io/gitea/modules/timeutil" ) type TechConvergeBaseInfo struct { ID int64 `xorm:"pk autoincr"` - ProjectNumber string `xorm:"index"` //项目立项编号 + ProjectNumber string `xorm:"UNIQUE NOT NULL"` //项目立项编号 ProjectName string //科技项目名称 Institution string //项目承担单位 ApplyYear int //申报年度 @@ -52,3 +55,44 @@ type RepoConvergeInfo struct { Repo *Repository `xorm:"-"` BaseInfo *TechConvergeBaseInfo `xorm:"-"` } + +func GetTechConvergeBaseInfoByProjectNumber(projectNumber string) (*TechConvergeBaseInfo, error) { + tb := &TechConvergeBaseInfo{ProjectNumber: projectNumber} + return getTechConvergeBaseInfo(tb) +} +func (baseInfo *TechConvergeBaseInfo) InsertOrUpdate() error { + if baseInfo.ID != 0 { + _, err := x.ID(baseInfo.ID).Update(baseInfo) + return err + } else { + _, err := x.InsertOne(baseInfo) + return err + } +} + +type ErrTechConvergeBaseInfoNotExist struct { + ID string +} + +func (err ErrTechConvergeBaseInfoNotExist) Error() string { + return fmt.Sprintf("TechConvergeBaseInfo does not exist [id: %s]", err.ID) +} + +func IsErrTechConvergeBaseInfoNotExist(err error) bool { + _, ok := err.(ErrTechConvergeBaseInfoNotExist) + return ok +} + +func getTechConvergeBaseInfo(tb *TechConvergeBaseInfo) (*TechConvergeBaseInfo, error) { + has, err := x.Get(tb) + if err != nil { + return nil, err + } else if !has { + if tb.ProjectNumber != "" { + return nil, ErrTechConvergeBaseInfoNotExist{tb.ProjectNumber} + } else { + return nil, ErrTechConvergeBaseInfoNotExist{strconv.FormatInt(tb.ID, 10)} + } + } + return tb, nil +} diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index ecd2598b7..375574132 100755 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3358,3 +3358,8 @@ get_file_fail= Can not get the image content, please try again later. content_type_unsupported=Allowed image type is jpg, jpeg or png. process_image_fail=Fail to process image, please try again later. +[tech] +get_file_fail= Can not get the file content, please try again later. +content_type_unsupported=The format of the file or file content is wrong. +sql_err=Fail to process data, please try again later. + diff --git a/options/locale/locale_zh-CN.ini b/options/locale/locale_zh-CN.ini index 5cef94087..66ffb0983 100755 --- a/options/locale/locale_zh-CN.ini +++ b/options/locale/locale_zh-CN.ini @@ -3378,4 +3378,10 @@ new_model_security_evaluation_tips = 模型安全评测只适用于图像分类 get_file_fail= 获取上传文件失败,请稍后再试。 content_type_unsupported=请上传jpg、jpeg或png图片。 process_image_fail=图片处理失败,请稍后再试。 +[tech] +get_file_fail= 获取上传文件失败。 +content_type_unsupported=上传文件的格式有误。 +sql_err=数据处理错误,请稍后再试。 + + diff --git a/routers/api/v1/tech/tech.go b/routers/api/v1/tech/tech.go index 78777b2f2..16f2e208b 100644 --- a/routers/api/v1/tech/tech.go +++ b/routers/api/v1/tech/tech.go @@ -15,27 +15,41 @@ import ( func ImportBasicInfo(ctx *context.APIContext) { file, _, err := ctx.GetFile("file") if err != nil { - ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("model_app.get_file_fail"))) + 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 { - ctx.JSON(http.StatusBadRequest, models.BaseErrorMessageApi(ctx.Tr("model_app.get_file_fail"))) + 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("model_app.get_file_fail"))) + 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])) - log.Warn("base info apply year parse err ", i) + 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]) @@ -59,14 +73,31 @@ func ImportBasicInfo(ctx *context.APIContext) { baseInfo.Topic4 = strings.TrimSpace(row[34]) baseInfo.Topic5 = strings.TrimSpace(row[35]) - replaceNewLineWithComma(strings.TrimSpace(row[36])) + 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 { @@ -86,7 +117,7 @@ func replaceNewLineWithComma(s string) string { } /** - 2019.12-2023.12 + input:2019.12-2023.12 output: 2019,2023 */ func getBeginEndYear(executePeriod string) (int, int) { period := strings.Split(executePeriod, "-")