material.go 17 KB

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