chart.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. br.Ret = 200
  211. br.Success = true
  212. br.Msg = "获取成功"
  213. br.Data = detail
  214. }
  215. // @Title 收藏、取消收藏
  216. // @Description 收藏、取消收藏
  217. // @Param request body models.ChartCollectReq true "type json string"
  218. // @Success 200 {object} models.FontsCollectResp
  219. // @router /collect [post]
  220. func (this *ChartController) ChartCollect() {
  221. br := new(models.BaseResponse).Init()
  222. defer func() {
  223. this.Data["json"] = br
  224. this.ServeJSON()
  225. }()
  226. user := this.User
  227. if user == nil {
  228. br.Msg = "请重新登录"
  229. br.Ret = 408
  230. return
  231. }
  232. uid := user.UserId
  233. mobile := user.Mobile
  234. var req models.ChartCollectReq
  235. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  236. if err != nil {
  237. br.Msg = "参数解析异常!"
  238. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  239. return
  240. }
  241. resp := new(models.ArticleCollectResp)
  242. count, err := models.GetChartCountByUserId(uid, req.ChartId)
  243. if err != nil {
  244. br.Msg = "获取数据失败!"
  245. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  246. return
  247. }
  248. chartUserTokenByMobile, _ := services.GetUserTokenByMobile(mobile)
  249. if chartUserTokenByMobile != "" {
  250. GetIsCollectionChart, err := services.GetIsCollectionChart(mobile, req.ChartId)
  251. if err != nil {
  252. br.Msg = "获取信息失败"
  253. br.ErrMsg = "获取三方关注信息失败,Err:" + err.Error()
  254. return
  255. }
  256. if !GetIsCollectionChart {
  257. if count <= 0 {
  258. item := new(models.CygxChartCollect)
  259. item.ChartId = req.ChartId
  260. item.UserId = uid
  261. item.RealName = user.RealName
  262. item.CreateTime = time.Now()
  263. item.Mobile = user.Mobile
  264. item.Email = user.Email
  265. item.CompanyId = user.CompanyId
  266. item.CompanyName = user.CompanyName
  267. _, err = models.AddCygxChartCollect(item)
  268. if err != nil {
  269. br.Msg = "收藏失败"
  270. br.ErrMsg = "收藏失败,Err:" + err.Error()
  271. return
  272. }
  273. }
  274. br.Msg = "收藏成功"
  275. resp.Status = 1
  276. } else {
  277. err = models.RemoveChartCollect(uid, req.ChartId)
  278. if err != nil {
  279. br.Msg = "取消收藏失败"
  280. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  281. return
  282. }
  283. br.Msg = "已取消收藏"
  284. resp.Status = 2
  285. }
  286. //如果存在就取消收藏、反之添加收藏
  287. if GetIsCollectionChart {
  288. go services.DeleteCollectionChart(mobile, req.ChartId)
  289. } else {
  290. go services.AddCollectionChart(mobile, req.ChartId)
  291. }
  292. } else {
  293. if count <= 0 {
  294. item := new(models.CygxChartCollect)
  295. item.ChartId = req.ChartId
  296. item.UserId = uid
  297. item.RealName = user.RealName
  298. item.CreateTime = time.Now()
  299. item.Mobile = user.Mobile
  300. item.Email = user.Email
  301. item.CompanyId = user.CompanyId
  302. item.CompanyName = user.CompanyName
  303. _, err = models.AddCygxChartCollect(item)
  304. if err != nil {
  305. br.Msg = "收藏失败"
  306. br.ErrMsg = "收藏失败,Err:" + err.Error()
  307. return
  308. }
  309. br.Msg = "收藏成功"
  310. resp.Status = 1
  311. } else {
  312. err = models.RemoveChartCollect(uid, req.ChartId)
  313. if err != nil {
  314. br.Msg = "取消收藏失败"
  315. br.ErrMsg = "取消收藏失败,Err:" + err.Error()
  316. return
  317. }
  318. br.Msg = "已取消收藏"
  319. resp.Status = 2
  320. }
  321. }
  322. br.Ret = 200
  323. br.Success = true
  324. br.Data = resp
  325. }
  326. // @Title 置顶、取消置顶
  327. // @Description 置顶、取消置顶
  328. // @Param request body models.ChartCollectReq true "type json string"
  329. // @Success 200 {object} models.FontsCollectResp
  330. // @router /top [post]
  331. func (this *ChartController) ChartTop() {
  332. br := new(models.BaseResponse).Init()
  333. defer func() {
  334. this.Data["json"] = br
  335. this.ServeJSON()
  336. }()
  337. user := this.User
  338. if user == nil {
  339. br.Msg = "请重新登录"
  340. br.Ret = 408
  341. return
  342. }
  343. uid := user.UserId
  344. var req models.ChartCollectReq
  345. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  346. if err != nil {
  347. br.Msg = "参数解析异常!"
  348. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  349. return
  350. }
  351. count, err := models.GetChartTopCountByUserId(uid, req.ChartId)
  352. if err != nil {
  353. br.Msg = "获取数据失败!"
  354. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  355. return
  356. }
  357. resp := new(models.ArticleCollectResp)
  358. if count <= 0 {
  359. item := new(models.CygxChartTop)
  360. item.ChartId = req.ChartId
  361. item.UserId = uid
  362. item.RealName = user.RealName
  363. item.CreateTime = time.Now()
  364. item.Mobile = user.Mobile
  365. item.Email = user.Email
  366. item.CompanyId = user.CompanyId
  367. item.CompanyName = user.CompanyName
  368. _, err = models.AddCygxChartTop(item)
  369. if err != nil {
  370. br.Msg = "置顶失败"
  371. br.ErrMsg = "收置顶失败,Err:" + err.Error()
  372. return
  373. }
  374. br.Msg = "置顶成功"
  375. resp.Status = 1
  376. } else {
  377. err = models.RemoveChartTop(uid, req.ChartId)
  378. if err != nil {
  379. br.Msg = "取消置顶失败"
  380. br.ErrMsg = "取消置顶失败,Err:" + err.Error()
  381. return
  382. }
  383. br.Msg = "已取消置顶"
  384. resp.Status = 2
  385. }
  386. br.Ret = 200
  387. br.Success = true
  388. br.Data = resp
  389. }
  390. // @Title 季度列表
  391. // @Description 季度列表接口
  392. // @Param PageSize query int true "每页数据条数"
  393. // @Param CurrentIndex query int true "当前页页码,从1开始"
  394. // @Success 200 {object} models.HomeChartListItem
  395. // @router /jidu [get]
  396. func (this *ChartController) Jidu() {
  397. br := new(models.BaseResponse).Init()
  398. defer func() {
  399. this.Data["json"] = br
  400. this.ServeJSON()
  401. }()
  402. user := this.User
  403. if user == nil {
  404. br.Msg = "请重新登录"
  405. br.Ret = 408
  406. return
  407. }
  408. mobile := user.Mobile
  409. uid := user.UserId
  410. pageSize, _ := this.GetInt("PageSize")
  411. currentIndex, _ := this.GetInt("CurrentIndex")
  412. var startSize int
  413. if pageSize <= 0 {
  414. pageSize = utils.PageSize20
  415. }
  416. if currentIndex <= 0 {
  417. currentIndex = 1
  418. }
  419. startSize = paging.StartIndex(currentIndex, pageSize)
  420. resp := new(models.HomeChartListItem)
  421. if mobile == "" {
  422. page := paging.GetPaging(currentIndex, pageSize, 0)
  423. resp.Paging = page
  424. br.Ret = 200
  425. br.Success = true
  426. br.Msg = "获取成功"
  427. br.Data = resp
  428. resp.IsBindingMobile = false
  429. return
  430. } else {
  431. resp.IsBindingMobile = true
  432. }
  433. var listCollection []*models.HomeChartListResp
  434. var total int
  435. var err error
  436. chartUserTokenByMobile, _ := services.GetUserTokenByMobile(mobile)
  437. chartIds := "5424,5587,5423,5589"
  438. if chartUserTokenByMobile != "" {
  439. var condition string
  440. var pars []interface{}
  441. condition = ` AND a.chart_id IN (` + chartIds + `) `
  442. total, err = models.GetChartCollentCount(condition, pars)
  443. if err != nil {
  444. br.Msg = "获取信息失败"
  445. br.Msg = "获取帖子总数失败,Err:" + err.Error()
  446. return
  447. }
  448. listCollection, err = models.GetChartListCollection(chartIds, uid, startSize, pageSize)
  449. if err != nil {
  450. br.Msg = "获取信息失败"
  451. br.Msg = "获取信息失败,GetChartListCollection Err:" + err.Error()
  452. return
  453. }
  454. } else {
  455. total, err = models.GetChartCountByUser(uid)
  456. if err != nil {
  457. br.Msg = "获取信息失败"
  458. br.Msg = "获取总数失败,Err:" + err.Error()
  459. return
  460. }
  461. listCollection, err = models.GetChartListCollectionWithCygx(uid, startSize, pageSize)
  462. if err != nil {
  463. br.Msg = "获取信息失败"
  464. br.Msg = "获取信息失败,GetChartListCollection Err:" + err.Error()
  465. return
  466. }
  467. }
  468. for k, v := range listCollection {
  469. if v.NumTop > 0 {
  470. listCollection[k].IsTop = true
  471. }
  472. listCollection[k].Source = 2
  473. if v.PtagName != "" {
  474. listCollection[k].CtagNamePc = v.PtagName
  475. }
  476. if v.CtagName != "" {
  477. listCollection[k].CtagNamePc += "," + v.CtagName
  478. }
  479. if v.PtagNameTwo != "" {
  480. listCollection[k].CtagNamePc += "," + v.PtagNameTwo
  481. }
  482. if v.CtagNameTwo != "" {
  483. listCollection[k].CtagNamePc += "," + v.CtagNameTwo
  484. }
  485. }
  486. page := paging.GetPaging(currentIndex, pageSize, total)
  487. resp.List = listCollection
  488. resp.Paging = page
  489. br.Ret = 200
  490. br.Success = true
  491. br.Msg = "获取成功"
  492. br.Data = resp
  493. }