smart_resource.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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","style",
  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. // Edit
  114. // @Title 编辑
  115. // @Description 编辑
  116. // @Param request body smart_report.SmartReportPublishReq true "type json string"
  117. // @Success 200 string "操作成功"
  118. // @router /resource/edit [post]
  119. func (this *SmartReportResourceController) Edit() {
  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.SmartReportResourceEditReq
  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","Style"}
  159. item.ImgName = req.ImgName
  160. item.Style = req.Style
  161. if e = item.Update(cols); e != nil {
  162. br.Msg = "操作失败"
  163. br.ErrMsg = "更新资源失败, Err: " + e.Error()
  164. return
  165. }
  166. br.Ret = 200
  167. br.Success = true
  168. br.Msg = "操作成功"
  169. }
  170. // Remove
  171. // @Title 删除
  172. // @Description 删除
  173. // @Param request body smart_report.SmartReportRemoveReq true "type json string"
  174. // @Success 200 string "操作成功"
  175. // @router /resource/remove [post]
  176. func (this *SmartReportResourceController) Remove() {
  177. br := new(models.BaseResponse).Init()
  178. defer func() {
  179. if br.ErrMsg == "" {
  180. br.IsSendEmail = false
  181. }
  182. this.Data["json"] = br
  183. this.ServeJSON()
  184. }()
  185. sysUser := this.SysUser
  186. if sysUser == nil {
  187. br.Msg = "请登录"
  188. br.ErrMsg = "请登录,SysUser Is Empty"
  189. br.Ret = 408
  190. return
  191. }
  192. var req smart_report.SmartReportResourceRemoveReq
  193. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  194. if err != nil {
  195. br.Msg = "参数解析异常!"
  196. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  197. return
  198. }
  199. if req.ResourceIds == "" {
  200. br.Msg = "参数有误"
  201. br.ErrMsg = "图片ID为空"
  202. return
  203. }
  204. ids := strings.Split(req.ResourceIds, ",")
  205. for _, idStr := range ids {
  206. ob := new(smart_report.SmartReportResource)
  207. id,err := strconv.Atoi(idStr)
  208. if err != nil {
  209. br.Msg = "参数解析异常!"
  210. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  211. return
  212. }
  213. item, e := ob.GetItemById(id)
  214. if e != nil {
  215. if e.Error() == utils.ErrNoRow() {
  216. br.Ret = 200
  217. br.Success = true
  218. br.Msg = "操作成功"
  219. return
  220. }
  221. br.Msg = "操作失败"
  222. br.ErrMsg = "获取资源失败, Err: " + e.Error()
  223. return
  224. }
  225. if e = item.Del(); e != nil {
  226. br.Msg = "操作失败"
  227. br.ErrMsg = "删除资源失败, Err: " + e.Error()
  228. return
  229. }
  230. }
  231. br.Ret = 200
  232. br.Success = true
  233. br.Msg = "操作成功"
  234. }
  235. // Add
  236. // @Title 新增
  237. // @Description 新增
  238. // @Param request body smart_report.SmartReportAddReq true "type json string"
  239. // @Success 200 {object} smart_report.SmartReportItem
  240. // @router /resource/add [post]
  241. func (this *SmartReportResourceController) Add() {
  242. br := new(models.BaseResponse).Init()
  243. defer func() {
  244. if br.ErrMsg == "" {
  245. br.IsSendEmail = false
  246. }
  247. this.Data["json"] = br
  248. this.ServeJSON()
  249. }()
  250. sysUser := this.SysUser
  251. if sysUser == nil {
  252. br.Msg = "请登录"
  253. br.ErrMsg = "请登录,SysUser Is Empty"
  254. br.Ret = 408
  255. return
  256. }
  257. var req smart_report.SmartReportResourceAddReq
  258. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  259. if err != nil {
  260. br.Msg = "参数解析异常!"
  261. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  262. return
  263. }
  264. if req.Type != 1 && req.Type != 2 {
  265. br.Msg = "请选择新增方式"
  266. return
  267. }
  268. req.ImgName = strings.TrimSpace(req.ImgName)
  269. if req.ImgName == "" {
  270. br.Msg = "请输入图片名称"
  271. return
  272. }
  273. item := new(smart_report.SmartReportResource)
  274. item.Type = req.Type
  275. item.ImgName = req.ImgName
  276. item.ImgUrl = req.ImgUrl
  277. item.CreateTime = time.Now().Local()
  278. item.Style = req.Style
  279. if e := item.Create(); e != nil {
  280. br.Msg = "操作失败"
  281. br.ErrMsg = "新增资源失败, Err: " + e.Error()
  282. return
  283. }
  284. br.Ret = 200
  285. br.Success = true
  286. br.Msg = "操作成功"
  287. }