material.go 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  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, materialClassifyInfo.ParentId)
  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. var total int
  652. page := paging.GetPaging(currentIndex, pageSize, total)
  653. var startSize int
  654. if pageSize <= 0 {
  655. pageSize = utils.PageSize20
  656. }
  657. if currentIndex <= 0 {
  658. currentIndex = 1
  659. }
  660. startSize = paging.StartIndex(currentIndex, pageSize)
  661. var condition string
  662. var pars []interface{}
  663. classifyIds := make([]string, 0)
  664. childClassifyMap := make(map[int]*material.MaterialClassify)
  665. if classifyId > 0 {
  666. classifyIds = append(classifyIds, strconv.Itoa(classifyId))
  667. // 查询当前的分类
  668. classifyInfo, err := material.GetMaterialClassifyById(classifyId)
  669. if err != nil {
  670. br.Msg = "分类不存在"
  671. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  672. return
  673. }
  674. // 获取所有子分类
  675. childList, e := material.GetMaterialClassifyByLevelPath(classifyInfo.LevelPath)
  676. if e != nil {
  677. err = fmt.Errorf("保存分类失败,Err:" + e.Error())
  678. return
  679. }
  680. // 把原先的父级levePath,替换成最新的父级序列
  681. classifyIdMap := make(map[string]struct{})
  682. for _, tmp := range childList {
  683. childClassifyMap[tmp.ClassifyId] = tmp
  684. //获取字符串前缀的位置
  685. after, _ := strings.CutPrefix(tmp.LevelPath, classifyInfo.LevelPath)
  686. fmt.Println("after", after)
  687. // 拼接字符串
  688. if after != "" {
  689. ids := strings.Split(after, ",")
  690. for _, v := range ids {
  691. if _, ok := classifyIdMap[v]; !ok {
  692. classifyIds = append(classifyIds, v)
  693. classifyIdMap[v] = struct{}{}
  694. }
  695. }
  696. }
  697. }
  698. }
  699. if len(classifyIds) > 0 {
  700. condition += " AND classify_id IN(" + utils.GetOrmInReplace(len(classifyIds)) + ") "
  701. pars = append(pars, classifyIds)
  702. }
  703. if keyword != "" {
  704. switch this.Lang {
  705. case utils.LANG_EN:
  706. condition += ` AND ( material_name_en LIKE '%` + keyword + `%' )`
  707. default:
  708. condition += ` AND ( material_name LIKE '%` + keyword + `%' )`
  709. }
  710. }
  711. //只看我的
  712. isShowMe, _ := this.GetBool("IsShowMe")
  713. if isShowMe {
  714. condition += ` AND sys_user_id = ? `
  715. pars = append(pars, sysUser.AdminId)
  716. }
  717. //获取图表信息
  718. list, err := material.GetMaterialListPageByCondition(condition, pars, startSize, pageSize)
  719. if err != nil && err.Error() != utils.ErrNoRow() {
  720. br.Success = true
  721. br.Msg = "获取素材库信息失败"
  722. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  723. return
  724. }
  725. resp := new(material.MaterialListResp)
  726. if list == nil || len(list) <= 0 || (err != nil && err.Error() == utils.ErrNoRow()) {
  727. items := make([]*material.MaterialListItems, 0)
  728. resp.Paging = page
  729. resp.List = items
  730. br.Ret = 200
  731. br.Success = true
  732. br.Msg = "获取成功"
  733. br.Data = resp
  734. return
  735. }
  736. dataCount, err := material.GetMaterialListCountByCondition(condition, pars)
  737. if err != nil && err.Error() != utils.ErrNoRow() {
  738. br.Msg = "获取指标信息失败"
  739. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  740. return
  741. }
  742. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  743. resp.Paging = page
  744. resp.List = list
  745. br.Ret = 200
  746. br.Success = true
  747. br.Msg = "获取成功"
  748. br.Data = resp
  749. }
  750. // SaveAsMaterial
  751. // @Title 将图表等封面上传至素材库
  752. // @Description 将图表等封面上传至素材库
  753. // @Param request body material.AddAndEditSandbox true "type json string"
  754. // @Success 200 {object} material.Material
  755. // @router /saveAs [post]
  756. func (this *MaterialController) SaveAsMaterial() {
  757. br := new(models.BaseResponse).Init()
  758. defer func() {
  759. this.Data["json"] = br
  760. this.ServeJSON()
  761. }()
  762. sysUser := this.SysUser
  763. if sysUser == nil {
  764. br.Msg = "请登录"
  765. br.ErrMsg = "请登录,SysUser Is Empty"
  766. br.Ret = 408
  767. return
  768. }
  769. var req material.SaveAsMaterialReq
  770. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  771. if err != nil {
  772. br.Msg = "参数解析异常!"
  773. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  774. return
  775. }
  776. if req.MaterialName == "" {
  777. br.Msg = "缺少素材库名称"
  778. return
  779. }
  780. var exist *material.Material
  781. switch this.Lang {
  782. case utils.LANG_EN:
  783. exist, err = material.GetMaterialByNameEn(req.MaterialName)
  784. default:
  785. // 判断名称是否重复
  786. exist, err = material.GetMaterialByName(req.MaterialName)
  787. }
  788. if err != nil && err.Error() != utils.ErrNoRow() {
  789. br.Msg = "获取失败"
  790. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  791. return
  792. }
  793. if err == nil && exist.MaterialId > 0 {
  794. br.Msg = "图片名称已存在"
  795. return
  796. }
  797. err, errMsg := materialService.AddToMaterial(req, sysUser.AdminId, sysUser.RealName)
  798. if err != nil {
  799. br.Msg = "保存失败!"
  800. if errMsg != `` {
  801. br.Msg = errMsg
  802. }
  803. br.ErrMsg = "保存失败,Err:" + err.Error()
  804. return
  805. }
  806. msg := "保存成功"
  807. br.Ret = 200
  808. br.Success = true
  809. br.Msg = msg
  810. }
  811. // MyChartSaveAsMaterial
  812. // @Title 将我的图表等封面上传至素材库
  813. // @Description 将图表等封面上传至素材库
  814. // @Param request body material.AddAndEditSandbox true "type json string"
  815. // @Success 200 {object} material.Material
  816. // @router /my_chart/saveAs [post]
  817. func (this *MaterialController) MyChartSaveAsMaterial() {
  818. br := new(models.BaseResponse).Init()
  819. defer func() {
  820. this.Data["json"] = br
  821. this.ServeJSON()
  822. }()
  823. sysUser := this.SysUser
  824. if sysUser == nil {
  825. br.Msg = "请登录"
  826. br.ErrMsg = "请登录,SysUser Is Empty"
  827. br.Ret = 408
  828. return
  829. }
  830. var req material.MyChartSaveAsMaterialReq
  831. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  832. if err != nil {
  833. br.Msg = "参数解析异常!"
  834. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  835. return
  836. }
  837. materialNames := make([]string, 0)
  838. namesMap := make(map[string]struct{})
  839. for _, v := range req.MaterialList {
  840. if v.ClassifyId <= 0 {
  841. br.Msg = "请选择分类"
  842. return
  843. }
  844. if v.MaterialName == "" {
  845. br.Msg = "请填写图片名称"
  846. return
  847. }
  848. if v.ChartInfoId <= 0 {
  849. br.Msg = "请选择图表"
  850. return
  851. }
  852. if v.MyChartId <= 0 {
  853. br.Msg = "请选择我的图表"
  854. return
  855. }
  856. if _, ok := namesMap[v.MaterialName]; ok {
  857. br.Msg = "图片名称不能重复"
  858. return
  859. }
  860. namesMap[v.MaterialName] = struct{}{}
  861. materialNames = append(materialNames, v.MaterialName)
  862. }
  863. if len(materialNames) == 0 {
  864. br.Msg = "请填写图片名称"
  865. return
  866. }
  867. existList := make([]*material.Material, 0)
  868. existNameMap := make(map[string]struct{})
  869. switch this.Lang {
  870. case utils.LANG_EN:
  871. existList, err = material.GetMaterialByNameEns(materialNames)
  872. if err != nil {
  873. br.Msg = "获取失败"
  874. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  875. return
  876. }
  877. if len(existList) > 0 {
  878. for _, v := range existList {
  879. existNameMap[v.MaterialNameEn] = struct{}{}
  880. }
  881. }
  882. default:
  883. // 判断文件名是否已存在
  884. existList, err = material.GetMaterialByNames(materialNames)
  885. if err != nil {
  886. br.Msg = "获取失败"
  887. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  888. return
  889. }
  890. if len(existList) > 0 {
  891. for _, v := range existList {
  892. existNameMap[v.MaterialName] = struct{}{}
  893. }
  894. }
  895. }
  896. if len(existList) > 0 {
  897. br.Msg = "图片名称已存在"
  898. respData := materialService.GetMyChartExistMaterialNameListMsg(existNameMap, req.MaterialList)
  899. br.Data = respData
  900. return
  901. }
  902. if len(req.MaterialList) > 30 {
  903. br.Msg = "最多支持选择30个图表"
  904. return
  905. }
  906. err, errMsg := materialService.MyChartAddToMaterial(req, sysUser.AdminId, sysUser.RealName)
  907. if err != nil {
  908. br.Msg = "保存失败!"
  909. if errMsg != `` {
  910. br.Msg = errMsg
  911. }
  912. br.ErrMsg = "保存失败,Err:" + err.Error()
  913. return
  914. }
  915. msg := "保存成功"
  916. br.Ret = 200
  917. br.Success = true
  918. br.Msg = msg
  919. }
  920. // Delete
  921. // @Title 删除素材库
  922. // @Description 删除素材库接口
  923. // @Param request body material.DeleteSandbox true "type json string"
  924. // @Success 200 标记成功
  925. // @router /del [post]
  926. func (this *MaterialController) Delete() {
  927. br := new(models.BaseResponse).Init()
  928. defer func() {
  929. this.Data["json"] = br
  930. this.ServeJSON()
  931. }()
  932. sysUser := this.SysUser
  933. if sysUser == nil {
  934. br.Msg = "请登录"
  935. br.ErrMsg = "请登录,SysUser Is Empty"
  936. br.Ret = 408
  937. return
  938. }
  939. var req material.DeleteMaterial
  940. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  941. if err != nil {
  942. br.Msg = "参数解析异常!"
  943. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  944. return
  945. }
  946. if req.MaterialId <= 0 {
  947. br.Msg = "缺少素材库编号"
  948. return
  949. }
  950. //删除素材库
  951. err = material.DeleteByMaterialIds([]int{req.MaterialId})
  952. if err != nil {
  953. br.Msg = "操作失败"
  954. br.ErrMsg = "操作失败,Err:" + err.Error()
  955. return
  956. }
  957. msg := "删除成功"
  958. br.Ret = 200
  959. br.Success = true
  960. br.Msg = msg
  961. }
  962. // BatchDelete
  963. // @Title 批量删除素材库
  964. // @Description 删除素材库接口
  965. // @Param request body material.DeleteSandbox true "type json string"
  966. // @Success 200 标记成功
  967. // @router /batch/del [post]
  968. func (this *MaterialController) BatchDelete() {
  969. br := new(models.BaseResponse).Init()
  970. defer func() {
  971. this.Data["json"] = br
  972. this.ServeJSON()
  973. }()
  974. sysUser := this.SysUser
  975. if sysUser == nil {
  976. br.Msg = "请登录"
  977. br.ErrMsg = "请登录,SysUser Is Empty"
  978. br.Ret = 408
  979. return
  980. }
  981. var req material.BatchDeleteMaterialReq
  982. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  983. if err != nil {
  984. br.Msg = "参数解析异常!"
  985. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  986. return
  987. }
  988. deleteMaterialIds := make([]int, 0)
  989. if req.IsSelectAll {
  990. classifyId := req.ClassifyId
  991. keyword := req.Keyword
  992. isShowMe := req.IsShowMe
  993. //获取图表信息
  994. list, e, msg := materialService.GetBatchSelectedMaterialList(classifyId, keyword, isShowMe, sysUser, this.Lang)
  995. if e != nil {
  996. br.Msg = "获取素材库信息失败"
  997. if msg != "" {
  998. br.Msg = msg
  999. }
  1000. br.ErrMsg = "获取素材库信息失败,Err:" + e.Error()
  1001. return
  1002. }
  1003. notSelectIds := make(map[int]struct{})
  1004. if len(req.MaterialIds) >= 0 {
  1005. for _, v := range req.MaterialIds {
  1006. notSelectIds[v] = struct{}{}
  1007. }
  1008. }
  1009. for _, v := range list {
  1010. if _, ok := notSelectIds[v.MaterialId]; !ok {
  1011. deleteMaterialIds = append(deleteMaterialIds, v.MaterialId)
  1012. }
  1013. }
  1014. } else {
  1015. deleteMaterialIds = req.MaterialIds
  1016. }
  1017. if len(deleteMaterialIds) <= 0 {
  1018. br.Msg = "请选择删除的素材"
  1019. return
  1020. }
  1021. //删除素材库
  1022. err = material.DeleteByMaterialIds(deleteMaterialIds)
  1023. if err != nil {
  1024. br.Msg = err.Error()
  1025. return
  1026. }
  1027. msg := "删除成功"
  1028. br.Ret = 200
  1029. br.Success = true
  1030. br.Msg = msg
  1031. }
  1032. // GetMaterialVersionDetail
  1033. // @Title 获取素材库版本数据详情(已保存的)
  1034. // @Description 获取素材库版本数据详情接口(已保存的)
  1035. // @Param SandboxVersionCode query string true "素材库版本code"
  1036. // @Success 200 {object} material.MaterialVersion
  1037. // @router /detail [get]
  1038. func (this *MaterialController) GetMaterialDetail() {
  1039. br := new(models.BaseResponse).Init()
  1040. defer func() {
  1041. this.Data["json"] = br
  1042. this.ServeJSON()
  1043. }()
  1044. sysUser := this.SysUser
  1045. if sysUser == nil {
  1046. br.Msg = "请登录"
  1047. br.ErrMsg = "请登录,SysUser Is Empty"
  1048. br.Ret = 408
  1049. return
  1050. }
  1051. sandboxId, _ := this.GetInt("SandboxId")
  1052. if sandboxId == 0 {
  1053. br.Msg = "缺少素材库Id"
  1054. return
  1055. }
  1056. //获取素材库数据详情(已保存的)
  1057. materialInfo, err := material.GetMaterialById(sandboxId)
  1058. if err != nil {
  1059. br.Msg = "获取失败"
  1060. br.ErrMsg = "获取失败,Err:" + err.Error()
  1061. return
  1062. }
  1063. msg := "获取成功"
  1064. br.Ret = 200
  1065. br.Success = true
  1066. br.Msg = msg
  1067. br.Data = materialInfo
  1068. }
  1069. // MaterialClassifyList
  1070. // @Title 获取所有素材库分类接口-不包含素材库
  1071. // @Description 获取所有素材库分类接口-不包含素材库
  1072. // @Param IsShowMe query bool true "是否只看我的,true、false"
  1073. // @Success 200 {object} data_manage.ChartClassifyListResp
  1074. // @router /classify/list [get]
  1075. func (this *MaterialController) MaterialClassifyList() {
  1076. br := new(models.BaseResponse).Init()
  1077. defer func() {
  1078. this.Data["json"] = br
  1079. this.ServeJSON()
  1080. }()
  1081. resp := new(material.MaterialClassifyListResp)
  1082. rootList, err := material.GetMaterialClassifyByParentId(0)
  1083. if err != nil && err.Error() != utils.ErrNoRow() {
  1084. br.Msg = "获取失败"
  1085. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1086. return
  1087. }
  1088. classifyAll, err := material.GetMaterialClassifyAll()
  1089. if err != nil && err.Error() != utils.ErrNoRow() {
  1090. br.Msg = "获取失败"
  1091. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1092. return
  1093. }
  1094. nodeAll := make([]*material.MaterialClassifyItems, 0)
  1095. for k := range rootList {
  1096. rootNode := rootList[k]
  1097. materialService.MaterialClassifyItemsMakeTree(this.SysUser, classifyAll, rootNode)
  1098. nodeAll = append(nodeAll, rootNode)
  1099. }
  1100. resp.AllNodes = nodeAll
  1101. br.Ret = 200
  1102. br.Success = true
  1103. br.Msg = "获取成功"
  1104. br.Data = resp
  1105. }
  1106. // BatchAdd
  1107. // @Title 批量新增素材
  1108. // @Description 新增/编辑保存素材库接口
  1109. // @Param request body material.AddAndEditSandbox true "type json string"
  1110. // @Success 200 {object} material.Material
  1111. // @router /batch/add [post]
  1112. func (this *MaterialController) BatchAdd() {
  1113. br := new(models.BaseResponse).Init()
  1114. defer func() {
  1115. this.Data["json"] = br
  1116. this.ServeJSON()
  1117. }()
  1118. sysUser := this.SysUser
  1119. if sysUser == nil {
  1120. br.Msg = "请登录"
  1121. br.ErrMsg = "请登录,SysUser Is Empty"
  1122. br.Ret = 408
  1123. return
  1124. }
  1125. var req material.BatchAddMaterialReq
  1126. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1127. if err != nil {
  1128. br.Msg = "参数解析异常!"
  1129. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1130. return
  1131. }
  1132. classifyId := req.ClassifyId
  1133. if classifyId <= 0 {
  1134. br.Msg = "请选择分类"
  1135. return
  1136. }
  1137. materialNames := make([]string, 0)
  1138. namesMap := make(map[string]struct{})
  1139. for _, v := range req.MaterialList {
  1140. if v.MaterialName == "" {
  1141. br.Msg = "请填写图片名称"
  1142. return
  1143. }
  1144. if v.ImgUrl == "" {
  1145. br.Msg = "请上传图片"
  1146. return
  1147. }
  1148. if _, ok := namesMap[v.MaterialName]; ok {
  1149. br.Msg = "图片名称不能重复"
  1150. return
  1151. }
  1152. namesMap[v.MaterialName] = struct{}{}
  1153. materialNames = append(materialNames, v.MaterialName)
  1154. }
  1155. if len(materialNames) == 0 {
  1156. br.Msg = "请填写图片名称"
  1157. return
  1158. }
  1159. existList := make([]*material.Material, 0)
  1160. switch this.Lang {
  1161. case utils.LANG_EN:
  1162. existList, err = material.GetMaterialByNameEns(materialNames)
  1163. if err != nil {
  1164. br.Msg = "获取失败"
  1165. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1166. return
  1167. }
  1168. if len(existList) > 0 {
  1169. msg := "图片名称:"
  1170. for _, v := range existList {
  1171. msg += v.MaterialNameEn + " "
  1172. }
  1173. br.Msg = fmt.Sprintf("%s 已存在", msg)
  1174. return
  1175. }
  1176. default:
  1177. // 判断文件名是否已存在
  1178. existList, err = material.GetMaterialByNames(materialNames)
  1179. if err != nil {
  1180. br.Msg = "获取失败"
  1181. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1182. return
  1183. }
  1184. if len(existList) > 0 {
  1185. msg := "图片名称:"
  1186. for _, v := range existList {
  1187. msg += v.MaterialName + " "
  1188. }
  1189. br.Msg = fmt.Sprintf("%s 已存在", msg)
  1190. return
  1191. }
  1192. }
  1193. err = materialService.BatchAddMaterial(req.MaterialList, classifyId, sysUser.AdminId, sysUser.AdminName)
  1194. if err != nil {
  1195. br.Msg = "文件上传失败"
  1196. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  1197. return
  1198. }
  1199. msg := "添加成功"
  1200. br.Ret = 200
  1201. br.Success = true
  1202. br.Msg = msg
  1203. }
  1204. // Upload
  1205. // @Title 上传素材
  1206. // @Description 上传素材接口
  1207. // @Param request body material.AddAndEditSandbox true "type json string"
  1208. // @Success 200 {object} material.Material
  1209. // @router /upload [post]
  1210. func (this *MaterialController) Upload() {
  1211. br := new(models.BaseResponse).Init()
  1212. defer func() {
  1213. this.Data["json"] = br
  1214. this.ServeJSON()
  1215. }()
  1216. sysUser := this.SysUser
  1217. if sysUser == nil {
  1218. br.Msg = "请登录"
  1219. br.ErrMsg = "请登录,SysUser Is Empty"
  1220. br.Ret = 408
  1221. return
  1222. }
  1223. f, h, err := this.GetFile("file")
  1224. if err != nil {
  1225. br.Msg = "获取资源信息失败"
  1226. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  1227. return
  1228. }
  1229. defer f.Close() //关闭上传文件
  1230. // 不依赖于文件扩展名检查文件格式
  1231. fileData, e := ioutil.ReadAll(f)
  1232. if e != nil {
  1233. br.Msg = "上传失败"
  1234. br.ErrMsg = "读取文件失败, Err: " + e.Error()
  1235. return
  1236. }
  1237. pass := filetype.IsImage(fileData)
  1238. if !pass {
  1239. br.Msg = "文件格式有误"
  1240. br.ErrMsg = "文件格式有误"
  1241. return
  1242. }
  1243. ext := path.Ext(h.Filename)
  1244. randStr := utils.GetRandStringNoSpecialChar(28)
  1245. fileName := randStr + ext
  1246. // 上传到阿里云
  1247. ossDir := utils.RESOURCE_DIR + "material_dir/"
  1248. savePath := ossDir + time.Now().Format("200601/20060102/") + fileName
  1249. // 上传文件
  1250. resourceUrl, err := services.CommonUploadToOssAndFileName(h, fileName, savePath)
  1251. if err != nil {
  1252. br.Msg = "文件上传失败"
  1253. br.ErrMsg = "文件上传失败,Err:" + err.Error()
  1254. return
  1255. }
  1256. resp := new(models.UploadImgResp)
  1257. resp.ResourceUrl = resourceUrl
  1258. br.Msg = "上传成功"
  1259. br.Ret = 200
  1260. br.Success = true
  1261. br.Data = resp
  1262. }
  1263. // BatchChangeClassify
  1264. // @Title 批量更换分类
  1265. // @Description 批量更换分类
  1266. // @Param request body material.DeleteSandbox true "type json string"
  1267. // @Success 200 标记成功
  1268. // @router /batch/changeClassify [post]
  1269. func (this *MaterialController) BatchChangeClassify() {
  1270. br := new(models.BaseResponse).Init()
  1271. defer func() {
  1272. this.Data["json"] = br
  1273. this.ServeJSON()
  1274. }()
  1275. sysUser := this.SysUser
  1276. if sysUser == nil {
  1277. br.Msg = "请登录"
  1278. br.ErrMsg = "请登录,SysUser Is Empty"
  1279. br.Ret = 408
  1280. return
  1281. }
  1282. var req material.BatchChangeClassifyMaterialReq
  1283. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1284. if err != nil {
  1285. br.Msg = "参数解析异常!"
  1286. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1287. return
  1288. }
  1289. if req.NewClassifyId <= 0 {
  1290. br.Msg = "请选择新的分类"
  1291. return
  1292. }
  1293. updateMaterialIds := make([]int, 0)
  1294. if req.IsSelectAll {
  1295. classifyId := req.ClassifyId
  1296. keyword := req.Keyword
  1297. isShowMe := req.IsShowMe
  1298. //获取图表信息
  1299. list, e, msg := materialService.GetBatchSelectedMaterialList(classifyId, keyword, isShowMe, sysUser, this.Lang)
  1300. if e != nil {
  1301. br.Msg = "获取素材库信息失败"
  1302. if msg != "" {
  1303. br.Msg = msg
  1304. }
  1305. br.ErrMsg = "获取素材库信息失败,Err:" + e.Error()
  1306. return
  1307. }
  1308. notSelectIds := make(map[int]struct{})
  1309. if len(req.MaterialIds) >= 0 {
  1310. for _, v := range req.MaterialIds {
  1311. notSelectIds[v] = struct{}{}
  1312. }
  1313. }
  1314. for _, v := range list {
  1315. if _, ok := notSelectIds[v.MaterialId]; !ok {
  1316. updateMaterialIds = append(updateMaterialIds, v.MaterialId)
  1317. }
  1318. }
  1319. } else {
  1320. updateMaterialIds = req.MaterialIds
  1321. }
  1322. if len(updateMaterialIds) <= 0 {
  1323. br.Msg = "请选择变更的素材"
  1324. return
  1325. }
  1326. // 判断新分类是否存在
  1327. _, err = material.GetMaterialClassifyById(req.NewClassifyId)
  1328. if err != nil {
  1329. if err.Error() == utils.ErrNoRow() {
  1330. br.Msg = "分类不存在"
  1331. return
  1332. }
  1333. br.Msg = "获取分类信息失败"
  1334. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  1335. }
  1336. //更换分类素材库
  1337. err = material.UpdateClassifyByMaterialIds(updateMaterialIds, req.NewClassifyId, time.Now())
  1338. if err != nil {
  1339. br.Msg = err.Error()
  1340. return
  1341. }
  1342. msg := "操作成功"
  1343. br.Ret = 200
  1344. br.Success = true
  1345. br.Msg = msg
  1346. }
  1347. // ChangeClassify
  1348. // @Title 更换分类
  1349. // @Description 批量更换分类
  1350. // @Param request body material.DeleteSandbox true "type json string"
  1351. // @Success 200 标记成功
  1352. // @router /changeClassify [post]
  1353. func (this *MaterialController) ChangeClassify() {
  1354. br := new(models.BaseResponse).Init()
  1355. defer func() {
  1356. this.Data["json"] = br
  1357. this.ServeJSON()
  1358. }()
  1359. sysUser := this.SysUser
  1360. if sysUser == nil {
  1361. br.Msg = "请登录"
  1362. br.ErrMsg = "请登录,SysUser Is Empty"
  1363. br.Ret = 408
  1364. return
  1365. }
  1366. var req material.ChangeClassifyMaterialReq
  1367. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1368. if err != nil {
  1369. br.Msg = "参数解析异常!"
  1370. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1371. return
  1372. }
  1373. if req.MaterialId <= 0 {
  1374. br.Msg = "缺少素材库编号"
  1375. return
  1376. }
  1377. if req.NewClassifyId <= 0 {
  1378. br.Msg = "请选择新的分类"
  1379. return
  1380. }
  1381. // 判断素材是否存在
  1382. info, err := material.GetMaterialById(req.MaterialId)
  1383. if err != nil {
  1384. if err.Error() == utils.ErrNoRow() {
  1385. br.Msg = "素材不存在"
  1386. return
  1387. }
  1388. br.Msg = "获取素材库信息失败"
  1389. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1390. return
  1391. }
  1392. if req.NewClassifyId == info.ClassifyId {
  1393. br.Msg = "请选择不同的分类"
  1394. return
  1395. }
  1396. // 判断新分类是否存在
  1397. _, err = material.GetMaterialClassifyById(req.NewClassifyId)
  1398. if err != nil {
  1399. if err.Error() == utils.ErrNoRow() {
  1400. br.Msg = "分类不存在"
  1401. return
  1402. }
  1403. br.Msg = "获取分类信息失败"
  1404. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  1405. }
  1406. info.ClassifyId = req.NewClassifyId
  1407. info.ModifyTime = time.Now()
  1408. //更换分类素材库
  1409. err = info.Update([]string{"ClassifyId", "ModifyTime"})
  1410. if err != nil {
  1411. br.Msg = err.Error()
  1412. return
  1413. }
  1414. msg := "操作成功"
  1415. br.Ret = 200
  1416. br.Success = true
  1417. br.Msg = msg
  1418. }
  1419. // Rename
  1420. // @Title 素材重命名
  1421. // @Description 素材重命名
  1422. // @Param request body material.DeleteSandbox true "type json string"
  1423. // @Success 200 标记成功
  1424. // @router /rename [post]
  1425. func (this *MaterialController) Rename() {
  1426. br := new(models.BaseResponse).Init()
  1427. defer func() {
  1428. this.Data["json"] = br
  1429. this.ServeJSON()
  1430. }()
  1431. sysUser := this.SysUser
  1432. if sysUser == nil {
  1433. br.Msg = "请登录"
  1434. br.ErrMsg = "请登录,SysUser Is Empty"
  1435. br.Ret = 408
  1436. return
  1437. }
  1438. var req material.RenameMaterialReq
  1439. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1440. if err != nil {
  1441. br.Msg = "参数解析异常!"
  1442. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1443. return
  1444. }
  1445. if req.MaterialId <= 0 {
  1446. br.Msg = "缺少素材库编号"
  1447. return
  1448. }
  1449. if req.MaterialName == "" {
  1450. br.Msg = "缺少素材库名称"
  1451. return
  1452. }
  1453. // 判断素材是否存在
  1454. info, err := material.GetMaterialById(req.MaterialId)
  1455. if err != nil {
  1456. if err.Error() == utils.ErrNoRow() {
  1457. br.Msg = "素材不存在"
  1458. return
  1459. }
  1460. br.Msg = "获取素材库信息失败"
  1461. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1462. return
  1463. }
  1464. var exist *material.Material
  1465. updateStr := make([]string, 0)
  1466. switch this.Lang {
  1467. case utils.LANG_EN:
  1468. // 判断名称是否重复
  1469. if info.MaterialNameEn == req.MaterialName {
  1470. br.Msg = "名称未修改"
  1471. return
  1472. }
  1473. exist, err = material.GetMaterialByNameEn(req.MaterialName)
  1474. info.MaterialNameEn = req.MaterialName
  1475. updateStr = append(updateStr, "MaterialNameEn")
  1476. default:
  1477. // 判断名称是否重复
  1478. if info.MaterialName == req.MaterialName {
  1479. br.Msg = "名称未修改"
  1480. return
  1481. }
  1482. exist, err = material.GetMaterialByName(req.MaterialName)
  1483. info.MaterialName = req.MaterialName
  1484. updateStr = append(updateStr, "MaterialName")
  1485. }
  1486. if err != nil && err.Error() != utils.ErrNoRow() {
  1487. br.Msg = "获取失败"
  1488. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1489. return
  1490. }
  1491. if err == nil && exist.MaterialId > 0 {
  1492. br.Msg = "图片名称已存在"
  1493. return
  1494. }
  1495. info.ModifyTime = time.Now()
  1496. updateStr = append(updateStr, "ModifyTime")
  1497. //更换分类素材库
  1498. err = info.Update(updateStr)
  1499. if err != nil {
  1500. br.Msg = err.Error()
  1501. return
  1502. }
  1503. msg := "操作成功"
  1504. br.Ret = 200
  1505. br.Success = true
  1506. br.Msg = msg
  1507. }
  1508. // BatchDownload
  1509. // @Title 批量下载素材
  1510. // @Description 批量下载素材
  1511. // @Param request body material.DeleteSandbox true "type json string"
  1512. // @Success 200 标记成功
  1513. // @router /batch/download [get]
  1514. func (this *MaterialController) BatchDownload() {
  1515. br := new(models.BaseResponse).Init()
  1516. defer func() {
  1517. this.Data["json"] = br
  1518. this.ServeJSON()
  1519. }()
  1520. sysUser := this.SysUser
  1521. if sysUser == nil {
  1522. br.Msg = "请登录"
  1523. br.ErrMsg = "请登录,SysUser Is Empty"
  1524. br.Ret = 408
  1525. return
  1526. }
  1527. //var req material.BatchDeleteMaterialReq
  1528. materialIdStr := this.GetString("MaterialIds")
  1529. isSelectAll, _ := this.GetBool("IsSelectAll")
  1530. classifyId, _ := this.GetInt("ClassifyId")
  1531. keyword := this.GetString("Keyword")
  1532. isShowMe, _ := this.GetBool("IsShowMe")
  1533. downMaterialList := make([]*material.MaterialListItems, 0)
  1534. materialIds := strings.Split(materialIdStr, ",")
  1535. var err error
  1536. if isSelectAll {
  1537. //获取图表信息
  1538. list, e, msg := materialService.GetBatchSelectedMaterialList(classifyId, keyword, isShowMe, sysUser, this.Lang)
  1539. if e != nil {
  1540. br.Msg = "获取素材库信息失败"
  1541. if msg != "" {
  1542. br.Msg = msg
  1543. }
  1544. br.ErrMsg = "获取素材库信息失败,Err:" + e.Error()
  1545. return
  1546. }
  1547. notSelectIds := make(map[int]struct{})
  1548. if len(materialIds) >= 0 {
  1549. //转成数组
  1550. for _, v := range materialIds {
  1551. id, _ := strconv.Atoi(v)
  1552. notSelectIds[id] = struct{}{}
  1553. }
  1554. }
  1555. for _, v := range list {
  1556. if _, ok := notSelectIds[v.MaterialId]; !ok {
  1557. downMaterialList = append(downMaterialList, v)
  1558. }
  1559. }
  1560. } else {
  1561. if len(materialIds) > 0 {
  1562. // 批量查询指标数据
  1563. materialIdsInt := make([]int, 0)
  1564. for _, v := range materialIds {
  1565. id, _ := strconv.Atoi(v)
  1566. materialIdsInt = append(materialIdsInt, id)
  1567. }
  1568. downMaterialList, err = material.GetMaterialByIds(materialIdsInt)
  1569. if err != nil {
  1570. br.Msg = "获取素材库信息失败"
  1571. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1572. return
  1573. }
  1574. }
  1575. }
  1576. if len(downMaterialList) <= 0 {
  1577. br.Msg = "请选择要下载的素材"
  1578. return
  1579. }
  1580. // 创建zip
  1581. zipName := time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandString(5) + ".zip"
  1582. savePath := zipName
  1583. zipFile, err := os.Create(zipName)
  1584. if err != nil {
  1585. br.Msg = "操作失败"
  1586. br.ErrMsg = "生成压缩文件失败, Err: " + err.Error()
  1587. return
  1588. }
  1589. zipWriter := zip.NewWriter(zipFile)
  1590. // 生成zip过程中报错关闭
  1591. defer func() {
  1592. if err != nil {
  1593. _ = zipWriter.Close()
  1594. _ = zipFile.Close()
  1595. }
  1596. _ = os.Remove(savePath)
  1597. }()
  1598. // 获取资源, 写入zip
  1599. zipFileName := ""
  1600. for i := range downMaterialList {
  1601. if downMaterialList[i].MaterialName == "" || downMaterialList[i].ImgUrl == "" {
  1602. continue
  1603. }
  1604. fmt.Printf("开始压缩第%d个文件\n", i+1)
  1605. dotIndex := strings.LastIndex(downMaterialList[i].ImgUrl, ".")
  1606. // 如果找不到点,或者点是文件名的第一个字符(不合法情况),则返回空字符串
  1607. if dotIndex == -1 || dotIndex == 0 {
  1608. continue
  1609. }
  1610. fileExt := downMaterialList[i].ImgUrl[dotIndex+1:]
  1611. ioWriter, err := zipWriter.Create(fmt.Sprintf("%s.%s", downMaterialList[i].MaterialName, fileExt))
  1612. if err != nil {
  1613. if os.IsPermission(err) {
  1614. br.Msg = "操作失败"
  1615. br.ErrMsg = "打包权限不足, Err: " + err.Error()
  1616. return
  1617. }
  1618. br.Msg = "操作失败"
  1619. br.ErrMsg = "压缩出错, Err: " + err.Error()
  1620. return
  1621. }
  1622. var content []byte
  1623. content, err = http.Get(downMaterialList[i].ImgUrl)
  1624. if err != nil {
  1625. br.Msg = "操作失败"
  1626. br.ErrMsg = "资源获取失败, Err: " + err.Error()
  1627. return
  1628. }
  1629. _, err = ioWriter.Write(content)
  1630. if err != nil {
  1631. br.Msg = "操作失败"
  1632. br.ErrMsg = "压缩文件写入失败, Err: " + err.Error()
  1633. return
  1634. }
  1635. if zipFileName == "" {
  1636. zipFileName = downMaterialList[i].MaterialName
  1637. }
  1638. fmt.Printf("第%d个文件写入成功\n", i+1)
  1639. }
  1640. // 生成zip后关闭,否则下载文件会损坏
  1641. _ = zipWriter.Close()
  1642. _ = zipFile.Close()
  1643. this.Ctx.Output.Download(savePath, fmt.Sprintf("%s.zip", zipFileName))
  1644. msg := "操作成功"
  1645. br.Ret = 200
  1646. br.Success = true
  1647. br.Msg = msg
  1648. }
  1649. // Download
  1650. // @Title 下载素材
  1651. // @Description 下载素材
  1652. // @Param request body material.DeleteSandbox true "type json string"
  1653. // @Success 200 标记成功
  1654. // @router /download [get]
  1655. func (this *MaterialController) Download() {
  1656. br := new(models.BaseResponse).Init()
  1657. defer func() {
  1658. this.Data["json"] = br
  1659. this.ServeJSON()
  1660. }()
  1661. sysUser := this.SysUser
  1662. if sysUser == nil {
  1663. br.Msg = "请登录"
  1664. br.ErrMsg = "请登录,SysUser Is Empty"
  1665. br.Ret = 408
  1666. return
  1667. }
  1668. materialId, _ := this.GetInt("MaterialId")
  1669. if materialId <= 0 {
  1670. br.Msg = "缺少素材库编号"
  1671. return
  1672. }
  1673. // 判断素材是否存在
  1674. info, err := material.GetMaterialById(materialId)
  1675. if err != nil {
  1676. if err.Error() == utils.ErrNoRow() {
  1677. br.Msg = "素材不存在"
  1678. return
  1679. }
  1680. br.Msg = "获取素材库信息失败"
  1681. br.ErrMsg = "获取素材库信息失败,Err:" + err.Error()
  1682. return
  1683. }
  1684. if info.ImgUrl == "" {
  1685. br.Msg = "素材地址为空"
  1686. return
  1687. }
  1688. fileName := info.MaterialName
  1689. // 查找文件名中最后一个点的位置
  1690. dotIndex := strings.LastIndex(info.ImgUrl, ".")
  1691. // 如果找不到点,或者点是文件名的第一个字符(不合法情况),则返回空字符串
  1692. if dotIndex == -1 || dotIndex == 0 {
  1693. br.Msg = "素材地址错误"
  1694. return
  1695. }
  1696. fileName += "." + info.ImgUrl[dotIndex+1:] // 添加扩展名到文件名
  1697. // 获取路径中的文件名
  1698. urlFileName := path.Base(info.ImgUrl)
  1699. uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
  1700. if e := os.MkdirAll(uploadDir, utils.DIR_MOD); e != nil {
  1701. br.Msg = "存储目录创建失败"
  1702. br.ErrMsg = "存储目录创建失败, Err:" + e.Error()
  1703. return
  1704. }
  1705. var content []byte
  1706. content, err = http.Get(info.ImgUrl)
  1707. if err != nil {
  1708. br.Msg = "操作失败"
  1709. br.ErrMsg = "资源获取失败, Err: " + err.Error()
  1710. return
  1711. }
  1712. filePath := uploadDir + "/" + urlFileName
  1713. ioWriter, err := os.Create(filePath)
  1714. if err != nil {
  1715. br.Msg = "操作失败"
  1716. br.ErrMsg = "文件创建失败, Err: " + err.Error()
  1717. return
  1718. }
  1719. n, err := ioWriter.Write(content)
  1720. fmt.Println("n", n)
  1721. if err != nil {
  1722. br.Msg = "操作失败"
  1723. br.ErrMsg = "压缩文件写入失败, Err: " + err.Error()
  1724. return
  1725. }
  1726. this.Ctx.Output.Download(filePath, fileName)
  1727. defer func() {
  1728. _ = os.Remove(filePath)
  1729. }()
  1730. br.Ret = 200
  1731. br.Success = true
  1732. br.Msg = "success"
  1733. }