From 4b63908ad085bc570623f7b0c0fd397b2ae7a80d Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期一, 07 八月 2023 15:41:32 +0800
Subject: [PATCH] 增加文件列表接口,文件上传时间,文件删除时间

---
 service/file.go        |    4 
 api/v1/file.go         |   73 ++++++------
 model/request/file.go  |    2 
 utils/upload/upload.go |   35 +++++
 go.mod                 |    3 
 docs/swagger.yaml      |   50 +++++---
 model/file.go          |   15 +-
 docs/docs.go           |   73 +++++++----
 router/file.go         |    1 
 docs/swagger.json      |   73 +++++++----
 10 files changed, 208 insertions(+), 121 deletions(-)

diff --git a/api/v1/file.go b/api/v1/file.go
index 1edb6b9..9fbdc78 100644
--- a/api/v1/file.go
+++ b/api/v1/file.go
@@ -8,11 +8,11 @@
 	"aps_crm/pkg/ecode"
 	"aps_crm/pkg/httpx"
 	"aps_crm/service"
-	"github.com/flipped-aurora/gin-vue-admin/server/utils/upload"
+	"aps_crm/utils/upload"
 	"github.com/gin-gonic/gin"
+	"github.com/spf13/cast"
 	"os"
 	"path/filepath"
-	"strconv"
 )
 
 type FileApi struct{}
@@ -21,7 +21,8 @@
 // @Tags		闄勪欢绠$悊
 // @Summary	娣诲姞闄勪欢
 // @Produce	application/json
-// @Param		object	body		request.AddFile	true	"鏌ヨ鍙傛暟"
+// @Param		object	formData	request.AddFile	true	"鏌ヨ鍙傛暟"
+// @Param		file	formData	file	true	"涓婁紶鏂囦欢"
 // @Success	200		{object}	contextx.Response{}
 // @Router		/api/file/add [post]
 func (s *FileApi) Add(c *gin.Context) {
@@ -44,7 +45,7 @@
 		return
 	}
 
-	_, filename := filepath.Split(filePath)
+	_, filename := filepath.Split(header.Filename)
 
 	fileRecord := &model.File{
 		Name:          filename,
@@ -80,17 +81,11 @@
 		return
 	}
 
-	id, _ := strconv.Atoi(c.Param("id"))
-
+	idx := c.Param("id")
+	id := cast.ToUint(idx)
 	file, err := model.NewFileSearch().SetId(id).First()
 	if err != nil {
 		ctx.FailWithMsg(ecode.ParamsErr, "鏌ユ壘鏂囦欢澶辫触")
-		return
-	}
-
-	err = os.Remove(file.FilePath)
-	if err != nil {
-		ctx.FailWithMsg(ecode.ParamsErr, "鍒犻櫎鏂囦欢澶辫触")
 		return
 	}
 
@@ -100,36 +95,42 @@
 		return
 	}
 
