Browse Source

add: 移动图表

hsun 3 years ago
parent
commit
534e3d3c32

+ 139 - 2
controller/chart/my_chart.go

@@ -4,10 +4,16 @@ import (
 	"github.com/gin-gonic/gin"
 	"hongze/hongze_yb/controller/response"
 	"hongze/hongze_yb/models/response/my_chart"
-	my_chart2 "hongze/hongze_yb/models/tables/my_chart"
+	myChartModel "hongze/hongze_yb/models/tables/my_chart"
+	"hongze/hongze_yb/models/tables/my_chart_classify"
+	myChartClassifyMappingModel "hongze/hongze_yb/models/tables/my_chart_classify_mapping"
+	myChartLogModel "hongze/hongze_yb/models/tables/my_chart_log"
 	"hongze/hongze_yb/services/chart"
 	"hongze/hongze_yb/services/user"
+	"hongze/hongze_yb/utils"
+	"io/ioutil"
 	"strconv"
+	"time"
 )
 
 // GetMyChartChassify 获取图表分类列表
@@ -111,7 +117,7 @@ func GetMyChartList(c *gin.Context) {
 		limit = 10
 	}
 
-	list, err := my_chart2.GetMyChartListByCondition(condition, pars, page, limit)
+	list, err := myChartModel.GetMyChartListByCondition(condition, pars, page, limit)
 	if err != nil {
 		response.Fail("获取图库列表失败, Err:" + err.Error(), c)
 		return
@@ -120,6 +126,137 @@ func GetMyChartList(c *gin.Context) {
 	response.OkData("获取成功", list, c)
 }
 
+// MoveMyChart 移动我的图表
+// @Tags 图库模块
+// @Summary  移动我的图表
+// @Description 移动我的图表
+// @Security ApiKeyAuth
+// @Param Authorization	header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
+// @Accept  json
+// @Product json
+// @Param data body myChartModel.MoveMyChartReq true "请求参数"
+// @Success 200 {string} string "操作成功"
+// @failure 400 {string} string "操作失败"
+// @Router /my_chart/moveMyChart [post]
 func MoveMyChart(c *gin.Context) {
+	// 参数校验
+	var req myChartModel.MoveMyChartReq
+	if c.ShouldBind(&req) != nil {
+		response.Fail("参数异常", c)
+		return
+	}
+	if req.MyChartId == 0 {
+		response.Fail("图表ID有误", c)
+		return
+	}
+	if req.MyChartClassifyId == 0 {
+		response.Fail("图表分类ID有误", c)
+		return
+	}
+
+	// 操作权限校验
+	userInfo := user.GetInfoByClaims(c)
+	ok, adminInfo, err := user.GetAdminByUserInfo(userInfo)
+	if err != nil {
+		response.Fail("操作人信息有误", c)
+		return
+	}
+	if !ok {
+		response.Fail("非内部人员无权进行操作", c)
+		return
+	}
+	adminId := int(adminInfo.AdminID)
+	myChassify, err := my_chart_classify.GetClassifyById(req.MyChartClassifyId)
+	if err != nil {
+		if err == utils.ErrNoRow {
+			response.Fail("该图表分类信息有误", c)
+			return
+		}
+		response.Fail("获取图表信息失败", c)
+		return
+	}
+	if myChassify.AdminID != adminId {
+		response.Fail("无权移动该图表", c)
+		return
+	}
+
+	// 图表排序
+	step := float64(65536)
+	var newSort float64
+	if req.PrevMyChartId <= 0 {
+		firstMapItem, err := myChartClassifyMappingModel.GetMyChartSort(adminId, req.MyChartClassifyId, 0)
+		if err != nil {
+			response.Fail("获取图表排序失败4001", c)
+			return
+		}
+		if firstMapItem.Sort <= 0 {
+			firstSort := step / float64(2)
+			err = myChartClassifyMappingModel.MyChartMove(firstSort, adminId, firstMapItem.MyChartId, firstMapItem.MyChartClassifyId)
+			if err != nil {
+				response.Fail("移动失败", c)
+				return
+			}
+			newSort = step / float64(4)
+		} else {
+			newSort = firstMapItem.Sort / float64(2)
+		}
+	} else if req.NextMyChartId <= 0 {
+		latestMapItem, err := myChartClassifyMappingModel.GetMyChartSort(adminId, req.MyChartClassifyId, 1)
+		if err != nil {
+			response.Fail("获取图表排序失败4002", c)
+			return
+		}
+		if latestMapItem.Sort <= 0 {
+			latestSort := step / float64(2)
+			err = myChartClassifyMappingModel.MyChartMove(latestSort, adminId, latestMapItem.MyChartId, latestMapItem.MyChartClassifyId)
+			if err != nil {
+				response.Fail("移动失败", c)
+				return
+			}
+			newSort = step / float64(4)
+		} else {
+			newSort = latestMapItem.Sort + (step / float64(2)) - 1
+		}
+	} else {
+		preMapItem, err := myChartClassifyMappingModel.GetMyChartClassifyMapping(adminId, req.PrevMyChartId, req.MyChartClassifyId)
+		if err != nil {
+			response.Fail("获取上级图表关联信息失败", c)
+			return
+		}
+		nextMapItem, err := myChartClassifyMappingModel.GetMyChartClassifyMapping(adminId, req.NextMyChartId, req.MyChartClassifyId)
+		if err != nil {
+			response.Fail("获取下级图表关联信息失败", c)
+			return
+		}
+		newSort = (preMapItem.Sort + nextMapItem.Sort) / 2
+	}
+	if newSort >= 0 {
+		err = myChartClassifyMappingModel.MyChartMove(newSort, adminId, req.MyChartId, req.MyChartClassifyId)
+		if err != nil {
+			response.Fail("移动失败", c)
+			return
+		}
+	}
+	/*else {
+		utils.FileLog.Info("MyChartMove Sort=0:" + requestBodyStr)
+	}*/
+
+	// 新增操作日志
+	{
+		bodyBytes, _ := ioutil.ReadAll(c.Request.Body)
+		requestBodyStr := string(bodyBytes)
+
+		edbLog := new(myChartLogModel.MyChartLog)
+		edbLog.MyChartId = 0
+		edbLog.ChartInfoId = 0
+		edbLog.SysUserId = adminId
+		edbLog.SysUserRealName = adminInfo.RealName
+		edbLog.CreateTime = time.Now()
+		edbLog.Content = requestBodyStr
+		edbLog.Status = "移动我的图表"
+		edbLog.Method = c.Request.URL.String()
+		go edbLog.Create()
+	}
 
+	response.Ok("操作成功", c)
 }

+ 69 - 0
docs/docs.go

@@ -669,6 +669,55 @@ var doc = `{
                 }
             }
         },
+        "/my_chart/moveMyChart": {
+            "post": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "description": "移动我的图表",
+                "consumes": [
+                    "application/json"
+                ],
+                "tags": [
+                    "图库模块"
+                ],
+                "summary": "移动我的图表",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "Bearer 31a165baebe6dec616b1f8f3207b4273",
+                        "name": "Authorization",
+                        "in": "header",
+                        "required": true
+                    },
+                    {
+                        "description": "请求参数",
+                        "name": "data",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/my_chart.MoveMyChartReq"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "操作成功",
+                        "schema": {
+                            "type": "string"
+                        }
+                    },
+                    "400": {
+                        "description": "操作失败",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/public/get_apply_variety_list": {
             "get": {
                 "security": [
@@ -1655,6 +1704,23 @@ var doc = `{
                 }
             }
         },
