material.go 49 KB

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