chart.go 11 KB

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