material.go 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  1. package material
  2. import (
  3. "archive/zip"
  4. "encoding/json"
  5. "eta/eta_mobile/controllers"
  6. "eta/eta_mobile/models"
  7. "eta/eta_mobile/models/material"
  8. materialService "eta/eta_mobile/services/material"
  9. "eta/eta_mobile/utils"
  10. "fmt"
  11. "github.com/rdlucklib/rdluck_tools/http"
  12. "github.com/rdlucklib/rdluck_tools/paging"
  13. "os"
  14. "path"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. // MaterialController 逻辑导图
  20. type MaterialController struct {
  21. controllers.BaseAuthController
  22. }
  23. // AddMaterialClassify
  24. // @Title 新增素材库分类
  25. // @Description 新增材库分类接口
  26. // @Param request body data_manage.AddChartClassifyReq true "type json string"
  27. // @Success 200 Ret=200 保存成功
  28. // @router /classify/add [post]
  29. func (this *MaterialController) AddMaterialClassify() {
  30. br := new(models.BaseResponse).Init()
  31. defer func() {
  32. this.Data["json"] = br
  33. this.ServeJSON()
  34. }()
  35. var req material.AddMaterialClassifyReq
  36. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  37. if err != nil {
  38. br.Msg = "参数解析异常!"
  39. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  40. return
  41. }
  42. if req.ClassifyName == "" {
  43. br.Msg = "请输入分类名称"
  44. br.IsSendEmail = false
  45. return
  46. }
  47. if req.ParentId < 0 {
  48. br.Msg = "参数错误"
  49. br.IsSendEmail = false
  50. return
  51. }
  52. level := 1
  53. levelPath := ""
  54. if req.ParentId > 0 {
  55. //查找父级分类
  56. parentClassify, e := material.GetMaterialClassifyById(req.ParentId)
  57. if e != nil {
  58. br.Msg = "获取父级分类失败"
  59. br.ErrMsg = "获取父级分类失败,Err:" + e.Error()
  60. return
  61. }
  62. level = parentClassify.Level + 1
  63. levelPath = parentClassify.LevelPath
  64. }
  65. var count int
  66. switch this.Lang {
  67. case utils.LANG_EN:
  68. count, err = material.GetMaterialClassifyNameEnCount(req.ClassifyName, req.ParentId)
  69. default:
  70. count, err = material.GetMaterialClassifyNameCount(req.ClassifyName, req.ParentId)
  71. }
  72. if err != nil {
  73. br.Msg = "判断名称是否已存在失败"
  74. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  75. return
  76. }
  77. if count > 0 {
  78. br.Msg = "分类名称已存在,请重新输入"
  79. br.IsSendEmail = false
  80. return
  81. }
  82. //获取该层级下最大的排序数
  83. classify := new(material.MaterialClassify)
  84. maxSort, _ := material.GetMaterialClassifyMaxSort(req.ParentId)
  85. classify.ParentId = req.ParentId
  86. classify.ClassifyName = req.ClassifyName
  87. classify.ClassifyNameEn = req.ClassifyName
  88. classify.CreateTime = time.Now()
  89. classify.ModifyTime = time.Now()
  90. classify.SysUserId = this.SysUser.AdminId
  91. classify.SysUserRealName = this.SysUser.RealName
  92. classify.Level = level
  93. classify.Sort = maxSort + 1
  94. classifyId, e := material.AddMaterialClassify(classify)
  95. if e != nil {
  96. br.Msg = "保存分类失败"
  97. br.ErrMsg = "保存分类失败,Err:" + e.Error()
  98. return
  99. }
  100. if req.ParentId > 0 {
  101. levelPath = fmt.Sprintf("%s,%d", levelPath, classifyId)
  102. } else {
  103. levelPath = fmt.Sprintf("%d", classifyId)
  104. }
  105. classify.ClassifyId = int(classifyId)
  106. classify.LevelPath = levelPath
  107. e = classify.Update([]string{"LevelPath"})
  108. if e != nil {
  109. br.Msg = "保存分类失败"
  110. br.ErrMsg = "保存分类失败,Err:" + e.Error()
  111. return
  112. }
  113. br.Ret = 200
  114. br.Msg = "保存成功"
  115. br.Success = true
  116. }
  117. // EditMaterialClassify
  118. // @Title 修改素材库分类
  119. // @Description 修改素材库分类接口
  120. // @Param request body data_manage.EditChartClassifyReq true "type json string"
  121. // @Success 200 Ret=200 修改成功
  122. // @router /classify/edit [post]
  123. func (this *MaterialController) EditMaterialClassify() {
  124. br := new(models.BaseResponse).Init()
  125. defer func() {
  126. this.Data["json"] = br
  127. this.ServeJSON()
  128. }()
  129. var req material.EditMaterialClassifyReq
  130. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  131. if err != nil {
  132. br.Msg = "参数解析异常!"
  133. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  134. return
  135. }
  136. if req.ClassifyName == "" {
  137. br.Msg = "请输入分类名称"
  138. br.IsSendEmail = false
  139. return
  140. }
  141. if req.ClassifyId <= 0 {
  142. br.Msg = "参数错误"
  143. br.IsSendEmail = false
  144. return
  145. }
  146. // 只允许修改分类名称
  147. item, err := material.GetMaterialClassifyById(req.ClassifyId)
  148. if err != nil {
  149. if err.Error() == utils.ErrNoRow() {
  150. br.Msg = "分类不存在"
  151. return
  152. }
  153. br.Msg = "保存失败"
  154. br.Msg = "获取分类信息失败,Err:" + err.Error()
  155. return
  156. }
  157. var count int
  158. updateStr := make([]string, 0)
  159. switch this.Lang {
  160. case utils.LANG_EN:
  161. count, err = material.GetMaterialClassifyNameEnNotSelfCount(req.ClassifyId, req.ClassifyName, item.ParentId)
  162. item.ClassifyNameEn = req.ClassifyName
  163. updateStr = append(updateStr, "ClassifyNameEn")
  164. default:
  165. count, err = material.GetMaterialClassifyNameNotSelfCount(req.ClassifyId, req.ClassifyName, item.ParentId)
  166. item.ClassifyName = req.ClassifyName
  167. updateStr = append(updateStr, "ClassifyName")
  168. }
  169. if err != nil {
  170. br.Msg = "判断名称是否已存在失败"
  171. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  172. return
  173. }
  174. if count > 0 {
  175. br.Msg = "分类名称已存在,请重新输入"
  176. br.IsSendEmail = false
  177. return
  178. }
  179. item.ModifyTime = time.Now()
  180. updateStr = append(updateStr, "ModifyTime")
  181. err = item.Update(updateStr)
  182. if err != nil {
  183. br.Msg = "保存分类失败"
  184. br.ErrMsg = "保存分类失败,Err:" + err.Error()
  185. return
  186. }
  187. //todo 测试更新子集的levelPath
  188. /*levelPath := ""
  189. oldLevelPath := item.LevelPath
  190. if item.ParentId > 0 {
  191. //查找父级分类
  192. parentClassify, e := material.GetMaterialClassifyById(item.ParentId)
  193. if e != nil {
  194. br.Msg = "获取父级分类失败"
  195. br.ErrMsg = "获取父级分类失败,Err:" + e.Error()
  196. return
  197. }
  198. levelPath = fmt.Sprintf("%s,%d", parentClassify.LevelPath, item.ClassifyId)
  199. tmpList, e := material.GetMaterialClassifyByLevelPath(oldLevelPath)
  200. if e != nil {
  201. br.Msg = "保存分类失败"
  202. br.ErrMsg = "保存分类失败,Err:" + e.Error()
  203. return
  204. }
  205. // 把原先的父级levePath,替换成最新的父级序列
  206. for _, tmp := range tmpList {
  207. tmp.LevelPath = strings.Replace(tmp.LevelPath, oldLevelPath, levelPath, -1)
  208. tmp.ModifyTime = time.Now()
  209. e = tmp.Update([]string{"LevelPath", "ModifyTime"})
  210. if e != nil {
  211. br.Msg = "保存分类失败"
  212. br.ErrMsg = "保存分类失败,Err:" + e.Error()
  213. return
  214. }
  215. }
  216. } else {
  217. // 只有更改了父级才需要更新levelPath
  218. }*/
  219. br.Ret = 200
  220. br.Msg = "保存成功"
  221. br.Success = true
  222. br.IsAddLog = true
  223. }
  224. // DeleteMaterialClassifyCheck
  225. // @Title 删除素材库检测接口
  226. // @Description 删除素材库检测接口
  227. // @Param request body data_manage.ChartClassifyDeleteCheckResp true "type json string"
  228. // @Success 200 Ret=200 检测成功
  229. // @router /classify/del/check [post]
  230. func (this *MaterialController) DeleteMaterialClassifyCheck() {
  231. br := new(models.BaseResponse).Init()
  232. defer func() {
  233. this.Data["json"] = br
  234. this.ServeJSON()
  235. }()
  236. var req material.MaterialClassifyDeleteCheckReq
  237. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  238. if err != nil {
  239. br.Msg = "参数解析异常!"
  240. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  241. return
  242. }
  243. if req.ClassifyId < 0 {
  244. br.Msg = "参数错误"
  245. br.IsSendEmail = false
  246. return
  247. }
  248. var deleteStatus int
  249. var tipsMsg string
  250. //删除分类
  251. // 查询当前的分类
  252. classifyInfo, err := material.GetMaterialClassifyById(req.ClassifyId)
  253. if err != nil {
  254. br.Msg = "分类不存在"
  255. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  256. return
  257. }
  258. classifyIds := strings.Split(classifyInfo.LevelPath, ",")
  259. if len(classifyIds) > 0 {
  260. //判断素材库分类下,是否含有素材库
  261. count, e := material.GetMaterialInfoCountByClassifyIds(classifyIds)
  262. if e != nil {
  263. br.Msg = "删除失败"
  264. br.ErrMsg = "分类下是否含有指标失败,Err:" + e.Error()
  265. return
  266. }
  267. if count > 0 {
  268. deleteStatus = 1
  269. tipsMsg = "该分类下关联素材库不可删除"
  270. } else {
  271. if len(classifyIds) > 1 {
  272. deleteStatus = 2
  273. tipsMsg = "确认删除当前目录及包含的子目录吗"
  274. }
  275. }
  276. }
  277. if deleteStatus == 0 {
  278. tipsMsg = "可删除,进行删除操作"
  279. }
  280. resp := new(material.MaterialClassifyDeleteCheckResp)
  281. resp.DeleteStatus = deleteStatus
  282. resp.TipsMsg = tipsMsg
  283. br.Ret = 200
  284. br.Msg = "检测成功"
  285. br.Success = true
  286. br.Data = resp
  287. }
  288. // DeleteMaterialClassify
  289. // @Title 删除素材库分类/素材库
  290. // @Description 删除素材库分类/素材库接口
  291. // @Param request body data_manage.DeleteChartClassifyReq true "type json string"
  292. // @Success 200 Ret=200 删除成功
  293. // @router /classify/del [post]
  294. func (this *MaterialController) DeleteMaterialClassify() {
  295. br := new(models.BaseResponse).Init()
  296. defer func() {
  297. this.Data["json"] = br
  298. this.ServeJSON()
  299. }()
  300. sysUser := this.SysUser
  301. if sysUser == nil {
  302. br.Msg = "请登录"
  303. br.ErrMsg = "请登录,SysUser Is Empty"
  304. br.Ret = 408
  305. return
  306. }
  307. var req material.DeleteMaterialClassifyReq
  308. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  309. if err != nil {
  310. br.Msg = "参数解析异常!"
  311. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  312. return
  313. }
  314. if req.ClassifyId < 0 {
  315. br.Msg = "参数错误"
  316. br.IsSendEmail = false
  317. return
  318. }
  319. // 查询当前的分类
  320. classifyInfo, err := material.GetMaterialClassifyById(req.ClassifyId)
  321. if err != nil {
  322. br.Msg = "分类不存在"
  323. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  324. return
  325. }
  326. classifyIds := strings.Split(classifyInfo.LevelPath, ",")
  327. if len(classifyIds) > 0 {
  328. //判断素材库分类下,是否含有素材库
  329. count, e := material.GetMaterialInfoCountByClassifyIds(classifyIds)
  330. if e != nil {
  331. br.Msg = "删除失败"
  332. br.ErrMsg = "分类下是否含有指标失败,Err:" + e.Error()
  333. return
  334. }
  335. if count > 0 {
  336. br.Msg = "该目录下存在关联指标,不可删除"
  337. br.IsSendEmail = false
  338. return
  339. }
  340. err = material.DeleteMaterialClassify(classifyIds)
  341. if err != nil {
  342. br.Msg = "删除失败"
  343. br.ErrMsg = "删除失败,Err:" + err.Error()
  344. return
  345. }
  346. }
  347. //删除素材库
  348. /*if req.MaterialId > 0 {
  349. materialInfo, err := material.GetMaterialById(req.MaterialId)
  350. if err != nil {
  351. if err.Error() == utils.ErrNoRow() {
  352. br.Msg = "素材库已删除,请刷新页面"
  353. br.ErrMsg = "指标不存在,Err:" + err.Error()
  354. return
  355. } else {
  356. br.Msg = "删除失败"
  357. br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error()
  358. return
  359. }
  360. }
  361. if materialInfo == nil {
  362. br.Msg = "素材库已删除,请刷新页面"
  363. return
  364. }
  365. err = materialService.DeleteMaterial(req.MaterialId)
  366. if err != nil {
  367. br.Msg = err.Error()
  368. return
  369. }
  370. }*/
  371. br.Ret = 200
  372. br.Msg = "删除成功"
  373. br.Success = true
  374. br.IsAddLog = true
  375. }
  376. // ClassifyMove
  377. // @Title 素材库分类移动接口
  378. // @Description 素材库分类移动接口
  379. // @Success 200 {object} data_manage.MoveChartClassifyReq
  380. // @router /classify/move [post]
  381. func (this *MaterialController) ClassifyMove() {
  382. br := new(models.BaseResponse).Init()
  383. defer func() {
  384. this.Data["json"] = br
  385. this.ServeJSON()
  386. }()
  387. sysUser := this.SysUser
  388. if sysUser == nil {
  389. br.Msg = "请登录"
  390. br.ErrMsg = "请登录,SysUser Is Empty"
  391. br.Ret = 408
  392. return
  393. }
  394. var req material.MoveMaterialClassifyReq
  395. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  396. if err != nil {
  397. br.Msg = "参数解析异常!"
  398. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  399. return
  400. }
  401. if req.ClassifyId <= 0 {
  402. br.Msg = "参数错误"
  403. br.ErrMsg = "分类id小于等于0"
  404. return
  405. }
  406. //判断分类是否存在
  407. materialClassifyInfo, err := material.GetMaterialClassifyById(req.ClassifyId)
  408. if err != nil {
  409. if err.Error() == utils.ErrNoRow() {
  410. br.Msg = "分类不存在,请刷新页面"
  411. br.ErrMsg = "分类不存在,Err:" + err.Error()
  412. return
  413. }
  414. br.Msg = "移动失败"
  415. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  416. return
  417. }
  418. if materialClassifyInfo.ParentId != req.ParentClassifyId {
  419. count, err := material.GetMaterialClassifyNameNotSelfCount(materialClassifyInfo.ClassifyId, materialClassifyInfo.ClassifyName, materialClassifyInfo.ParentId)
  420. if err != nil {
  421. br.Msg = "判断名称是否已存在失败"
  422. br.ErrMsg = "判断名称是否已存在失败,Err:" + err.Error()
  423. return
  424. }
  425. if count > 0 {
  426. br.Msg = "移动失败,分类名称已存在"
  427. br.IsSendEmail = false
  428. return
  429. }
  430. }
  431. err, errMsg := materialService.MoveMaterialClassify(materialClassifyInfo, &req)
  432. if err != nil {
  433. br.Msg = errMsg
  434. if errMsg == "" {
  435. br.Msg = "移动失败"
  436. }
  437. br.ErrMsg = err.Error()
  438. }
  439. // todo权限校验
  440. //移动的是分类
  441. //判断上级id是否一致,如果不一致的话,那么需要移动该分类层级
  442. /*updateCol := make([]string, 0)
  443. if MaterialClassifyInfo.ParentId != req.ParentClassifyId && req.ParentClassifyId != 0 {
  444. parentChartClassifyInfo, err := material.GetMaterialClassifyById(req.ParentClassifyId)
  445. if err != nil {
  446. br.Msg = "移动失败"
  447. br.ErrMsg = "获取上级分类信息失败,Err:" + err.Error()
  448. return
  449. }
  450. MaterialClassifyInfo.ParentId = parentChartClassifyInfo.ClassifyId
  451. MaterialClassifyInfo.Level = parentChartClassifyInfo.Level + 1
  452. MaterialClassifyInfo.ModifyTime = time.Now()
  453. updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
  454. } else if MaterialClassifyInfo.ParentId != req.ParentClassifyId && req.ParentClassifyId == 0 {
  455. //改为一级分类
  456. MaterialClassifyInfo.ParentId = req.ParentClassifyId
  457. MaterialClassifyInfo.Level = 1
  458. MaterialClassifyInfo.ModifyTime = time.Now()
  459. updateCol = append(updateCol, "ParentId", "Level", "ModifyTime")
  460. }
  461. //如果有传入 上一个兄弟节点分类id
  462. if req.PrevId > 0 {
  463. if req.PrevType == 1 {
  464. //上一个节点是分类
  465. //上一个兄弟节点
  466. prevClassify, err := material.GetMaterialClassifyById(req.PrevId)
  467. if err != nil {
  468. br.Msg = "移动失败"
  469. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  470. return
  471. }
  472. //如果是移动在两个兄弟节点之间
  473. if req.NextId > 0 {
  474. if req.NextType == 1 {
  475. //上一个节点是分类 下一个节点是分类的情况
  476. //下一个兄弟节点
  477. nextClassify, err := material.GetMaterialClassifyById(req.NextId)
  478. if err != nil {
  479. br.Msg = "移动失败"
  480. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  481. return
  482. }
  483. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  484. if prevClassify.Sort == nextClassify.Sort || prevClassify.Sort == MaterialClassifyInfo.Sort {
  485. //变更兄弟节点的排序
  486. updateSortStr := `sort + 2`
  487. _ = material.UpdateMaterialClassifySortByParentId(prevClassify.ParentId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr)
  488. _ = material.UpdateMaterialSortByClassifyId(prevClassify.ClassifyId, prevClassify.Sort, 0, updateSortStr)
  489. } else {
  490. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  491. if nextClassify.Sort-prevClassify.Sort == 1 {
  492. //变更兄弟节点的排序
  493. updateSortStr := `sort + 1`
  494. _ = material.UpdateMaterialClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.Sort, updateSortStr)
  495. _ = material.UpdateMaterialSortByClassifyId(prevClassify.ClassifyId, prevClassify.Sort, 0, updateSortStr)
  496. }
  497. }
  498. } else {
  499. //上一个节点是分类 下一个节点是素材库的情况
  500. //下一个兄弟节点
  501. nextChartInfo, err := material.GetMaterialById(req.NextId)
  502. if err != nil {
  503. br.Msg = "移动失败"
  504. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  505. return
  506. }
  507. //如果上一个兄弟(分类)与下一个兄弟(素材库)的排序权重是一致的,那么需要将下一个兄弟(素材库)(以及下个兄弟(素材库)的同样排序权重)的排序权重+2,自己变成上一个兄弟(分类)的排序权重+1
  508. if prevClassify.Sort == nextChartInfo.Sort || prevClassify.Sort == MaterialClassifyInfo.Sort {
  509. //变更兄弟节点的排序
  510. updateSortStr := `sort + 2`
  511. _ = material.UpdateMaterialClassifySortByParentId(prevClassify.ParentId, prevClassify.ClassifyId, prevClassify.Sort, updateSortStr)
  512. _ = material.UpdateMaterialSortByClassifyId(prevClassify.ClassifyId, prevClassify.Sort, 0, updateSortStr)
  513. } else {
  514. //如果下一个兄弟(素材库)的排序权重正好是上个兄弟节点(分类)的下一层,那么需要再加一层了
  515. if nextChartInfo.Sort-prevClassify.Sort == 1 {
  516. //变更兄弟节点的排序
  517. updateSortStr := `sort + 1`
  518. _ = material.UpdateMaterialClassifySortByParentId(prevClassify.ParentId, 0, prevClassify.ClassifyId, updateSortStr)
  519. _ = material.UpdateMaterialSortByClassifyId(prevClassify.ClassifyId, prevClassify.Sort, 0, updateSortStr)
  520. }
  521. }
  522. }
  523. }
  524. MaterialClassifyInfo.Sort = prevClassify.Sort + 1
  525. MaterialClassifyInfo.ModifyTime = time.Now()
  526. updateCol = append(updateCol, "Sort", "ModifyTime")
  527. } else {
  528. //上一个节点是素材库
  529. prevMaterial, err := material.GetMaterialById(req.PrevId)
  530. if err != nil {
  531. br.Msg = "移动失败"
  532. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  533. return
  534. }
  535. //如果是移动在两个兄弟节点之间
  536. if req.NextId > 0 {
  537. if req.NextType == 1 {
  538. //上一个节点是素材库 下一个节点是分类的情况
  539. //下一个兄弟节点
  540. nextClassify, err := material.GetMaterialClassifyById(req.NextId)
  541. if err != nil {
  542. br.Msg = "移动失败"
  543. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  544. return
  545. }
  546. //如果上一个兄弟(素材库)与下一个兄弟(分类)的排序权重是一致的,那么需要将下一个兄弟(分类)(以及下个兄弟(分类)的同样排序权重)的排序权重+2,自己变成上一个兄弟(素材库)的排序权重+1
  547. if prevMaterial.Sort == nextClassify.Sort || prevMaterial.Sort == MaterialClassifyInfo.Sort {
  548. //变更兄弟节点的排序
  549. updateSortStr := `sort + 2`
  550. _ = material.UpdateMaterialClassifySortByParentId(prevMaterial.ClassifyId, 0, prevMaterial.Sort, updateSortStr)
  551. _ = material.UpdateMaterialSortByClassifyId(prevMaterial.ClassifyId, prevMaterial.Sort, prevMaterial.MaterialId, updateSortStr)
  552. } else {
  553. //如果下一个兄弟(分类)的排序权重正好是上个兄弟(素材库)节点的下一层,那么需要再加一层了
  554. if nextClassify.Sort-prevMaterial.Sort == 1 {
  555. //变更兄弟节点的排序
  556. updateSortStr := `sort + 1`
  557. _ = material.UpdateMaterialClassifySortByParentId(prevMaterial.ClassifyId, 0, prevMaterial.Sort, updateSortStr)
  558. _ = material.UpdateMaterialSortByClassifyId(prevMaterial.ClassifyId, prevMaterial.Sort, prevMaterial.MaterialId, updateSortStr)
  559. }
  560. }
  561. } else {
  562. //上一个节点是素材库 下一个节点是素材库的情况
  563. //下一个兄弟节点
  564. nextChartInfo, err := material.GetMaterialById(req.NextId)
  565. if err != nil {
  566. br.Msg = "移动失败"
  567. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  568. return
  569. }
  570. //如果上一个兄弟(素材库)与下一个兄弟(分类)的排序权重是一致的,那么需要将下一个兄弟(分类)(以及下个兄弟(分类)的同样排序权重)的排序权重+2,自己变成上一个兄弟(素材库)的排序权重+1
  571. if prevMaterial.Sort == nextChartInfo.Sort || prevMaterial.Sort == MaterialClassifyInfo.Sort {
  572. //变更兄弟节点的排序
  573. updateSortStr := `sort + 2`
  574. _ = material.UpdateMaterialClassifySortByParentId(prevMaterial.ClassifyId, 0, prevMaterial.Sort, updateSortStr)
  575. _ = material.UpdateMaterialSortByClassifyId(prevMaterial.ClassifyId, prevMaterial.Sort, prevMaterial.MaterialId, updateSortStr)
  576. } else {
  577. //如果下一个兄弟(分类)的排序权重正好是上个兄弟(素材库)节点的下一层,那么需要再加一层了
  578. if nextChartInfo.Sort-prevMaterial.Sort == 1 {
  579. //变更兄弟节点的排序
  580. updateSortStr := `sort + 1`
  581. _ = material.UpdateMaterialClassifySortByParentId(prevMaterial.ClassifyId, 0, prevMaterial.Sort, updateSortStr)
  582. _ = material.UpdateMaterialSortByClassifyId(prevMaterial.ClassifyId, prevMaterial.Sort, prevMaterial.MaterialId, updateSortStr)
  583. }
  584. }
  585. }
  586. }
  587. MaterialClassifyInfo.Sort = prevMaterial.Sort + 1
  588. MaterialClassifyInfo.ModifyTime = time.Now()
  589. updateCol = append(updateCol, "Sort", "ModifyTime")
  590. }
  591. } else {
  592. firstClassify, err := material.GetFirstMaterialClassifyByParentId(MaterialClassifyInfo.ParentId)
  593. if err != nil && err.Error() != utils.ErrNoRow() {
  594. br.Msg = "移动失败"
  595. br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
  596. return
  597. }
  598. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  599. if firstClassify != nil && firstClassify.Sort == 0 {
  600. updateSortStr := ` sort + 1 `
  601. _ = material.UpdateMaterialClassifySortByParentId(firstClassify.ParentId, firstClassify.ClassifyId-1, 0, updateSortStr)
  602. }
  603. MaterialClassifyInfo.Sort = 0 //那就是排在第一位
  604. MaterialClassifyInfo.ModifyTime = time.Now()
  605. updateCol = append(updateCol, "Sort", "ModifyTime")
  606. }
  607. //更新
  608. if len(updateCol) > 0 {
  609. err = MaterialClassifyInfo.Update(updateCol)
  610. if err != nil {
  611. br.Msg = "移动失败"
  612. br.ErrMsg = "修改失败,Err:" + err.Error()
  613. return
  614. }
  615. // todo 记录整个层级的分类ID
  616. }*/
  617. br.Ret = 200
  618. br.Success = true
  619. br.Msg = "移动成功"
  620. }
  621. // List
  622. // @Title 素材列表接口
  623. // @Description 素材列表接口
  624. // @Param PageSize query int true "每页数据条数"
  625. // @Param CurrentIndex query int true "当前页页码,从1开始"
  626. // @Param ClassifyId query int true "分类id"
  627. // @Param Keyword query string true "搜索关键词"
  628. // @Param IsShowMe query bool true "是否只看我的,true、false"
  629. // @Success 200 {object} data_manage.ChartListResp
  630. // @router /list [get]
  631. func (this *MaterialController) List() {
  632. br := new(models.BaseResponse).Init()
  633. defer func() {
  634. this.Data["json"] = br
  635. this.ServeJSON()
  636. }()
  637. sysUser := this.SysUser
  638. if sysUser == nil {
  639. br.Msg = "请登录"
  640. br.ErrMsg = "请登录,SysUser Is Empty"
  641. br.Ret = 408
  642. return
  643. }
  644. classifyId, _ := this.GetInt("ClassifyId")
  645. pageSize, _ := this.GetInt("PageSize")
  646. currentIndex, _ := this.GetInt("CurrentIndex")
  647. keyword := this.GetString("Keyword")
  648. var total int
  649. page := paging.GetPaging(currentIndex, pageSize, total)
  650. var startSize int
  651. if pageSize <= 0 {
  652. pageSize = utils.PageSize20
  653. }
  654. if currentIndex <= 0 {
  655. currentIndex = 1
  656. }
  657. startSize = paging.StartIndex(currentIndex, pageSize)
  658. var condition string
  659. var pars []interface{}
  660. classifyIds := make([]string, 0)
  661. childClassifyMap := make(map[int]*material.MaterialClassify)
  662. if classifyId > 0 {
  663. classifyIds = append(classifyIds, strconv.Itoa(classifyId))
  664. // 查询当前的分类
  665. classifyInfo, err := material.GetMaterialClassifyById(classifyId)
  666. if err != nil {
  667. br.Msg = "分类不存在"
  668. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  669. return
  670. }
  671. // 获取所有子分类
  672. childList, e := material.GetMaterialClassifyByLevelPath(classifyInfo.LevelPath)
  673. if e != nil {
  674. err = fmt.Errorf("保存分类失败,Err:" + e.Error())
  675. return
  676. }
  677. // 把原先的父级levePath,替换成最新的父级序列
  678. classifyIdMap := make(map[string]struct{})
  679. for _, tmp := range childList {
  680. childClassifyMap[tmp.ClassifyId] = tmp
  681. //获取字符串前缀的位置
  682. after, _ := strings.CutPrefix(tmp.LevelPath, classifyInfo.LevelPath)
  683. fmt.Println("after", after)
  684. // 拼接字符串
  685. if after != "" {
  686. ids := strings.Split(after, ",")
  687. for _, v := range ids {
  688. if _, ok := classifyIdMap[v]; !ok {
  689. classifyIds = append(classifyIds, v)
  690. classifyIdMap[v] = struct{}{}
  691. }
  692. }
  693. }
  694. }
  695. }
  696. if len(classifyIds) > 0 {
  697. condition += " AND classify_id IN(" + utils.GetOrmInReplace(len(classifyIds)) + ") "
  698. pars = append(pars, classifyIds)
  699. }
  700. if keyword != "" {
  701. switch this.Lang {
  702. case utils.LANG_EN:
  703. condition += ` AND ( material_name_en LIKE '%` + keyword + `%' )`
  704. default:
  705. condition += ` AND ( material_name LIKE '%` + keyword + `%' )`
  706. }
  707. }
  708. //只看我的
  709. isShowMe, _ := this.GetBool("IsShowMe")
  710. if isShowMe {
  711. condition += ` AND sys_user_id = ? `
  712. pars = append(pars, sysUser.AdminId)
  713. }
  714. //获取图表信息
  715. list, err := material.GetMaterialListPageByCondition(condition, pars, startSize, pageSize)
  716. if err != nil && err.Error() != utils.ErrNoRow() {
  717. br.Success = true
  718. br.Msg = "获取素材库信息失败"
  719. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  720. return
  721. }
  722. resp := new(material.MaterialListResp)
  723. if list == nil || len(list) <= 0 || (err != nil && err.Error() == utils.ErrNoRow()) {
  724. items := make([]*material.MaterialListItems, 0)
  725. resp.Paging = page
  726. resp.List = items
  727. br.Ret = 200
  728. br.Success = true
  729. br.Msg = "获取成功"
  730. br.Data = resp
  731. return
  732. }
  733. dataCount, err := material.GetMaterialListCountByCondition(condition, pars)
  734. if err != nil && err.Error() != utils.ErrNoRow() {
  735. br.Msg = "获取指标信息失败"
  736. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  737. return
  738. }
  739. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  740. resp.Paging = page
  741. resp.List = list
  742. br.Ret = 200
  743. br.Success = true
  744. br.Msg = "获取成功"
  745. br.Data = resp
  746. }
  747. // SaveAsMaterial
  748. // @Title 将图表等封面上传至素材库
  749. // @Description 将图表等封面上传至素材库
  750. // @Param request body material.AddAndEditSandbox true "type json string"
  751. // @Success 200 {object} material.Material
  752. // @router /saveAs [post]
  753. func (this *MaterialController) SaveAsMaterial() {
  754. br := new(models.BaseResponse).Init()
  755. defer func() {
  756. this.Data["json"] = br
  757. this.ServeJSON()
  758. }()
  759. sysUser := this.SysUser
  760. if sysUser == nil {
  761. br.Msg = "请登录"
  762. br.ErrMsg = "请登录,SysUser Is Empty"
  763. br.Ret = 408
  764. return
  765. }
  766. var req material.SaveAsMaterialReq
  767. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  768. if err != nil {
  769. br.Msg = "参数解析异常!"
  770. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  771. return
  772. }
  773. if req.MaterialName == "" {
  774. br.Msg = "缺少素材库名称"
  775. return
  776. }
  777. var exist *material.Material
  778. switch this.Lang {
  779. case utils.LANG_EN:
  780. exist, err = material.GetMaterialByNameEn(req.MaterialName)
  781. default:
  782. // 判断名称是否重复
  783. exist, err = material.GetMaterialByName(req.MaterialName)
  784. }
  785. if err != nil && err.Error() != utils.ErrNoRow() {
  786. br.Msg = "获取失败"
  787. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  788. return
  789. }
  790. if err == nil && exist.MaterialId > 0 {
  791. br.Msg = "图片名称已存在"
  792. return
  793. }
  794. err, errMsg := materialService.AddToMaterial(req, sysUser.AdminId, sysUser.RealName)
  795. if err != nil {
  796. br.Msg = "保存失败!"
  797. if errMsg != `` {
  798. br.Msg = errMsg
  799. }
  800. br.ErrMsg = "保存失败,Err:" + err.Error()
  801. return
  802. }
  803. msg := "保存成功"
  804. br.Ret = 200
  805. br.Success = true
  806. br.Msg = msg
  807. }
  808. // MyChartSaveAsMaterial
  809. // @Title 将我的图表等封面上传至素材库
  810. // @Description 将图表等封面上传至素材库
  811. // @Param request body material.AddAndEditSandbox true "type json string"
  812. // @Success 200 {object} material.Material
  813. // @router /my_chart/saveAs [post]
  814. func (this *MaterialController) MyChartSaveAsMaterial() {
  815. br := new(models.BaseResponse).Init()
  816. defer func() {
  817. this.Data["json"] = br
  818. this.ServeJSON()
  819. }()
  820. sysUser := this.SysUser
  821. if sysUser == nil {
  822. br.Msg = "请登录"
  823. br.ErrMsg = "请登录,SysUser Is Empty"
  824. br.Ret = 408
  825. return
  826. }
  827. var req material.MyChartSaveAsMaterialReq
  828. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  829. if err != nil {
  830. br.Msg = "参数解析异常!"
  831. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  832. return
  833. }
  834. materialNames := make([]string, 0)
  835. namesMap := make(map[string]struct{})
  836. for _, v := range req.MaterialList {
  837. if v.ClassifyId <= 0 {
  838. br.Msg = "请选择分类"
  839. return
  840. }
  841. if v.MaterialName == "" {
  842. br.Msg = "请填写图片名称"
  843. return
  844. }
  845. if v.ChartInfoId <= 0 {
  846. br.Msg = "请选择图表"
  847. return
  848. }
  849. if v.MyChartId <= 0 {
  850. br.Msg = "请选择我的图表"
  851. return
  852. }
  853. if _, ok := namesMap[v.MaterialName]; ok {
  854. br.Msg = "图片名称不能重复"
  855. return
  856. }
  857. namesMap[v.MaterialName] = struct{}{}
  858. materialNames = append(materialNames, v.MaterialName)
  859. }
  860. if len(materialNames) == 0 {
  861. br.Msg = "请填写图片名称"
  862. return
  863. }
  864. existList := make([]*material.Material, 0)
  865. switch this.Lang {
  866. case utils.LANG_EN:
  867. existList, err = material.GetMaterialByNameEns(materialNames)
  868. if err != nil {
  869. br.Msg = "获取失败"
  870. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  871. return
  872. }
  873. if len(existList) > 0 {
  874. msg := "图片名称:"
  875. for _, v := range existList {
  876. msg += v.MaterialNameEn + " "
  877. }
  878. br.Msg = fmt.Sprintf("%s 已存在", msg)
  879. return
  880. }
  881. default:
  882. // 判断文件名是否已存在
  883. existList, err = material.GetMaterialByNames(materialNames)
  884. if err != nil {
  885. br.Msg = "获取失败"
  886. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  887. return
  888. }
  889. if len(existList) > 0 {
  890. msg := "图片名称:"
  891. for _, v := range existList {
  892. msg += v.MaterialName + " "
  893. }
  894. br.Msg = fmt.Sprintf("%s 已存在", msg)
  895. return
  896. }
  897. }
  898. if len(req.MaterialList) > 30 {
  899. br.Msg = "最多支持选择30个图表"
  900. return
  901. }
  902. err, errMsg := materialService.MyChartAddToMaterial(req, sysUser.AdminId, sysUser.RealName)
  903. if err != nil {
  904. br.Msg = "保存失败!"
  905. if errMsg != `` {
  906. br.Msg = errMsg
  907. }
  908. br.ErrMsg = "保存失败,Err:" + err.Error()
  909. return
  910. }
  911. msg := "保存成功"
  912. br.Ret = 200
  913. br.Success = true
  914. br.Msg = msg
  915. }
  916. // Delete
  917. // @Title 删除素材库
  918. // @Description 删除素材库接口
  919. // @Param request body material.DeleteSandbox true "type json string"
  920. // @Success 200 标记成功
  921. // @router /del [post]
  922. func (this *MaterialController) Delete() {
  923. br := new(models.BaseResponse).Init()
  924. defer func() {
  925. this.Data["json"] = br
  926. this.ServeJSON()
  927. }()
  928. sysUser := this.SysUser
  929. if sysUser == nil {
  930. br.Msg = "请登录"
  931. br.ErrMsg = "请登录,SysUser Is Empty"
  932. br.Ret = 408
  933. return
  934. }
  935. var req material.DeleteMaterial
  936. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  937. if err != nil {
  938. br.Msg = "参数解析异常!"
  939. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  940. return
  941. }
  942. if req.MaterialId <= 0 {
  943. br.Msg = "缺少素材库编号"
  944. return
  945. }
  946. //删除素材库
  947. err = material.DeleteByMaterialIds([]int{req.MaterialId})
  948. if err != nil {
  949. br.Msg = "操作失败"
  950. br.ErrMsg = "操作失败,Err:" + err.Error()
  951. return
  952. }
  953. msg := "删除成功"
  954. br.Ret = 200
  955. br.Success = true
  956. br.Msg = msg
  957. }
  958. // BatchDelete
  959. // @Title 批量删除素材库
  960. // @Description 删除素材库接口
  961. // @Param request body material.DeleteSandbox true "type json string"
  962. // @Success 200 标记成功
  963. // @router /batch/del [post]
  964. func (this *MaterialController) BatchDelete() {
  965. br := new(models.BaseResponse).Init()
  966. defer func() {
  967. this.Data["json"] = br
  968. this.ServeJSON()
  969. }()
  970. sysUser := this.SysUser
  971. if sysUser == nil {
  972. br.Msg = "请登录"
  973. br.ErrMsg = "请登录,SysUser Is Empty"
  974. br.Ret = 408
  975. return
  976. }
  977. var req material.BatchDeleteMaterialReq
  978. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  979. if err != nil {
  980. br.Msg = "参数解析异常!"
  981. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  982. return
  983. }
  984. deleteMaterialIds := make([]int, 0)
  985. if req.IsSelectAll {
  986. classifyId := req.ClassifyId
  987. keyword := req.Keyword
  988. isShowMe := req.IsShowMe
  989. //获取图表信息
  990. list, e, msg := materialService.GetBatchSelectedMaterialList(classifyId, keyword, isShowMe, sysUser, this.Lang)
  991. if e != nil {
  992. br.Msg = "获取素材库信息失败"
  993. if msg != "" {
  994. br.Msg = msg
  995. }
  996. br.ErrMsg = "获取素材库信息失败,Err:" + e.Error()
  997. return
  998. }
  999. notSelectIds := make(map[int]struct{})
  1000. if len(req.MaterialIds) >= 0 {
  1001. for _, v := range req.MaterialIds {
  1002. notSelectIds[v] = struct{}{}
  1003. }
  1004. }
  1005. for _, v := range list {
  1006. if _, ok := notSelectIds[v.MaterialId]; !ok {
  1007. deleteMaterialIds = append(deleteMaterialIds, v.MaterialId)
  1008. }
  1009. }
  1010. } else {
  1011. deleteMaterialIds = req.MaterialIds
  1012. }
  1013. if len(deleteMaterialIds) <= 0 {
  1014. br.Msg = "请选择删除的素材"
  1015. return
  1016. }
  1017. //删除素材库
  1018. err = material.DeleteByMaterialIds(deleteMaterialIds)
  1019. if err != nil {
  1020. br.Msg = err.Error()
  1021. return
  1022. }
  1023. msg := "删除成功"
  1024. br.Ret = 200
  1025. br.Success = true
  1026. br.Msg = msg
  1027. }
  1028. // MaterialClassifyList
  1029. // @Title 获取所有素材库分类接口-不包含素材库
  1030. // @Description 获取所有素材库分类接口-不包含素材库
  1031. // @Param IsShowMe query bool true "是否只看我的,true、false"
  1032. // @Success 200 {object} data_manage.ChartClassifyListResp
  1033. // @router /classify/list [get]
  1034. func (this *MaterialController) MaterialClassifyList() {
  1035. br := new(models.BaseResponse).Init()
  1036. defer func() {
  1037. this.Data["json"] = br
  1038. this.ServeJSON()
  1039. }()
  1040. resp := new(material.MaterialClassifyListResp)
  1041. rootList, err := material.GetMaterialClassifyByParentId(0)
  1042. if err != nil && err.Error() != utils.ErrNoRow() {
  1043. br.Msg = "获取失败"
  1044. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1045. return
  1046. }
  1047. classifyAll, err := material.GetMaterialClassifyAll()
  1048. if err != nil && err.Error() != utils.ErrNoRow() {
  1049. br.Msg = "获取失败"
  1050. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1051. return
  1052. }
  1053. nodeAll := make([]*material.MaterialClassifyItems, 0)
  1054. for k := range rootList {
  1055. rootNode := rootList[k]
  1056. materialService.MaterialClassifyItemsMakeTree(this.SysUser, classifyAll, rootNode)
  1057. nodeAll = append(nodeAll, rootNode)
  1058. }
  1059. resp.AllNodes = nodeAll
  1060. br.Ret = 200
  1061. br.Success = true
  1062. br.Msg = "获取成功"
  1063. br.Data = resp
  1064. }
  1065. // BatchAdd
  1066. // @Title 批量新增素材
  1067. // @Description 新增/编辑保存素材库接口
  1068. // @Param request body material.AddAndEditSandbox true "type json string"
  1069. // @Success 200 {object} material.Material
  1070. // @router /batch/add [post]
  1071. func (this *MaterialController) BatchAdd() {
  1072. br := new(models.BaseResponse).Init()
  1073. defer func() {
  1074. this.Data["json"] = br
  1075. this.ServeJSON()
  1076. }()
  1077. sysUser := this.SysUser
  1078. if sysUser == nil {
  1079. br.Msg = "请登录"
  1080. br.ErrMsg = "请登录,SysUser Is Empty"
  1081. br.Ret = 408
  1082. return
  1083. }
  1084. var req material.BatchAddMaterialReq
  1085. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1086. if err != nil {
  1087. br.Msg = "参数解析异常!"
  1088. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1089. return
  1090. }
  1091. classifyId := req.ClassifyId
  1092. if classifyId <= 0 {
  1093. br.Msg = "请选择分类"
  1094. return
  1095. }
  1096. materialNames := make([]string, 0)
  1097. namesMap := make(map[string]struct{})
  1098. for _, v := range req.MaterialList {
  1099. if v.MaterialName == "" {
  1100. br.Msg = "请填写图片名称"
  1101. return
  1102. }
  1103. if v.ImgUrl == "" {
  1104. br.Msg = "请上传图片"
  1105. return
  1106. }
  1107. if _, ok := namesMap[v.MaterialName]; ok {
  1108. br.Msg = "图片名称不能重复"
  1109. return
  1110. }
  1111. namesMap[v.MaterialName] = struct{}{}
  1112. materialNames = append(materialNames, v.MaterialName)
  1113. }
  1114. if len(materialNames) == 0 {
  1115. br.Msg = "请填写图片名称"
  1116. return
  1117. }
  1118. existList := make([]*material.Material, 0)
  1119. switch this.Lang {
  1120. case utils.LANG_EN:
  1121. existList, err = material.GetMaterialByNameEns(materialNames)
  1122. if err != nil {
  1123. br.Msg = "获取失败"
  1124. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1125. return
  1126. }
  1127. if len(existList) > 0 {
  1128. msg := "图片名称:"
  1129. for _, v := range existList {
  1130. msg += v.MaterialNameEn + " "
  1131. }
  1132. br.Msg = fmt.Sprintf("%s 已存在", msg)
  1133. return
  1134. }
  1135. default:
  1136. // 判断文件名是否已存在
  1137. existList, err = material.GetMaterialByNames(materialNames)
  1138. if err != nil {
  1139. br.Msg = "获取失败"
  1140. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1141. return
  1142. }
  1143. if len(existList) > 0 {
  1144. msg := "图片名称:"
  1145. for _, v := range existList {
  1146. msg += v.MaterialName + " "
  1147. }
  1148. br.Msg = fmt.Sprintf("%s 已存在", msg)
  1149. return
  1150. }
  1151. }
  1152. err = materialService.BatchAddMaterial(req.MaterialList, classifyId, sysUser.AdminId, sysUser.AdminName)
  1153. if err != nil {
  1154. br.Msg = "文件上传失败"
  1155. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  1156. return
  1157. }
  1158. msg := "添加成功"
  1159. br.Ret = 200
  1160. br.Success = true
  1161. br.Msg = msg
  1162. }
  1163. // BatchChangeClassify
  1164. // @Title 批量更换分类
  1165. // @Description 批量更换分类
  1166. // @Param request body material.DeleteSandbox true "type json string"
  1167. // @Success 200 标记成功
  1168. // @router /batch/changeClassify [post]
  1169. func (this *MaterialController) BatchChangeClassify() {
  1170. br := new(models.BaseResponse).Init()
  1171. defer func() {
  1172. this.Data["json"] = br
  1173. this.ServeJSON()
  1174. }()
  1175. sysUser := this.SysUser
  1176. if sysUser == nil {
  1177. br.Msg = "请登录"
  1178. br.ErrMsg = "请登录,SysUser Is Empty"
  1179. br.Ret = 408
  1180. return
  1181. }
  1182. var req material.BatchChangeClassifyMaterialReq
  1183. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1184. if err != nil {
  1185. br.Msg = "参数解析异常!"
  1186. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1187. return
  1188. }
  1189. if req.NewClassifyId <= 0 {
  1190. br.Msg = "请选择新的分类"
  1191. return
  1192. }
  1193. updateMaterialIds := make([]int, 0)
  1194. if req.IsSelectAll {
  1195. classifyId := req.ClassifyId
  1196. keyword := req.Keyword
  1197. isShowMe := req.IsShowMe
  1198. //获取图表信息
  1199. list, e, msg := materialService.GetBatchSelectedMaterialList(classifyId, keyword, isShowMe, sysUser, this.Lang)
  1200. if e != nil {
  1201. br.Msg = "获取素材库信息失败"
  1202. if msg != "" {
  1203. br.Msg = msg
  1204. }
  1205. br.ErrMsg = "获取素材库信息失败,Err:" + e.Error()
  1206. return
  1207. }
  1208. notSelectIds := make(map[int]struct{})
  1209. if len(req.MaterialIds) >= 0 {
  1210. for _, v := range req.MaterialIds {
  1211. notSelectIds[v] = struct{}{}
  1212. }
  1213. }
  1214. for _, v := range list {
  1215. if _, ok := notSelectIds[v.MaterialId]; !ok {
  1216. updateMaterialIds = append(updateMaterialIds, v.MaterialId)
  1217. }
  1218. }
  1219. } else {
  1220. updateMaterialIds = req.MaterialIds
  1221. }
  1222. if len(updateMaterialIds) <= 0 {
  1223. br.Msg = "请选择变更的素材"
  1224. return
  1225. }
  1226. // 判断新分类是否存在
  1227. _, err = material.GetMaterialClassifyById(req.NewClassifyId)
  1228. if err != nil {
  1229. if err.Error() == utils.ErrNoRow() {
  1230. br.Msg = "分类不存在"
  1231. return
  1232. }
  1233. br.Msg = "获取分类信息失败"
  1234. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  1235. }
  1236. //更换分类素材库
  1237. err = material.UpdateClassifyByMaterialIds(updateMaterialIds, req.NewClassifyId, time.Now())
  1238. if err != nil {
  1239. br.Msg = err.Error()
  1240. return
  1241. }
  1242. msg := "操作成功"
  1243. br.Ret = 200
  1244. br.Success = true
  1245. br.Msg = msg
  1246. }
  1247. // ChangeClassify
  1248. // @Title 更换分类
  1249. // @Description 批量更换分类
  1250. // @Param request body material.DeleteSandbox true "type json string"
  1251. // @Success 200 标记成功
  1252. // @router /changeClassify [post]
  1253. func (this *MaterialController) ChangeClassify() {
  1254. br := new(models.BaseResponse).Init()
  1255. defer func() {
  1256. this.Data["json"] = br
  1257. this.ServeJSON()
  1258. }()
  1259. sysUser := this.SysUser
  1260. if sysUser == nil {
  1261. br.Msg = "请登录"
  1262. br.ErrMsg = "请登录,SysUser Is Empty"
  1263. br.Ret = 408
  1264. return
  1265. }
  1266. var req material.ChangeClassifyMaterialReq
  1267. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1268. if err != nil {
  1269. br.Msg = "参数解析异常!"
  1270. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1271. return
  1272. }
  1273. if req.MaterialId <= 0 {
  1274. br.Msg = "缺少素材库编号"
  1275. return
  1276. }
  1277. if req.NewClassifyId <= 0 {
  1278. br.Msg = "请选择新的分类"
  1279. return
  1280. }
  1281. // 判断素材是否存在
  1282. info, err := material.GetMaterialById(req.MaterialId)
  1283. if err != nil {
  1284. if err.Error() == utils.ErrNoRow() {
  1285. br.Msg = "素材不存在"
  1286. return
  1287. }
  1288. br.Msg = "获取素材库信息失败"
  1289. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1290. return
  1291. }
  1292. if req.NewClassifyId == info.ClassifyId {
  1293. br.Msg = "请选择不同的分类"
  1294. return
  1295. }
  1296. // 判断新分类是否存在
  1297. _, err = material.GetMaterialClassifyById(req.NewClassifyId)
  1298. if err != nil {
  1299. if err.Error() == utils.ErrNoRow() {
  1300. br.Msg = "分类不存在"
  1301. return
  1302. }
  1303. br.Msg = "获取分类信息失败"
  1304. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  1305. }
  1306. info.ClassifyId = req.NewClassifyId
  1307. info.ModifyTime = time.Now()
  1308. //更换分类素材库
  1309. err = info.Update([]string{"ClassifyId", "ModifyTime"})
  1310. if err != nil {
  1311. br.Msg = err.Error()
  1312. return
  1313. }
  1314. msg := "操作成功"
  1315. br.Ret = 200
  1316. br.Success = true
  1317. br.Msg = msg
  1318. }
  1319. // Rename
  1320. // @Title 素材重命名
  1321. // @Description 素材重命名
  1322. // @Param request body material.DeleteSandbox true "type json string"
  1323. // @Success 200 标记成功
  1324. // @router /rename [post]
  1325. func (this *MaterialController) Rename() {
  1326. br := new(models.BaseResponse).Init()
  1327. defer func() {
  1328. this.Data["json"] = br
  1329. this.ServeJSON()
  1330. }()
  1331. sysUser := this.SysUser
  1332. if sysUser == nil {
  1333. br.Msg = "请登录"
  1334. br.ErrMsg = "请登录,SysUser Is Empty"
  1335. br.Ret = 408
  1336. return
  1337. }
  1338. var req material.RenameMaterialReq
  1339. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1340. if err != nil {
  1341. br.Msg = "参数解析异常!"
  1342. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1343. return
  1344. }
  1345. if req.MaterialId <= 0 {
  1346. br.Msg = "缺少素材库编号"
  1347. return
  1348. }
  1349. if req.MaterialName == "" {
  1350. br.Msg = "缺少素材库名称"
  1351. return
  1352. }
  1353. // 判断素材是否存在
  1354. info, err := material.GetMaterialById(req.MaterialId)
  1355. if err != nil {
  1356. if err.Error() == utils.ErrNoRow() {
  1357. br.Msg = "素材不存在"
  1358. return
  1359. }
  1360. br.Msg = "获取素材库信息失败"
  1361. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1362. return
  1363. }
  1364. var exist *material.Material
  1365. updateStr := make([]string, 0)
  1366. switch this.Lang {
  1367. case utils.LANG_EN:
  1368. // 判断名称是否重复
  1369. if info.MaterialNameEn == req.MaterialName {
  1370. br.Msg = "名称未修改"
  1371. return
  1372. }
  1373. exist, err = material.GetMaterialByNameEn(req.MaterialName)
  1374. info.MaterialNameEn = req.MaterialName
  1375. updateStr = append(updateStr, "MaterialNameEn")
  1376. default:
  1377. // 判断名称是否重复
  1378. if info.MaterialName == req.MaterialName {
  1379. br.Msg = "名称未修改"
  1380. return
  1381. }
  1382. exist, err = material.GetMaterialByName(req.MaterialName)
  1383. info.MaterialName = req.MaterialName
  1384. updateStr = append(updateStr, "MaterialName")
  1385. }
  1386. if err != nil && err.Error() != utils.ErrNoRow() {
  1387. br.Msg = "获取失败"
  1388. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1389. return
  1390. }
  1391. if err == nil && exist.MaterialId > 0 {
  1392. br.Msg = "图片名称已存在"
  1393. return
  1394. }
  1395. info.ModifyTime = time.Now()
  1396. updateStr = append(updateStr, "ModifyTime")
  1397. //更换分类素材库
  1398. err = info.Update(updateStr)
  1399. if err != nil {
  1400. br.Msg = err.Error()
  1401. return
  1402. }
  1403. msg := "操作成功"
  1404. br.Ret = 200
  1405. br.Success = true
  1406. br.Msg = msg
  1407. }
  1408. // BatchDownload
  1409. // @Title 批量下载素材
  1410. // @Description 批量下载素材
  1411. // @Param request body material.DeleteSandbox true "type json string"
  1412. // @Success 200 标记成功
  1413. // @router /batch/download [get]
  1414. func (this *MaterialController) BatchDownload() {
  1415. br := new(models.BaseResponse).Init()
  1416. defer func() {
  1417. this.Data["json"] = br
  1418. this.ServeJSON()
  1419. }()
  1420. sysUser := this.SysUser
  1421. if sysUser == nil {
  1422. br.Msg = "请登录"
  1423. br.ErrMsg = "请登录,SysUser Is Empty"
  1424. br.Ret = 408
  1425. return
  1426. }
  1427. //var req material.BatchDeleteMaterialReq
  1428. materialIdStr := this.GetString("MaterialIds")
  1429. isSelectAll, _ := this.GetBool("IsSelectAll")
  1430. classifyId, _ := this.GetInt("ClassifyId")
  1431. keyword := this.GetString("Keyword")
  1432. isShowMe, _ := this.GetBool("IsShowMe")
  1433. downMaterialList := make([]*material.MaterialListItems, 0)
  1434. materialIds := strings.Split(materialIdStr, ",")
  1435. var err error
  1436. if isSelectAll {
  1437. //获取图表信息
  1438. list, e, msg := materialService.GetBatchSelectedMaterialList(classifyId, keyword, isShowMe, sysUser, this.Lang)
  1439. if e != nil {
  1440. br.Msg = "获取素材库信息失败"
  1441. if msg != "" {
  1442. br.Msg = msg
  1443. }
  1444. br.ErrMsg = "获取素材库信息失败,Err:" + e.Error()
  1445. return
  1446. }
  1447. notSelectIds := make(map[int]struct{})
  1448. if len(materialIds) >= 0 {
  1449. //转成数组
  1450. for _, v := range materialIds {
  1451. id, _ := strconv.Atoi(v)
  1452. notSelectIds[id] = struct{}{}
  1453. }
  1454. }
  1455. for _, v := range list {
  1456. if _, ok := notSelectIds[v.MaterialId]; !ok {
  1457. downMaterialList = append(downMaterialList, v)
  1458. }
  1459. }
  1460. } else {
  1461. if len(materialIds) > 0 {
  1462. // 批量查询指标数据
  1463. materialIdsInt := make([]int, 0)
  1464. for _, v := range materialIds {
  1465. id, _ := strconv.Atoi(v)
  1466. materialIdsInt = append(materialIdsInt, id)
  1467. }
  1468. downMaterialList, err = material.GetMaterialByIds(materialIdsInt)
  1469. if err != nil {
  1470. br.Msg = "获取素材库信息失败"
  1471. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1472. return
  1473. }
  1474. }
  1475. }
  1476. if len(downMaterialList) <= 0 {
  1477. br.Msg = "请选择要下载的素材"
  1478. return
  1479. }
  1480. // 创建zip
  1481. zipName := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
  1482. savePath := zipName
  1483. zipFile, err := os.Create(zipName)
  1484. if err != nil {
  1485. br.Msg = "操作失败"
  1486. br.ErrMsg = "生成压缩文件失败, Err: " + err.Error()
  1487. return
  1488. }
  1489. zipWriter := zip.NewWriter(zipFile)
  1490. // 生成zip过程中报错关闭
  1491. defer func() {
  1492. if err != nil {
  1493. _ = zipWriter.Close()
  1494. _ = zipFile.Close()
  1495. }
  1496. _ = os.Remove(savePath)
  1497. }()
  1498. // 获取资源, 写入zip
  1499. zipFileName := ""
  1500. for i := range downMaterialList {
  1501. if downMaterialList[i].MaterialName == "" || downMaterialList[i].ImgUrl == "" {
  1502. continue
  1503. }
  1504. fmt.Printf("开始压缩第%d个文件\n", i+1)
  1505. dotIndex := strings.LastIndex(downMaterialList[i].ImgUrl, ".")
  1506. // 如果找不到点,或者点是文件名的第一个字符(不合法情况),则返回空字符串
  1507. if dotIndex == -1 || dotIndex == 0 {
  1508. continue
  1509. }
  1510. fileExt := downMaterialList[i].ImgUrl[dotIndex+1:]
  1511. ioWriter, err := zipWriter.Create(fmt.Sprintf("%s.%s", downMaterialList[i].MaterialName, fileExt))
  1512. if err != nil {
  1513. if os.IsPermission(err) {
  1514. br.Msg = "操作失败"
  1515. br.ErrMsg = "打包权限不足, Err: " + err.Error()
  1516. return
  1517. }
  1518. br.Msg = "操作失败"
  1519. br.ErrMsg = "压缩出错, Err: " + err.Error()
  1520. return
  1521. }
  1522. var content []byte
  1523. content, err = http.Get(downMaterialList[i].ImgUrl)
  1524. if err != nil {
  1525. br.Msg = "操作失败"
  1526. br.ErrMsg = "资源获取失败, Err: " + err.Error()
  1527. return
  1528. }
  1529. _, err = ioWriter.Write(content)
  1530. if err != nil {
  1531. br.Msg = "操作失败"
  1532. br.ErrMsg = "压缩文件写入失败, Err: " + err.Error()
  1533. return
  1534. }
  1535. if zipFileName == "" {
  1536. zipFileName = downMaterialList[i].MaterialName
  1537. }
  1538. fmt.Printf("第%d个文件写入成功\n", i+1)
  1539. }
  1540. // 生成zip后关闭,否则下载文件会损坏
  1541. _ = zipWriter.Close()
  1542. _ = zipFile.Close()
  1543. this.Ctx.Output.Download(savePath, fmt.Sprintf("%s.zip", zipFileName))
  1544. msg := "操作成功"
  1545. br.Ret = 200
  1546. br.Success = true
  1547. br.Msg = msg
  1548. }
  1549. // Download
  1550. // @Title 下载素材
  1551. // @Description 下载素材
  1552. // @Param request body material.DeleteSandbox true "type json string"
  1553. // @Success 200 标记成功
  1554. // @router /download [get]
  1555. func (this *MaterialController) Download() {
  1556. br := new(models.BaseResponse).Init()
  1557. defer func() {
  1558. this.Data["json"] = br
  1559. this.ServeJSON()
  1560. }()
  1561. sysUser := this.SysUser
  1562. if sysUser == nil {
  1563. br.Msg = "请登录"
  1564. br.ErrMsg = "请登录,SysUser Is Empty"
  1565. br.Ret = 408
  1566. return
  1567. }
  1568. materialId, _ := this.GetInt("MaterialId")
  1569. if materialId <= 0 {
  1570. br.Msg = "缺少素材库编号"
  1571. return
  1572. }
  1573. // 判断素材是否存在
  1574. info, err := material.GetMaterialById(materialId)
  1575. if err != nil {
  1576. if err.Error() == utils.ErrNoRow() {
  1577. br.Msg = "素材不存在"
  1578. return
  1579. }
  1580. br.Msg = "获取素材库信息失败"
  1581. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1582. return
  1583. }
  1584. if info.ImgUrl == "" {
  1585. br.Msg = "素材地址为空"
  1586. return
  1587. }
  1588. fileName := info.MaterialName
  1589. // 查找文件名中最后一个点的位置
  1590. dotIndex := strings.LastIndex(info.ImgUrl, ".")
  1591. // 如果找不到点,或者点是文件名的第一个字符(不合法情况),则返回空字符串
  1592. if dotIndex == -1 || dotIndex == 0 {
  1593. br.Msg = "素材地址错误"
  1594. return
  1595. }
  1596. fileName += "." + info.ImgUrl[dotIndex+1:] // 添加扩展名到文件名
  1597. // 获取路径中的文件名
  1598. urlFileName := path.Base(info.ImgUrl)
  1599. uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
  1600. if e := os.MkdirAll(uploadDir, utils.DIR_MOD); e != nil {
  1601. br.Msg = "存储目录创建失败"
  1602. br.ErrMsg = "存储目录创建失败, Err:" + e.Error()
  1603. return
  1604. }
  1605. var content []byte
  1606. content, err = http.Get(info.ImgUrl)
  1607. if err != nil {
  1608. br.Msg = "操作失败"
  1609. br.ErrMsg = "资源获取失败, Err: " + err.Error()
  1610. return
  1611. }
  1612. filePath := uploadDir + "/" + urlFileName
  1613. ioWriter, err := os.Create(filePath)
  1614. if err != nil {
  1615. br.Msg = "操作失败"
  1616. br.ErrMsg = "文件创建失败, Err: " + err.Error()
  1617. return
  1618. }
  1619. n, err := ioWriter.Write(content)
  1620. fmt.Println("n", n)
  1621. if err != nil {
  1622. br.Msg = "操作失败"
  1623. br.ErrMsg = "压缩文件写入失败, Err: " + err.Error()
  1624. return
  1625. }
  1626. this.Ctx.Output.Download(filePath, fileName)
  1627. defer func() {
  1628. _ = os.Remove(filePath)
  1629. }()
  1630. br.Ret = 200
  1631. br.Success = true
  1632. br.Msg = "success"
  1633. }