material.go 53 KB

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