chart.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. }
  89. func GetChartCountById(chartId int) (count int, err error) {
  90. o := orm.NewOrm()
  91. sql := `SELECT COUNT(1) AS count FROM cygx_chart WHERE chart_id = ? `
  92. err = o.Raw(sql, chartId).QueryRow(&count)
  93. return
  94. }
  95. // 新增图表
  96. func AddCygxChart(item *CygxChart) (lastId int64, err error) {
  97. o := orm.NewOrm()
  98. lastId, err = o.Insert(item)
  99. return
  100. }
  101. // 标签分类
  102. type ChartPtagResultApi struct {
  103. Data []ChartPtagResultApidate `json:"data"`
  104. Code int `json:"code"`
  105. Msg string `json:"msg"`
  106. }
  107. type ChartPtagResultApidate struct {
  108. ChartPermissionId int `json:"id"`
  109. ChartPermissionName string `json:"name"`
  110. Ctag []Ctag `json:"ctag"`
  111. }
  112. type ChartPtagResp struct {
  113. ChartPermissionId int `description:"分类ID"`
  114. PermissionName string `description:"分类名称"`
  115. List []*CtagResp `description:"子分类"`
  116. }
  117. type CtagResp struct {
  118. CtagId int `description:"子分类ID"`
  119. Name string `description:"子分类名称"`
  120. }
  121. type ChartUserTokenResult struct {
  122. AccessToken string `json:"access_token"`
  123. }
  124. type ChartUserTokenResultApi struct {
  125. Data ChartUserTokenResult `json:"data"`
  126. Code int `json:"code"`
  127. Msg string `json:"msg"`
  128. }
  129. type HomeChartListResp struct {
  130. ChartId int `description:"图表ID"`
  131. Title string `description:"标题"`
  132. TitleEn string `description:"英文标题 "`
  133. CreateDate string `description:"创建时间"`
  134. PtagName string `description:"父类名称"`
  135. CtagName string `description:"子类名称"`
  136. PtagNameTwo string `description:"父类名称"`
  137. CtagNameTwo string `description:"子类名称"`
  138. CtagNamePc string `description:"Pc端所有的分类名称"`
  139. BodyHtml string `orm:"column(cover)";description:"图片链接"`
  140. HttpUrl string `orm:"column(iframe)";description:"文章链接跳转地址"`
  141. IsNeedJump bool `description:"是否需要跳转链接地址"`
  142. IsTop bool `description:"是否置顶"`
  143. NumTop int `description:"置顶数量"`
  144. Source int `description:"来源 1:文章, 2:图表"`
  145. }
  146. type HomeChartListItem struct {
  147. IsBindingMobile bool `description:"是否绑定过手机号"`
  148. Paging *paging.PagingItem
  149. List []*HomeChartListResp `description:"图表列表"`
  150. }
  151. type ChartCollectReq struct {
  152. ChartId int `description:"图表ID"`
  153. }
  154. // 获取图表列表
  155. func GetChartList(condition string, pars []interface{}, startSize, pageSize int) (items []*HomeChartListResp, err error) {
  156. o := orm.NewOrm()
  157. sql := ` SELECT * FROM cygx_chart AS a WHERE a.publish_status=1 `
  158. if condition != "" {
  159. sql += condition
  160. }
  161. sql += ` ORDER BY chart_id DESC LIMIT ?,? `
  162. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  163. return
  164. }
  165. // 获取所有同步过来的图表
  166. func GetChartListAll() (items []*HomeChartListResp, err error) {
  167. o := orm.NewOrm()
  168. sql := ` SELECT chart_id FROM cygx_chart `
  169. _, err = o.Raw(sql).QueryRows(&items)
  170. return
  171. }
  172. ////获取图表列表
  173. //func GetChartCtagIds(ctagId int ) (items []*HomeChartListResp, err error) {
  174. // o := orm.NewOrm()
  175. // sql := ` SELECT * FROM cygx_chart AS a WHERE a.publish_status=1 AND ctag_id = 1 ORDER BY create_date DESC LIMIT 2 `
  176. // _, err = o.Raw(sql,ctagId).QueryRows(&items)
  177. // return
  178. //}
  179. // 获取图表列表
  180. func GetChartListCollection(chartIds string, userId, startSize, pageSize int) (items []*HomeChartListResp, err error) {
  181. o := orm.NewOrm()
  182. sql := ` SELECT a.*,
  183. t.create_time AS t_create_time,
  184. c.create_time AS c_create_time,
  185. ( SELECT COUNT(*) FROM cygx_chart_top AS t WHERE t.chart_id = a.chart_id AND t.user_id = ? ) AS num_top,
  186. ( SELECT COUNT(*) FROM cygx_chart_collect AS c WHERE c.chart_id = a.chart_id AND c.user_id = ? ) AS num_c
  187. FROM
  188. cygx_chart_all AS a
  189. LEFT JOIN cygx_chart_top AS t ON t.chart_id = a.chart_id
  190. LEFT JOIN cygx_chart_collect AS c ON c.chart_id = a.chart_id
  191. WHERE
  192. 1=1
  193. AND a.chart_id IN (` + chartIds + `)
  194. GROUP BY
  195. a.chart_id
  196. ORDER BY
  197. t_create_time DESC,
  198. c_create_time DESC LIMIT ?,? `
  199. _, err = o.Raw(sql, userId, userId, startSize, pageSize).QueryRows(&items)
  200. return
  201. }
  202. // 获取图表列表
  203. func GetChartListCollectionNew(condition string, pars []interface{}, userId, startSize, pageSize int) (items []*HomeChartListResp, err error) {
  204. o := orm.NewOrm()
  205. sql := ` SELECT a.*,
  206. t.create_time AS t_create_time,
  207. c.create_time AS c_create_time,
  208. ( SELECT COUNT(*) FROM cygx_chart_top AS t WHERE t.chart_id = a.chart_id AND t.user_id = ? ) AS is_top,
  209. ( SELECT COUNT(*) FROM cygx_chart_collect AS c WHERE c.chart_id = a.chart_id AND c.user_id = ? ) AS num_c
  210. FROM
  211. cygx_chart_all AS a
  212. LEFT JOIN cygx_chart_top AS t ON t.chart_id = a.chart_id
  213. LEFT JOIN cygx_chart_collect AS c ON c.chart_id = a.chart_id
  214. WHERE
  215. 1=1
  216. ` + condition + `
  217. GROUP BY
  218. a.chart_id
  219. ORDER BY
  220. t_create_time DESC,
  221. c_create_time DESC LIMIT ?,? `
  222. _, err = o.Raw(sql, userId, userId, pars, startSize, pageSize).QueryRows(&items)
  223. return
  224. }
  225. // 获取图表列表本地
  226. func GetChartListCollectionWithCygx(userId, startSize, pageSize int) (items []*HomeChartListResp, err error) {
  227. o := orm.NewOrm()
  228. sql := ` SELECT
  229. a.*,
  230. t.create_time AS t_create_time,
  231. c.create_time AS c_create_time,
  232. ( SELECT COUNT(*) FROM cygx_chart_top AS t WHERE t.chart_id = a.chart_id AND t.user_id = ? ) AS num_top,
  233. ( SELECT COUNT(*) FROM cygx_chart_collect AS c WHERE c.chart_id = a.chart_id AND c.user_id = ? ) AS num_c
  234. FROM
  235. cygx_chart_collect AS c
  236. INNER JOIN cygx_chart AS a ON c.chart_id = a.chart_id
  237. LEFT JOIN cygx_chart_top AS t ON t.chart_id = a.chart_id
  238. WHERE
  239. 1=1
  240. AND c.user_id =?
  241. GROUP BY
  242. a.chart_id
  243. ORDER BY
  244. t_create_time DESC,
  245. c_create_time DESC LIMIT ?,? `
  246. _, err = o.Raw(sql, userId, userId, userId, startSize, pageSize).QueryRows(&items)
  247. return
  248. }
  249. func GetChartListConfig() (items []*HomeChartListResp, err error) {
  250. o := orm.NewOrm()
  251. 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 `
  252. _, err = o.Raw(sql).QueryRows(&items)
  253. return
  254. }
  255. // 获取图表数量
  256. func GetChartCount(condition string, pars []interface{}) (count int, err error) {
  257. o := orm.NewOrm()
  258. sql := `SELECT COUNT(1) AS count
  259. FROM cygx_chart AS a
  260. WHERE a.publish_status=1 `
  261. if condition != "" {
  262. sql += condition
  263. }
  264. err = o.Raw(sql, pars).QueryRow(&count)
  265. return
  266. }
  267. // 获取图表数量
  268. func GetChartCollentCount(condition string, pars []interface{}) (count int, err error) {
  269. o := orm.NewOrm()
  270. sql := `SELECT COUNT(1) AS count
  271. FROM cygx_chart_all AS a
  272. WHERE 1=1 `
  273. if condition != "" {
  274. sql += condition
  275. }
  276. err = o.Raw(sql, pars).QueryRow(&count)
  277. return
  278. }
  279. func GetChartDetailById(chartId, uid int) (item *CygxChartDetail, err error) {
  280. o := orm.NewOrm()
  281. 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
  282. FROM
  283. cygx_chart_all as a
  284. WHERE
  285. a.chart_id =? `
  286. err = o.Raw(sql, uid, chartId).QueryRow(&item)
  287. return
  288. }
  289. // 删除
  290. func DeleteCygxChart(chartId int) (err error) {
  291. o, err := orm.NewOrm().Begin()
  292. if err != nil {
  293. return
  294. }
  295. defer func() {
  296. if err == nil {
  297. o.Commit()
  298. } else {
  299. o.Rollback()
  300. }
  301. }()
  302. sql := `DELETE FROM cygx_chart WHERE chart_id=? `
  303. _, err = o.Raw(sql, chartId).Exec()
  304. if err != nil {
  305. return err
  306. }
  307. sql = `DELETE FROM cygx_chart_all WHERE chart_id=? `
  308. _, err = o.Raw(sql, chartId).Exec()
  309. return
  310. }