-	ctx.Ok()
-}
-
-// Update
-// @Tags		闄勪欢绠$悊
-// @Summary	鏇存柊闄勪欢
-// @Produce	application/json
-// @Param		object	body		request.UpdateFile	true	"鏌ヨ鍙傛暟"
-// @Success	200		{object}	contextx.Response{}
-// @Router		/api/file/update [put]
-func (s *FileApi) Update(c *gin.Context) {
-	var params request.UpdateFile
-	ctx, ok := contextx.NewContext(c, &params)
-	if !ok {
-		return
-	}
-	if params.Id == 0 {
-		ctx.Fail(ecode.ParamsErr)
-	}
-	params.File.Id = params.Id
-
-	errCode := service.NewFileService().UpdateFile(&params.File)
-	if errCode != ecode.OK {
-		ctx.Fail(errCode)
+	err = os.Remove(file.FilePath)
+	if err != nil {
+		ctx.FailWithMsg(ecode.ParamsErr, "鍒犻櫎鏂囦欢澶辫触")
 		return
 	}
 
 	ctx.Ok()
 }
 
+//// Update
+//// @Tags		闄勪欢绠$悊
+//// @Summary	鏇存柊闄勪欢
+//// @Produce	application/json
+//// @Param		object	body		request.UpdateFile	true	"鏌ヨ鍙傛暟"
+//// @Success	200		{object}	contextx.Response{}
+//// @Router		/api/file/update [put]
+//func (s *FileApi) Update(c *gin.Context) {
+//	var params request.UpdateFile
+//	ctx, ok := contextx.NewContext(c, &params)
+//	if !ok {
+//		return
+//	}
+//	if params.Id == 0 {
+//		ctx.Fail(ecode.ParamsErr)
+//	}
+//	params.File.ID = params.Id
+//
+//	errCode := service.NewFileService().UpdateFile(&params.File)
+//	if errCode != ecode.OK {
+//		ctx.Fail(errCode)
+//		return
+//	}
+//
+//	ctx.Ok()
+//}
+
 // List
 // @Tags		闄勪欢绠$悊
 // @Summary	鑾峰彇闄勪欢鍒楄〃
diff --git a/docs/docs.go b/docs/docs.go
index 4364ed1..93d955e 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -2503,13 +2503,25 @@
                 "summary": "娣诲姞闄勪欢",
                 "parameters": [
                     {
-                        "description": "鏌ヨ鍙傛暟",
-                        "name": "object",
-                        "in": "body",
-                        "required": true,
-                        "schema": {
-                            "$ref": "#/definitions/request.AddFile"
-                        }
+                        "type": "integer",
+                        "description": "鏉ユ簮id",
+                        "name": "sourceId",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "闄勪欢鏉ユ簮",
+                        "name": "sourceType",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "file",
+                        "description": "涓婁紶鏂囦欢",
+                        "name": "file",
+                        "in": "formData",
+                        "required": true
                     }
                 ],
                 "responses": {
@@ -8741,6 +8753,21 @@
                 "BankAccountQueryClassExpireLessThen60Days"
             ]
         },
+        "constvar.CollectionStatus": {
+            "type": "integer",
+            "enum": [
+                1,
+                2
+            ],
+            "x-enum-comments": {
+                "CollectionStatusCollected": "宸叉敹娆�",
+                "CollectionStatusUnCollected": "寰呮敹娆�"
+            },
+            "x-enum-varnames": [
+                "CollectionStatusUnCollected",
+                "CollectionStatusCollected"
+            ]
+        },
         "constvar.FaqKeywordType": {
             "type": "string",
             "enum": [
@@ -10454,7 +10481,11 @@
                 },
                 "status": {
                     "description": "鐘舵�侊紙1鏈敹2宸叉敹锛�",
-                    "type": "integer"
+                    "allOf": [
+                        {
+                            "$ref": "#/definitions/constvar.CollectionStatus"
+                        }
+                    ]
                 },
                 "term": {
                     "description": "鏈熸",
@@ -11274,23 +11305,6 @@
                 }
             }
         },
