chart.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "hongze/hongze_cygx/models"
  6. "hongze/hongze_cygx/services"
  7. "hongze/hongze_cygx/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type ChartController struct {
  13. BaseAuthController
  14. }
  15. type BaseChartController struct {
  16. BaseCommonController
  17. }
  18. // @Title 图表标签分类
  19. // @Description 图表标签分类接口
  20. // @Success 200 {object} models.ChartPtagResp
  21. // @router /patg [get]
  22. func (this *BaseChartController) Patg() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. list, err := services.GetChartPtagByApi()
  29. if err != nil {
  30. br.Msg = "获取图表分类失败!"
  31. br.ErrMsg = "获取图表分类失败 Err:" + err.Error()
  32. return
  33. }
  34. br.Ret = 200
  35. br.Success = true
  36. br.Msg = "获取成功"
  37. br.Data = list
  38. }
  39. // @Title 我的收藏列表
  40. // @Description 我的收藏列表接口
  41. // @Param PageSize query int true "每页数据条数"
  42. // @Param CurrentIndex query int true "当前页页码,从1开始"
  43. // @Success 200 {object} models.HomeChartListItem
  44. // @router /my/collection [get]
  45. func (this *ChartController) Collection() {
  46. br := new(models.BaseResponse).Init()
  47. defer func() {
  48. this.Data["json"] = br
  49. this.ServeJSON()
  50. }()
  51. user := this.User
  52. if user == nil {
  53. br.Msg = "请重新登录"
  54. br.Ret = 408
  55. return
  56. }
  57. mobile := user.Mobile
  58. uid := user.UserId
  59. pageSize, _ := this.GetInt("PageSize")
  60. currentIndex, _ := this.GetInt("CurrentIndex")
  61. var startSize int
  62. if pageSize <= 0 {
  63. pageSize = utils.PageSize20
  64. }
  65. if currentIndex <= 0 {
  66. currentIndex = 1
  67. }
  68. startSize = paging.StartIndex(currentIndex, pageSize)
  69. resp := new(models.HomeChartListItem)
  70. if mobile == "" {
  71. page := paging.GetPaging(currentIndex, pageSize, 0)
  72. resp.Paging = page
  73. br.Ret = 200
  74. br.Success = true
  75. br.Msg = "获取成功"
  76. br.Data = resp
  77. resp.IsBindingMobile = false
  78. return
  79. } else {
  80. resp.IsBindingMobile = true
  81. }
  82. var listCollection []*models.HomeChartListResp
  83. var total int
  84. //var err error
  85. chartUserTokenByMobile, _ := services.GetUserTokenByMobile(mobile)
  86. if chartUserTokenByMobile != "" {
  87. list, err, _ := services.GetChartCollectionByApi(mobile, 9999, 0)
  88. if err != nil && err.Error() != utils.ErrNoRow() {
  89. br.Msg = "获取我的收藏失败!"
  90. br.ErrMsg = "获取图表分类失败 Err:" + err.Error()
  91. return
  92. }
  93. var chartIds string
  94. var condition string
  95. var pars []interface{}
  96. if len(list) > 0 {
  97. for _, v := range list {
  98. chartIds += strconv.Itoa(v.ChartId) + ","
  99. }
  100. }
  101. chartIds = strings.TrimRight(chartIds, ",")
  102. if chartIds == "" {
  103. chartIds = "0"
  104. }
  105. condition = ` AND a.chart_id IN (` + chartIds + `) `
  106. total, err = models.GetChartCollentCount(condition, pars)
  107. if err != nil {
  108. br.Msg = "获取信息失败"
  109. br.Msg = "获取帖子总数失败,Err:" + err.Error()
  110. return
  111. }
  112. listCollection, err = models.GetChartListCollection(chartIds, uid, startSize, pageSize)
  113. if err != nil {
  114. br.Msg = "获取信息失败"
  115. br.Msg = "获取信息失败,GetChartListCollection Err:" + err.Error()
  116. return
  117. }
  118. } else {
  119. var err error
  120. total, err = models.GetChartCountByUser(uid)
  121. if err != nil {
  122. br.Msg = "获取信息失败"
  123. br.Msg = "获取总数失败,Err:" + err.Error()
  124. return
  125. }
  126. listCollection, err = models.GetChartListCollectionWithCygx(uid, startSize, pageSize)
  127. if err != nil {
  128. br.Msg = "获取信息失败"
  129. br.Msg = "获取信息失败,GetChartListCollection Err:" + err.Error()
  130. return
  131. }
  132. }
  133. for k, v := range listCollection {
  134. if v.NumTop > 0 {
  135. listCollection[k].IsTop = true
  136. }
  137. listCollection[k].Source = 2
  138. if v.PtagName != "" {
  139. listCollection[k].CtagNamePc = v.PtagName
  140. }
  141. if v.CtagName != "" {
  142. listCollection[k].CtagNamePc += "," + v.CtagName
  143. }
  144. if v.PtagNameTwo != "" {
  145. listCollection[k].CtagNamePc += "," + v.PtagNameTwo
  146. }
  147. if v.CtagNameTwo != "" {
  148. listCollection[k].CtagNamePc += "," + v.CtagNameTwo
  149. }
  150. }
  151. page := paging.GetPaging(currentIndex, pageSize, total)
  152. resp.List = listCollection
  153. resp.Paging = page
  154. br.Ret = 200
  155. br.Success = true
  156. br.Msg = "获取成功"
  157. br.Data = resp
  158. }
  159. // @Title 图表详情
  160. // @Description 我的收藏接口
  161. // @Param ChartId query int true "图表ID"
  162. // @Success 200 {object} models.CygxChartDetail
  163. // @router /detail [get]
  164. func (this *ChartController) Detail() {
  165. br := new(models.BaseResponse).Init()
  166. defer func() {
  167. this.Data["json"] = br
  168. this.ServeJSON()
  169. }()
  170. user := this.User
  171. if user == nil {
  172. br.Msg = "请重新登录"
  173. br.Ret = 408
  174. return
  175. }
  176. mobile := user.Mobile
  177. if mobile == "" {
  178. br.Msg = "请绑定手机号!"
  179. return
  180. }
  181. chartId, _ := this.GetInt("ChartId")
  182. if chartId <= 0 {
  183. br.Msg = "图表信息不存在!"
  184. return
  185. }
  186. detail, err := models.GetChartDetailById(chartId, user.UserId)
  187. if err != nil {
  188. br.Msg = "获取信息失败"
  189. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  190. return
  191. }
  192. chartUserTokenByMobile, _ := services.GetUserTokenByMobile(mobile)
  193. if chartUserTokenByMobile != "" {
  194. GetIsCollectionChart, err := services.GetIsCollectionChart(mobile, chartId)
  195. if err != nil {
  196. br.Msg = "获取信息失败"
  197. br.ErrMsg = "获取三方关注信息失败,Err:" + err.Error()
  198. return
  199. }
  200. detail.IsCollection = GetIsCollectionChart
  201. detail.HttpUrl = utils.CHART_INFO_HTTP_URL + strconv.Itoa(chartId) + "?token=" + chartUserTokenByMobile
  202. //if !detail.IsCollection {
  203. // err = models.RemoveChartCollect(user.UserId, chartId)
  204. //}
  205. } else {
  206. if detail.CollectionNum > 0 {
  207. detail.IsCollection = true
  208. }
  209. }
  210. go services.ServerAddCygxChartRecord(user, chartId) //用户阅读图表,添加浏览记录
  211. br.Ret = 200
  212. br.Success = true
  213. br.Msg = "获取成功"
  214. br.Data = detail
  215. }
  216. // @Title 收藏、取消收藏
  217. // @Description 收藏、取消收藏
  218. // @Param request body models.ChartCollectReq true "type json string"
  219. // @Success 200 {object} models.FontsCollectResp
  220. // @router /collect [post]
  221. func (this *ChartController) ChartCollect() {
  222. br := new(models.BaseResponse).Init()
  223. defer func() {
  224. this.Data["json"] = br
  225. this.ServeJSON()
  226. }()
  227. user := this.User
  228. if user == nil {
  229. br.Msg = "请重新登录"
  230. br.Ret = 408
  231. return
  232. }
  233. uid := user.UserId
  234. mobile := user.Mobile
  235. var req models.ChartCollectReq
  236. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  237. if err != nil {
  238. br.Msg = "参数解析异常!"
  239. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  240. return
  241. }
  242. resp := new(models.ArticleCollectResp)
  243. count, err := models.GetChartCountByUserId(uid, req.ChartId)
  244. if err != nil {
  245. br.Msg = "获取数据失败!"
  246. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  247. return
  248. }
  249. chartUserTokenByMobile, _ := services.GetUserTokenByMobile(mobile)
  250. if chartUserTokenByMobile != "" {
  251. GetIsCollectionChart, err := services.GetIsCollectionChart(mobile, req.ChartId)
  252. if err != nil {
  253. br.Msg = "获取信息失败"
  254. br.ErrMsg = "获取三方关注信息失败,Err:" + err.Error()
  255. return
  256. }
  257. if !GetIsCollectionChart {
  258. if count <= 0 {
  259. item := new(models.CygxChartCollect)
  260. item.ChartId = req.ChartId
  261. item.UserId = uid
  262. item.RealName = user.RealName
  263. item.CreateTime = time.Now()
  264. item.Mobile = user.Mobile
  265. item.Email = user.Email
  266. item.CompanyId = user.CompanyId
  267. item.CompanyName = user.CompanyName
  268. _, err = models.AddCygxChartCollect(item)
  269. if err != nil {
  270. br.Msg = "收藏失败"
  271. br.ErrMsg = "收藏失败,Err:" + err.Error()
  272. return
  273. }
  274. }
  275. br.Msg = "收藏成功"
  276. resp.Status = 1
  277. } else {
  278. err = models.RemoveChartCollect(uid, req.ChartId)
  279. if err != nil {
  280. br.Msg = "取消收藏失败"
  281. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  282. return
  283. }
  284. br.Msg = "已取消收藏"
  285. resp.Status = 2
  286. }
  287. //如果存在就取消收藏、反之添加收藏
  288. if GetIsCollectionChart {
  289. go services.DeleteCollectionChart(mobile, req.ChartId)
  290. } else {
  291. go services.AddCollectionChart(mobile, req.ChartId)
  292. }
  293. } else {
  294. if count <= 0 {
  295. item := new(models.CygxChartCollect)
  296. item.ChartId = req.ChartId
  297. item.UserId = uid
  298. item.RealName = user.RealName
  299. item.CreateTime = time.Now()
  300. item.Mobile = user.Mobile
  301. item.Email = user.Email
  302. item.CompanyId = user.CompanyId
  303. item.CompanyName = user.CompanyName
  304. _, err = models.AddCygxChartCollect(item)
  305. if err != nil {
  306. br.Msg = "收藏失败"
  307. br.ErrMsg = "收藏失败,Err:" + err.Error()
  308. return
  309. }
  310. br.Msg = "收藏成功"
  311. resp.Status = 1
  312. } else {
  313. err = models.RemoveChartCollect(uid, req.ChartId)
  314. if err != nil {
  315. br.Msg = "取消收藏失败"
  316. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  317. return
  318. }
  319. br.Msg = "已取消收藏"
  320. resp.Status = 2
  321. }
  322. }
  323. br.Ret = 200
  324. br.Success = true
  325. br.Data = resp
  326. }
  327. // @Title 置顶、取消置顶
  328. // @Description 置顶、取消置顶
  329. // @Param request body models.ChartCollectReq true "type json string"
  330. // @Success 200 {object} models.FontsCollectResp
  331. // @router /top [post]
  332. func (this *ChartController) ChartTop() {
  333. br := new(models.BaseResponse).Init()
  334. defer func() {
  335. this.Data["json"] = br
  336. this.ServeJSON()
  337. }()
  338. user := this.User
  339. if user == nil {
  340. br.Msg = "请重新登录"
  341. br.Ret = 408
  342. return
  343. }
  344. uid := user.UserId
  345. var req models.ChartCollectReq
  346. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  347. if err != nil {
  348. br.Msg = "参数解析异常!"
  349. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  350. return
  351. }
  352. count, err := models.GetChartTopCountByUserId(uid, req.ChartId)
  353. if err != nil {
  354. br.Msg = "获取数据失败!"
  355. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  356. return
  357. }
  358. resp := new(models.ArticleCollectResp)
  359. if count <= 0 {
  360. item := new(models.CygxChartTop)
  361. item.ChartId = req.ChartId
  362. item.UserId = uid
  363. item.RealName = user.RealName
  364. item.CreateTime = time.Now()
  365. item.Mobile = user.Mobile
  366. item.Email = user.Email
  367. item.CompanyId = user.CompanyId
  368. item.CompanyName = user.CompanyName
  369. _, err = models.AddCygxChartTop(item)
  370. if err != nil {
  371. br.Msg = "置顶失败"
  372. br.ErrMsg = "收置顶失败,Err:" + err.Error()
  373. return
  374. }
  375. br.Msg = "置顶成功"
  376. resp.Status = 1
  377. } else {
  378. err = models.RemoveChartTop(uid, req.ChartId)
  379. if err != nil {
  380. br.Msg = "取消置顶失败"
  381. br.ErrMsg = "取消置顶失败,Err:" + err.Error()
  382. return
  383. }
  384. br.Msg = "已取消置顶"
  385. resp.Status = 2
  386. }
  387. br.Ret = 200
  388. br.Success = true
  389. br.Data = resp
  390. }
  391. // @Title 季度列表
  392. // @Description 季度列表接口
  393. // @Param PageSize query int true "每页数据条数"
  394. // @Param CurrentIndex query int true "当前页页码,从1开始"
  395. // @Success 200 {object} models.HomeChartListItem
  396. // @router /jidu [get]
  397. func (this *ChartController) Jidu() {
  398. br := new(models.BaseResponse).Init()
  399. defer func() {
  400. this.Data["json"] = br
  401. this.ServeJSON()
  402. }()
  403. user := this.User
  404. if user == nil {
  405. br.Msg = "请重新登录"
  406. br.Ret = 408
  407. return
  408. }
  409. mobile := user.Mobile
  410. uid := user.UserId
  411. pageSize, _ := this.GetInt("PageSize")
  412. currentIndex, _ := this.GetInt("CurrentIndex")
  413. var startSize int
  414. if pageSize <= 0 {
  415. pageSize = utils.PageSize20
  416. }
  417. if currentIndex <= 0 {
  418. currentIndex = 1
  419. }
  420. startSize = paging.StartIndex(currentIndex, pageSize)
  421. resp := new(models.HomeChartListItem)
  422. if mobile == "" {
  423. page := paging.GetPaging(currentIndex, pageSize, 0)
  424. resp.Paging = page
  425. br.Ret = 200
  426. br.Success = true
  427. br.Msg = "获取成功"
  428. br.Data = resp
  429. resp.IsBindingMobile = false
  430. return
  431. } else {
  432. resp.IsBindingMobile = true
  433. }
  434. var listCollection []*models.HomeChartListResp
  435. var total int
  436. var err error
  437. chartUserTokenByMobile, _ := services.GetUserTokenByMobile(mobile)
  438. chartIds := "5424,5587,5423,5589"
  439. if chartUserTokenByMobile != "" {
  440. var condition string
  441. var pars []interface{}
  442. condition = ` AND a.chart_id IN (` + chartIds + `) `
  443. total, err = models.GetChartCollentCount(condition, pars)
  444. if err != nil {
  445. br.Msg = "获取信息失败"
  446. br.Msg = "获取帖子总数失败,Err:" + err.Error()
  447. return
  448. }
  449. listCollection, err = models.GetChartListCollection(chartIds, uid, startSize, pageSize)
  450. if err != nil {
  451. br.Msg = "获取信息失败"
  452. br.Msg = "获取信息失败,GetChartListCollection Err:" + err.Error()
  453. return
  454. }
  455. } else {
  456. total, err = models.GetChartCountByUser(uid)
  457. if err != nil {
  458. br.Msg = "获取信息失败"
  459. br.Msg = "获取总数失败,Err:" + err.Error()
  460. return
  461. }
  462. listCollection, err = models.GetChartListCollectionWithCygx(uid, startSize, pageSize)
  463. if err != nil {
  464. br.Msg = "获取信息失败"
  465. br.Msg = "获取信息失败,GetChartListCollection Err:" + err.Error()
  466. return
  467. }
  468. }
  469. for k, v := range listCollection {
  470. if v.NumTop > 0 {
  471. listCollection[k].IsTop = true
  472. }
  473. listCollection[k].Source = 2
  474. if v.PtagName != "" {
  475. listCollection[k].CtagNamePc = v.PtagName
  476. }
  477. if v.CtagName != "" {
  478. listCollection[k].CtagNamePc += "," + v.CtagName
  479. }
  480. if v.PtagNameTwo != "" {
  481. listCollection[k].CtagNamePc += "," + v.PtagNameTwo
  482. }
  483. if v.CtagNameTwo != "" {
  484. listCollection[k].CtagNamePc += "," + v.CtagNameTwo
  485. }
  486. }
  487. page := paging.GetPaging(currentIndex, pageSize, total)
  488. resp.List = listCollection
  489. resp.Paging = page
  490. br.Ret = 200
  491. br.Success = true
  492. br.Msg = "获取成功"
  493. br.Data = resp
  494. }