material.go 52 KB

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