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 }