material.go 53 KB

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