package smart_report import ( "encoding/json" "eta/eta_api/controllers" "eta/eta_api/models" "eta/eta_api/models/smart_report" "eta/eta_api/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "strconv" "strings" "time" ) // SmartReportResourceController 智能研报资源库 type SmartReportResourceController struct { controllers.BaseAuthController } // List // @Title 资源库列表 // @Description 资源库列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码" // @Param Type query int false "资源类型: 1-版头; 2-版尾" // @Param Keyword query string false "搜索关键词" // @Success 200 {object} smart_report.SmartReportListResp // @router /resource/list [get] func (this *SmartReportResourceController) List() { 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 SmartReportResourceListReq struct { PageSize int `form:"PageSize"` CurrentIndex int `form:"CurrentIndex"` Type int `form:"Type"` Keyword string `form:"Keyword"` } params := new(SmartReportResourceListReq) if e := this.ParseForm(params); e != nil { br.Msg = "获取失败" br.ErrMsg = "入参解析失败, Err: " + e.Error() return } var condition string var pars []interface{} // 筛选项 { keyword := strings.TrimSpace(params.Keyword) if keyword != "" { kw := fmt.Sprint("%", keyword, "%") condition += fmt.Sprintf(` AND img_name LIKE ?`) pars = append(pars, kw) } if params.Type > 0 { condition += ` AND type = ?` pars = append(pars, params.Type) } } resp := new(smart_report.SmartReportResourceListResp) reportOB := new(smart_report.SmartReportResource) total, e := reportOB.GetCountByCondition(condition, pars) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取报告总数失败, Err:" + e.Error() return } if total <= 0 { page := paging.GetPaging(params.CurrentIndex, params.PageSize, total) resp.Paging = page br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp return } // 分页列表 var startSize int if params.PageSize <= 0 { params.PageSize = utils.PageSize20 } if params.CurrentIndex <= 0 { params.CurrentIndex = 1 } startSize = utils.StartIndex(params.CurrentIndex, params.PageSize) fields := []string{ "resource_id", "create_time", "img_name", "img_url", "type","style", } list, e := reportOB.GetPageItemsByCondition(condition, pars, fields, startSize, params.PageSize) if e != nil { br.Msg = "获取失败" br.ErrMsg = "获取资源库分页列表失败, Err:" + e.Error() return } page := paging.GetPaging(params.CurrentIndex, params.PageSize, total) resp.Paging = page resp.List = list br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // Edit // @Title 编辑 // @Description 编辑 // @Param request body smart_report.SmartReportPublishReq true "type json string" // @Success 200 string "操作成功" // @router /resource/edit [post] func (this *SmartReportResourceController) Edit() { 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 smart_report.SmartReportResourceEditReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ResourceId <= 0 { br.Msg = "参数有误" br.ErrMsg = "报告ID为空" return } ob := new(smart_report.SmartReportResource) item, e := ob.GetItemById(req.ResourceId) if e != nil { if e.Error() == utils.ErrNoRow() { br.Msg = "资源不存在, 请刷新页面" return } br.Msg = "操作失败" br.ErrMsg = "获取资源图片失败, Err: " + e.Error() return } cols := []string{"ImgName","Style"} item.ImgName = req.ImgName item.Style = req.Style if e = item.Update(cols); e != nil { br.Msg = "操作失败" br.ErrMsg = "更新资源失败, Err: " + e.Error() return } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Remove // @Title 删除 // @Description 删除 // @Param request body smart_report.SmartReportRemoveReq true "type json string" // @Success 200 string "操作成功" // @router /resource/remove [post] func (this *SmartReportResourceController) Remove() { 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 smart_report.SmartReportResourceRemoveReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ResourceIds == "" { br.Msg = "参数有误" br.ErrMsg = "图片ID为空" return } ids := strings.Split(req.ResourceIds, ",") for _, idStr := range ids { ob := new(smart_report.SmartReportResource) id,err := strconv.Atoi(idStr) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } item, e := ob.GetItemById(id) if e != nil { if e.Error() == utils.ErrNoRow() { br.Ret = 200 br.Success = true br.Msg = "操作成功" return } br.Msg = "操作失败" br.ErrMsg = "获取资源失败, Err: " + e.Error() return } if e = item.Del(); e != nil { br.Msg = "操作失败" br.ErrMsg = "删除资源失败, Err: " + e.Error() return } } br.Ret = 200 br.Success = true br.Msg = "操作成功" } // Add // @Title 新增 // @Description 新增 // @Param request body smart_report.SmartReportAddReq true "type json string" // @Success 200 {object} smart_report.SmartReportItem // @router /resource/add [post] func (this *SmartReportResourceController) Add() { 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 smart_report.SmartReportResourceAddReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.Type != 1 && req.Type != 2 { br.Msg = "请选择新增方式" return } req.ImgName = strings.TrimSpace(req.ImgName) if req.ImgName == "" { br.Msg = "请输入图片名称" return } item := new(smart_report.SmartReportResource) item.Type = req.Type item.ImgName = req.ImgName item.ImgUrl = req.ImgUrl item.CreateTime = time.Now().Local() item.Style = req.Style if e := item.Create(); e != nil { br.Msg = "操作失败" br.ErrMsg = "新增资源失败, Err: " + e.Error() return } br.Ret = 200 br.Success = true br.Msg = "操作成功" }