material.go 53 KB

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