chart.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. type CygxChartCollect struct {
  240. Mobile string `description:"手机号"`
  241. UserId int `description:"手机号"`
  242. }
  243. // 获取图表列表
  244. func GetChartCollectList(condition string) (items []*UserInteraction, err error) {
  245. o := orm.NewOrmUsingDB("hz_cygx")
  246. sql := `SELECT * FROM cygx_chart_collect AS r
  247. WHERE 1= 1 ` + condition + ` GROUP BY r.mobile `
  248. _, err = o.Raw(sql).QueryRows(&items)
  249. return
  250. }
  251. // 获取图表列表(关联用户跟销售的绑定关系跨库查询)
  252. func GetChartCollectListWeekly(condition string) (items []*UserInteraction, err error) {
  253. o := orm.NewOrmUsingDB("hz_cygx")
  254. databaseName := utils.GetWeeklyDatabase()
  255. sql := `SELECT r.* FROM cygx_chart_collect AS r
  256. INNER JOIN %s.user_seller_relation AS us ON us.user_id = r.user_id AND us.product_id = 2
  257. WHERE 1= 1 `
  258. sql = fmt.Sprintf(sql, databaseName)
  259. sql += condition + ` GROUP BY r.mobile `
  260. _, err = o.Raw(sql).QueryRows(&items)
  261. return
  262. }
  263. // 获取图表列表
  264. func GetChartCollectBycompanyList(condition string, startSize, pageSize int) (items []*UserInteraction, err error) {
  265. o := orm.NewOrmUsingDB("hz_cygx")
  266. //sql := `SELECT u.mobile,u.user_id,u.real_name,r.company_name,
  267. // cp.seller_name FROM cygx_chart_collect AS r
  268. // INNER JOIN wx_user AS u ON u.user_id = r.user_id
  269. // INNER JOIN cygx_user_interaction_num AS ui ON ui.user_id = r.user_id
  270. // LEFT JOIN company_product AS cp ON cp.company_id = r.company_id AND cp.product_id = 2
  271. // INNER JOIN cygx_company_interaction_num AS ci ON ci.company_id = r.company_id
  272. // WHERE 1= 1 ` + condition + ` GROUP BY r.mobile ORDER BY ci.chart_count_num DESC , ui.chart_count_num DESC `
  273. sql := `SELECT r.*
  274. FROM cygx_chart_collect AS r
  275. WHERE 1= 1 ` + condition + ` GROUP BY r.mobile `
  276. if startSize > 0 || pageSize > 0 {
  277. sql += ` LIMIT ` + strconv.Itoa(startSize) + "," + strconv.Itoa(pageSize)
  278. }
  279. _, err = o.Raw(sql).QueryRows(&items)
  280. return
  281. }
  282. // 获取一共有多少用户收藏图表
  283. func GetChartCountByUserCount(condition string) (count int, err error) {
  284. o := orm.NewOrmUsingDB("hz_cygx")
  285. sql := ` SELECT
  286. COUNT( 1 ) AS count
  287. FROM
  288. (
  289. SELECT
  290. COUNT( 1 )
  291. FROM
  292. cygx_chart_collect AS r
  293. INNER JOIN cygx_user_interaction_num AS ui ON ui.user_id = r.user_id
  294. INNER JOIN cygx_company_interaction_num AS ci ON ci.company_id = r.company_id
  295. WHERE 1= 1 ` + condition + ` GROUP BY r.user_id ) AS count`
  296. err = o.Raw(sql).QueryRow(&count)
  297. return
  298. }
  299. func GetChartCountBySeller(condition string) (list []*company.CompanyReportRecordGroup, err error) {
  300. o := orm.NewOrmUsingDB("hz_cygx")
  301. sql := `SELECT
  302. count(
  303. DISTINCT ( r.company_id )) num,
  304. GROUP_CONCAT( DISTINCT r.company_id SEPARATOR ',' ) AS company_ids
  305. FROM
  306. cygx_chart_collect AS r
  307. INNER JOIN cygx_chart_all AS a ON a.chart_id = r.chart_id
  308. WHERE
  309. 1 = 1 ` + condition
  310. _, err = o.Raw(sql).QueryRows(&list)
  311. return
  312. }