From 6ca4d2757a0489ffd3d32829a763704d83575f30 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 11 七月 2024 21:52:58 +0800
Subject: [PATCH] 从指定路径excel获取产品编码和图片,传到文件服务器并保存的接口,用于嘉联为所有产品上传图片。
---
controllers/other.go | 188 ++++++++++++++++++++++++++
docs/swagger.yaml | 47 ++++++
docs/docs.go | 67 +++++++++
docs/swagger.json | 67 +++++++++
router/router.go | 7 +
5 files changed, 367 insertions(+), 9 deletions(-)
diff --git a/controllers/other.go b/controllers/other.go
new file mode 100644
index 0000000..49d715f
--- /dev/null
+++ b/controllers/other.go
@@ -0,0 +1,188 @@
+package controllers
+
+import (
+ "fmt"
+ "github.com/gin-gonic/gin"
+ "github.com/xuri/excelize/v2"
+ "gorm.io/gorm"
+ "strings"
+ "wms/constvar"
+ "wms/extend/code"
+ "wms/extend/util"
+ "wms/models"
+ "wms/pkg/logx"
+ "wms/utils/upload"
+)
+
+type OtherController struct{}
+
+type saveProductImagesFromExcelRequest struct {
+ ExcelFilePath string `json:"excelFilePath"` //excel璺緞 鐢╠ocker cp浼犲埌瀹瑰櫒閲�
+ SheetIndex int `json:"sheetIndex"` //绗嚑涓猻heet
+ ProductIdColumnIndex int `json:"productIdColumnIndex"` //浜у搧ID鏄鍑犲垪(浠�0寮�濮�)
+ ImageColumn string `json:"imageColumn"` //鍥剧墖鎵�鍦ㄥ垪鍚�
+}
+
+// SaveProductImagesFromExcel
+// @Tags 鍏朵粬
+// @Summary 浠巈xcel涓幏鍙栦骇鍝佸浘鐗囧苟淇濆瓨
+// @Produce application/json
+// @Param object body saveProductImagesFromExcelRequest true "灞炴�т俊鎭�"
+// @Param Authorization header string true "token"
+// @Success 200 {object} util.Response "鎴愬姛"
+// @Router /api-wms/v1/other/saveProductImagesFromExcel [post]
+func (slf OtherController) SaveProductImagesFromExcel(c *gin.Context) {
+ var params saveProductImagesFromExcelRequest
+ if err := c.BindJSON(¶ms); err != nil {
+ util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�:"+err.Error())
+ return
+ }
+ if params.ExcelFilePath == "" || params.ImageColumn == "" {
+ util.ResponseFormat(c, code.RequestParamError, "鍙傛暟缂哄け")
+ return
+ }
+
+ productImageMap, err := UploadProductImagesFromExcel(params.ExcelFilePath, params.SheetIndex, params.ProductIdColumnIndex, params.ImageColumn)
+ if err != nil {
+ logx.Errorf("SaveProductImagesFromExcel err:%v", err)
+ util.ResponseFormat(c, code.SaveFail, "鎿嶄綔澶辫触")
+ return
+ }
+
+ for productId, urls := range productImageMap {
+ if len(urls) == 0 {
+ continue
+ }
+ attachmentList := make([]*models.Attachment, 0)
+ materialAttachmentList := make([]*models.MaterialAttachment, 0)
+ for _, url := range urls {
+ attachmentList = append(attachmentList, &models.Attachment{
+ FileUrl: url,
+ FileType: constvar.FileType_Picture,
+ })
+ }
+
+ err = models.WithTransaction(func(db *gorm.DB) error {
+ //淇濆瓨闄勪欢
+ if attachmentList, err = models.NewAttachmentSearch().CreateBatchWithResp(attachmentList); err != nil {
+ logx.Errorf("attachment create err: %v", err)
+ return err
+ }
+
+ //淇濆瓨鐗╂枡鍜岄檮浠舵槧灏�
+ for _, v := range attachmentList {
+ ma := &models.MaterialAttachment{MaterialID: productId, AttachmentID: v.Id}
+ materialAttachmentList = append(materialAttachmentList, ma)
+ }
+ if err := models.NewMaterialAttachmentSearch().SetOrm(db).CreateBatch(materialAttachmentList); err != nil {
+ return err
+ }
+ return nil
+ })
+ if err != nil {
+ logx.Errorf("SaveProductImagesFromExcel save db err:%v, productId:%v, urls:%v", err, productId, urls)
+ }
+ }
+
+ util.ResponseFormat(c, code.Success, "淇濆瓨鎴愬姛")
+}
+
+// UploadProductImagesFromExcel 浠嶦xcel鏂囦欢涓笂浼犱骇鍝佸浘鐗�
+func UploadProductImagesFromExcel(filePath string, sheetIndex int, productIdColumn int, imageColumn string) (productImageMap map[string][]string, err error) {
+ productImageMap = make(map[string][]string)
+ // 鍔犺浇Excel鏂囦欢
+ f, err := excelize.OpenFile(filePath)
+ if err != nil {
+ logx.Errorf("Failed to open Excel file: %v", err)
+ return
+ }
+ defer f.Close()
+
+ // 鑾峰彇宸ヤ綔琛ㄥ悕绉�
+ sheetName := f.GetSheetName(sheetIndex)
+ // 璇诲彇宸ヤ綔琛ㄤ腑鐨勬墍鏈夎
+ rows, err := f.GetRows(sheetName)
+ if err != nil {
+ logx.Errorf("Failed to get rows from sheet:%v", err)
+ return
+ }
+
+ // 鑾峰彇鎸囧畾鍒楃殑鍚堝苟鍗曞厓鏍间俊鎭�
+ mergedCells, err := getColumnMergeCells(f, sheetName, imageColumn)
+ if err != nil {
+ logx.Errorf("Failed to get merged cells:%v", err)
+ return
+ }
+
+ mergeCellMap := make(map[string]excelize.MergeCell)
+ for _, cell := range mergedCells {
+ mergeCellMap[cell.GetStartAxis()] = cell
+ }
+
+ var imagePaths []string
+ // 閬嶅巻Excel琛岋紝璇诲彇鏁版嵁
+ for rowIndex, row := range rows[1:] { // 鍋囪绗竴琛屾槸鏍囬琛岋紝璺宠繃
+ if len(row) < 1 {
+ continue
+ }
+ productId := row[productIdColumn]
+ currentCell := fmt.Sprintf("%s%d", imageColumn, rowIndex+2)
+
+ // 妫�鏌ュ綋鍓嶅崟鍏冩牸鏄惁鍦ㄥ悎骞跺崟鍏冩牸涓�
+ if mergeCell, ok := mergeCellMap[currentCell]; ok {
+ imagePaths = make([]string, 0)
+ imageCell := mergeCell.GetStartAxis()
+
+ // 鑾峰彇鍥剧墖
+ pictures, err := f.GetPictures(sheetName, imageCell)
+ if err != nil {
+ logx.Errorf("Failed to get picture for cell %s: %v", imageCell, err)
+ continue
+ }
+
+ if len(pictures) == 0 {
+ logx.Errorf("No picture found for cell %s", imageCell)
+ continue
+ }
+
+ for k, picture := range pictures {
+ fileBytes := picture.File
+ fileName := fmt.Sprintf("image_%s_%d.png", imageCell, k+1)
+
+ // 淇濆瓨鍥剧墖鍒版湰鍦板苟鑾峰彇璺緞
+ imagePath, err := upload.UploadFileToSeaWeed(string(constvar.FileType_Picture), fileName, fileBytes)
+ if err != nil {
+ logx.Errorf("Failed to save image for product %s: %v\n", productId, err)
+ continue
+ }
+ imagePaths = append(imagePaths, imagePath)
+ }
+ }
+
+ if productId == "" {
+ continue
+ }
+
+ // imagePaths
+ productImageMap[productId] = imagePaths
+ logx.Infof("UploadProductImagesFromExcel Product ID: %s, Image Urls: %s\n", productId, imagePaths)
+ }
+ return productImageMap, nil
+}
+
+// getColumnMergeCells 鑾峰彇鎸囧畾鍒楃殑鍚堝苟鍗曞厓鏍�
+func getColumnMergeCells(f *excelize.File, sheetName string, targetColumn string) ([]excelize.MergeCell, error) {
+ // 鑾峰彇鎵�鏈夊悎骞跺崟鍏冩牸
+ mergeCells, err := f.GetMergeCells(sheetName)
+ if err != nil {
+ return nil, err
+ }
+
+ var columnMergeCells []excelize.MergeCell
+ for _, mergeCell := range mergeCells {
+ if strings.HasPrefix(mergeCell.GetStartAxis(), targetColumn) {
+ columnMergeCells = append(columnMergeCells, mergeCell)
+ }
+ }
+ return columnMergeCells, nil
+}
diff --git a/docs/docs.go b/docs/docs.go
index aee23f5..f46a305 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -2625,6 +2625,43 @@
}
}
},
+ "/api-wms/v1/other/saveProductImagesFromExcel": {
+ "post": {
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "鍏朵粬"
+ ],
+ "summary": "浠巈xcel涓幏鍙栦骇鍝佸浘鐗囧苟淇濆瓨",
+ "parameters": [
+ {
+ "description": "灞炴�т俊鎭�",
+ "name": "object",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/controllers.saveProductImagesFromExcelRequest"
+ }
+ },
+ {
+ "type": "string",
+ "description": "token",
+ "name": "Authorization",
+ "in": "header",
+ "required": true
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "鎴愬姛",
+ "schema": {
+ "$ref": "#/definitions/util.Response"
+ }
+ }
+ }
+ }
+ },
"/api-wms/v1/product/addDisuse": {
"post": {
"produces": [
@@ -4395,6 +4432,27 @@
"WhetherTypeAlways",
"ReservationNever"
]
+ },
+ "controllers.saveProductImagesFromExcelRequest": {
+ "type": "object",
+ "properties": {
+ "excelFilePath": {
+ "description": "excel璺緞 鐢╠ocker cp浼犲埌瀹瑰櫒閲�",
+ "type": "string"
+ },
+ "imageColumn": {
+ "description": "鍥剧墖鎵�鍦ㄥ垪鍚�",
+ "type": "string"
+ },
+ "productIdColumnIndex": {
+ "description": "浜у搧ID鏄鍑犲垪(浠�0寮�濮�)",
+ "type": "integer"
+ },
+ "sheetIndex": {
+ "description": "绗嚑涓猻heet",
+ "type": "integer"
+ }
+ }
},
"gorm.DeletedAt": {
"type": "object",
@@ -6333,9 +6391,9 @@
"description": "姣忛〉澶у皬",
"type": "integer"
},
- "warehouseCode": {
- "description": "浠撳簱缂╁啓",
- "type": "string"
+ "warehouseId": {
+ "description": "WarehouseCode string ` + "`" + `json:\"warehouseCode\"` + "`" + ` //浠撳簱缂╁啓",
+ "type": "integer"
}
}
},
@@ -6373,6 +6431,9 @@
"unit": {
"description": "鍗曚綅",
"type": "string"
+ },
+ "warehouseCode": {
+ "type": "string"
}
}
},
diff --git a/docs/swagger.json b/docs/swagger.json
index 76cdd10..e32dcf8 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -2614,6 +2614,43 @@
}
}
},
+ "/api-wms/v1/other/saveProductImagesFromExcel": {
+ "post": {
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "鍏朵粬"
+ ],
+ "summary": "浠巈xcel涓幏鍙栦骇鍝佸浘鐗囧苟淇濆瓨",
+ "parameters": [
+ {
+ "description": "灞炴�т俊鎭�",
+ "name": "object",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/controllers.saveProductImagesFromExcelRequest"
+ }
+ },
+ {
+ "type": "string",
+ "description": "token",
+ "name": "Authorization",
+ "in": "header",
+ "required": true
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "鎴愬姛",
+ "schema": {
+ "$ref": "#/definitions/util.Response"
+ }
+ }
+ }
+ }
+ },
"/api-wms/v1/product/addDisuse": {
"post": {
"produces": [
@@ -4384,6 +4421,27 @@
"WhetherTypeAlways",
"ReservationNever"
]
+ },
+ "controllers.saveProductImagesFromExcelRequest": {
+ "type": "object",
+ "properties": {
+ "excelFilePath": {
+ "description": "excel璺緞 鐢╠ocker cp浼犲埌瀹瑰櫒閲�",
+ "type": "string"
+ },
+ "imageColumn": {
+ "description": "鍥剧墖鎵�鍦ㄥ垪鍚�",
+ "type": "string"
+ },
+ "productIdColumnIndex": {
+ "description": "浜у搧ID鏄鍑犲垪(浠�0寮�濮�)",
+ "type": "integer"
+ },
+ "sheetIndex": {
+ "description": "绗嚑涓猻heet",
+ "type": "integer"
+ }
+ }
},
"gorm.DeletedAt": {
"type": "object",
@@ -6322,9 +6380,9 @@
"description": "姣忛〉澶у皬",
"type": "integer"
},
- "warehouseCode": {
- "description": "浠撳簱缂╁啓",
- "type": "string"
+ "warehouseId": {
+ "description": "WarehouseCode string `json:\"warehouseCode\"` //浠撳簱缂╁啓",
+ "type": "integer"
}
}
},
@@ -6362,6 +6420,9 @@
"unit": {
"description": "鍗曚綅",
"type": "string"
+ },
+ "warehouseCode": {
+ "type": "string"
}
}
},
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index 336f0d7..3504c9f 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -361,6 +361,21 @@
- WhetherTypeAsk
- WhetherTypeAlways
- ReservationNever
+ controllers.saveProductImagesFromExcelRequest:
+ properties:
+ excelFilePath:
+ description: excel璺緞 鐢╠ocker cp浼犲埌瀹瑰櫒閲�
+ type: string
+ imageColumn:
+ description: 鍥剧墖鎵�鍦ㄥ垪鍚�
+ type: string
+ productIdColumnIndex:
+ description: 浜у搧ID鏄鍑犲垪(浠�0寮�濮�)
+ type: integer
+ sheetIndex:
+ description: 绗嚑涓猻heet
+ type: integer
+ type: object
gorm.DeletedAt:
properties:
time:
@@ -1693,9 +1708,9 @@
pageSize:
description: 姣忛〉澶у皬
type: integer
- warehouseCode:
- description: 浠撳簱缂╁啓
- type: string
+ warehouseId:
+ description: WarehouseCode string `json:"warehouseCode"` //浠撳簱缂╁啓
+ type: integer
type: object
request.GetInventoryHistory:
properties:
@@ -1720,6 +1735,8 @@
type: string
unit:
description: 鍗曚綅
+ type: string
+ warehouseCode:
type: string
type: object
request.GetList:
@@ -4038,6 +4055,30 @@
summary: 缂栬緫閮ㄩ棬淇℃伅
tags:
- 閮ㄩ棬淇℃伅
+ /api-wms/v1/other/saveProductImagesFromExcel:
+ post:
+ parameters:
+ - description: 灞炴�т俊鎭�
+ in: body
+ name: object
+ required: true
+ schema:
+ $ref: '#/definitions/controllers.saveProductImagesFromExcelRequest'
+ - description: token
+ in: header
+ name: Authorization
+ required: true
+ type: string
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: 鎴愬姛
+ schema:
+ $ref: '#/definitions/util.Response'
+ summary: 浠巈xcel涓幏鍙栦骇鍝佸浘鐗囧苟淇濆瓨
+ tags:
+ - 鍏朵粬
/api-wms/v1/product/addDisuse:
post:
parameters:
diff --git a/router/router.go b/router/router.go
index 815904d..5135e5a 100644
--- a/router/router.go
+++ b/router/router.go
@@ -239,5 +239,12 @@
sysCfgApi.GET("get", sysCfgCtl.GetSystemConfig) //鑾峰彇绯荤粺閰嶇疆
}
+ //鍏朵粬
+ otherCtl := new(controllers.OtherController)
+ otherApi := r.Group(urlPrefix + "/other")
+ {
+ otherApi.POST("saveProductImagesFromExcel", otherCtl.SaveProductImagesFromExcel) //浠庢寚瀹氳矾寰勭殑excel鑾峰彇浜у搧缂栧彿鍜屽浘鐗囷紝涓婁紶骞朵繚瀛�
+ }
+
return r
}
--
Gitblit v1.8.0