price_driven.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package yb
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. // PriceDriven 价格驱动表
  7. type PriceDriven struct {
  8. PriceDrivenId int `orm:"column(price_driven_id);pk" description:"价格驱动ID"`
  9. //ChartPermissionId int `json:"chart_permission_id" description:"品种权限ID"`
  10. VarietyTagId int `json:"variety_tag_id" description:"标签ID"`
  11. VarietyTagName string `json:"variety_tag_name" description:"标签名称"`
  12. MainVariable string `json:"main_variable" description:"关键变量"`
  13. CoreDrivenType int `json:"core_driven_type" description:"核心驱动类型 0-多 1-空"`
  14. CoreDrivenContent string `json:"core_driven_content" description:"核心驱动内容"`
  15. CoreContent string `json:"core_content" description:"核心内容"`
  16. LastUpdateAdminId int `json:"last_update_admin_id" description:"最后更新人ID"`
  17. LastUpdateAdminName string `json:"last_update_admin_name" description:"最后更新人名称"`
  18. ThsMsgState int `json:"ths_msg_state" description:"同花顺推送状态:0-未推送 1-已推送"`
  19. TemplateMsgState int `json:"template_msg_state" description:"模板消息推送状态:0-未推送 1-已推送"`
  20. PublishState int `json:"publish_state" description:"发布状态:0-未发布 1-已发布"`
  21. SendThsMsgTime time.Time `json:"last_ths_msg_time" description:"最后推送同花顺客群消息时间"`
  22. SendTemplateMsgTime time.Time `json:"last_template_msg_time" description:"最后推送模板消息时间"`
  23. CreateTime time.Time `json:"create_time" description:"创建时间"`
  24. ModifyTime time.Time `json:"modify_time" description:"更新时间"`
  25. }
  26. // TableName 表名变更
  27. func (priceDrivenInfo *PriceDriven) TableName() string {
  28. return "yb_price_driven"
  29. }
  30. // Add 新增
  31. func (priceDrivenInfo *PriceDriven) Add() (err error) {
  32. o := orm.NewOrm()
  33. id, err := o.Insert(priceDrivenInfo)
  34. if err != nil {
  35. return
  36. }
  37. priceDrivenInfo.PriceDrivenId = int(id)
  38. return
  39. }
  40. // Update 更新
  41. func (priceDrivenInfo *PriceDriven) Update(cols []string) (err error) {
  42. o := orm.NewOrm()
  43. _, err = o.Update(priceDrivenInfo, cols...)
  44. return
  45. }
  46. // GetPriceDrivenByChartPermissionId 通过品种获取价格驱动
  47. //func GetPriceDrivenByChartPermissionId(chartPermissionId int) (item *PriceDriven, err error) {
  48. // o := orm.NewOrm()
  49. // sql := `SELECT * FROM yb_price_driven WHERE chart_permission_id = ? ORDER BY create_time DESC LIMIT 1`
  50. // err = o.Raw(sql, chartPermissionId).QueryRow(&item)
  51. // return
  52. //}
  53. // GetPriceDrivenByVarietyTagId 通过标签ID获取价格驱动
  54. func GetPriceDrivenByVarietyTagId(chartPermissionId int) (item *PriceDriven, err error) {
  55. o := orm.NewOrm()
  56. sql := `SELECT * FROM yb_price_driven WHERE variety_tag_id = ? ORDER BY create_time DESC LIMIT 1`
  57. err = o.Raw(sql, chartPermissionId).QueryRow(&item)
  58. return
  59. }
  60. // GetPriceDrivenById 主键获取价格驱动
  61. func GetPriceDrivenById(priceDrivenId int) (item *PriceDriven, err error) {
  62. o := orm.NewOrm()
  63. sql := `SELECT * FROM yb_price_driven WHERE price_driven_id = ? LIMIT 1`
  64. err = o.Raw(sql, priceDrivenId).QueryRow(&item)
  65. return
  66. }
  67. // PriceDrivenSaveLog 价格驱动保存记录表
  68. type PriceDrivenSaveLog struct {
  69. Id int `orm:"column(id);pk"`
  70. PriceDrivenId int `json:"price_driven_id" description:"价格驱动ID"`
  71. //ChartPermissionId int `json:"chart_permission_id" description:"品种权限ID"`
  72. VarietyTagId int `json:"variety_tag_id" description:"标签ID"`
  73. MainVariable string `json:"main_variable" description:"关键变量"`
  74. CoreDrivenType int `json:"core_driven_type" description:"核心驱动类型 0-多 1-空"`
  75. CoreDrivenContent string `json:"core_driven_content" description:"核心驱动内容"`
  76. CoreContent string `json:"core_content" description:"核心内容"`
  77. AdminId int `json:"admin_id" description:"更新人ID"`
  78. AdminName string `json:"admin_name" description:"更新人名称"`
  79. CreateTime time.Time `json:"create_time" description:"创建时间"`
  80. }
  81. // TableName 表名变更
  82. func (priceDrivenSaveLogInfo *PriceDrivenSaveLog) TableName() string {
  83. return "yb_price_driven_save_log"
  84. }
  85. // Add 新增保存记录
  86. func (priceDrivenSaveLogInfo *PriceDrivenSaveLog) Add() (err error) {
  87. o := orm.NewOrm()
  88. lastId, err := o.Insert(priceDrivenSaveLogInfo)
  89. if err != nil {
  90. return
  91. }
  92. priceDrivenSaveLogInfo.PriceDrivenId = int(lastId)
  93. return
  94. }
  95. // GetPriceDrivenSaveLogByPriceDrivenId 获取最新一条保存记录
  96. func GetPriceDrivenSaveLogByPriceDrivenId(priceDrivenId int) (item *PriceDrivenSaveLog, err error) {
  97. o := orm.NewOrm()
  98. sql := `SELECT * FROM yb_price_driven_save_log WHERE price_driven_id = ? ORDER BY create_time DESC LIMIT 1`
  99. err = o.Raw(sql, priceDrivenId).QueryRow(&item)
  100. return
  101. }
  102. // GetRecentPriceDrivenList 获取每个标签最新价格驱动信息
  103. func GetRecentPriceDrivenList() (list []*PriceDriven, err error) {
  104. o := orm.NewOrm()
  105. sql := `SELECT
  106. pd.price_driven_id,
  107. pd.variety_tag_id,
  108. pd.variety_tag_name,
  109. pd.main_variable,
  110. pd.core_driven_type,
  111. pd.core_driven_content,
  112. pd.modify_time
  113. FROM
  114. ( SELECT price_driven_id, MAX( create_time ) AS create_time FROM yb_price_driven WHERE publish_state = 1 GROUP BY variety_tag_id ) AS pd2
  115. LEFT JOIN yb_price_driven AS pd ON pd.create_time = pd2.create_time
  116. WHERE pd.publish_state = 1
  117. ORDER BY
  118. pd.modify_time DESC`
  119. _, err = o.Raw(sql).QueryRows(&list)
  120. return
  121. }
  122. // PriceDrivenVarietyTagClick 价格驱动标签点击统计
  123. type PriceDrivenVarietyTagClick struct {
  124. VarietyTagId int `json:"variety_tag_id" description:"标签ID"`
  125. CountNum int `json:"count_num" description:"点击量"`
  126. RecentClickTime string `json:"recent_click_time" description:"最近点击时间"`
  127. }
  128. // GetPriceDrivenClick 获取价格驱动点击量(根据标签ID分组)
  129. func GetPriceDrivenClick(priceDrivenIds string) (list []*PriceDrivenVarietyTagClick, err error) {
  130. o := orm.NewOrm()
  131. sql := `SELECT
  132. vl.variety_tag_id,
  133. COUNT(1) AS count_num,
  134. MAX(vl.create_time) AS recent_click_time
  135. FROM
  136. yb_price_driven_visit_log AS vl
  137. WHERE 1=1 `
  138. if priceDrivenIds != "" {
  139. sql += ` AND vl.price_driven_id IN (` + priceDrivenIds + `) `
  140. }
  141. sql += ` GROUP BY vl.variety_tag_id`
  142. _, err = o.Raw(sql).QueryRows(&list)
  143. return
  144. }
  145. // GetPriceDrivenPageClick 获取价格驱动分页点击量(根据标签ID分组)
  146. func GetPriceDrivenPageClick(startSize, pageSize int, priceDrivenIds, joinRule, groupRule, orderRule string) (list []*PriceDrivenVarietyTagClick, err error) {
  147. o := orm.NewOrm()
  148. sql := `SELECT
  149. pd.variety_tag_id,
  150. COUNT( vl.id ) AS count_num
  151. FROM
  152. ( SELECT price_driven_id, variety_tag_id FROM yb_price_driven
  153. WHERE price_driven_id IN (` + priceDrivenIds + `) ) AS pd
  154. LEFT JOIN yb_price_driven_visit_log AS vl ON ` + joinRule
  155. if groupRule != "" {
  156. sql += ` GROUP BY ` + groupRule
  157. }
  158. if orderRule != "" {
  159. sql += ` ORDER BY ` + orderRule
  160. }
  161. sql += ` LIMIT ?,? `
  162. _, err = o.Raw(sql, startSize, pageSize).QueryRows(&list)
  163. return
  164. }
  165. // GetDefaultPriceDrivenPageClick 获取价格驱动默认分页点击量统计列表(根据标签ID分组)
  166. func GetDefaultPriceDrivenPageClick(startSize, pageSize int, priceDrivenIds string) (list []*PriceDrivenVarietyTagClick, err error) {
  167. o := orm.NewOrm()
  168. sql := `SELECT
  169. variety_tag_id
  170. FROM
  171. yb_price_driven
  172. WHERE
  173. price_driven_id IN (` + priceDrivenIds + `) `
  174. sql += `ORDER BY modify_time DESC LIMIT ?,?`
  175. _, err = o.Raw(sql, startSize, pageSize).QueryRows(&list)
  176. return
  177. }
  178. // PriceDrivenVarietyTagClickDetail 价格驱动标签点击统计详情
  179. type PriceDrivenVarietyTagClickDetail struct {
  180. UserId int `json:"user_id" description:"用户ID"`
  181. RealName string `json:"real_name" description:"用户姓名"`
  182. CompanyName string `json:"company_name" description:"公司名称"`
  183. CompanyStatus string `json:"company_status" description:"公司状态"`
  184. CountNum int `json:"count_num" description:"点击量"`
  185. //SourceAgent int `json:"source_agent" description:"来源"`
  186. NewSourceAgent int `json:"new_source_agent" description:"来源"`
  187. RecentClickTime string `json:"recent_click_time" description:"最近点击时间"`
  188. }
  189. // GetPriceDrivenPageClickDetail 获取价格驱动分页点击量详情(根据用户分组)
  190. func GetPriceDrivenPageClickDetail(startSize, pageSize, varietyTagId, priceDrivenId int) (total int, list []*PriceDrivenVarietyTagClickDetail, err error) {
  191. o := orm.NewOrm()
  192. pars := make([]interface{}, 0)
  193. sql := `SELECT
  194. user_id,
  195. real_name,
  196. IF(source_agent=4,2,source_agent) as new_source_agent,
  197. company_name,
  198. company_status,
  199. COUNT(1) AS count_num,
  200. MAX(create_time) AS recent_click_time
  201. FROM
  202. yb_price_driven_visit_log
  203. WHERE 1=1 `
  204. if varietyTagId > 0 {
  205. sql += ` AND variety_tag_id = ?`
  206. pars = append(pars, varietyTagId)
  207. }
  208. if priceDrivenId > 0 {
  209. sql += ` AND price_driven_id = ?`
  210. pars = append(pars, priceDrivenId)
  211. }
  212. sql += ` GROUP BY user_id, new_source_agent ORDER BY recent_click_time DESC`
  213. totalSQL := `SELECT COUNT(1) total FROM (` + sql + `) z `
  214. err = o.Raw(totalSQL, pars).QueryRow(&total)
  215. if err != nil {
  216. return
  217. }
  218. sql += ` LIMIT ?,? `
  219. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  220. return
  221. }