+        "my_chart.MoveMyChartReq": {
+            "type": "object",
+            "properties": {
+                "myChartClassifyId": {
+                    "type": "integer"
+                },
+                "myChartId": {
+                    "type": "integer"
+                },
+                "nextMyChartId": {
+                    "type": "integer"
+                },
+                "prevMyChartId": {
+                    "type": "integer"
+                }
+            }
+        },
         "my_chart.MyChartClassifyListResp": {
             "type": "object",
             "properties": {
@@ -1774,6 +1840,9 @@ var doc = `{
                 "modifyTime": {
                     "type": "string"
                 },
+                "myChartClassifyId": {
+                    "type": "integer"
+                },
                 "myChartClassifyName": {
                     "description": "分类名称",
                     "type": "string"

+ 69 - 0
docs/swagger.json

@@ -655,6 +655,55 @@
                 }
             }
         },
+        "/my_chart/moveMyChart": {
+            "post": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "description": "移动我的图表",
+                "consumes": [
+                    "application/json"
+                ],
+                "tags": [
+                    "图库模块"
+                ],
+                "summary": "移动我的图表",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "Bearer 31a165baebe6dec616b1f8f3207b4273",
+                        "name": "Authorization",
+                        "in": "header",
+                        "required": true
+                    },
+                    {
+                        "description": "请求参数",
+                        "name": "data",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "$ref": "#/definitions/my_chart.MoveMyChartReq"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "操作成功",
+                        "schema": {
+                            "type": "string"
+                        }
+                    },
+                    "400": {
+                        "description": "操作失败",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/public/get_apply_variety_list": {
             "get": {
                 "security": [
@@ -1641,6 +1690,23 @@
                 }
             }
         },
+        "my_chart.MoveMyChartReq": {
+            "type": "object",
+            "properties": {
+                "myChartClassifyId": {
+                    "type": "integer"
+                },
+                "myChartId": {
+                    "type": "integer"
+                },
+                "nextMyChartId": {
+                    "type": "integer"
+                },
+                "prevMyChartId": {
+                    "type": "integer"
+                }
+            }
+        },
         "my_chart.MyChartClassifyListResp": {
             "type": "object",
             "properties": {
@@ -1760,6 +1826,9 @@
                 "modifyTime": {
                     "type": "string"
                 },
+                "myChartClassifyId": {
+                    "type": "integer"
+                },
                 "myChartClassifyName": {
                     "description": "分类名称",
                     "type": "string"

+ 44 - 0
docs/swagger.yaml

@@ -323,6 +323,17 @@ definitions:
       pic_url:
         type: string
     type: object
+  my_chart.MoveMyChartReq:
+    properties:
+      myChartClassifyId:
+        type: integer
+      myChartId:
+        type: integer
+      nextMyChartId:
+        type: integer
+      prevMyChartId:
+        type: integer
+    type: object
   my_chart.MyChartClassifyListResp:
     properties:
       private_classify:
@@ -403,6 +414,8 @@ definitions:
         type: integer
       modifyTime:
         type: string
+      myChartClassifyId:
+        type: integer
       myChartClassifyName:
         description: 分类名称
         type: string
@@ -1048,6 +1061,37 @@ paths:
       summary: 获取图表详情
       tags:
       - 图库模块
+  /my_chart/moveMyChart:
+    post:
+      consumes:
+      - application/json
+      description: 移动我的图表
+      parameters:
+      - description: Bearer 31a165baebe6dec616b1f8f3207b4273
+        in: header
+        name: Authorization
+        required: true
+        type: string
+      - description: 请求参数
+        in: body
+        name: data
+        required: true
+        schema:
+          $ref: '#/definitions/my_chart.MoveMyChartReq'
+      responses:
+        "200":
+          description: 操作成功
+          schema:
+            type: string
+        "400":
+          description: 操作失败
+          schema:
+            type: string
+      security:
+      - ApiKeyAuth: []
+      summary: 移动我的图表
+      tags:
+      - 图库模块
   /public/get_apply_variety_list:
     get:
       consumes:

+ 7 - 0
models/tables/my_chart/query.go

@@ -41,6 +41,13 @@ type MyChartView struct {
 	MyChartClassifyId string
 }
 
+type MoveMyChartReq struct {
+	MyChartId         int `description:"移动的,我的图表图表ID"`
+	PrevMyChartId     int `description:"移动到的位置,上一级的,我的图表ID,如果没有上一级,则传0"`
+	NextMyChartId     int `description:"移动到的位置,下一级的,我的图表ID,如果没有下一级,则传0"`
+	MyChartClassifyId int `description:"当前分类id"`
+}
+
 // GetMyChartListByCondition 条件获取我的图表列表
 func GetMyChartListByCondition(condition string, pars []interface{}, page, limit int) (list []*MyChartList, err error) {
 	sql := ` SELECT a.my_chart_id,c.sort,b.* FROM my_chart AS a

+ 7 - 0
models/tables/my_chart_classify/query.go

@@ -17,3 +17,10 @@ func GetClassifyListByCondition(condition map[string]interface{}) (list []*MyCha
 
 	return
 }
+
+// GetClassifyById 根据主键获取图表分类信息
+func GetClassifyById(chartClassifyId int) (item *MyChartClassify, err error) {
+	err = global.MYSQL["data"].Model(MyChartClassify{}).Where("my_chart_classify_id = ?", chartClassifyId).First(&item).Error
+
+	return
+}

+ 5 - 5
models/tables/my_chart_classify_mapping/my_chart_classify_mapping.go

@@ -2,12 +2,12 @@ package my_chart_classify_mapping
 
 import "time"
 
-// MyChartClassifyMapping 我的图及分类关联表
+// MyChartClassifyMapping 我的图及分类关联表
 type MyChartClassifyMapping struct {
-	MyChartClassifyMappingID int       `gorm:"primaryKey;column:my_chart_classify_mapping_id;type:int(11);not null" json:"-"`
-	AdminID                  int       `gorm:"column:admin_id;type:int(11)" json:"adminId"`
-	MyChartID                int       `gorm:"column:my_chart_id;type:int(11)" json:"myChartId"`
-	MyChartClassifyID        int       `gorm:"column:my_chart_classify_id;type:int(11)" json:"myChartClassifyId"`
+	MyChartClassifyMappingId int       `gorm:"primaryKey;column:my_chart_classify_mapping_id;type:int(11);not null" json:"myChartClassifyMappingId"`
+	AdminId                  int       `gorm:"column:admin_id;type:int(11)" json:"adminId"`
+	MyChartId                int       `gorm:"column:my_chart_id;type:int(11)" json:"myChartId"`
+	MyChartClassifyId        int       `gorm:"column:my_chart_classify_id;type:int(11)" json:"myChartClassifyId"`
 	CreateTime               time.Time `gorm:"column:create_time;type:datetime" json:"createTime"`
 	ModifyTime               time.Time `gorm:"column:modify_time;type:datetime" json:"modifyTime"`
 	Sort                     float64   `gorm:"column:sort;type:double;default:0" json:"sort"` // 排序

+ 24 - 0
models/tables/my_chart_classify_mapping/query.go

@@ -0,0 +1,24 @@
+package my_chart_classify_mapping
+
+import "hongze/hongze_yb/global"
+
+// GetMyChartSort 获取图表排序
+func GetMyChartSort(adminId, myChartClassifyId, sort int) (item *MyChartClassifyMapping, err error) {
+	sql := ` SELECT * FROM my_chart_classify_mapping WHERE 1=1 AND admin_id = ? AND my_chart_classify_id = ? `
+	if sort == 1 {
+		sql += ` ORDER BY sort DESC LIMIT 1 `
+	} else {
+		sql += ` ORDER BY sort ASC LIMIT 1 `
+	}
+	err = global.MYSQL["data"].Raw(sql, adminId, myChartClassifyId).First(&item).Error
+
+	return
+}
+
+// GetMyChartClassifyMapping 获取图表分类关联信息
+func GetMyChartClassifyMapping(adminId, myChartId, myChartClassifyId int) (item *MyChartClassifyMapping, err error) {
+	sql := `SELECT * FROM my_chart_classify_mapping WHERE admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
+	err = global.MYSQL["data"].Raw(sql, adminId, myChartId, myChartClassifyId).First(&item).Error
+
+	return
+}

+ 11 - 0
models/tables/my_chart_classify_mapping/update.go

@@ -0,0 +1,11 @@
+package my_chart_classify_mapping
+
+import "hongze/hongze_yb/global"
+
+// MyChartMove 移动我的图表
+func MyChartMove(sort float64, adminId int, myChartId, myChartClassifyId int) (err error) {
+	sql := ` UPDATE my_chart_classify_mapping SET sort = ?,modify_time=NOW() WHERE admin_id=? AND my_chart_id=? AND my_chart_classify_id=? `
+	err = global.MYSQL["data"].Exec(sql, sort, adminId, myChartId, myChartClassifyId).Error
+
+	return
+}

+ 10 - 0
models/tables/my_chart_log/create.go

@@ -0,0 +1,10 @@
+package my_chart_log
+
+import "hongze/hongze_yb/global"
+
+func (myCharLogInfo *MyChartLog) Create() (myChartInfoLogId int, err error) {
+	err = global.MYSQL["data"].Create(myCharLogInfo).Error
+	myChartInfoLogId = myCharLogInfo.MyChartLogId
+	return
+}
+

+ 4 - 4
models/tables/my_chart_log/my_chart_log.go

@@ -6,10 +6,10 @@ import (
 
 // MyChartLog 我的图表操作日志
 type MyChartLog struct {
-	MyChartLogID    int       `gorm:"primaryKey;column:my_chart_log_id;type:int(11);not null" json:"-"`
-	MyChartID       int       `gorm:"index:idx_my_chart_id;column:my_chart_id;type:int(11)" json:"myChartId"`
-	ChartInfoID     int       `gorm:"column:chart_info_id;type:int(11)" json:"chartInfoId"`
-	SysUserID       int       `gorm:"column:sys_user_id;type:int(11)" json:"sysUserId"`
+	MyChartLogId    int       `gorm:"primaryKey;column:my_chart_log_id;type:int(11);not null" json:"myChartLogId"`
+	MyChartId       int       `gorm:"index:idx_my_chart_id;column:my_chart_id;type:int(11)" json:"myChartId"`
+	ChartInfoId     int       `gorm:"column:chart_info_id;type:int(11)" json:"chartInfoId"`
+	SysUserId       int       `gorm:"column:sys_user_id;type:int(11)" json:"sysUserId"`
 	CreateTime      time.Time `gorm:"column:create_time;type:datetime" json:"createTime"`
 	SysUserRealName string    `gorm:"column:sys_user_real_name;type:varchar(50);default:''" json:"sysUserRealName"`
 	Content         string    `gorm:"column:content;type:text" json:"content"`

+ 1 - 0
routers/chart.go

@@ -11,6 +11,7 @@ func InitChart(r *gin.Engine) {
 	{
 		rGroup.GET("/getChartChassify", chart.GetMyChartChassify)
 		rGroup.GET("/getChartList", chart.GetMyChartList)
+		rGroup.POST("/moveMyChart", chart.MoveMyChart)
 		rGroup.GET("/getChartDetail", chart.GetChartInfoDetail)
 		rGroup.GET("/editChartInfo", chart.EditChartInfo)
 	}