material.go 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  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. req.MaterialName = strings.Trim(req.MaterialName, " ")
  774. if req.MaterialName == "" {
  775. br.Msg = "缺少素材库名称"
  776. return
  777. }
  778. var exist *material.Material
  779. switch this.Lang {
  780. case utils.LANG_EN:
  781. exist, err = material.GetMaterialByNameEn(req.MaterialName)
  782. default:
  783. // 判断名称是否重复
  784. exist, err = material.GetMaterialByName(req.MaterialName)
  785. }
  786. if err != nil && err.Error() != utils.ErrNoRow() {
  787. br.Msg = "获取失败"
  788. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  789. return
  790. }
  791. if err == nil && exist.MaterialId > 0 {
  792. br.Msg = "图片名称已存在"
  793. return
  794. }
  795. err, errMsg := materialService.AddToMaterial(req, sysUser.AdminId, sysUser.RealName)
  796. if err != nil {
  797. br.Msg = "保存失败!"
  798. if errMsg != `` {
  799. br.Msg = errMsg
  800. }
  801. br.ErrMsg = "保存失败,Err:" + err.Error()
  802. return
  803. }
  804. msg := "保存成功"
  805. br.Ret = 200
  806. br.Success = true
  807. br.Msg = msg
  808. }
  809. // MyChartSaveAsMaterial
  810. // @Title 将我的图表等封面上传至素材库
  811. // @Description 将图表等封面上传至素材库
  812. // @Param request body material.AddAndEditSandbox true "type json string"
  813. // @Success 200 {object} material.Material
  814. // @router /my_chart/saveAs [post]
  815. func (this *MaterialController) MyChartSaveAsMaterial() {
  816. br := new(models.BaseResponse).Init()
  817. defer func() {
  818. this.Data["json"] = br
  819. this.ServeJSON()
  820. }()
  821. sysUser := this.SysUser
  822. if sysUser == nil {
  823. br.Msg = "请登录"
  824. br.ErrMsg = "请登录,SysUser Is Empty"
  825. br.Ret = 408
  826. return
  827. }
  828. var req material.MyChartSaveAsMaterialReq
  829. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  830. if err != nil {
  831. br.Msg = "参数解析异常!"
  832. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  833. return
  834. }
  835. materialNames := make([]string, 0)
  836. namesMap := make(map[string]struct{})
  837. for _, v := range req.MaterialList {
  838. if v.ClassifyId <= 0 {
  839. br.Msg = "请选择分类"
  840. return
  841. }
  842. v.MaterialName = strings.Trim(v.MaterialName, " ")
  843. if v.MaterialName == "" {
  844. br.Msg = "请填写图片名称"
  845. return
  846. }
  847. if v.ChartInfoId <= 0 {
  848. br.Msg = "请选择图表"
  849. return
  850. }
  851. if v.MyChartId <= 0 {
  852. br.Msg = "请选择我的图表"
  853. return
  854. }
  855. if _, ok := namesMap[v.MaterialName]; ok {
  856. br.Msg = "图片名称不能重复"
  857. return
  858. }
  859. namesMap[v.MaterialName] = struct{}{}
  860. materialNames = append(materialNames, v.MaterialName)
  861. }
  862. if len(materialNames) == 0 {
  863. br.Msg = "请填写图片名称"
  864. return
  865. }
  866. existList := make([]*material.Material, 0)
  867. switch this.Lang {
  868. case utils.LANG_EN:
  869. existList, err = material.GetMaterialByNameEns(materialNames)
  870. if err != nil {
  871. br.Msg = "获取失败"
  872. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  873. return
  874. }
  875. if len(existList) > 0 {
  876. msg := "图片名称:"
  877. for _, v := range existList {
  878. msg += v.MaterialNameEn + " "
  879. }
  880. br.Msg = fmt.Sprintf("%s 已存在", msg)
  881. return
  882. }
  883. default:
  884. // 判断文件名是否已存在
  885. existList, err = material.GetMaterialByNames(materialNames)
  886. if err != nil {
  887. br.Msg = "获取失败"
  888. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  889. return
  890. }
  891. if len(existList) > 0 {
  892. msg := "图片名称:"
  893. for _, v := range existList {
  894. msg += v.MaterialName + " "
  895. }
  896. br.Msg = fmt.Sprintf("%s 已存在", msg)
  897. return
  898. }
  899. }
  900. if len(req.MaterialList) > 30 {
  901. br.Msg = "最多支持选择30个图表"
  902. return
  903. }
  904. err, errMsg := materialService.MyChartAddToMaterial(req, sysUser.AdminId, sysUser.RealName)
  905. if err != nil {
  906. br.Msg = "保存失败!"
  907. if errMsg != `` {
  908. br.Msg = errMsg
  909. }
  910. br.ErrMsg = "保存失败,Err:" + err.Error()
  911. return
  912. }
  913. msg := "保存成功"
  914. br.Ret = 200
  915. br.Success = true
  916. br.Msg = msg
  917. }
  918. // Delete
  919. // @Title 删除素材库
  920. // @Description 删除素材库接口
  921. // @Param request body material.DeleteSandbox true "type json string"
  922. // @Success 200 标记成功
  923. // @router /del [post]
  924. func (this *MaterialController) Delete() {
  925. br := new(models.BaseResponse).Init()
  926. defer func() {
  927. this.Data["json"] = br
  928. this.ServeJSON()
  929. }()
  930. sysUser := this.SysUser
  931. if sysUser == nil {
  932. br.Msg = "请登录"
  933. br.ErrMsg = "请登录,SysUser Is Empty"
  934. br.Ret = 408
  935. return
  936. }
  937. var req material.DeleteMaterial
  938. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  939. if err != nil {
  940. br.Msg = "参数解析异常!"
  941. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  942. return
  943. }
  944. if req.MaterialId <= 0 {
  945. br.Msg = "缺少素材库编号"
  946. return
  947. }
  948. //删除素材库
  949. err = material.DeleteByMaterialIds([]int{req.MaterialId})
  950. if err != nil {
  951. br.Msg = "操作失败"
  952. br.ErrMsg = "操作失败,Err:" + err.Error()
  953. return
  954. }
  955. msg := "删除成功"
  956. br.Ret = 200
  957. br.Success = true
  958. br.Msg = msg
  959. }
  960. // BatchDelete
  961. // @Title 批量删除素材库
  962. // @Description 删除素材库接口
  963. // @Param request body material.DeleteSandbox true "type json string"
  964. // @Success 200 标记成功
  965. // @router /batch/del [post]
  966. func (this *MaterialController) BatchDelete() {
  967. br := new(models.BaseResponse).Init()
  968. defer func() {
  969. this.Data["json"] = br
  970. this.ServeJSON()
  971. }()
  972. sysUser := this.SysUser
  973. if sysUser == nil {
  974. br.Msg = "请登录"
  975. br.ErrMsg = "请登录,SysUser Is Empty"
  976. br.Ret = 408
  977. return
  978. }
  979. var req material.BatchDeleteMaterialReq
  980. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  981. if err != nil {
  982. br.Msg = "参数解析异常!"
  983. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  984. return
  985. }
  986. deleteMaterialIds := make([]int, 0)
  987. if req.IsSelectAll {
  988. classifyId := req.ClassifyId
  989. keyword := req.Keyword
  990. isShowMe := req.IsShowMe
  991. //获取图表信息
  992. list, e, msg := materialService.GetBatchSelectedMaterialList(classifyId, keyword, isShowMe, sysUser, this.Lang)
  993. if e != nil {
  994. br.Msg = "获取素材库信息失败"
  995. if msg != "" {
  996. br.Msg = msg
  997. }
  998. br.ErrMsg = "获取素材库信息失败,Err:" + e.Error()
  999. return
  1000. }
  1001. notSelectIds := make(map[int]struct{})
  1002. if len(req.MaterialIds) >= 0 {
  1003. for _, v := range req.MaterialIds {
  1004. notSelectIds[v] = struct{}{}
  1005. }
  1006. }
  1007. for _, v := range list {
  1008. if _, ok := notSelectIds[v.MaterialId]; !ok {
  1009. deleteMaterialIds = append(deleteMaterialIds, v.MaterialId)
  1010. }
  1011. }
  1012. } else {
  1013. deleteMaterialIds = req.MaterialIds
  1014. }
  1015. if len(deleteMaterialIds) <= 0 {
  1016. br.Msg = "请选择删除的素材"
  1017. return
  1018. }
  1019. //删除素材库
  1020. err = material.DeleteByMaterialIds(deleteMaterialIds)
  1021. if err != nil {
  1022. br.Msg = err.Error()
  1023. return
  1024. }
  1025. msg := "删除成功"
  1026. br.Ret = 200
  1027. br.Success = true
  1028. br.Msg = msg
  1029. }
  1030. // MaterialClassifyList
  1031. // @Title 获取所有素材库分类接口-不包含素材库
  1032. // @Description 获取所有素材库分类接口-不包含素材库
  1033. // @Param IsShowMe query bool true "是否只看我的,true、false"
  1034. // @Success 200 {object} data_manage.ChartClassifyListResp
  1035. // @router /classify/list [get]
  1036. func (this *MaterialController) MaterialClassifyList() {
  1037. br := new(models.BaseResponse).Init()
  1038. defer func() {
  1039. this.Data["json"] = br
  1040. this.ServeJSON()
  1041. }()
  1042. resp := new(material.MaterialClassifyListResp)
  1043. rootList, err := material.GetMaterialClassifyByParentId(0)
  1044. if err != nil && err.Error() != utils.ErrNoRow() {
  1045. br.Msg = "获取失败"
  1046. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1047. return
  1048. }
  1049. classifyAll, err := material.GetMaterialClassifyAll()
  1050. if err != nil && err.Error() != utils.ErrNoRow() {
  1051. br.Msg = "获取失败"
  1052. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1053. return
  1054. }
  1055. nodeAll := make([]*material.MaterialClassifyItems, 0)
  1056. for k := range rootList {
  1057. rootNode := rootList[k]
  1058. materialService.MaterialClassifyItemsMakeTree(this.SysUser, classifyAll, rootNode)
  1059. nodeAll = append(nodeAll, rootNode)
  1060. }
  1061. resp.AllNodes = nodeAll
  1062. br.Ret = 200
  1063. br.Success = true
  1064. br.Msg = "获取成功"
  1065. br.Data = resp
  1066. }
  1067. // BatchAdd
  1068. // @Title 批量新增素材
  1069. // @Description 新增/编辑保存素材库接口
  1070. // @Param request body material.AddAndEditSandbox true "type json string"
  1071. // @Success 200 {object} material.Material
  1072. // @router /batch/add [post]
  1073. func (this *MaterialController) BatchAdd() {
  1074. br := new(models.BaseResponse).Init()
  1075. defer func() {
  1076. this.Data["json"] = br
  1077. this.ServeJSON()
  1078. }()
  1079. sysUser := this.SysUser
  1080. if sysUser == nil {
  1081. br.Msg = "请登录"
  1082. br.ErrMsg = "请登录,SysUser Is Empty"
  1083. br.Ret = 408
  1084. return
  1085. }
  1086. var req material.BatchAddMaterialReq
  1087. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1088. if err != nil {
  1089. br.Msg = "参数解析异常!"
  1090. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1091. return
  1092. }
  1093. classifyId := req.ClassifyId
  1094. if classifyId <= 0 {
  1095. br.Msg = "请选择分类"
  1096. return
  1097. }
  1098. materialNames := make([]string, 0)
  1099. namesMap := make(map[string]struct{})
  1100. for _, v := range req.MaterialList {
  1101. if v.MaterialName == "" {
  1102. br.Msg = "请填写图片名称"
  1103. return
  1104. }
  1105. if v.ImgUrl == "" {
  1106. br.Msg = "请上传图片"
  1107. return
  1108. }
  1109. if _, ok := namesMap[v.MaterialName]; ok {
  1110. br.Msg = "图片名称不能重复"
  1111. return
  1112. }
  1113. namesMap[v.MaterialName] = struct{}{}
  1114. materialNames = append(materialNames, v.MaterialName)
  1115. }
  1116. if len(materialNames) == 0 {
  1117. br.Msg = "请填写图片名称"
  1118. return
  1119. }
  1120. existList := make([]*material.Material, 0)
  1121. switch this.Lang {
  1122. case utils.LANG_EN:
  1123. existList, err = material.GetMaterialByNameEns(materialNames)
  1124. if err != nil {
  1125. br.Msg = "获取失败"
  1126. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1127. return
  1128. }
  1129. if len(existList) > 0 {
  1130. msg := "图片名称:"
  1131. for _, v := range existList {
  1132. msg += v.MaterialNameEn + " "
  1133. }
  1134. br.Msg = fmt.Sprintf("%s 已存在", msg)
  1135. return
  1136. }
  1137. default:
  1138. // 判断文件名是否已存在
  1139. existList, err = material.GetMaterialByNames(materialNames)
  1140. if err != nil {
  1141. br.Msg = "获取失败"
  1142. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1143. return
  1144. }
  1145. if len(existList) > 0 {
  1146. msg := "图片名称:"
  1147. for _, v := range existList {
  1148. msg += v.MaterialName + " "
  1149. }
  1150. br.Msg = fmt.Sprintf("%s 已存在", msg)
  1151. return
  1152. }
  1153. }
  1154. err = materialService.BatchAddMaterial(req.MaterialList, classifyId, sysUser.AdminId, sysUser.AdminName)
  1155. if err != nil {
  1156. br.Msg = "文件上传失败"
  1157. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  1158. return
  1159. }
  1160. msg := "添加成功"
  1161. br.Ret = 200
  1162. br.Success = true
  1163. br.Msg = msg
  1164. }
  1165. // BatchChangeClassify
  1166. // @Title 批量更换分类
  1167. // @Description 批量更换分类
  1168. // @Param request body material.DeleteSandbox true "type json string"
  1169. // @Success 200 标记成功
  1170. // @router /batch/changeClassify [post]
  1171. func (this *MaterialController) BatchChangeClassify() {
  1172. br := new(models.BaseResponse).Init()
  1173. defer func() {
  1174. this.Data["json"] = br
  1175. this.ServeJSON()
  1176. }()
  1177. sysUser := this.SysUser
  1178. if sysUser == nil {
  1179. br.Msg = "请登录"
  1180. br.ErrMsg = "请登录,SysUser Is Empty"
  1181. br.Ret = 408
  1182. return
  1183. }
  1184. var req material.BatchChangeClassifyMaterialReq
  1185. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1186. if err != nil {
  1187. br.Msg = "参数解析异常!"
  1188. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1189. return
  1190. }
  1191. if req.NewClassifyId <= 0 {
  1192. br.Msg = "请选择新的分类"
  1193. return
  1194. }
  1195. updateMaterialIds := make([]int, 0)
  1196. if req.IsSelectAll {
  1197. classifyId := req.ClassifyId
  1198. keyword := req.Keyword
  1199. isShowMe := req.IsShowMe
  1200. //获取图表信息
  1201. list, e, msg := materialService.GetBatchSelectedMaterialList(classifyId, keyword, isShowMe, sysUser, this.Lang)
  1202. if e != nil {
  1203. br.Msg = "获取素材库信息失败"
  1204. if msg != "" {
  1205. br.Msg = msg
  1206. }
  1207. br.ErrMsg = "获取素材库信息失败,Err:" + e.Error()
  1208. return
  1209. }
  1210. notSelectIds := make(map[int]struct{})
  1211. if len(req.MaterialIds) >= 0 {
  1212. for _, v := range req.MaterialIds {
  1213. notSelectIds[v] = struct{}{}
  1214. }
  1215. }
  1216. for _, v := range list {
  1217. if _, ok := notSelectIds[v.MaterialId]; !ok {
  1218. updateMaterialIds = append(updateMaterialIds, v.MaterialId)
  1219. }
  1220. }
  1221. } else {
  1222. updateMaterialIds = req.MaterialIds
  1223. }
  1224. if len(updateMaterialIds) <= 0 {
  1225. br.Msg = "请选择变更的素材"
  1226. return
  1227. }
  1228. // 判断新分类是否存在
  1229. _, err = material.GetMaterialClassifyById(req.NewClassifyId)
  1230. if err != nil {
  1231. if err.Error() == utils.ErrNoRow() {
  1232. br.Msg = "分类不存在"
  1233. return
  1234. }
  1235. br.Msg = "获取分类信息失败"
  1236. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  1237. }
  1238. //更换分类素材库
  1239. err = material.UpdateClassifyByMaterialIds(updateMaterialIds, req.NewClassifyId, time.Now())
  1240. if err != nil {
  1241. br.Msg = err.Error()
  1242. return
  1243. }
  1244. msg := "操作成功"
  1245. br.Ret = 200
  1246. br.Success = true
  1247. br.Msg = msg
  1248. }
  1249. // ChangeClassify
  1250. // @Title 更换分类
  1251. // @Description 批量更换分类
  1252. // @Param request body material.DeleteSandbox true "type json string"
  1253. // @Success 200 标记成功
  1254. // @router /changeClassify [post]
  1255. func (this *MaterialController) ChangeClassify() {
  1256. br := new(models.BaseResponse).Init()
  1257. defer func() {
  1258. this.Data["json"] = br
  1259. this.ServeJSON()
  1260. }()
  1261. sysUser := this.SysUser
  1262. if sysUser == nil {
  1263. br.Msg = "请登录"
  1264. br.ErrMsg = "请登录,SysUser Is Empty"
  1265. br.Ret = 408
  1266. return
  1267. }
  1268. var req material.ChangeClassifyMaterialReq
  1269. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1270. if err != nil {
  1271. br.Msg = "参数解析异常!"
  1272. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1273. return
  1274. }
  1275. if req.MaterialId <= 0 {
  1276. br.Msg = "缺少素材库编号"
  1277. return
  1278. }
  1279. if req.NewClassifyId <= 0 {
  1280. br.Msg = "请选择新的分类"
  1281. return
  1282. }
  1283. // 判断素材是否存在
  1284. info, err := material.GetMaterialById(req.MaterialId)
  1285. if err != nil {
  1286. if err.Error() == utils.ErrNoRow() {
  1287. br.Msg = "素材不存在"
  1288. return
  1289. }
  1290. br.Msg = "获取素材库信息失败"
  1291. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1292. return
  1293. }
  1294. if req.NewClassifyId == info.ClassifyId {
  1295. br.Msg = "请选择不同的分类"
  1296. return
  1297. }
  1298. // 判断新分类是否存在
  1299. _, err = material.GetMaterialClassifyById(req.NewClassifyId)
  1300. if err != nil {
  1301. if err.Error() == utils.ErrNoRow() {
  1302. br.Msg = "分类不存在"
  1303. return
  1304. }
  1305. br.Msg = "获取分类信息失败"
  1306. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  1307. }
  1308. info.ClassifyId = req.NewClassifyId
  1309. info.ModifyTime = time.Now()
  1310. //更换分类素材库
  1311. err = info.Update([]string{"ClassifyId", "ModifyTime"})
  1312. if err != nil {
  1313. br.Msg = err.Error()
  1314. return
  1315. }
  1316. msg := "操作成功"
  1317. br.Ret = 200
  1318. br.Success = true
  1319. br.Msg = msg
  1320. }
  1321. // Rename
  1322. // @Title 素材重命名
  1323. // @Description 素材重命名
  1324. // @Param request body material.DeleteSandbox true "type json string"
  1325. // @Success 200 标记成功
  1326. // @router /rename [post]
  1327. func (this *MaterialController) Rename() {
  1328. br := new(models.BaseResponse).Init()
  1329. defer func() {
  1330. this.Data["json"] = br
  1331. this.ServeJSON()
  1332. }()
  1333. sysUser := this.SysUser
  1334. if sysUser == nil {
  1335. br.Msg = "请登录"
  1336. br.ErrMsg = "请登录,SysUser Is Empty"
  1337. br.Ret = 408
  1338. return
  1339. }
  1340. var req material.RenameMaterialReq
  1341. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1342. if err != nil {
  1343. br.Msg = "参数解析异常!"
  1344. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1345. return
  1346. }
  1347. if req.MaterialId <= 0 {
  1348. br.Msg = "缺少素材库编号"
  1349. return
  1350. }
  1351. req.MaterialName = strings.Trim(req.MaterialName, " ")
  1352. if req.MaterialName == "" {
  1353. br.Msg = "缺少素材库名称"
  1354. return
  1355. }
  1356. // 判断素材是否存在
  1357. info, err := material.GetMaterialById(req.MaterialId)
  1358. if err != nil {
  1359. if err.Error() == utils.ErrNoRow() {
  1360. br.Msg = "素材不存在"
  1361. return
  1362. }
  1363. br.Msg = "获取素材库信息失败"
  1364. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1365. return
  1366. }
  1367. var exist *material.Material
  1368. updateStr := make([]string, 0)
  1369. switch this.Lang {
  1370. case utils.LANG_EN:
  1371. // 判断名称是否重复
  1372. if info.MaterialNameEn == req.MaterialName {
  1373. br.Msg = "名称未修改"
  1374. return
  1375. }
  1376. exist, err = material.GetMaterialByNameEn(req.MaterialName)
  1377. info.MaterialNameEn = req.MaterialName
  1378. updateStr = append(updateStr, "MaterialNameEn")
  1379. default:
  1380. // 判断名称是否重复
  1381. if info.MaterialName == req.MaterialName {
  1382. br.Msg = "名称未修改"
  1383. return
  1384. }
  1385. exist, err = material.GetMaterialByName(req.MaterialName)
  1386. info.MaterialName = req.MaterialName
  1387. updateStr = append(updateStr, "MaterialName")
  1388. }
  1389. if err != nil && err.Error() != utils.ErrNoRow() {
  1390. br.Msg = "获取失败"
  1391. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1392. return
  1393. }
  1394. if err == nil && exist.MaterialId > 0 {
  1395. br.Msg = "图片名称已存在"
  1396. return
  1397. }
  1398. info.ModifyTime = time.Now()
  1399. updateStr = append(updateStr, "ModifyTime")
  1400. //更换分类素材库
  1401. err = info.Update(updateStr)
  1402. if err != nil {
  1403. br.Msg = err.Error()
  1404. return
  1405. }
  1406. msg := "操作成功"
  1407. br.Ret = 200
  1408. br.Success = true
  1409. br.Msg = msg
  1410. }
  1411. // BatchDownload
  1412. // @Title 批量下载素材
  1413. // @Description 批量下载素材
  1414. // @Param request body material.DeleteSandbox true "type json string"
  1415. // @Success 200 标记成功
  1416. // @router /batch/download [get]
  1417. func (this *MaterialController) BatchDownload() {
  1418. br := new(models.BaseResponse).Init()
  1419. defer func() {
  1420. this.Data["json"] = br
  1421. this.ServeJSON()
  1422. }()
  1423. sysUser := this.SysUser
  1424. if sysUser == nil {
  1425. br.Msg = "请登录"
  1426. br.ErrMsg = "请登录,SysUser Is Empty"
  1427. br.Ret = 408
  1428. return
  1429. }
  1430. //var req material.BatchDeleteMaterialReq
  1431. materialIdStr := this.GetString("MaterialIds")
  1432. isSelectAll, _ := this.GetBool("IsSelectAll")
  1433. classifyId, _ := this.GetInt("ClassifyId")
  1434. keyword := this.GetString("Keyword")
  1435. isShowMe, _ := this.GetBool("IsShowMe")
  1436. downMaterialList := make([]*material.MaterialListItems, 0)
  1437. materialIds := strings.Split(materialIdStr, ",")
  1438. var err error
  1439. if isSelectAll {
  1440. //获取图表信息
  1441. list, e, msg := materialService.GetBatchSelectedMaterialList(classifyId, keyword, isShowMe, sysUser, this.Lang)
  1442. if e != nil {
  1443. br.Msg = "获取素材库信息失败"
  1444. if msg != "" {
  1445. br.Msg = msg
  1446. }
  1447. br.ErrMsg = "获取素材库信息失败,Err:" + e.Error()
  1448. return
  1449. }
  1450. notSelectIds := make(map[int]struct{})
  1451. if len(materialIds) >= 0 {
  1452. //转成数组
  1453. for _, v := range materialIds {
  1454. id, _ := strconv.Atoi(v)
  1455. notSelectIds[id] = struct{}{}
  1456. }
  1457. }
  1458. for _, v := range list {
  1459. if _, ok := notSelectIds[v.MaterialId]; !ok {
  1460. downMaterialList = append(downMaterialList, v)
  1461. }
  1462. }
  1463. } else {
  1464. if len(materialIds) > 0 {
  1465. // 批量查询指标数据
  1466. materialIdsInt := make([]int, 0)
  1467. for _, v := range materialIds {
  1468. id, _ := strconv.Atoi(v)
  1469. materialIdsInt = append(materialIdsInt, id)
  1470. }
  1471. downMaterialList, err = material.GetMaterialByIds(materialIdsInt)
  1472. if err != nil {
  1473. br.Msg = "获取素材库信息失败"
  1474. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1475. return
  1476. }
  1477. }
  1478. }
  1479. if len(downMaterialList) <= 0 {
  1480. br.Msg = "请选择要下载的素材"
  1481. return
  1482. }
  1483. // 创建zip
  1484. zipName := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
  1485. savePath := zipName
  1486. zipFile, err := os.Create(zipName)
  1487. if err != nil {
  1488. br.Msg = "操作失败"
  1489. br.ErrMsg = "生成压缩文件失败, Err: " + err.Error()
  1490. return
  1491. }
  1492. zipWriter := zip.NewWriter(zipFile)
  1493. // 生成zip过程中报错关闭
  1494. defer func() {
  1495. if err != nil {
  1496. _ = zipWriter.Close()
  1497. _ = zipFile.Close()
  1498. }
  1499. _ = os.Remove(savePath)
  1500. }()
  1501. // 获取资源, 写入zip
  1502. zipFileName := ""
  1503. for i := range downMaterialList {
  1504. if downMaterialList[i].MaterialName == "" || downMaterialList[i].ImgUrl == "" {
  1505. continue
  1506. }
  1507. fmt.Printf("开始压缩第%d个文件\n", i+1)
  1508. dotIndex := strings.LastIndex(downMaterialList[i].ImgUrl, ".")
  1509. // 如果找不到点,或者点是文件名的第一个字符(不合法情况),则返回空字符串
  1510. if dotIndex == -1 || dotIndex == 0 {
  1511. continue
  1512. }
  1513. fileExt := downMaterialList[i].ImgUrl[dotIndex+1:]
  1514. ioWriter, err := zipWriter.Create(fmt.Sprintf("%s.%s", downMaterialList[i].MaterialName, fileExt))
  1515. if err != nil {
  1516. if os.IsPermission(err) {
  1517. br.Msg = "操作失败"
  1518. br.ErrMsg = "打包权限不足, Err: " + err.Error()
  1519. return
  1520. }
  1521. br.Msg = "操作失败"
  1522. br.ErrMsg = "压缩出错, Err: " + err.Error()
  1523. return
  1524. }
  1525. var content []byte
  1526. content, err = http.Get(downMaterialList[i].ImgUrl)
  1527. if err != nil {
  1528. br.Msg = "操作失败"
  1529. br.ErrMsg = "资源获取失败, Err: " + err.Error()
  1530. return
  1531. }
  1532. _, err = ioWriter.Write(content)
  1533. if err != nil {
  1534. br.Msg = "操作失败"
  1535. br.ErrMsg = "压缩文件写入失败, Err: " + err.Error()
  1536. return
  1537. }
  1538. if zipFileName == "" {
  1539. zipFileName = downMaterialList[i].MaterialName
  1540. }
  1541. fmt.Printf("第%d个文件写入成功\n", i+1)
  1542. }
  1543. // 生成zip后关闭,否则下载文件会损坏
  1544. _ = zipWriter.Close()
  1545. _ = zipFile.Close()
  1546. this.Ctx.Output.Download(savePath, fmt.Sprintf("%s.zip", zipFileName))
  1547. msg := "操作成功"
  1548. br.Ret = 200
  1549. br.Success = true
  1550. br.Msg = msg
  1551. }
  1552. // Download
  1553. // @Title 下载素材
  1554. // @Description 下载素材
  1555. // @Param request body material.DeleteSandbox true "type json string"
  1556. // @Success 200 标记成功
  1557. // @router /download [get]
  1558. func (this *MaterialController) Download() {
  1559. br := new(models.BaseResponse).Init()
  1560. defer func() {
  1561. this.Data["json"] = br
  1562. this.ServeJSON()
  1563. }()
  1564. sysUser := this.SysUser
  1565. if sysUser == nil {
  1566. br.Msg = "请登录"
  1567. br.ErrMsg = "请登录,SysUser Is Empty"
  1568. br.Ret = 408
  1569. return
  1570. }
  1571. materialId, _ := this.GetInt("MaterialId")
  1572. if materialId <= 0 {
  1573. br.Msg = "缺少素材库编号"
  1574. return
  1575. }
  1576. // 判断素材是否存在
  1577. info, err := material.GetMaterialById(materialId)
  1578. if err != nil {
  1579. if err.Error() == utils.ErrNoRow() {
  1580. br.Msg = "素材不存在"
  1581. return
  1582. }
  1583. br.Msg = "获取素材库信息失败"
  1584. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1585. return
  1586. }
  1587. if info.ImgUrl == "" {
  1588. br.Msg = "素材地址为空"
  1589. return
  1590. }
  1591. fileName := info.MaterialName
  1592. // 查找文件名中最后一个点的位置
  1593. dotIndex := strings.LastIndex(info.ImgUrl, ".")
  1594. // 如果找不到点,或者点是文件名的第一个字符(不合法情况),则返回空字符串
  1595. if dotIndex == -1 || dotIndex == 0 {
  1596. br.Msg = "素材地址错误"
  1597. return
  1598. }
  1599. fileName += "." + info.ImgUrl[dotIndex+1:] // 添加扩展名到文件名
  1600. // 获取路径中的文件名
  1601. urlFileName := path.Base(info.ImgUrl)
  1602. uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
  1603. if e := os.MkdirAll(uploadDir, utils.DIR_MOD); e != nil {
  1604. br.Msg = "存储目录创建失败"
  1605. br.ErrMsg = "存储目录创建失败, Err:" + e.Error()
  1606. return
  1607. }
  1608. var content []byte
  1609. content, err = http.Get(info.ImgUrl)
  1610. if err != nil {
  1611. br.Msg = "操作失败"
  1612. br.ErrMsg = "资源获取失败, Err: " + err.Error()
  1613. return
  1614. }
  1615. filePath := uploadDir + "/" + urlFileName
  1616. ioWriter, err := os.Create(filePath)
  1617. if err != nil {
  1618. br.Msg = "操作失败"
  1619. br.ErrMsg = "文件创建失败, Err: " + err.Error()
  1620. return
  1621. }
  1622. n, err := ioWriter.Write(content)
  1623. fmt.Println("n", n)
  1624. if err != nil {
  1625. br.Msg = "操作失败"
  1626. br.ErrMsg = "压缩文件写入失败, Err: " + err.Error()
  1627. return
  1628. }
  1629. this.Ctx.Output.Download(filePath, fileName)
  1630. defer func() {
  1631. _ = os.Remove(filePath)
  1632. }()
  1633. br.Ret = 200
  1634. br.Success = true
  1635. br.Msg = "success"
  1636. }