material.go 53 KB

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