-        "request.AddFile": {
-            "type": "object",
-            "required": [
-                "sourceId",
-                "sourceType"
-            ],
-            "properties": {
-                "sourceId": {
-                    "description": "鏉ユ簮id",
-                    "type": "integer"
-                },
-                "sourceType": {
-                    "description": "闄勪欢鏉ユ簮",
-                    "type": "string"
-                }
-            }
-        },
         "request.AddFollowRecord": {
             "type": "object",
             "required": [
@@ -11840,6 +11854,9 @@
         },
         "request.AddServiceCollectionPlan": {
             "type": "object",
+            "required": [
+                "list"
+            ],
             "properties": {
                 "list": {
                     "type": "array",
@@ -14662,7 +14679,11 @@
                 },
                 "status": {
                     "description": "鐘舵�侊紙1鏈敹2宸叉敹锛�",
-                    "type": "integer"
+                    "allOf": [
+                        {
+                            "$ref": "#/definitions/constvar.CollectionStatus"
+                        }
+                    ]
                 },
                 "term": {
                     "description": "鏈熸",
diff --git a/docs/swagger.json b/docs/swagger.json
index d5c765e..930c3eb 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -2491,13 +2491,25 @@
                 "summary": "娣诲姞闄勪欢",
                 "parameters": [
                     {
-                        "description": "鏌ヨ鍙傛暟",
-                        "name": "object",
-                        "in": "body",
-                        "required": true,
-                        "schema": {
-                            "$ref": "#/definitions/request.AddFile"
-                        }
+                        "type": "integer",
+                        "description": "鏉ユ簮id",
+                        "name": "sourceId",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "闄勪欢鏉ユ簮",
+                        "name": "sourceType",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "file",
+                        "description": "涓婁紶鏂囦欢",
+                        "name": "file",
+                        "in": "formData",
+                        "required": true
                     }
                 ],
                 "responses": {
@@ -8729,6 +8741,21 @@
                 "BankAccountQueryClassExpireLessThen60Days"
             ]
         },
+        "constvar.CollectionStatus": {
+            "type": "integer",
+            "enum": [
+                1,
+                2
+            ],
+            "x-enum-comments": {
+                "CollectionStatusCollected": "宸叉敹娆�",
+                "CollectionStatusUnCollected": "寰呮敹娆�"
+            },
+            "x-enum-varnames": [
+                "CollectionStatusUnCollected",
+                "CollectionStatusCollected"
+            ]
+        },
         "constvar.FaqKeywordType": {
             "type": "string",
             "enum": [
@@ -10442,7 +10469,11 @@
                 },
                 "status": {
                     "description": "鐘舵�侊紙1鏈敹2宸叉敹锛�",
-                    "type": "integer"
+                    "allOf": [
+                        {
+                            "$ref": "#/definitions/constvar.CollectionStatus"
+                        }
+                    ]
                 },
                 "term": {
                     "description": "鏈熸",
@@ -11262,23 +11293,6 @@
                 }
             }
         },
-        "request.AddFile": {
-            "type": "object",
-            "required": [
-                "sourceId",
-                "sourceType"
-            ],
-            "properties": {
-                "sourceId": {
-                    "description": "鏉ユ簮id",
-                    "type": "integer"
-                },
-                "sourceType": {
-                    "description": "闄勪欢鏉ユ簮",
-                    "type": "string"
-                }
-            }
-        },
         "request.AddFollowRecord": {
             "type": "object",
             "required": [
@@ -11828,6 +11842,9 @@
         },
         "request.AddServiceCollectionPlan": {
             "type": "object",
+            "required": [
+                "list"
+            ],
             "properties": {
                 "list": {
                     "type": "array",
@@ -14650,7 +14667,11 @@
                 },
                 "status": {
                     "description": "鐘舵�侊紙1鏈敹2宸叉敹锛�",
-                    "type": "integer"
+                    "allOf": [
+                        {
+                            "$ref": "#/definitions/constvar.CollectionStatus"
+                        }
+                    ]
                 },
                 "term": {
                     "description": "鏈熸",
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index a70480d..de6c84e 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -11,6 +11,17 @@
     type: string
     x-enum-varnames:
     - BankAccountQueryClassExpireLessThen60Days
+  constvar.CollectionStatus:
+    enum:
+    - 1
+    - 2
+    type: integer
+    x-enum-comments:
+      CollectionStatusCollected: 宸叉敹娆�
+      CollectionStatusUnCollected: 寰呮敹娆�
+    x-enum-varnames:
+    - CollectionStatusUnCollected
+    - CollectionStatusCollected
   constvar.FaqKeywordType:
     enum:
     - ""
@@ -1173,8 +1184,9 @@
         description: 婧愬崟绫诲瀷锛�1閿�鍞槑缁�2鏈嶅姟鍚堝悓3閿�鍞彂绁級
         type: integer
       status:
+        allOf:
+        - $ref: '#/definitions/constvar.CollectionStatus'
         description: 鐘舵�侊紙1鏈敹2宸叉敹锛�
-        type: integer
       term:
         description: 鏈熸
         type: integer
@@ -1731,18 +1743,6 @@
       name:
         type: string
     type: object
-  request.AddFile:
-    properties:
-      sourceId:
-        description: 鏉ユ簮id
-        type: integer
-      sourceType:
-        description: 闄勪欢鏉ユ簮
-        type: string
-    required:
-    - sourceId
-    - sourceType
-    type: object
   request.AddFollowRecord:
     properties:
       follow_record:
@@ -2110,6 +2110,8 @@
         items:
           $ref: '#/definitions/model.ServiceCollectionPlan'
         type: array
+    required:
+    - list
     type: object
   request.AddServiceContract:
     properties:
@@ -4022,8 +4024,9 @@
         description: 婧愬崟绫诲瀷锛�1閿�鍞槑缁�2鏈嶅姟鍚堝悓3閿�鍞彂绁級
         type: integer
       status:
+        allOf:
+        - $ref: '#/definitions/constvar.CollectionStatus'
         description: 鐘舵�侊紙1鏈敹2宸叉敹锛�
-        type: integer
       term:
         description: 鏈熸
         type: integer
@@ -6518,12 +6521,21 @@
   /api/file/add:
     post:
       parameters:
-      - description: 鏌ヨ鍙傛暟
-        in: body
-        name: object
+      - description: 鏉ユ簮id
+        in: formData
+        name: sourceId
         required: true
-        schema:
-          $ref: '#/definitions/request.AddFile'
+        type: integer
+      - description: 闄勪欢鏉ユ簮
+        in: formData
+        name: sourceType
+        required: true
+        type: string
+      - description: 涓婁紶鏂囦欢
+        in: formData
+        name: file
+        required: true
+        type: file
       produces:
       - application/json
       responses:
diff --git a/go.mod b/go.mod
index 2d77042..b9fcfe1 100644
--- a/go.mod
+++ b/go.mod
@@ -16,6 +16,7 @@
 	github.com/mojocn/base64Captcha v1.3.5
 	github.com/pkg/errors v0.9.1
 	github.com/robfig/cron/v3 v3.0.1
+	github.com/shopspring/decimal v1.3.1
 	github.com/songzhibin97/gkit v1.2.11
 	github.com/spf13/viper v1.16.0
 	github.com/stretchr/testify v1.8.4
@@ -31,7 +32,6 @@
 	gorm.io/driver/mysql v1.5.1
 	gorm.io/gorm v1.25.1
 	moul.io/zapgorm2 v1.3.0
-	github.com/shopspring/decimal v1.3.1
 )
 
 require (
@@ -97,7 +97,6 @@
 	github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
 	github.com/rogpeppe/go-internal v1.10.0 // indirect
 	github.com/shirou/gopsutil/v3 v3.22.5 // indirect
-	github.com/shopspring/decimal v1.3.1 // indirect
 	github.com/spf13/afero v1.9.5 // indirect
 	github.com/spf13/cast v1.5.1 // indirect
 	github.com/spf13/jwalterweatherman v1.1.0 // indirect
diff --git a/model/file.go b/model/file.go
index 3b919f3..bd1c46a 100644
--- a/model/file.go
+++ b/model/file.go
@@ -11,7 +11,6 @@
 type (
 	// File 闄勪欢
 	File struct {
-		Id            int    `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
 		Name          string `gorm:"name" json:"name"`
 		Size          int64  `gorm:"size" json:"size"`                    // 鏂囦欢澶у皬
 		FilePath      string `gorm:"file_path" json:"filePath"`           // 鏂囦欢璺緞
@@ -22,9 +21,7 @@
 		FileType      string `gorm:"file_type" json:"fileType"`           // 鏂囦欢绫诲瀷
 		SourceType    string `gorm:"source_type" json:"sourceType"`       // 闄勪欢鏉ユ簮
 		SourceId      int    `gorm:"source_id" json:"sourceId"`           // 鏉ユ簮id
-		CreateTime    string `gorm:"create_time" json:"createTime"`       // 鍒涘缓鏃堕棿
-		UpdateTime    string `gorm:"update_time" json:"updateTime"`
-		Content       string `gorm:"content" json:"content"` // 鏂囦欢鍐呭
+		gorm.Model    `json:"-"`
 	}
 
 	// FileSearch 闄勪欢鎼滅储鏉′欢
@@ -51,8 +48,8 @@
 
 func (slf *FileSearch) build() *gorm.DB {
 	var db = slf.Orm.Model(&File{})
-	if slf.Id != 0 {
-		db = db.Where("id = ?", slf.Id)
+	if slf.ID != 0 {
+		db = db.Where("id = ?", slf.ID)
 	}
 
 	return db
@@ -85,8 +82,8 @@
 	return record, err
 }
 
-func (slf *FileSearch) SetId(id int) *FileSearch {
-	slf.Id = id
+func (slf *FileSearch) SetId(id uint) *FileSearch {
+	slf.ID = id
 	return slf
 }
 
@@ -108,7 +105,7 @@
 }
 
 func (slf *FileSearch) Save(record *File) error {
-	if record.Id == 0 {
+	if record.ID == 0 {
 		return errors.New("id涓虹┖")
 	}
 	var db = slf.build()
diff --git a/model/request/file.go b/model/request/file.go
index 5b17b6b..096675b 100644
--- a/model/request/file.go
+++ b/model/request/file.go
@@ -11,7 +11,7 @@
 }
 
 type UpdateFile struct {
-	Id int `json:"id"`
+	Id uint `json:"id"`
 	model.File
 }
 
diff --git a/router/file.go b/router/file.go
index 60757b4..3946e5d 100644
--- a/router/file.go
+++ b/router/file.go
@@ -11,5 +11,6 @@
 	{
 		FileRouter.POST("add", FileApi.Add)             // 娣诲姞闄勪欢
 		FileRouter.DELETE("delete/:id", FileApi.Delete) // 鍒犻櫎闄勪欢
+		FileRouter.GET("list", FileApi.List)            // 闄勪欢鍒楄〃
 	}
 }
diff --git a/service/file.go b/service/file.go
index af78875..584aa11 100644
--- a/service/file.go
+++ b/service/file.go
@@ -22,7 +22,7 @@
 	return ecode.OK
 }
 
-func (FileService) DeleteFile(id int) int {
+func (FileService) DeleteFile(id uint) int {
 	err := model.NewFileSearch().SetId(id).Delete()
 	if err != nil {
 		return ecode.DBErr
@@ -57,7 +57,7 @@
 }
 
 func (FileService) UpdateFile(file *model.File) int {
-	err := model.NewFileSearch().SetId(file.Id).Save(file)
+	err := model.NewFileSearch().SetId(file.ID).Save(file)
 	if err != nil {
 		return ecode.DBErr
 	}
diff --git a/utils/upload/upload.go b/utils/upload/upload.go
new file mode 100644
index 0000000..a6823ac
--- /dev/null
+++ b/utils/upload/upload.go
@@ -0,0 +1,35 @@
+package upload
+
+import (
+	"mime/multipart"
+)
+
+// OSS 瀵硅薄瀛樺偍鎺ュ彛
+// Author [SliverHorn](https://github.com/SliverHorn)
+// Author [ccfish86](https://github.com/ccfish86)
+type OSS interface {
+	UploadFile(file *multipart.FileHeader) (string, string, error)
+	DeleteFile(key string) error
+}
+
+type ossType string
+
+const (
+	ossTypeLocal   ossType = "local"
+	currentOssType         = ossTypeLocal
+)
+
+// NewOss OSS鐨勫疄渚嬪寲鏂规硶
+// Author [SliverHorn](https://github.com/SliverHorn)
+// Author [ccfish86](https://github.com/ccfish86)
+func NewOss() OSS {
+	switch currentOssType {
+	case ossTypeLocal:
+		return &Local{
+			StorePath: "/data/uploads/file",
+			Path:      "/data/uploads/file",
+		}
+	default:
+		return &Local{}
+	}
+}

--
Gitblit v1.8.0