material.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. package materialService
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/data_manage/excel"
  7. "eta/eta_api/models/material"
  8. "eta/eta_api/models/sandbox"
  9. "eta/eta_api/models/semantic_analysis"
  10. "eta/eta_api/models/system"
  11. "eta/eta_api/services"
  12. _interface "eta/eta_api/services/interface"
  13. "eta/eta_api/utils"
  14. "fmt"
  15. "github.com/rdlucklib/rdluck_tools/http"
  16. "os"
  17. "path"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. func materialClassifyHaveChild(allNode []*material.MaterialClassifyItems, node *material.MaterialClassifyItems) (childs []*material.MaterialClassifyItems, yes bool) {
  23. for _, v := range allNode {
  24. if v.ParentId == node.ClassifyId {
  25. childs = append(childs, v)
  26. }
  27. }
  28. if len(childs) > 0 {
  29. yes = true
  30. }
  31. return
  32. }
  33. func MaterialClassifyItemsMakeTree(sysUser *system.Admin, allNode []*material.MaterialClassifyItems, node *material.MaterialClassifyItems) {
  34. childs, _ := materialClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回
  35. if len(childs) > 0 {
  36. node.Children = append(node.Children, childs[0:]...) //添加子节点
  37. for _, v := range childs { //查询子节点的子节点,并添加到子节点
  38. _, has := materialClassifyHaveChild(allNode, v)
  39. if has {
  40. MaterialClassifyItemsMakeTree(sysUser, allNode, v) //递归添加节点
  41. } else {
  42. childrenArr := make([]*material.MaterialClassifyItems, 0)
  43. v.Children = childrenArr
  44. }
  45. }
  46. } else {
  47. childrenArr := make([]*material.MaterialClassifyItems, 0)
  48. node.Children = childrenArr
  49. }
  50. }
  51. func BatchAddMaterial(materialList []material.BatchAddMaterialItem, classifyId, opUserId int, opUserName string) (err error) {
  52. addList := make([]*material.Material, 0)
  53. sort, err := material.GetMaterialMaxSort()
  54. if err != nil {
  55. return
  56. }
  57. for _, v := range materialList {
  58. sort = sort + 1
  59. addList = append(addList, &material.Material{
  60. MaterialName: v.MaterialName,
  61. MaterialNameEn: v.MaterialName,
  62. ImgUrl: v.ImgUrl,
  63. SysUserId: opUserId,
  64. SysUserRealName: opUserName,
  65. ModifyTime: time.Now(),
  66. CreateTime: time.Now(),
  67. ClassifyId: classifyId,
  68. Sort: sort,
  69. })
  70. }
  71. if len(addList) > 0 {
  72. err = material.AddMultiMaterial(addList)
  73. }
  74. return
  75. }
  76. // AddToMaterial 将图库等封面上传至素材库
  77. func AddToMaterial(req material.SaveAsMaterialReq, opUserId int, opUserName string) (err error, errMsg string) {
  78. // 判断出对应的类型,得倒最终的资源地址
  79. oldRsourceUrl := ""
  80. switch req.ObjectType {
  81. case "chart":
  82. // 获取图表封面地址
  83. chartInfo, e := data_manage.GetChartInfoById(req.ObjectId)
  84. if e != nil {
  85. if utils.IsErrNoRow(e) {
  86. errMsg = "图表不存在"
  87. err = fmt.Errorf("图表不存在")
  88. return
  89. }
  90. errMsg = "获取图表信息失败"
  91. err = e
  92. return
  93. }
  94. if chartInfo.ChartImage == "" {
  95. errMsg = "图表封面为空"
  96. err = fmt.Errorf("图表封面为空")
  97. return
  98. }
  99. oldRsourceUrl = chartInfo.ChartImage
  100. case "sandbox":
  101. // 获取逻辑图
  102. sandboxInfo, e := sandbox.GetSandboxById(req.ObjectId)
  103. if e != nil {
  104. if utils.IsErrNoRow(e) {
  105. errMsg = "逻辑图不存在"
  106. err = fmt.Errorf("逻辑图不存在")
  107. return
  108. }
  109. errMsg = "获取逻辑图信息失败"
  110. err = e
  111. return
  112. }
  113. if sandboxInfo.PicUrl == "" { // 获取逻辑图封面地址
  114. errMsg = "逻辑图封面为空"
  115. err = fmt.Errorf("逻辑图封面为空")
  116. return
  117. }
  118. oldRsourceUrl = sandboxInfo.PicUrl
  119. case "excel":
  120. // 获取表格封面地址
  121. excelInfo, e := excel.GetExcelViewInfoByExcelInfoId(req.ObjectId)
  122. if e != nil {
  123. if utils.IsErrNoRow(e) {
  124. errMsg = "表格不存在"
  125. err = fmt.Errorf("表格不存在")
  126. return
  127. }
  128. errMsg = "获取表格信息失败"
  129. err = e
  130. return
  131. }
  132. if excelInfo.ExcelImage == "" {
  133. errMsg = "表格封面为空"
  134. err = fmt.Errorf("表格封面为空")
  135. return
  136. }
  137. oldRsourceUrl = excelInfo.ExcelImage
  138. case "sa_doc":
  139. // 获取文档封面地址
  140. docObj := new(semantic_analysis.SaCompare)
  141. e := docObj.GetItemById(req.ObjectId)
  142. if e != nil { // 获取文档信息
  143. if utils.IsErrNoRow(e) {
  144. errMsg = "文档不存在"
  145. err = fmt.Errorf("文档不存在")
  146. return
  147. }
  148. errMsg = "获取文档信息失败"
  149. err = e
  150. return
  151. }
  152. if docObj.ResultImg == "" {
  153. errMsg = "文档封面为空"
  154. err = fmt.Errorf("文档封面为空")
  155. return
  156. }
  157. oldRsourceUrl = docObj.ResultImg
  158. default:
  159. errMsg = "不支持的类型"
  160. err = fmt.Errorf("不支持的类型")
  161. return
  162. }
  163. // 兼容部分内网客户替换OSS地址
  164. conf, e := models.GetBusinessConfByKey(models.BusinessConfOssUrlReplace)
  165. if e != nil && !utils.IsErrNoRow(e) {
  166. errMsg = "操作失败"
  167. err = fmt.Errorf("获取内网配置失败, %v", e)
  168. return
  169. }
  170. if conf != nil && conf.ConfVal != "" {
  171. var urlReplace models.OssUrlReplace
  172. if e := json.Unmarshal([]byte(conf.ConfVal), &urlReplace); e != nil {
  173. errMsg = "操作失败"
  174. err = fmt.Errorf("内网配置解析失败, %v", e)
  175. return
  176. }
  177. if urlReplace.IsReplace && urlReplace.OssUrlOrigin != "" {
  178. oldRsourceUrl = strings.ReplaceAll(oldRsourceUrl, urlReplace.OssUrlOrigin, urlReplace.OssUrlNew)
  179. }
  180. }
  181. resourceUrl, err, errMsg := uploadToMaterial(oldRsourceUrl)
  182. if err != nil {
  183. return
  184. }
  185. // 新增素材库
  186. sort, err := material.GetMaterialMaxSort()
  187. if err != nil {
  188. return
  189. }
  190. //素材主表信息
  191. materialInfo := &material.Material{
  192. MaterialName: req.MaterialName,
  193. MaterialNameEn: req.MaterialName,
  194. ImgUrl: resourceUrl,
  195. SysUserId: opUserId,
  196. SysUserRealName: opUserName,
  197. ModifyTime: time.Now(),
  198. CreateTime: time.Now(),
  199. ClassifyId: req.ClassifyId,
  200. Sort: sort + 1,
  201. }
  202. //新增素材
  203. id, err := material.AddMaterial(materialInfo)
  204. if err != nil {
  205. return
  206. }
  207. materialInfo.MaterialId = int(id)
  208. return
  209. }
  210. // MyChartAddToMaterial 将我的
  211. func MyChartAddToMaterial(req material.MyChartSaveAsMaterialReq, opUserId int, opUserName string) (err error, errMsg string) {
  212. // 判断出对应的类型,得倒最终的资源地址
  213. // 获取图表ID
  214. chartInfoIds := make([]int, 0)
  215. for _, v := range req.MaterialList {
  216. chartInfoIds = append(chartInfoIds, v.ChartInfoId)
  217. }
  218. // 获取图表信息
  219. if len(chartInfoIds) <= 0 {
  220. return
  221. }
  222. chartInfoList, e := data_manage.GetChartInfoByIdList(chartInfoIds)
  223. if e != nil {
  224. if utils.IsErrNoRow(e) {
  225. errMsg = "图表不存在"
  226. err = fmt.Errorf("图表不存在")
  227. return
  228. }
  229. errMsg = "获取图表信息失败"
  230. err = e
  231. return
  232. }
  233. chartInfoMap := make(map[int]string)
  234. for _, v := range chartInfoList {
  235. if v.ChartImage == "" {
  236. errMsg = "图表封面为空"
  237. err = fmt.Errorf("图表封面为空")
  238. return
  239. }
  240. chartInfoMap[v.ChartInfoId] = v.ChartImage
  241. }
  242. addList := make([]*material.Material, 0)
  243. sort, err := material.GetMaterialMaxSort()
  244. if err != nil {
  245. return
  246. }
  247. for _, v := range req.MaterialList {
  248. sort = sort + 1
  249. oldResourceUrl, ok := chartInfoMap[v.ChartInfoId]
  250. if !ok {
  251. return
  252. }
  253. resourceUrl := ""
  254. resourceUrl, err, errMsg = uploadToMaterial(oldResourceUrl)
  255. if err != nil {
  256. return
  257. }
  258. // 新增素材库
  259. //素材主表信息
  260. materialInfo := &material.Material{
  261. MaterialName: v.MaterialName,
  262. MaterialNameEn: v.MaterialName,
  263. ImgUrl: resourceUrl,
  264. SysUserId: opUserId,
  265. SysUserRealName: opUserName,
  266. ModifyTime: time.Now(),
  267. CreateTime: time.Now(),
  268. ClassifyId: v.ClassifyId,
  269. Sort: sort,
  270. }
  271. addList = append(addList, materialInfo)
  272. }
  273. if len(addList) > 0 {
  274. err = material.AddMultiMaterial(addList)
  275. }
  276. return
  277. }
  278. func uploadToMaterial(oldRsourceUrl string) (resourceUrl string, err error, errMsg string) {
  279. // 下载资源地址内容,并上传至存储空间得倒最终的素材地址
  280. urlFileName := path.Base(oldRsourceUrl)
  281. uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
  282. if e := os.MkdirAll(uploadDir, utils.DIR_MOD); e != nil {
  283. errMsg = "存储目录创建失败"
  284. err = fmt.Errorf("存储目录创建失败, Err:" + e.Error())
  285. return
  286. }
  287. var content []byte
  288. content, err = http.Get(oldRsourceUrl)
  289. if err != nil {
  290. errMsg = "操作失败"
  291. err = fmt.Errorf("资源获取失败, Err: " + err.Error())
  292. return
  293. }
  294. filePath := uploadDir + "/" + urlFileName
  295. ioWriter, err := os.Create(filePath)
  296. if err != nil {
  297. errMsg = "操作失败"
  298. err = fmt.Errorf("文件创建失败, Err: " + err.Error())
  299. return
  300. }
  301. n, err := ioWriter.Write(content)
  302. fmt.Println("n", n)
  303. if err != nil {
  304. errMsg = "操作失败"
  305. err = fmt.Errorf("压缩文件写入失败, Err: " + err.Error())
  306. return
  307. }
  308. ext := path.Ext(urlFileName)
  309. randStr := utils.GetRandStringNoSpecialChar(28)
  310. newFileName := randStr + ext
  311. // 上传到阿里云
  312. ossDir := utils.RESOURCE_DIR + "material_dir/"
  313. savePath := ossDir + time.Now().Format("200601/20060102/") + newFileName
  314. // 上传文件
  315. ossClient := services.NewOssClient()
  316. if ossClient == nil {
  317. err = fmt.Errorf("初始化OSS服务失败")
  318. return
  319. }
  320. resourceUrl, err = ossClient.UploadFile(newFileName, filePath, savePath)
  321. if err != nil {
  322. err = fmt.Errorf("文件上传失败, Err: %s", err.Error())
  323. return
  324. }
  325. defer func() {
  326. os.Remove(filePath)
  327. }()
  328. return
  329. }
  330. func MoveMaterialClassify(classifyInfo *material.MaterialClassify, req *material.MoveMaterialClassifyReq) (err error, errMsg string) {
  331. nodeMove := models.SingleMoveNodeReq{}
  332. nodeMove.NodeId = req.ClassifyId
  333. nodeMove.ParentNodeId = req.ParentClassifyId
  334. nodeMove.PrevNodeId = req.PrevClassifyId
  335. nodeMove.NextNodeId = req.NextClassifyId
  336. materialClassifyMove := new(ClassifyMove)
  337. nodeInfo, updateCol, err, errMsg := _interface.MoveSingleNode(materialClassifyMove, nodeMove)
  338. if err != nil {
  339. return
  340. }
  341. oldParentId := classifyInfo.ParentId
  342. oldLevelPath := classifyInfo.LevelPath
  343. if len(updateCol) > 0 {
  344. classifyInfo.Sort = nodeInfo.Sort
  345. classifyInfo.ModifyTime = nodeInfo.ModifyTime
  346. classifyInfo.ParentId = nodeInfo.ParentId
  347. levelPath := classifyInfo.LevelPath
  348. if classifyInfo.ParentId != oldParentId {
  349. //查找父级分类
  350. parentClassify, e := material.GetMaterialClassifyById(classifyInfo.ParentId)
  351. if e != nil {
  352. errMsg = "获取父级分类失败"
  353. err = fmt.Errorf("获取父级分类失败,Err:" + e.Error())
  354. return
  355. }
  356. levelPath = fmt.Sprintf("%s%d,", parentClassify.LevelPath, classifyInfo.ClassifyId)
  357. classifyInfo.LevelPath = levelPath
  358. updateCol = append(updateCol, "LevelPath")
  359. }
  360. err = classifyInfo.Update(updateCol)
  361. if err != nil {
  362. err = fmt.Errorf("修改失败,Err:" + err.Error())
  363. return
  364. }
  365. if classifyInfo.ParentId != oldParentId {
  366. tmpList, e := material.GetMaterialClassifyByLevelPath(oldLevelPath)
  367. if e != nil {
  368. err = fmt.Errorf("保存分类失败,Err:" + e.Error())
  369. return
  370. }
  371. // 把原先的父级levePath,替换成最新的父级序列
  372. for _, tmp := range tmpList {
  373. //获取字符串前缀的位置
  374. after, _ := strings.CutPrefix(tmp.LevelPath, oldLevelPath)
  375. fmt.Println("after", after)
  376. // 拼接字符串
  377. if after != "" {
  378. tmp.LevelPath = levelPath + after
  379. tmp.ModifyTime = time.Now()
  380. e = tmp.Update([]string{"LevelPath", "ModifyTime"})
  381. if e != nil {
  382. err = fmt.Errorf("修改子分类,Err:" + e.Error())
  383. return
  384. }
  385. }
  386. }
  387. }
  388. }
  389. return
  390. }
  391. type ClassifyMove struct{}
  392. func (m *ClassifyMove) GetNodeInfoById(nodeId int) (nodeInfo *models.NodeInfo, err error) {
  393. classifyInfo, err := material.GetMaterialClassifyById(nodeId)
  394. if err != nil {
  395. return
  396. }
  397. nodeInfo = &models.NodeInfo{
  398. NodeId: classifyInfo.ClassifyId,
  399. NodeName: classifyInfo.ClassifyName,
  400. ParentId: classifyInfo.ParentId,
  401. Level: classifyInfo.Level,
  402. Sort: classifyInfo.Sort,
  403. ModifyTime: classifyInfo.ModifyTime,
  404. }
  405. return
  406. }
  407. func (m *ClassifyMove) UpdateNodeInfoSortByParentIdAndSource(parentNodeId, nodeId, prevNodeSort int, updateSortStr string, nodeType int) (err error) {
  408. err = material.UpdateMaterialClassifySortByParentId(parentNodeId, nodeId, prevNodeSort, updateSortStr)
  409. return
  410. }
  411. func (m *ClassifyMove) GetNodeMaxSort(parentId, nodeType int) (maxSort int, err error) {
  412. maxSort, err = material.GetMaterialClassifyMaxSort(parentId)
  413. return
  414. }
  415. func (m *ClassifyMove) GetFirstNodeInfoByParentId(parentId int) (nodeInfo *models.NodeInfo, err error) {
  416. classifyInfo, err := material.GetFirstMaterialClassifyByParentId(parentId)
  417. if err != nil {
  418. return
  419. }
  420. nodeInfo = &models.NodeInfo{
  421. NodeId: classifyInfo.ClassifyId,
  422. NodeName: classifyInfo.ClassifyName,
  423. ParentId: classifyInfo.ParentId,
  424. Level: classifyInfo.Level,
  425. Sort: classifyInfo.Sort,
  426. ModifyTime: classifyInfo.ModifyTime,
  427. }
  428. return
  429. }
  430. func GetBatchSelectedMaterialList(classifyId int, keyword string, isShowMe bool, sysUser *system.Admin, lang string) (list []*material.MaterialListItems, err error, errMsg string) {
  431. var condition string
  432. var pars []interface{}
  433. if classifyId <= 0 {
  434. errMsg = "请选择分类"
  435. err = fmt.Errorf(errMsg)
  436. return
  437. }
  438. // 查询当前的分类
  439. classifyInfo, e := material.GetMaterialClassifyById(classifyId)
  440. if e != nil {
  441. errMsg = "分类不存在"
  442. err = fmt.Errorf("获取分类信息失败,Err:" + e.Error())
  443. return
  444. }
  445. // 获取所有子分类
  446. childList, e := material.GetMaterialClassifyByLevelPath(classifyInfo.LevelPath)
  447. if e != nil {
  448. errMsg = "获取分类失败"
  449. err = fmt.Errorf("获取子分类失败,Err:" + e.Error())
  450. return
  451. }
  452. // 把原先的父级levePath,替换成最新的父级序列
  453. classifyIdMap := make(map[string]struct{})
  454. classifyIds := make([]string, 0)
  455. childClassifyMap := make(map[int]*material.MaterialClassify)
  456. for _, tmp := range childList {
  457. childClassifyMap[tmp.ClassifyId] = tmp
  458. //获取字符串前缀的位置
  459. after, _ := strings.CutPrefix(tmp.LevelPath, classifyInfo.LevelPath)
  460. fmt.Println("after", after)
  461. // 拼接字符串
  462. if after != "" {
  463. ids := strings.Split(after, ",")
  464. for _, v := range ids {
  465. if _, ok := classifyIdMap[v]; !ok {
  466. classifyIds = append(classifyIds, v)
  467. classifyIdMap[v] = struct{}{}
  468. }
  469. }
  470. }
  471. }
  472. classifyIds = append(classifyIds, strconv.Itoa(classifyId))
  473. if len(classifyIds) > 0 {
  474. condition += " AND classify_id IN(" + utils.GetOrmInReplace(len(classifyIds)) + ") "
  475. pars = append(pars, classifyIds)
  476. }
  477. if keyword != "" {
  478. switch lang {
  479. case utils.LANG_EN:
  480. condition += ` AND ( material_name_en LIKE '%` + keyword + `%' )`
  481. default:
  482. condition += ` AND ( material_name LIKE '%` + keyword + `%' )`
  483. }
  484. }
  485. //只看我的
  486. if isShowMe {
  487. condition += ` AND sys_user_id = ? `
  488. pars = append(pars, sysUser.AdminId)
  489. }
  490. //获取图表信息
  491. list, err = material.GetMaterialListByCondition(condition, pars)
  492. if err != nil && !utils.IsErrNoRow(err) {
  493. errMsg = "获取素材库信息失败"
  494. err = fmt.Errorf("获取素材库信息失败,Err:" + err.Error())
  495. return
  496. }
  497. return
  498. }
  499. func GetMyChartExistMaterialNameListMsg(nameList map[string]struct{}, reqList []*material.MyChartSaveAsMaterialItem) (nameResp material.MyChartSaveAsMaterialResp) {
  500. existNameList := make([]*material.MyChartSaveAsMaterialItem, 0)
  501. for _, v := range reqList {
  502. if _, ok := nameList[v.MaterialName]; ok {
  503. existNameList = append(existNameList, v)
  504. }
  505. }
  506. nameResp = material.MyChartSaveAsMaterialResp{
  507. ExistList: existNameList,
  508. }
  509. return
  510. }