chart.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "time"
  6. )
  7. type ChartResultApi struct {
  8. Data []ChartResultApidate `json:"data"`
  9. Code int `json:"code"`
  10. Msg string `json:"msg"`
  11. }
  12. type ChartResultDetailApi struct {
  13. Data ChartResultApidate `json:"data"`
  14. Code int `json:"code"`
  15. Msg string `json:"msg"`
  16. }
  17. type ChartFavoritesResultApi struct {
  18. Data []ChartInfo `json:"data"`
  19. Code int `json:"code"`
  20. Msg string `json:"msg"`
  21. Pagination Pagination `json:"pagination"`
  22. }
  23. type Pagination struct {
  24. Total int `json:"total"`
  25. Page int `json:"id"`
  26. PageSize int `json:"page_size"`
  27. PageTotal int `json:"page_total"`
  28. }
  29. type ChartInfo struct {
  30. ChartInfo *ChartResultApidate `json:"chart_info"`
  31. ChartId int `json:"chart_id"`
  32. CreateDate string `json:"add_favorites_time"`
  33. }
  34. type ChartResultApidate struct {
  35. ChartId int `json:"id"`
  36. PtagId int `json:"ptag_id"`
  37. CtagId int `json:"ctag_id"`
  38. Title string `json:"title"`
  39. TitleEn string `json:"title_en"`
  40. CreateDate string `json:"create_date"`
  41. UpdateDate string `json:"update_date"`
  42. PublishStatus int `json:"publish_status"`
  43. Cover string `json:"cover"`
  44. Iframe string `json:"iframe"`
  45. Ptag Ptag `json:"ptag"`
  46. Ctag Ptag `json:"ctag"`
  47. PtagTwo Ptag `json:"ptag1"`
  48. CtagTwo Ptag `json:"ctag1"`
  49. IsActive bool `json:"is_active"`
  50. }
  51. type Ptag struct {
  52. Id int `json:"id"`
  53. Name string `json:"name"`
  54. Description string `json:"description"`
  55. }
  56. type Ctag struct {
  57. Id int `json:"id"`
  58. Name string `json:"name"`
  59. Description string `json:"description"`
  60. PtagId int `json:"ptag_id"`
  61. }
  62. type CygxChart struct {
  63. Id int `orm:"column(id);pk"`
  64. ChartId int `description:"图表id"`
  65. PtagId int `description:"图表父类分类id"`
  66. CtagId int `description:"图表子类分类id"`
  67. PtagIdTwo int `description:"图表父类分类id"`
  68. CtagIdTwo int `description:"图表子类分类id"`
  69. Title string `description:"标题"`
  70. TitleEn string `description:"英文标题 "`
  71. CreateDate string `description:"本地创建时间"`
  72. CreateDateApi time.Time `description:"图表创建时间"`
  73. PublishStatus int `description:"发布状态"`
  74. PtagName string `description:"父类名称"`
  75. CtagName string `description:"子类名称"`
  76. Cover string `description:"图表图片"`
  77. Iframe string `description:"图表详情跳转地址"`
  78. PtagNameTwo string `description:"父类名称"`
  79. CtagNameTwo string `description:"子类名称"`
  80. }
  81. type CygxChartDetail struct {
  82. ChartId int `description:"图表id"`
  83. Title string `description:"标题"`
  84. TitleEn string `description:"英文标题 "`
  85. IsCollection bool `description:"是否收藏 "`
  86. CollectionNum int `description:"本人收藏数量 "`
  87. HttpUrl string `orm:"column(iframe)";description:"文章链接跳转地址"`
  88. CreateDate string `description:"本地创建时间"`
  89. }
  90. func GetChartCountById(chartId int) (count int, err error) {
  91. o := orm.NewOrm()
  92. sql := `SELECT COUNT(1) AS count FROM cygx_chart WHERE chart_id = ? `
  93. err = o.Raw(sql, chartId).QueryRow(&count)
  94. return
  95. }
  96. // 新增图表
  97. func AddCygxChart(item *CygxChart) (lastId int64, err error) {
  98. o := orm.NewOrm()
  99. lastId, err = o.Insert(item)
  100. return
  101. }
  102. // 标签分类
  103. type ChartPtagResultApi struct {
  104. Data []ChartPtagResultApidate `json:"data"`
  105. Code int `json:"code"`
  106. Msg string `json:"msg"`
  107. }
  108. type ChartPtagResultApidate struct {
  109. ChartPermissionId int `json:"id"`
  110. ChartPermissionName string `json:"name"`
  111. Ctag []Ctag `json:"ctag"`
  112. }
  113. type ChartPtagResp struct {
  114. ChartPermissionId int `description:"分类ID"`
  115. PermissionName string `description:"分类名称"`
  116. List []*CtagResp `description:"子分类"`
  117. }
  118. type CtagResp struct {
  119. CtagId int `description:"子分类ID"`
  120. Name string `description:"子分类名称"`
  121. }
  122. type ChartUserTokenResult struct {
  123. AccessToken string `json:"access_token"`
  124. }
  125. type ChartUserTokenResultApi struct {
  126. Data ChartUserTokenResult `json:"data"`
  127. Code int `json:"code"`
  128. Msg string `json:"msg"`
  129. }
  130. type HomeChartListResp struct {
  131. ChartId int `description:"图表ID"`
  132. Title string `description:"标题"`
  133. TitleEn string `description:"英文标题 "`
  134. CreateDate string `description:"创建时间"`
  135. PtagName string `description:"父类名称"`
  136. CtagName string `description:"子类名称"`
  137. PtagNameTwo string `description:"父类名称"`
  138. CtagNameTwo string `description:"子类名称"`
  139. CtagNamePc string `description:"Pc端所有的分类名称"`
  140. BodyHtml string `orm:"column(cover)";description:"图片链接"`
  141. HttpUrl string `orm:"column(iframe)";description:"文章链接跳转地址"`
  142. IsNeedJump bool `description:"是否需要跳转链接地址"`
  143. IsTop bool `description:"是否置顶"`
  144. NumTop int `description:"置顶数量"`
  145. Source int `description:"来源 1:文章, 2:图表"`
  146. }
  147. type HomeChartListItem struct {
  148. IsBindingMobile bool `description:"是否绑定过手机号"`
  149. Paging *paging.PagingItem
  150. List []*HomeChartListResp `description:"图表列表"`
  151. }
  152. type ChartCollectReq struct {
  153. ChartId int `description:"图表ID"`
  154. }
  155. // 获取图表列表
  156. func GetChartList(condition string, pars []interface{}, startSize, pageSize int) (items []*HomeChartListResp, err error) {
  157. o := orm.NewOrm()
  158. sql := ` SELECT * FROM cygx_chart AS a WHERE a.publish_status=1 `
  159. if condition != "" {
  160. sql += condition
  161. }
  162. sql += ` ORDER BY chart_id DESC LIMIT ?,? `
  163. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  164. return
  165. }
  166. // 获取所有同步过来的图表
  167. func GetChartListAll() (items []*HomeChartListResp, err error) {
  168. o := orm.NewOrm()
  169. sql := ` SELECT chart_id FROM cygx_chart `
  170. _, err = o.Raw(sql).QueryRows(&items)
  171. return
  172. }
  173. ////获取图表列表
  174. //func GetChartCtagIds(ctagId int ) (items []*HomeChartListResp, err error) {
  175. // o := orm.NewOrm()
  176. // sql := ` SELECT * FROM cygx_chart AS a WHERE a.publish_status=1 AND ctag_id = 1 ORDER BY create_date DESC LIMIT 2 `
  177. // _, err = o.Raw(sql,ctagId).QueryRows(&items)
  178. // return
  179. //}
  180. // 获取图表列表
  181. func GetChartListCollection(chartIds string, userId, startSize, pageSize int) (items []*HomeChartListResp, err error) {
  182. o := orm.NewOrm()
  183. sql := ` SELECT a.*,
  184. t.create_time AS t_create_time,
  185. c.create_time AS c_create_time,
  186. ( SELECT COUNT(*) FROM cygx_chart_top AS t WHERE t.chart_id = a.chart_id AND t.user_id = ? ) AS num_top,
  187. ( SELECT COUNT(*) FROM cygx_chart_collect AS c WHERE c.chart_id = a.chart_id AND c.user_id = ? ) AS num_c
  188. FROM
  189. cygx_chart_all AS a
  190. LEFT JOIN cygx_chart_top AS t ON t.chart_id = a.chart_id
  191. LEFT JOIN cygx_chart_collect AS c ON c.chart_id = a.chart_id
  192. WHERE
  193. 1=1
  194. AND a.chart_id IN (` + chartIds + `)
  195. GROUP BY
  196. a.chart_id
  197. ORDER BY
  198. t_create_time DESC,
  199. c_create_time DESC LIMIT ?,? `
  200. _, err = o.Raw(sql, userId, userId, startSize, pageSize).QueryRows(&items)
  201. return
  202. }
  203. // 获取图表列表
  204. func GetChartListCollectionNew(condition string, pars []interface{}, userId, startSize, pageSize int) (items []*HomeChartListResp, err error) {
  205. o := orm.NewOrm()
  206. sql := ` SELECT a.*,
  207. t.create_time AS t_create_time,
  208. c.create_time AS c_create_time,
  209. ( SELECT COUNT(*) FROM cygx_chart_top AS t WHERE t.chart_id = a.chart_id AND t.user_id = ? ) AS is_top,
  210. ( SELECT COUNT(*) FROM cygx_chart_collect AS c WHERE c.chart_id = a.chart_id AND c.user_id = ? ) AS num_c
  211. FROM
  212. cygx_chart AS a
  213. LEFT JOIN cygx_chart_top AS t ON t.chart_id = a.chart_id
  214. LEFT JOIN cygx_chart_collect AS c ON c.chart_id = a.chart_id
  215. WHERE
  216. 1=1
  217. ` + condition + `
  218. GROUP BY
  219. a.chart_id
  220. ORDER BY
  221. t_create_time DESC,
  222. c_create_time DESC LIMIT ?,? `
  223. _, err = o.Raw(sql, userId, userId, pars, startSize, pageSize).QueryRows(&items)
  224. return
  225. }
  226. // 获取图表列表本地
  227. func GetChartListCollectionWithCygx(userId, startSize, pageSize int) (items []*HomeChartListResp, err error) {
  228. o := orm.NewOrm()
  229. sql := ` SELECT
  230. a.*,
  231. t.create_time AS t_create_time,
  232. c.create_time AS c_create_time,
  233. ( SELECT COUNT(*) FROM cygx_chart_top AS t WHERE t.chart_id = a.chart_id AND t.user_id = ? ) AS num_top,
  234. ( SELECT COUNT(*) FROM cygx_chart_collect AS c WHERE c.chart_id = a.chart_id AND c.user_id = ? ) AS num_c
  235. FROM
  236. cygx_chart_collect AS c
  237. INNER JOIN cygx_chart AS a ON c.chart_id = a.chart_id
  238. LEFT JOIN cygx_chart_top AS t ON t.chart_id = a.chart_id
  239. WHERE
  240. 1=1
  241. AND c.user_id =?
  242. GROUP BY
  243. a.chart_id
  244. ORDER BY
  245. t_create_time DESC,
  246. c_create_time DESC LIMIT ?,? `
  247. _, err = o.Raw(sql, userId, userId, userId, startSize, pageSize).QueryRows(&items)
  248. return
  249. }
  250. func GetChartListConfig() (items []*HomeChartListResp, err error) {
  251. o := orm.NewOrm()
  252. sql := ` SELECT * FROM cygx_chart AS a WHERE a.publish_status=1 AND ctag_id IN (7,8,9,10) GROUP BY ctag_id ORDER BY create_date DESC LIMIT 4 `
  253. _, err = o.Raw(sql).QueryRows(&items)
  254. return
  255. }
  256. // 获取图表数量
  257. func GetChartCount(condition string, pars []interface{}) (count int, err error) {
  258. o := orm.NewOrm()
  259. sql := `SELECT COUNT(1) AS count
  260. FROM cygx_chart AS a
  261. WHERE a.publish_status=1 `
  262. if condition != "" {
  263. sql += condition
  264. }
  265. err = o.Raw(sql, pars).QueryRow(&count)
  266. return
  267. }
  268. // 获取图表数量
  269. func GetChartCollentCount(condition string, pars []interface{}) (count int, err error) {
  270. o := orm.NewOrm()
  271. sql := `SELECT COUNT(1) AS count
  272. FROM cygx_chart_all AS a
  273. WHERE 1=1 `
  274. if condition != "" {
  275. sql += condition
  276. }
  277. err = o.Raw(sql, pars).QueryRow(&count)
  278. return
  279. }
  280. func GetChartDetailById(chartId, uid int) (item *CygxChartDetail, err error) {
  281. o := orm.NewOrm()
  282. sql := `SELECT * ,( SELECT COUNT( 1 ) FROM cygx_chart_collect AS c WHERE c.chart_id = a.chart_id AND c.user_id = ? ) AS collection_num
  283. FROM
  284. cygx_chart_all as a
  285. WHERE
  286. a.chart_id =? `
  287. err = o.Raw(sql, uid, chartId).QueryRow(&item)
  288. return
  289. }
  290. func GetChartDetailByChartId(chartId int) (item *CygxChartDetail, err error) {
  291. o := orm.NewOrm()
  292. sql := `SELECT *
  293. FROM
  294. cygx_chart as a
  295. WHERE
  296. a.chart_id =? `
  297. err = o.Raw(sql, chartId).QueryRow(&item)
  298. return
  299. }
  300. // 删除
  301. func DeleteCygxChart(chartId int) (err error) {
  302. o, err := orm.NewOrm().Begin()
  303. if err != nil {
  304. return
  305. }
  306. defer func() {
  307. if err == nil {
  308. o.Commit()
  309. } else {
  310. o.Rollback()
  311. }
  312. }()
  313. sql := `DELETE FROM cygx_chart WHERE chart_id=? `
  314. _, err = o.Raw(sql, chartId).Exec()
  315. if err != nil {
  316. return err
  317. }
  318. sql = `DELETE FROM cygx_chart_all WHERE chart_id=? `
  319. _, err = o.Raw(sql, chartId).Exec()
  320. return
  321. }