smart_resource.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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",
  98. }
  99. if params.Type > 0 {
  100. fields = []string{
  101. "resource_id", "create_time", "img_name", "img_url", "type",
  102. }
  103. }
  104. list, e := reportOB.GetPageItemsByCondition(condition, pars, fields, startSize, params.PageSize)
  105. if e != nil {
  106. br.Msg = "获取失败"
  107. br.ErrMsg = "获取资源库分页列表失败, Err:" + e.Error()
  108. return
  109. }
  110. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  111. resp.Paging = page
  112. resp.List = list
  113. br.Ret = 200
  114. br.Success = true
  115. br.Msg = "获取成功"
  116. br.Data = resp
  117. }
  118. // Rename
  119. // @Title 重命名
  120. // @Description 重命名
  121. // @Param request body smart_report.SmartReportPublishReq true "type json string"
  122. // @Success 200 string "操作成功"
  123. // @router /resource/rename [post]
  124. func (this *SmartReportResourceController) Rename() {
  125. br := new(models.BaseResponse).Init()
  126. defer func() {
  127. if br.ErrMsg == "" {
  128. br.IsSendEmail = false
  129. }
  130. this.Data["json"] = br
  131. this.ServeJSON()
  132. }()
  133. sysUser := this.SysUser
  134. if sysUser == nil {
  135. br.Msg = "请登录"
  136. br.ErrMsg = "请登录,SysUser Is Empty"
  137. br.Ret = 408
  138. return
  139. }
  140. var req smart_report.SmartReportRenameReq
  141. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  142. if err != nil {
  143. br.Msg = "参数解析异常!"
  144. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  145. return
  146. }
  147. if req.ResourceId <= 0 {
  148. br.Msg = "参数有误"
  149. br.ErrMsg = "报告ID为空"
  150. return
  151. }
  152. ob := new(smart_report.SmartReportResource)
  153. item, e := ob.GetItemById(req.ResourceId)
  154. if e != nil {
  155. if e.Error() == utils.ErrNoRow() {
  156. br.Msg = "资源不存在, 请刷新页面"
  157. return
  158. }
  159. br.Msg = "操作失败"
  160. br.ErrMsg = "获取资源图片失败, Err: " + e.Error()
  161. return
  162. }
  163. cols := []string{"ImgName"}
  164. item.ImgName = req.ImgName
  165. if e = item.Update(cols); e != nil {
  166. br.Msg = "操作失败"
  167. br.ErrMsg = "更新资源失败, Err: " + e.Error()
  168. return
  169. }
  170. br.Ret = 200
  171. br.Success = true
  172. br.Msg = "操作成功"
  173. }
  174. // Remove
  175. // @Title 删除
  176. // @Description 删除
  177. // @Param request body smart_report.SmartReportRemoveReq true "type json string"
  178. // @Success 200 string "操作成功"
  179. // @router /resource/remove [post]
  180. func (this *SmartReportResourceController) Remove() {
  181. br := new(models.BaseResponse).Init()
  182. defer func() {
  183. if br.ErrMsg == "" {
  184. br.IsSendEmail = false
  185. }
  186. this.Data["json"] = br
  187. this.ServeJSON()
  188. }()
  189. sysUser := this.SysUser
  190. if sysUser == nil {
  191. br.Msg = "请登录"
  192. br.ErrMsg = "请登录,SysUser Is Empty"
  193. br.Ret = 408
  194. return
  195. }
  196. var req smart_report.SmartReportResourceRemoveReq
  197. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  198. if err != nil {
  199. br.Msg = "参数解析异常!"
  200. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  201. return
  202. }
  203. if req.ResourceIds == "" {
  204. br.Msg = "参数有误"
  205. br.ErrMsg = "图片ID为空"
  206. return
  207. }
  208. ids := strings.Split(req.ResourceIds, ",")
  209. for _, idStr := range ids {
  210. ob := new(smart_report.SmartReportResource)
  211. id,err := strconv.Atoi(idStr)
  212. if err != nil {
  213. br.Msg = "参数解析异常!"
  214. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  215. return
  216. }
  217. item, e := ob.GetItemById(id)
  218. if e != nil {
  219. if e.Error() == utils.ErrNoRow() {
  220. br.Ret = 200
  221. br.Success = true
  222. br.Msg = "操作成功"
  223. return
  224. }
  225. br.Msg = "操作失败"
  226. br.ErrMsg = "获取资源失败, Err: " + e.Error()
  227. return
  228. }
  229. if e = item.Del(); e != nil {
  230. br.Msg = "操作失败"
  231. br.ErrMsg = "删除资源失败, Err: " + e.Error()
  232. return
  233. }
  234. }
  235. br.Ret = 200
  236. br.Success = true
  237. br.Msg = "操作成功"
  238. }
  239. // Add
  240. // @Title 新增
  241. // @Description 新增
  242. // @Param request body smart_report.SmartReportAddReq true "type json string"
  243. // @Success 200 {object} smart_report.SmartReportItem
  244. // @router /resource/add [post]
  245. func (this *SmartReportResourceController) Add() {
  246. br := new(models.BaseResponse).Init()
  247. defer func() {
  248. if br.ErrMsg == "" {
  249. br.IsSendEmail = false
  250. }
  251. this.Data["json"] = br
  252. this.ServeJSON()
  253. }()
  254. sysUser := this.SysUser
  255. if sysUser == nil {
  256. br.Msg = "请登录"
  257. br.ErrMsg = "请登录,SysUser Is Empty"
  258. br.Ret = 408
  259. return
  260. }
  261. var req smart_report.SmartReportResourceAddReq
  262. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  263. if err != nil {
  264. br.Msg = "参数解析异常!"
  265. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  266. return
  267. }
  268. if req.Type != 1 && req.Type != 2 {
  269. br.Msg = "请选择新增方式"
  270. return
  271. }
  272. req.ImgName = strings.TrimSpace(req.ImgName)
  273. if req.ImgName == "" {
  274. br.Msg = "请输入图片名称"
  275. return
  276. }
  277. item := new(smart_report.SmartReportResource)
  278. item.Type = req.Type
  279. item.ImgName = req.ImgName
  280. item.ImgUrl = req.ImgUrl
  281. item.CreateTime = time.Now().Local()
  282. if e := item.Create(); e != nil {
  283. br.Msg = "操作失败"
  284. br.ErrMsg = "新增资源失败, Err: " + e.Error()
  285. return
  286. }
  287. br.Ret = 200
  288. br.Success = true
  289. br.Msg = "操作成功"
  290. }