image_conf_controller.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/utils"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. )
  8. type ImageConfController struct {
  9. BaseAuthController
  10. }
  11. // GetImageMaterial
  12. // @Title 根据条件查询图片素材
  13. // @Description 根据条件查询图片素材
  14. // @Success 200 {object} models.ImageConf
  15. // @router /get/image/material [get]
  16. func (this *ImageConfController) GetImageMaterial() {
  17. br := new(models.BaseResponse).Init()
  18. defer func() {
  19. if br.ErrMsg == "" {
  20. br.IsSendEmail = false
  21. }
  22. this.Data["json"] = br
  23. this.ServeJSON()
  24. }()
  25. sysUser := this.SysUser
  26. if sysUser == nil {
  27. br.Msg = "请登录"
  28. br.ErrMsg = "请登录,SysUser Is Empty"
  29. br.Ret = 408
  30. return
  31. }
  32. confType, err := this.GetInt("ConfType")
  33. if err != nil {
  34. return
  35. }
  36. if confType <= 0 {
  37. br.Msg = "请选择配置类型!"
  38. br.ErrMsg = "请选择配置类型!"
  39. return
  40. }
  41. imageType, _ := this.GetInt("ImageType")
  42. imageName := this.GetString("ImageName")
  43. pageSize, _ := this.GetInt("PageSize")
  44. currentIndex, _ := this.GetInt("CurrentIndex")
  45. if pageSize <= 0 {
  46. pageSize = utils.PageSize20
  47. }
  48. if currentIndex <= 0 {
  49. currentIndex = 1
  50. }
  51. startSize := paging.StartIndex(currentIndex, pageSize)
  52. var condition string
  53. var pars []interface{}
  54. condition += ` conf_type=? `
  55. pars = append(pars, confType)
  56. if imageType > 0 {
  57. condition += ` AND image_type=? `
  58. pars = append(pars, imageType)
  59. }
  60. if imageName != "" {
  61. condition += ` AND image_name LIKE '%` + imageName + `%' `
  62. }
  63. count, err := models.GetImageConfByConditionCount(condition, pars)
  64. if err != nil {
  65. return
  66. }
  67. pagingItem := paging.GetPaging(currentIndex, pageSize, int(count))
  68. var ImageConfPage models.ImageConfPage
  69. ImageConfPage.Paging = pagingItem
  70. if count <= 0 {
  71. ImageConfPage.List = []*models.ImageConf{}
  72. br.Msg = "操作成功"
  73. br.Ret = 200
  74. br.Data = ImageConfPage
  75. return
  76. }
  77. condition += ` ORDER BY create_time asc `
  78. condition += ` LIMIT ?, ? `
  79. pars = append(pars, startSize, pageSize)
  80. imageConfList, err := models.GetImageConfByCondition(condition, pars)
  81. ImageConfPage.List = imageConfList
  82. br.Msg = "操作成功"
  83. br.Ret = 200
  84. br.Data = ImageConfPage
  85. return
  86. }
  87. // AddImageMaterial
  88. // @Title 新增图片素材
  89. // @Description 新增图片素材
  90. // @Success 200
  91. // @router /add/image/material [post]
  92. func (this *ImageConfController) AddImageMaterial() {
  93. br := new(models.BaseResponse).Init()
  94. defer func() {
  95. if br.ErrMsg == "" {
  96. br.IsSendEmail = false
  97. }
  98. this.Data["json"] = br
  99. this.ServeJSON()
  100. }()
  101. sysUser := this.SysUser
  102. if sysUser == nil {
  103. br.Msg = "请登录"
  104. br.ErrMsg = "请登录,SysUser Is Empty"
  105. br.Ret = 408
  106. return
  107. }
  108. var req []*models.ImageConf
  109. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  110. br.Msg = "参数解析异常!"
  111. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  112. return
  113. }
  114. // 校验图片名称是否重复
  115. for _, item := range req {
  116. if item.ImageType <= 0 {
  117. br.Msg = "请选择图片类型!"
  118. br.ErrMsg = "请选择图片类型!"
  119. return
  120. }
  121. if item.Url == "" {
  122. br.Msg = "请上传图片!"
  123. br.ErrMsg = "请上传图片!"
  124. return
  125. }
  126. // 校验名称是否重复
  127. imageConfByName, err := models.GetImageConfByName(item.ImageName)
  128. if err != nil {
  129. return
  130. }
  131. if imageConfByName != nil {
  132. br.Msg = "图片名称已存在,请重新上传!"
  133. br.ErrMsg = "图片名称已存在,请重新上传!"
  134. return
  135. }
  136. }
  137. err := models.BatchAddImageMaterials(req, len(req))
  138. if err != nil {
  139. br.Msg = "操作失败"
  140. br.ErrMsg = "操作失败,Err:" + err.Error()
  141. return
  142. }
  143. br.Msg = "操作成功"
  144. br.Ret = 200
  145. return
  146. }
  147. // EditImageMaterial
  148. // @Title 修改图片素材
  149. // @Description 修改图片素材
  150. // @Success 200
  151. // @router /edit/image/material [post]
  152. func (this *ImageConfController) EditImageMaterial() {
  153. br := new(models.BaseResponse).Init()
  154. defer func() {
  155. if br.ErrMsg == "" {
  156. br.IsSendEmail = false
  157. }
  158. this.Data["json"] = br
  159. this.ServeJSON()
  160. }()
  161. sysUser := this.SysUser
  162. if sysUser == nil {
  163. br.Msg = "请登录"
  164. br.ErrMsg = "请登录,SysUser Is Empty"
  165. br.Ret = 408
  166. return
  167. }
  168. var req *models.ImageConf
  169. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  170. br.Msg = "参数解析异常!"
  171. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  172. return
  173. }
  174. if req.ImageType <= 0 {
  175. br.Msg = "请选择图片类型!"
  176. br.ErrMsg = "请选择图片类型!"
  177. return
  178. }
  179. if req.Url == "" {
  180. br.Msg = "请上传图片!"
  181. br.ErrMsg = "请上传图片!"
  182. return
  183. }
  184. // 校验名称是否重复
  185. imageConfByName, err := models.GetImageConfByName(req.ImageName)
  186. if err != nil {
  187. return
  188. }
  189. if imageConfByName != nil && imageConfByName.ImageConfId != req.ImageConfId {
  190. br.Msg = "图片名称已存在,请重新上传!"
  191. br.ErrMsg = "图片名称已存在,请重新上传!"
  192. return
  193. }
  194. imageConf, err := models.GetImageConfById(req.ImageConfId)
  195. if err != nil {
  196. return
  197. }
  198. if imageConf.ImageConfId == 0 {
  199. br.Msg = "该素材已被删除"
  200. br.ErrMsg = "操作失败,Err: 该素材已被删除"
  201. return
  202. }
  203. err = models.EditImageMaterial(req)
  204. if err != nil {
  205. br.Msg = "操作失败"
  206. br.ErrMsg = "操作失败,Err:" + err.Error()
  207. return
  208. }
  209. br.Msg = "操作成功"
  210. br.Ret = 200
  211. return
  212. }
  213. // DeleteImageMaterial
  214. // @Title 删除图片素材
  215. // @Description 删除图片素材
  216. // @Success 200
  217. // @router /delete/image/material [post]
  218. func (this *ImageConfController) DeleteImageMaterial() {
  219. br := new(models.BaseResponse).Init()
  220. defer func() {
  221. if br.ErrMsg == "" {
  222. br.IsSendEmail = false
  223. }
  224. this.Data["json"] = br
  225. this.ServeJSON()
  226. }()
  227. sysUser := this.SysUser
  228. if sysUser == nil {
  229. br.Msg = "请登录"
  230. br.ErrMsg = "请登录,SysUser Is Empty"
  231. br.Ret = 408
  232. return
  233. }
  234. type DeleteReq struct {
  235. ImageConfId int `json:"ImageConfId"`
  236. }
  237. var req []DeleteReq
  238. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  239. br.Msg = "参数解析异常!"
  240. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  241. return
  242. }
  243. var imageConfIds []int
  244. for _, deleteReq := range req {
  245. // 查看图片是否被使用
  246. count, err := models.GetPptByImageIdCount(deleteReq.ImageConfId)
  247. if err != nil {
  248. return
  249. }
  250. if count > 0 {
  251. br.Msg = "图片已被使用,不允许删除"
  252. br.ErrMsg = "图片已被使用,不允许删除"
  253. return
  254. }
  255. imageConfIds = append(imageConfIds, deleteReq.ImageConfId)
  256. }
  257. err := models.DeleteImageConfByIds(imageConfIds)
  258. if err != nil {
  259. return
  260. }
  261. br.Msg = "操作成功"
  262. br.Ret = 200
  263. return
  264. }