material.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. package materialService
  2. import (
  3. "eta/eta_mobile/models"
  4. "eta/eta_mobile/models/data_manage"
  5. "eta/eta_mobile/models/data_manage/excel"
  6. "eta/eta_mobile/models/material"
  7. "eta/eta_mobile/models/semantic_analysis"
  8. "eta/eta_mobile/models/system"
  9. "eta/eta_mobile/services"
  10. _interface "eta/eta_mobile/services/interface"
  11. "eta/eta_mobile/utils"
  12. "fmt"
  13. "github.com/rdlucklib/rdluck_tools/http"
  14. "os"
  15. "path"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. func materialClassifyHaveChild(allNode []*material.MaterialClassifyItems, node *material.MaterialClassifyItems) (childs []*material.MaterialClassifyItems, yes bool) {
  21. for _, v := range allNode {
  22. if v.ParentId == node.ClassifyId {
  23. childs = append(childs, v)
  24. }
  25. }
  26. if len(childs) > 0 {
  27. yes = true
  28. }
  29. return
  30. }
  31. func MaterialClassifyItemsMakeTree(sysUser *system.Admin, allNode []*material.MaterialClassifyItems, node *material.MaterialClassifyItems) {
  32. childs, _ := materialClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回
  33. if len(childs) > 0 {
  34. node.Children = append(node.Children, childs[0:]...) //添加子节点
  35. for _, v := range childs { //查询子节点的子节点,并添加到子节点
  36. _, has := materialClassifyHaveChild(allNode, v)
  37. if has {
  38. MaterialClassifyItemsMakeTree(sysUser, allNode, v) //递归添加节点
  39. } else {
  40. childrenArr := make([]*material.MaterialClassifyItems, 0)
  41. v.Children = childrenArr
  42. }
  43. }
  44. } else {
  45. childrenArr := make([]*material.MaterialClassifyItems, 0)
  46. node.Children = childrenArr
  47. }
  48. }
  49. // GetMaterialClassifyListForMe 获取我创建的素材
  50. func GetMaterialClassifyListForMe(adminInfo system.Admin, resp *material.MaterialClassifyListResp, classifyId int) (errMsg string, err error) {
  51. classifyAll, err := material.GetMaterialClassifyByParentId(classifyId)
  52. if err != nil && err.Error() != utils.ErrNoRow() {
  53. errMsg = "获取失败"
  54. return
  55. }
  56. nodeAll := make([]*material.MaterialClassifyItems, 0)
  57. for k := range classifyAll {
  58. rootNode := classifyAll[k]
  59. MaterialClassifyItemsMakeTree(&adminInfo, classifyAll, rootNode)
  60. nodeAll = append(nodeAll, rootNode)
  61. }
  62. resp.AllNodes = nodeAll
  63. return
  64. }
  65. func BatchAddMaterial(materialList []material.BatchAddMaterialItem, classifyId, opUserId int, opUserName string) (err error) {
  66. addList := make([]*material.Material, 0)
  67. sort, err := material.GetMaterialMaxSort()
  68. if err != nil {
  69. return
  70. }
  71. for _, v := range materialList {
  72. sort = sort + 1
  73. addList = append(addList, &material.Material{
  74. MaterialName: v.MaterialName,
  75. MaterialNameEn: v.MaterialName,
  76. ImgUrl: v.ImgUrl,
  77. SysUserId: opUserId,
  78. SysUserRealName: opUserName,
  79. ModifyTime: time.Now(),
  80. CreateTime: time.Now(),
  81. ClassifyId: classifyId,
  82. Sort: sort,
  83. })
  84. }
  85. if len(addList) > 0 {
  86. err = material.AddMultiMaterial(addList)
  87. }
  88. return
  89. }
  90. // AddToMaterial 将图库等封面上传至素材库
  91. func AddToMaterial(req material.SaveAsMaterialReq, opUserId int, opUserName string) (err error, errMsg string) {
  92. // 判断出对应的类型,得倒最终的资源地址
  93. oldRsourceUrl := ""
  94. switch req.ObjectType {
  95. case "chart":
  96. // 获取图表封面地址
  97. chartInfo, e := data_manage.GetChartInfoById(req.ObjectId)
  98. if e != nil {
  99. if e.Error() == utils.ErrNoRow() {
  100. errMsg = "图表不存在"
  101. err = fmt.Errorf("图表不存在")
  102. return
  103. }
  104. errMsg = "获取图表信息失败"
  105. err = e
  106. return
  107. }
  108. if chartInfo.ChartImage == "" {
  109. errMsg = "图表封面为空"
  110. err = fmt.Errorf("图表封面为空")
  111. return
  112. }
  113. oldRsourceUrl = chartInfo.ChartImage
  114. /*case "sandbox":
  115. // 获取逻辑图
  116. sandboxInfo, e := sandbox.GetSandboxById(req.ObjectId)
  117. if e != nil {
  118. if e.Error() == utils.ErrNoRow() {
  119. errMsg = "逻辑图不存在"
  120. err = fmt.Errorf("逻辑图不存在")
  121. return
  122. }
  123. errMsg = "获取逻辑图信息失败"
  124. err = e
  125. return
  126. }
  127. if sandboxInfo.PicUrl == "" { // 获取逻辑图封面地址
  128. errMsg = "逻辑图封面为空"
  129. err = fmt.Errorf("逻辑图封面为空")
  130. return
  131. }
  132. oldRsourceUrl = sandboxInfo.PicUrl*/
  133. case "excel":
  134. // 获取表格封面地址
  135. excelInfo, e := excel.GetExcelViewInfoByExcelInfoId(req.ObjectId)
  136. if e != nil {
  137. if e.Error() == utils.ErrNoRow() {
  138. errMsg = "表格不存在"
  139. err = fmt.Errorf("表格不存在")
  140. return
  141. }
  142. errMsg = "获取表格信息失败"
  143. err = e
  144. return
  145. }
  146. if excelInfo.ExcelImage == "" {
  147. errMsg = "表格封面为空"
  148. err = fmt.Errorf("表格封面为空")
  149. return
  150. }
  151. oldRsourceUrl = excelInfo.ExcelImage
  152. case "sa_doc":
  153. // 获取文档封面地址
  154. docObj := new(semantic_analysis.SaCompare)
  155. e := docObj.GetItemById(req.ObjectId)
  156. if e != nil { // 获取文档信息
  157. if e.Error() == utils.ErrNoRow() {
  158. errMsg = "文档不存在"
  159. err = fmt.Errorf("文档不存在")
  160. return
  161. }
  162. errMsg = "获取文档信息失败"
  163. err = e
  164. return
  165. }
  166. if docObj.ResultImg == "" {
  167. errMsg = "文档封面为空"
  168. err = fmt.Errorf("文档封面为空")
  169. return
  170. }
  171. oldRsourceUrl = docObj.ResultImg
  172. default:
  173. errMsg = "不支持的类型"
  174. err = fmt.Errorf("不支持的类型")
  175. return
  176. }
  177. resourceUrl, err, errMsg := uploadToMaterial(oldRsourceUrl)
  178. if err != nil {
  179. return
  180. }
  181. // 新增素材库
  182. sort, err := material.GetMaterialMaxSort()
  183. if err != nil {
  184. return
  185. }
  186. //素材主表信息
  187. materialInfo := &material.Material{
  188. MaterialName: utils.TrimStr(req.MaterialName),
  189. MaterialNameEn: utils.TrimStr(req.MaterialName),
  190. ImgUrl: utils.TrimStr(resourceUrl),
  191. SysUserId: opUserId,
  192. SysUserRealName: opUserName,
  193. ModifyTime: time.Now(),
  194. CreateTime: time.Now(),
  195. ClassifyId: req.ClassifyId,
  196. Sort: sort + 1,
  197. }
  198. //新增素材
  199. id, err := material.AddMaterial(materialInfo)
  200. if err != nil {
  201. return
  202. }
  203. materialInfo.MaterialId = int(id)
  204. return
  205. }
  206. // MyChartAddToMaterial 将我的
  207. func MyChartAddToMaterial(req material.MyChartSaveAsMaterialReq, opUserId int, opUserName string) (err error, errMsg string) {
  208. // 判断出对应的类型,得倒最终的资源地址
  209. // 获取图表ID
  210. chartInfoIds := make([]int, 0)
  211. for _, v := range req.MaterialList {
  212. chartInfoIds = append(chartInfoIds, v.ChartInfoId)
  213. }
  214. // 获取图表信息
  215. if len(chartInfoIds) <= 0 {
  216. return
  217. }
  218. chartInfoList, e := data_manage.GetChartInfoByIdList(chartInfoIds)
  219. if e != nil {
  220. if e.Error() == utils.ErrNoRow() {
  221. errMsg = "图表不存在"
  222. err = fmt.Errorf("图表不存在")
  223. return
  224. }
  225. errMsg = "获取图表信息失败"
  226. err = e
  227. return
  228. }
  229. chartInfoMap := make(map[int]string)
  230. for _, v := range chartInfoList {
  231. if v.ChartImage == "" {
  232. errMsg = "图表封面为空"
  233. err = fmt.Errorf("图表封面为空")
  234. return
  235. }
  236. chartInfoMap[v.ChartInfoId] = v.ChartImage
  237. }
  238. addList := make([]*material.Material, 0)
  239. sort, err := material.GetMaterialMaxSort()
  240. if err != nil {
  241. return
  242. }
  243. for _, v := range req.MaterialList {
  244. sort = sort + 1
  245. oldResourceUrl, ok := chartInfoMap[v.ChartInfoId]
  246. if !ok {
  247. return
  248. }
  249. resourceUrl := ""
  250. resourceUrl, err, errMsg = uploadToMaterial(oldResourceUrl)
  251. if err != nil {
  252. return
  253. }
  254. // 新增素材库
  255. //素材主表信息
  256. materialInfo := &material.Material{
  257. MaterialName: utils.TrimStr(v.MaterialName),
  258. MaterialNameEn: utils.TrimStr(v.MaterialName),
  259. ImgUrl: utils.TrimStr(resourceUrl),
  260. SysUserId: opUserId,
  261. SysUserRealName: opUserName,
  262. ModifyTime: time.Now(),
  263. CreateTime: time.Now(),
  264. ClassifyId: v.ClassifyId,
  265. Sort: sort,
  266. }
  267. addList = append(addList, materialInfo)
  268. }
  269. if len(addList) > 0 {
  270. err = material.AddMultiMaterial(addList)
  271. }
  272. return
  273. }
  274. func uploadToMaterial(oldRsourceUrl string) (resourceUrl string, err error, errMsg string) {
  275. // 下载资源地址内容,并上传至存储空间得倒最终的素材地址
  276. urlFileName := path.Base(oldRsourceUrl)
  277. uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
  278. if e := os.MkdirAll(uploadDir, utils.DIR_MOD); e != nil {
  279. errMsg = "存储目录创建失败"
  280. err = fmt.Errorf("存储目录创建失败, Err:" + e.Error())
  281. return
  282. }
  283. var content []byte
  284. content, err = http.Get(oldRsourceUrl)
  285. if err != nil {
  286. errMsg = "操作失败"
  287. err = fmt.Errorf("资源获取失败, Err: " + err.Error())
  288. return
  289. }
  290. filePath := uploadDir + "/" + urlFileName
  291. fmt.Println("filepath", filePath)
  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. }