123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- 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",
- }
- 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
- }
- // Rename
- // @Title 重命名
- // @Description 重命名
- // @Param request body smart_report.SmartReportPublishReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /resource/rename [post]
- func (this *SmartReportResourceController) Rename() {
- 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.SmartReportRenameReq
- 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"}
- item.ImgName = req.ImgName
- 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()
- if e := item.Create(); e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "新增资源失败, Err: " + e.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
|