123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- package controllers
- import (
- "encoding/json"
- "eta_gn/eta_api/models"
- "eta_gn/eta_api/utils"
- "github.com/rdlucklib/rdluck_tools/paging"
- )
- type ImageConfController struct {
- BaseAuthController
- }
- // GetImageMaterial
- // @Title 根据条件查询图片素材
- // @Description 根据条件查询图片素材
- // @Success 200 {object} models.ImageConf
- // @router /get/image/material [get]
- func (this *ImageConfController) GetImageMaterial() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- confType, err := this.GetInt("ConfType")
- if err != nil {
- return
- }
- if confType <= 0 {
- br.Msg = "请选择配置类型!"
- br.ErrMsg = "请选择配置类型!"
- return
- }
- imageType, _ := this.GetInt("ImageType")
- imageName := this.GetString("ImageName")
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize := paging.StartIndex(currentIndex, pageSize)
- var condition string
- var pars []interface{}
- condition += ` conf_type=? `
- pars = append(pars, confType)
- if imageType > 0 {
- condition += ` AND image_type=? `
- pars = append(pars, imageType)
- }
- if imageName != "" {
- condition += ` AND image_name LIKE '%` + imageName + `%' `
- }
- count, err := models.GetImageConfByConditionCount(condition, pars)
- if err != nil {
- return
- }
- pagingItem := paging.GetPaging(currentIndex, pageSize, int(count))
- var ImageConfPage models.ImageConfPage
- ImageConfPage.Paging = pagingItem
- if count <= 0 {
- ImageConfPage.List = []*models.ImageConf{}
- br.Msg = "操作成功"
- br.Ret = 200
- br.Data = ImageConfPage
- return
- }
- condition += ` ORDER BY create_time asc `
- condition += ` LIMIT ?, ? `
- pars = append(pars, startSize, pageSize)
- imageConfList, err := models.GetImageConfByCondition(condition, pars)
- ImageConfPage.List = imageConfList
- br.Msg = "操作成功"
- br.Ret = 200
- br.Data = ImageConfPage
- return
- }
- // AddImageMaterial
- // @Title 新增图片素材
- // @Description 新增图片素材
- // @Success 200
- // @router /add/image/material [post]
- func (this *ImageConfController) AddImageMaterial() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req []*models.ImageConf
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- // 校验图片名称是否重复
- for _, item := range req {
- if item.ImageType <= 0 {
- br.Msg = "请选择图片类型!"
- br.ErrMsg = "请选择图片类型!"
- return
- }
- if item.Url == "" {
- br.Msg = "请上传图片!"
- br.ErrMsg = "请上传图片!"
- return
- }
- // 校验名称是否重复
- imageConfByName, err := models.GetImageConfByName(item.ImageName)
- if err != nil {
- return
- }
- if imageConfByName != nil {
- br.Msg = "图片名称已存在,请重新上传!"
- br.ErrMsg = "图片名称已存在,请重新上传!"
- return
- }
- }
- err := models.BatchAddImageMaterials(req, len(req))
- if err != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "操作失败,Err:" + err.Error()
- return
- }
- br.Msg = "操作成功"
- br.Ret = 200
- return
- }
- // EditImageMaterial
- // @Title 修改图片素材
- // @Description 修改图片素材
- // @Success 200
- // @router /edit/image/material [post]
- func (this *ImageConfController) EditImageMaterial() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req *models.ImageConf
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if req.ImageType <= 0 {
- br.Msg = "请选择图片类型!"
- br.ErrMsg = "请选择图片类型!"
- return
- }
- if req.Url == "" {
- br.Msg = "请上传图片!"
- br.ErrMsg = "请上传图片!"
- return
- }
- // 校验名称是否重复
- imageConfByName, err := models.GetImageConfByName(req.ImageName)
- if err != nil {
- return
- }
- if imageConfByName != nil && imageConfByName.ImageConfId != req.ImageConfId {
- br.Msg = "图片名称已存在,请重新上传!"
- br.ErrMsg = "图片名称已存在,请重新上传!"
- return
- }
- imageConf, err := models.GetImageConfById(req.ImageConfId)
- if err != nil {
- return
- }
- if imageConf.ImageConfId == 0 {
- br.Msg = "该素材已被删除"
- br.ErrMsg = "操作失败,Err: 该素材已被删除"
- return
- }
- err = models.EditImageMaterial(req)
- if err != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "操作失败,Err:" + err.Error()
- return
- }
- br.Msg = "操作成功"
- br.Ret = 200
- return
- }
- // DeleteImageMaterial
- // @Title 删除图片素材
- // @Description 删除图片素材
- // @Success 200
- // @router /delete/image/material [post]
- func (this *ImageConfController) DeleteImageMaterial() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- type DeleteReq struct {
- ImageConfId int `json:"ImageConfId"`
- }
- var req []DeleteReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- var imageConfIds []int
- for _, deleteReq := range req {
- // 查看图片是否被使用
- count, err := models.GetPptByImageIdCount(deleteReq.ImageConfId)
- if err != nil {
- return
- }
- if count > 0 {
- br.Msg = "图片已被使用,不允许删除"
- br.ErrMsg = "图片已被使用,不允许删除"
- return
- }
- imageConfIds = append(imageConfIds, deleteReq.ImageConfId)
- }
- err := models.DeleteImageConfByIds(imageConfIds)
- if err != nil {
- return
- }
- br.Msg = "操作成功"
- br.Ret = 200
- return
- }
|