瀏覽代碼

Merge branch 'feature/eta2.2.6_material' into debug

xyxie 4 月之前
父節點
當前提交
adfd75b9fb
共有 4 個文件被更改,包括 397 次插入80 次删除
  1. 157 66
      controllers/material/material.go
  2. 16 4
      models/material/material.go
  3. 13 4
      routers/commentsRouter.go
  4. 211 6
      services/material/material.go

+ 157 - 66
controllers/material/material.go

@@ -870,13 +870,13 @@ func (this *MaterialController) List() {
 	br.Data = resp
 }
 
-// Save
-// @Title 新增/编辑保存素材库
-// @Description 新增/编辑保存素材库接口
+// SaveAsMaterial
+// @Title 将图表等封面上传至素材库
+// @Description 将图表等封面上传至素材库
 // @Param	request	body material.AddAndEditSandbox true "type json string"
 // @Success 200 {object} material.Material
-// @router /save [post]
-func (this *MaterialController) Save() {
+// @router /saveAs [post]
+func (this *MaterialController) SaveAsMaterial() {
 	br := new(models.BaseResponse).Init()
 	defer func() {
 		this.Data["json"] = br
@@ -889,60 +889,153 @@ func (this *MaterialController) Save() {
 		br.Ret = 408
 		return
 	}
-	var req material.AddAndEditMaterial
+	var req material.SaveAsMaterialReq
 	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
 	if err != nil {
 		br.Msg = "参数解析异常!"
 		br.ErrMsg = "参数解析失败,Err:" + err.Error()
 		return
 	}
+	if req.MaterialName == "" {
+		br.Msg = "缺少素材库名称"
+		return
+	}
+	var exist *material.Material
+	switch this.Lang {
+	case utils.LANG_EN:
+		exist, err = material.GetMaterialByNameEn(req.MaterialName)
+	default:
+		// 判断名称是否重复
+		exist, err = material.GetMaterialByName(req.MaterialName)
+	}
+	if err != nil && err.Error() != utils.ErrNoRow() {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取数据失败,Err:" + err.Error()
+		return
+	}
+	if err == nil && exist.MaterialId > 0 {
+		br.Msg = "图片名称已存在"
+		return
+	}
+	err, errMsg := materialService.AddToMaterial(req, sysUser.AdminId, sysUser.RealName)
+	if err != nil {
+		br.Msg = "保存失败!"
+		if errMsg != `` {
+			br.Msg = errMsg
+		}
+		br.ErrMsg = "保存失败,Err:" + err.Error()
+		return
+	}
 
-	var materialResp *material.MaterialSaveResp
-
-	var errMsg string
+	msg := "保存成功"
+	br.Ret = 200
+	br.Success = true
+	br.Msg = msg
+}
 
-	if req.MaterialId <= 0 {
-		//新增素材库
-		materialResp, err = materialService.AddMaterial(req, sysUser.AdminId, sysUser.RealName)
+// MyChartSaveAsMaterial
+// @Title 将我的图表等封面上传至素材库
+// @Description 将图表等封面上传至素材库
+// @Param	request	body material.AddAndEditSandbox true "type json string"
+// @Success 200 {object} material.Material
+// @router /my_chart/saveAs [post]
+func (this *MaterialController) MyChartSaveAsMaterial() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	sysUser := this.SysUser
+	if sysUser == nil {
+		br.Msg = "请登录"
+		br.ErrMsg = "请登录,SysUser Is Empty"
+		br.Ret = 408
+		return
+	}
+	var req material.MyChartSaveAsMaterialReq
+	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
+	if err != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + err.Error()
+		return
+	}
+	materialNames := make([]string, 0)
+	for _, v := range req.MaterialList {
+		if v.ClassifyId <= 0 {
+			br.Msg = "请选择分类"
+			return
+		}
+		if v.MaterialName == "" {
+			br.Msg = "请填写图片名称"
+			return
+		}
+		if v.ChartInfoId <= 0 {
+			br.Msg = "请选择图表"
+			return
+		}
+		if v.MyChartId <= 0 {
+			br.Msg = "请选择我的图表"
+			return
+		}
+		materialNames = append(materialNames, v.MaterialName)
+	}
+	if len(materialNames) == 0 {
+		br.Msg = "请填写图片名称"
+		return
+	}
+	existList := make([]*material.Material, 0)
+	switch this.Lang {
+	case utils.LANG_EN:
+		existList, err = material.GetMaterialByNameEns(materialNames)
 		if err != nil {
-			br.Msg = "保存失败!"
-			if errMsg != `` {
-				br.Msg = errMsg
-			}
-			br.ErrMsg = "保存失败,Err:" + err.Error()
+			br.Msg = "获取失败"
+			br.ErrMsg = "获取数据失败,Err:" + err.Error()
 			return
 		}
-	} else {
-		//编辑素材库
-		materialInfo := &material.Material{
-			MaterialId:   req.MaterialId,
-			MaterialName: utils.TrimStr(req.MaterialName),
-			ImgUrl:       utils.TrimStr(req.ImgUrl),
-			ModifyTime:   time.Now(),
-			ClassifyId:   req.ClassifyId,
-		}
-		//缩略图为空时不更新
-		var updateSandboxColumn = []string{}
-		if req.ImgUrl == "" {
-			updateSandboxColumn = []string{"Name", "Content", "MindmapData", "ModifyTime", "MaterialClassifyId", "Style"}
-		} else {
-			updateSandboxColumn = []string{"Name", "Content", "MindmapData", "ImgUrl", "ModifyTime", "MaterialClassifyId", "Style"}
+		if len(existList) > 0 {
+			msg := "图片名称:"
+			for _, v := range existList {
+				msg += v.MaterialNameEn + " "
+			}
+			br.Msg = fmt.Sprintf("%s 已存在", msg)
+			return
 		}
-		err = materialInfo.Update(updateSandboxColumn)
+	default:
+		// 判断文件名是否已存在
+		existList, err = material.GetMaterialByNames(materialNames)
 		if err != nil {
-			br.Msg = "保存失败!"
-			if errMsg != `` {
-				br.Msg = errMsg
+			br.Msg = "获取失败"
+			br.ErrMsg = "获取数据失败,Err:" + err.Error()
+			return
+		}
+		if len(existList) > 0 {
+			msg := "图片名称:"
+			for _, v := range existList {
+				msg += v.MaterialName + " "
 			}
-			br.ErrMsg = "保存失败,Err:" + err.Error()
+			br.Msg = fmt.Sprintf("%s 已存在", msg)
 			return
 		}
 	}
+	if len(req.MaterialList) > 30 {
+		br.Msg = "最多支持选择30个图表"
+		return
+	}
+
+	err, errMsg := materialService.MyChartAddToMaterial(req, sysUser.AdminId, sysUser.RealName)
+	if err != nil {
+		br.Msg = "保存失败!"
+		if errMsg != `` {
+			br.Msg = errMsg
+		}
+		br.ErrMsg = "保存失败,Err:" + err.Error()
+		return
+	}
+
 	msg := "保存成功"
 	br.Ret = 200
 	br.Success = true
 	br.Msg = msg
-	br.Data = materialResp
 }
 
 // Delete
@@ -1570,7 +1663,7 @@ func (this *MaterialController) Rename() {
 // @Description 批量下载素材
 // @Param	request	body material.DeleteSandbox true "type json string"
 // @Success 200 标记成功
-// @router /batch/download [post]
+// @router /batch/download [get]
 func (this *MaterialController) BatchDownload() {
 	br := new(models.BaseResponse).Init()
 	defer func() {
@@ -1584,19 +1677,17 @@ func (this *MaterialController) BatchDownload() {
 		br.Ret = 408
 		return
 	}
-	var req material.BatchDeleteMaterialReq
-	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
-	if err != nil {
-		br.Msg = "参数解析异常!"
-		br.ErrMsg = "参数解析失败,Err:" + err.Error()
-		return
-	}
+	//var req material.BatchDeleteMaterialReq
+	materialIdStr := this.GetString("MaterialIds")
+	isSelectAll, _ := this.GetBool("IsSelectAll")
+	classifyId, _ := this.GetInt("ClassifyId")
+	keyword := this.GetString("Keyword")
+	isShowMe, _ := this.GetBool("IsShowMe")
 
 	downMaterialList := make([]*material.MaterialListItems, 0)
-	if req.IsSelectAll {
-		classifyId := req.ClassifyId
-		keyword := req.Keyword
-		isShowMe := req.IsShowMe
+	materialIds := strings.Split(materialIdStr, ",")
+	var err error
+	if isSelectAll {
 		//获取图表信息
 		list, e, msg := materialService.GetBatchSelectedMaterialList(classifyId, keyword, isShowMe, sysUser)
 		if e != nil {
@@ -1608,9 +1699,11 @@ func (this *MaterialController) BatchDownload() {
 			return
 		}
 		notSelectIds := make(map[int]struct{})
-		if len(req.MaterialIds) >= 0 {
-			for _, v := range req.MaterialIds {
-				notSelectIds[v] = struct{}{}
+		if len(materialIds) >= 0 {
+			//转成数组
+			for _, v := range materialIds {
+				id, _ := strconv.Atoi(v)
+				notSelectIds[id] = struct{}{}
 			}
 		}
 		for _, v := range list {
@@ -1619,9 +1712,14 @@ func (this *MaterialController) BatchDownload() {
 			}
 		}
 	} else {
-		if len(req.MaterialIds) > 0 {
+		if len(materialIds) > 0 {
 			// 批量查询指标数据
-			downMaterialList, err = material.GetMaterialByIds(req.MaterialIds)
+			materialIdsInt := make([]int, 0)
+			for _, v := range materialIds {
+				id, _ := strconv.Atoi(v)
+				materialIdsInt = append(materialIdsInt, id)
+			}
+			downMaterialList, err = material.GetMaterialByIds(materialIdsInt)
 			if err != nil {
 				br.Msg = "获取素材库信息失败"
 				br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
@@ -1715,7 +1813,7 @@ func (this *MaterialController) BatchDownload() {
 // @Description 下载素材
 // @Param	request	body material.DeleteSandbox true "type json string"
 // @Success 200 标记成功
-// @router /download [post]
+// @router /download [get]
 func (this *MaterialController) Download() {
 	br := new(models.BaseResponse).Init()
 	defer func() {
@@ -1729,20 +1827,13 @@ func (this *MaterialController) Download() {
 		br.Ret = 408
 		return
 	}
-	var req material.RenameMaterialReq
-	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
-	if err != nil {
-		br.Msg = "参数解析异常!"
-		br.ErrMsg = "参数解析失败,Err:" + err.Error()
-		return
-	}
-
-	if req.MaterialId <= 0 {
+	materialId, _ := this.GetInt("MaterialId")
+	if materialId <= 0 {
 		br.Msg = "缺少素材库编号"
 		return
 	}
 	// 判断素材是否存在
-	info, err := material.GetMaterialById(req.MaterialId)
+	info, err := material.GetMaterialById(materialId)
 	if err != nil {
 		if err.Error() == utils.ErrNoRow() {
 			br.Msg = "素材不存在"

+ 16 - 4
models/material/material.go

@@ -253,11 +253,23 @@ type DetailParams struct {
 	ClassifyId int    `json:"classifyId"`
 }
 
-// AddAndEditMaterial 添加/编辑素材的请求数据
-type AddAndEditMaterial struct {
-	MaterialId   int    `description:"素材id"`
+// SaveAsMaterialReq 添加素材的请求数据
+type SaveAsMaterialReq struct {
 	MaterialName string `description:"素材名称"`
-	ImgUrl       string `description:"素材图片地址"`
+	ClassifyId   int    `description:"分类id"`
+	ObjectId     int    `description:"对象id"`
+	ObjectType   string `description:"对象类型:chart,excel,sandbox"`
+}
+
+// MyChartSaveAsMaterialReq 添加素材的
+type MyChartSaveAsMaterialReq struct {
+	MaterialList []*MyChartSaveAsMaterialItem
+}
+
+type MyChartSaveAsMaterialItem struct {
+	MaterialName string `description:"素材名称"`
+	ChartInfoId  int    `description:"图表id"`
+	MyChartId    int    `description:"我的图表ID"`
 	ClassifyId   int    `description:"分类id"`
 }
 

+ 13 - 4
routers/commentsRouter.go

@@ -7868,7 +7868,7 @@ func init() {
         beego.ControllerComments{
             Method: "BatchDownload",
             Router: `/batch/download`,
-            AllowHTTPMethods: []string{"post"},
+            AllowHTTPMethods: []string{"get"},
             MethodParams: param.Make(),
             Filters: nil,
             Params: nil})
@@ -7967,7 +7967,7 @@ func init() {
         beego.ControllerComments{
             Method: "Download",
             Router: `/download`,
-            AllowHTTPMethods: []string{"post"},
+            AllowHTTPMethods: []string{"get"},
             MethodParams: param.Make(),
             Filters: nil,
             Params: nil})
@@ -7981,6 +7981,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_api/controllers/material:MaterialController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/material:MaterialController"],
+        beego.ControllerComments{
+            Method: "MyChartSaveAsMaterial",
+            Router: `/my_chart/saveAs`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_api/controllers/material:MaterialController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/material:MaterialController"],
         beego.ControllerComments{
             Method: "Rename",
@@ -7992,8 +8001,8 @@ func init() {
 
     beego.GlobalControllerRouter["eta/eta_api/controllers/material:MaterialController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/material:MaterialController"],
         beego.ControllerComments{
-            Method: "Save",
-            Router: `/save`,
+            Method: "SaveAsMaterial",
+            Router: `/saveAs`,
             AllowHTTPMethods: []string{"post"},
             MethodParams: param.Make(),
             Filters: nil,

+ 211 - 6
services/material/material.go

@@ -2,11 +2,18 @@ package materialService
 
 import (
 	"eta/eta_api/models"
+	"eta/eta_api/models/data_manage"
+	"eta/eta_api/models/data_manage/excel"
 	"eta/eta_api/models/material"
+	"eta/eta_api/models/sandbox"
 	"eta/eta_api/models/system"
+	"eta/eta_api/services"
 	_interface "eta/eta_api/services/interface"
 	"eta/eta_api/utils"
 	"fmt"
+	"github.com/rdlucklib/rdluck_tools/http"
+	"os"
+	"path"
 	"strconv"
 	"strings"
 	"time"
@@ -89,20 +96,95 @@ func BatchAddMaterial(materialList []material.BatchAddMaterialItem, classifyId,
 	return
 }
 
-// AddMaterial 新增素材
-func AddMaterial(req material.AddAndEditMaterial, opUserId int, opUserName string) (resp *material.MaterialSaveResp, err error) {
-	resp = new(material.MaterialSaveResp)
+// AddToMaterial 将图库等封面上传至素材库
+func AddToMaterial(req material.SaveAsMaterialReq, opUserId int, opUserName string) (err error, errMsg string) {
+	// 判断出对应的类型,得倒最终的资源地址
+	oldRsourceUrl := ""
+	switch req.ObjectType {
+	case "chart":
+		// 获取图表封面地址
+		chartInfo, e := data_manage.GetChartInfoById(req.ObjectId)
+		if e != nil {
+			if e.Error() == utils.ErrNoRow() {
+				errMsg = "图表不存在"
+				err = fmt.Errorf("图表不存在")
+				return
+			}
+			errMsg = "获取图表信息失败"
+			err = e
+			return
+		}
+		if chartInfo.ChartImage == "" {
+			errMsg = "图表封面为空"
+			err = fmt.Errorf("图表封面为空")
+			return
+		}
+		oldRsourceUrl = chartInfo.ChartImage
+	case "sandbox":
+		// 获取逻辑图
+		sandboxInfo, e := sandbox.GetSandboxById(req.ObjectId)
+		if e != nil {
+			if e.Error() == utils.ErrNoRow() {
+				errMsg = "逻辑图不存在"
+				err = fmt.Errorf("逻辑图不存在")
+				return
+			}
+			errMsg = "获取逻辑图信息失败"
+			err = e
+			return
+		}
+		if sandboxInfo.PicUrl == "" { // 获取逻辑图封面地址
+			errMsg = "逻辑图封面为空"
+			err = fmt.Errorf("逻辑图封面为空")
+			return
+		}
+		oldRsourceUrl = sandboxInfo.PicUrl
+	case "excel":
+		// 获取表格封面地址
+		excelInfo, e := excel.GetExcelViewInfoByExcelInfoId(req.ObjectId)
+		if e != nil {
+			if e.Error() == utils.ErrNoRow() {
+				errMsg = "表格不存在"
+				err = fmt.Errorf("表格不存在")
+				return
+			}
+			errMsg = "获取表格信息失败"
+			err = e
+			return
+		}
+		if excelInfo.ExcelImage == "" {
+			errMsg = "表格封面为空"
+			err = fmt.Errorf("表格封面为空")
+			return
+		}
+		oldRsourceUrl = excelInfo.ExcelImage
+	default:
+		errMsg = "不支持的类型"
+		err = fmt.Errorf("不支持的类型")
+		return
+	}
+
+	resourceUrl, err, errMsg := uploadToMaterial(oldRsourceUrl)
+	if err != nil {
+		return
+	}
+	// 新增素材库
+
+	sort, err := material.GetMaterialMaxSort()
+	if err != nil {
+		return
+	}
 	//素材主表信息
 	materialInfo := &material.Material{
 		MaterialName:    utils.TrimStr(req.MaterialName),
 		MaterialNameEn:  utils.TrimStr(req.MaterialName),
-		ImgUrl:          utils.TrimStr(req.ImgUrl),
+		ImgUrl:          utils.TrimStr(resourceUrl),
 		SysUserId:       opUserId,
 		SysUserRealName: opUserName,
 		ModifyTime:      time.Now(),
 		CreateTime:      time.Now(),
 		ClassifyId:      req.ClassifyId,
-		Sort:            0,
+		Sort:            sort + 1,
 	}
 
 	//新增素材
@@ -111,7 +193,130 @@ func AddMaterial(req material.AddAndEditMaterial, opUserId int, opUserName strin
 		return
 	}
 	materialInfo.MaterialId = int(id)
-	resp.Material = materialInfo
+	return
+}
+
+// MyChartAddToMaterial 将我的
+func MyChartAddToMaterial(req material.MyChartSaveAsMaterialReq, opUserId int, opUserName string) (err error, errMsg string) {
+	// 判断出对应的类型,得倒最终的资源地址
+	// 获取图表ID
+	chartInfoIds := make([]int, 0)
+	for _, v := range req.MaterialList {
+		chartInfoIds = append(chartInfoIds, v.ChartInfoId)
+	}
+	// 获取图表信息
+	if len(chartInfoIds) <= 0 {
+		return
+	}
+	chartInfoList, e := data_manage.GetChartInfoByIdList(chartInfoIds)
+	if e != nil {
+		if e.Error() == utils.ErrNoRow() {
+			errMsg = "图表不存在"
+			err = fmt.Errorf("图表不存在")
+			return
+		}
+		errMsg = "获取图表信息失败"
+		err = e
+		return
+	}
+	chartInfoMap := make(map[int]string)
+	for _, v := range chartInfoList {
+		if v.ChartImage == "" {
+			errMsg = "图表封面为空"
+			err = fmt.Errorf("图表封面为空")
+			return
+		}
+		chartInfoMap[v.ChartInfoId] = v.ChartImage
+	}
+	addList := make([]*material.Material, 0)
+	sort, err := material.GetMaterialMaxSort()
+	if err != nil {
+		return
+	}
+	for _, v := range req.MaterialList {
+		sort = sort + 1
+		oldResourceUrl, ok := chartInfoMap[v.ChartInfoId]
+		if !ok {
+			return
+		}
+		resourceUrl := ""
+		resourceUrl, err, errMsg = uploadToMaterial(oldResourceUrl)
+		if err != nil {
+			return
+		}
+		// 新增素材库
+
+		//素材主表信息
+		materialInfo := &material.Material{
+			MaterialName:    utils.TrimStr(v.MaterialName),
+			MaterialNameEn:  utils.TrimStr(v.MaterialName),
+			ImgUrl:          utils.TrimStr(resourceUrl),
+			SysUserId:       opUserId,
+			SysUserRealName: opUserName,
+			ModifyTime:      time.Now(),
+			CreateTime:      time.Now(),
+			ClassifyId:      v.ClassifyId,
+			Sort:            sort,
+		}
+		addList = append(addList, materialInfo)
+	}
+	if len(addList) > 0 {
+		err = material.AddMultiMaterial(addList)
+	}
+	return
+}
+func uploadToMaterial(oldRsourceUrl string) (resourceUrl string, err error, errMsg string) {
+	// 下载资源地址内容,并上传至存储空间得倒最终的素材地址
+	urlFileName := path.Base(oldRsourceUrl)
+	uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
+	if e := os.MkdirAll(uploadDir, utils.DIR_MOD); e != nil {
+		errMsg = "存储目录创建失败"
+		err = fmt.Errorf("存储目录创建失败, Err:" + e.Error())
+		return
+	}
+	var content []byte
+	content, err = http.Get(oldRsourceUrl)
+	if err != nil {
+		errMsg = "操作失败"
+		err = fmt.Errorf("资源获取失败, Err: " + err.Error())
+		return
+	}
+	filePath := uploadDir + "/" + urlFileName
+	ioWriter, err := os.Create(filePath)
+	if err != nil {
+		errMsg = "操作失败"
+		err = fmt.Errorf("文件创建失败, Err: " + err.Error())
+		return
+	}
+	n, err := ioWriter.Write(content)
+	fmt.Println("n", n)
+	if err != nil {
+		errMsg = "操作失败"
+		err = fmt.Errorf("压缩文件写入失败, Err: " + err.Error())
+		return
+	}
+	ext := path.Ext(urlFileName)
+	randStr := utils.GetRandStringNoSpecialChar(28)
+	newFileName := randStr + ext
+
+	// 上传到阿里云
+	ossDir := utils.RESOURCE_DIR + "material_dir/"
+	savePath := ossDir + time.Now().Format("200601/20060102/") + newFileName
+	// 上传文件
+	ossClient := services.NewOssClient()
+	if ossClient == nil {
+		err = fmt.Errorf("初始化OSS服务失败")
+		return
+	}
+	resourceUrl, err = ossClient.UploadFile(newFileName, filePath, savePath)
+	if err != nil {
+		err = fmt.Errorf("文件上传失败, Err: %s", err.Error())
+		return
+	}
+
+	defer func() {
+		os.Remove(filePath)
+	}()
 	return
 }