material.go 15 KB

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