chart.go 12 KB

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