smart_resource.go 7.4 KB

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