smart_resource.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package smart_report
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/smart_report"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // SmartReportResourceController 智能研报资源库
  15. type SmartReportResourceController struct {
  16. controllers.BaseAuthController
  17. }
  18. // List
  19. // @Title 资源库列表
  20. // @Description 资源库列表
  21. // @Param PageSize query int true "每页数据条数"
  22. // @Param CurrentIndex query int true "当前页页码"
  23. // @Param Type query int false "资源类型: 1-版头; 2-版尾"
  24. // @Param Keyword query string false "搜索关键词"
  25. // @Success 200 {object} smart_report.SmartReportListResp
  26. // @router /resource/list [get]
  27. func (this *SmartReportResourceController) List() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. if br.ErrMsg == "" {
  31. br.IsSendEmail = false
  32. }
  33. this.Data["json"] = br
  34. this.ServeJSON()
  35. }()
  36. sysUser := this.SysUser
  37. if sysUser == nil {
  38. br.Msg = "请登录"
  39. br.ErrMsg = "请登录,SysUser Is Empty"
  40. br.Ret = 408
  41. return
  42. }
  43. type SmartReportResourceListReq struct {
  44. PageSize int `form:"PageSize"`
  45. CurrentIndex int `form:"CurrentIndex"`
  46. Type int `form:"Type"`
  47. Keyword string `form:"Keyword"`
  48. }
  49. params := new(SmartReportResourceListReq)
  50. if e := this.ParseForm(params); e != nil {
  51. br.Msg = "获取失败"
  52. br.ErrMsg = "入参解析失败, Err: " + e.Error()
  53. return
  54. }
  55. var condition string
  56. var pars []interface{}
  57. // 筛选项
  58. {
  59. keyword := strings.TrimSpace(params.Keyword)
  60. if keyword != "" {
  61. kw := fmt.Sprint("%", keyword, "%")
  62. condition += fmt.Sprintf(` AND img_name LIKE ?`)
  63. pars = append(pars, kw)
  64. }
  65. if params.Type > 0 {
  66. condition += ` AND type = ?`
  67. pars = append(pars, params.Type)
  68. }
  69. }
  70. resp := new(smart_report.SmartReportResourceListResp)
  71. reportOB := new(smart_report.SmartReportResource)
  72. total, e := reportOB.GetCountByCondition(condition, pars)
  73. if e != nil {
  74. br.Msg = "获取失败"
  75. br.ErrMsg = "获取报告总数失败, Err:" + e.Error()
  76. return
  77. }
  78. if total <= 0 {
  79. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  80. resp.Paging = page
  81. br.Ret = 200
  82. br.Success = true
  83. br.Msg = "获取成功"
  84. br.Data = resp
  85. return
  86. }
  87. // 分页列表
  88. var startSize int
  89. if params.PageSize <= 0 {
  90. params.PageSize = utils.PageSize20
  91. }
  92. if params.CurrentIndex <= 0 {
  93. params.CurrentIndex = 1
  94. }
  95. startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
  96. fields := []string{
  97. "resource_id", "create_time", "img_name", "img_url", "type",
  98. }
  99. list, e := reportOB.GetPageItemsByCondition(condition, pars, fields, startSize, params.PageSize)
  100. if e != nil {
  101. br.Msg = "获取失败"
  102. br.ErrMsg = "获取资源库分页列表失败, Err:" + e.Error()
  103. return
  104. }
  105. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  106. resp.Paging = page
  107. resp.List = list
  108. br.Ret = 200
  109. br.Success = true
  110. br.Msg = "获取成功"
  111. br.Data = resp
  112. }
  113. // Rename
  114. // @Title 重命名
  115. // @Description 重命名
  116. // @Param request body smart_report.SmartReportPublishReq true "type json string"
  117. // @Success 200 string "操作成功"
  118. // @router /resource/rename [post]
  119. func (this *SmartReportResourceController) Rename() {
  120. br := new(models.BaseResponse).Init()
  121. defer func() {
  122. if br.ErrMsg == "" {
  123. br.IsSendEmail = false
  124. }
  125. this.Data["json"] = br
  126. this.ServeJSON()
  127. }()
  128. sysUser := this.SysUser
  129. if sysUser == nil {
  130. br.Msg = "请登录"
  131. br.ErrMsg = "请登录,SysUser Is Empty"
  132. br.Ret = 408
  133. return
  134. }
  135. var req smart_report.SmartReportRenameReq
  136. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  137. if err != nil {
  138. br.Msg = "参数解析异常!"
  139. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  140. return
  141. }
  142. if req.ResourceId <= 0 {
  143. br.Msg = "参数有误"
  144. br.ErrMsg = "报告ID为空"
  145. return
  146. }
  147. ob := new(smart_report.SmartReportResource)
  148. item, e := ob.GetItemById(req.ResourceId)
  149. if e != nil {
  150. if e.Error() == utils.ErrNoRow() {
  151. br.Msg = "资源不存在, 请刷新页面"
  152. return
  153. }
  154. br.Msg = "操作失败"
  155. br.ErrMsg = "获取资源图片失败, Err: " + e.Error()
  156. return
  157. }
  158. cols := []string{"ImgName"}
  159. item.ImgName = req.ImgName
  160. if e = item.Update(cols); e != nil {
  161. br.Msg = "操作失败"
  162. br.ErrMsg = "更新资源失败, Err: " + e.Error()
  163. return
  164. }
  165. br.Ret = 200
  166. br.Success = true
  167. br.Msg = "操作成功"
  168. }
  169. // Remove
  170. // @Title 删除
  171. // @Description 删除
  172. // @Param request body smart_report.SmartReportRemoveReq true "type json string"
  173. // @Success 200 string "操作成功"
  174. // @router /resource/remove [post]
  175. func (this *SmartReportResourceController) Remove() {
  176. br := new(models.BaseResponse).Init()
  177. defer func() {
  178. if br.ErrMsg == "" {
  179. br.IsSendEmail = false
  180. }
  181. this.Data["json"] = br
  182. this.ServeJSON()
  183. }()
  184. sysUser := this.SysUser
  185. if sysUser == nil {
  186. br.Msg = "请登录"
  187. br.ErrMsg = "请登录,SysUser Is Empty"
  188. br.Ret = 408
  189. return
  190. }
  191. var req smart_report.SmartReportResourceRemoveReq
  192. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  193. if err != nil {
  194. br.Msg = "参数解析异常!"
  195. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  196. return
  197. }
  198. if req.ResourceIds == "" {
  199. br.Msg = "参数有误"
  200. br.ErrMsg = "图片ID为空"
  201. return
  202. }
  203. ids := strings.Split(req.ResourceIds, ",")
  204. for _, idStr := range ids {
  205. ob := new(smart_report.SmartReportResource)
  206. id,err := strconv.Atoi(idStr)
  207. if err != nil {
  208. br.Msg = "参数解析异常!"
  209. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  210. return
  211. }
  212. item, e := ob.GetItemById(id)
  213. if e != nil {
  214. if e.Error() == utils.ErrNoRow() {
  215. br.Ret = 200
  216. br.Success = true
  217. br.Msg = "操作成功"
  218. return
  219. }
  220. br.Msg = "操作失败"
  221. br.ErrMsg = "获取资源失败, Err: " + e.Error()
  222. return
  223. }
  224. if e = item.Del(); e != nil {
  225. br.Msg = "操作失败"
  226. br.ErrMsg = "删除资源失败, Err: " + e.Error()
  227. return
  228. }
  229. }
  230. br.Ret = 200
  231. br.Success = true
  232. br.Msg = "操作成功"
  233. }
  234. // Add
  235. // @Title 新增
  236. // @Description 新增
  237. // @Param request body smart_report.SmartReportAddReq true "type json string"
  238. // @Success 200 {object} smart_report.SmartReportItem
  239. // @router /resource/add [post]
  240. func (this *SmartReportResourceController) Add() {
  241. br := new(models.BaseResponse).Init()
  242. defer func() {
  243. if br.ErrMsg == "" {
  244. br.IsSendEmail = false
  245. }
  246. this.Data["json"] = br
  247. this.ServeJSON()
  248. }()
  249. sysUser := this.SysUser
  250. if sysUser == nil {
  251. br.Msg = "请登录"
  252. br.ErrMsg = "请登录,SysUser Is Empty"
  253. br.Ret = 408
  254. return
  255. }
  256. var req smart_report.SmartReportResourceAddReq
  257. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  258. if err != nil {
  259. br.Msg = "参数解析异常!"
  260. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  261. return
  262. }
  263. if req.Type != 1 && req.Type != 2 {
  264. br.Msg = "请选择新增方式"
  265. return
  266. }
  267. req.ImgName = strings.TrimSpace(req.ImgName)
  268. if req.ImgName == "" {
  269. br.Msg = "请输入图片名称"
  270. return
  271. }
  272. item := new(smart_report.SmartReportResource)
  273. item.Type = req.Type
  274. item.ImgName = req.ImgName
  275. item.ImgUrl = req.ImgUrl
  276. item.CreateTime = time.Now().Local()
  277. if e := item.Create(); e != nil {
  278. br.Msg = "操作失败"
  279. br.ErrMsg = "新增资源失败, Err: " + e.Error()
  280. return
  281. }
  282. br.Ret = 200
  283. br.Success = true
  284. br.Msg = "操作成功"
  285. }