industry_fllow.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_cygx/utils"
  6. "time"
  7. )
  8. type CygxIndustryFllow struct {
  9. Id int `orm:"column(id);pk"`
  10. IndustrialManagementId int `description:"产业D"`
  11. UserId int `description:"用户ID"`
  12. Mobile string `description:"手机号"`
  13. Email string `description:"邮箱"`
  14. CompanyId int `description:"公司id"`
  15. CompanyName string `description:"公司名称"`
  16. Type int `description:"操作方式,1报名,2取消报名"`
  17. CreateTime time.Time `description:"创建时间"`
  18. ModifyTime time.Time `description:"更新时间"`
  19. RealName string `description:"用户实际名称"`
  20. Source int `description:"来源1查研观向,2查研观向小助手,3勾选全部赛道的用户进行自动关注"`
  21. FollowType int `description:"1,重点关注,3不感兴趣,0默认接受推送"`
  22. }
  23. type CygxIndustryFllowRep struct {
  24. IndustrialManagementId int `description:"产业D"`
  25. FollowType int `description:"1,重点关注,3不感兴趣,0默认接受推送"`
  26. }
  27. type IndustryFllowArryReq struct {
  28. SourceId int `description:"资源ID"`
  29. Source string `description:"资源类型 报告 :article 、活动 :activity"`
  30. DoType string `description:"操作方式 关注 :add 、取消关注 :cancel"`
  31. }
  32. // 添加
  33. func AddCygxIndustryFllow(item *CygxIndustryFllow) (lastId int64, err error) {
  34. o, err := orm.NewOrm().Begin()
  35. if err != nil {
  36. return
  37. }
  38. defer func() {
  39. if err == nil {
  40. o.Commit()
  41. } else {
  42. o.Rollback()
  43. }
  44. }()
  45. sql := `DELETE FROM cygx_industry_fllow WHERE user_id=? AND industrial_management_id=? `
  46. _, err = o.Raw(sql, item.UserId, item.IndustrialManagementId).Exec()
  47. if err != nil {
  48. return
  49. }
  50. lastId, err = o.Insert(item)
  51. return
  52. }
  53. // 批量添加
  54. func AddCygxIndustryFllowMulti(items []*CygxIndustryFllow) (err error) {
  55. o := orm.NewOrm()
  56. if len(items) > 0 {
  57. //批量添加新的关注记录
  58. _, err = o.InsertMulti(len(items), items)
  59. }
  60. return
  61. }
  62. type CygxIndustryFllowResp struct {
  63. Status int `description:"1:关注,2:取消关注"`
  64. GoFollow bool `description:"是否去关注"`
  65. }
  66. func RemoveCygxIndustryFllow(userId, industrialManagementId int) (err error) {
  67. o := orm.NewOrm()
  68. sql := `DELETE FROM cygx_industry_fllow WHERE user_id=? AND industrial_management_id=? `
  69. _, err = o.Raw(sql, userId, industrialManagementId).Exec()
  70. return
  71. }
  72. // RemoveCygxIndustryFllowArry 多个产业同时取消关注
  73. func RemoveCygxIndustryFllowArry(userId int, condition string, pars []interface{}) (err error) {
  74. o := orm.NewOrm()
  75. if condition == "" {
  76. return
  77. }
  78. sql := `DELETE FROM cygx_industry_fllow WHERE user_id=? ` + condition
  79. _, err = o.Raw(sql, userId, pars).Exec()
  80. return
  81. }
  82. // 获取数量
  83. func GetCountCygxIndustryFllow(userId, industrialManagementId int, condition string) (count int, err error) {
  84. sql := `SELECT COUNT(1) AS count FROM cygx_industry_fllow WHERE user_id=? AND industrial_management_id=? ` + condition
  85. err = orm.NewOrm().Raw(sql, userId, industrialManagementId).QueryRow(&count)
  86. return
  87. }
  88. // 获取关注数量
  89. func GetCountCygxIndustryFllowByType(userId, industrialManagementId int) (count int, err error) {
  90. sql := `SELECT COUNT(1) AS count FROM cygx_industry_fllow WHERE user_id=? AND industrial_management_id=? AND follow_type = 1 `
  91. err = orm.NewOrm().Raw(sql, userId, industrialManagementId).QueryRow(&count)
  92. return
  93. }
  94. // 获取数量
  95. func GetCountCygxIndustryFllowByUid(userId int) (count int, err error) {
  96. sql := `SELECT COUNT(1) AS count FROM cygx_industry_fllow WHERE user_id=? `
  97. err = orm.NewOrm().Raw(sql, userId).QueryRow(&count)
  98. return
  99. }
  100. // 获取列表信息根据手机号分组
  101. func GetCygxIndustryFllowList(condition string) (items []*CygxIndustryFllow, err error) {
  102. o := orm.NewOrm()
  103. sql := `SELECT * FROM cygx_industry_fllow WHERE 1 =1 ` + condition + ` GROUP BY user_id `
  104. _, err = o.Raw(sql).QueryRows(&items)
  105. return
  106. }
  107. // 根据手机号获取用户关注的产业
  108. func GetCygxIndustryFllowListByMobile(mobile string) (items []*CygxIndustryFllow, err error) {
  109. o := orm.NewOrm()
  110. sql := `SELECT * FROM cygx_industry_fllow WHERE mobile = ?`
  111. _, err = o.Raw(sql, mobile).QueryRows(&items)
  112. return
  113. }
  114. // 获取列表信息根据手机号分组
  115. func GetCygxIndustryFllowListByUserId(condition string) (items []*CygxIndustryFllow, err error) {
  116. o := orm.NewOrm()
  117. sql := `SELECT * FROM cygx_industry_fllow WHERE 1 =1 ` + condition
  118. _, err = o.Raw(sql).QueryRows(&items)
  119. return
  120. }
  121. // 修改用户关注的相关信息
  122. func UpdateCygxIndustryFllow(wxUser *WxUserItem) (err error) {
  123. o := orm.NewOrm()
  124. var sql string
  125. if wxUser.Mobile != "" {
  126. sql = `UPDATE cygx_industry_fllow SET email=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE mobile=? `
  127. _, err = o.Raw(sql, wxUser.Email, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Mobile).Exec()
  128. } else if wxUser.Email != "" {
  129. sql = `UPDATE cygx_industry_fllow SET mobile=?,company_id=?,company_name=?,user_id=?,real_name=? WHERE mobile=? `
  130. _, err = o.Raw(sql, wxUser.Mobile, wxUser.CompanyId, wxUser.CompanyName, wxUser.UserId, wxUser.RealName, wxUser.Email).Exec()
  131. }
  132. return
  133. }
  134. // 获取用户关注的产业列表
  135. func GetUserFllowIndustrialList(userId int) (items []*CygxIndustryFllow, err error) {
  136. o := orm.NewOrm()
  137. sql := `SELECT
  138. f.user_id,
  139. m.industry_name,
  140. m.industrial_management_id
  141. FROM
  142. cygx_industry_fllow AS f
  143. INNER JOIN cygx_industrial_management AS m ON m.industrial_management_id = f.industrial_management_id
  144. WHERE
  145. 1 = 1
  146. AND f.user_id = ? `
  147. _, err = o.Raw(sql, userId).QueryRows(&items)
  148. return
  149. }
  150. type CygxIndustryFllowCountRep struct {
  151. IndustrialManagementId int `description:"产业D"`
  152. Num int `description:"数量"`
  153. }
  154. // 获取产业被关注的数量
  155. func GetUserFllowIndustrialCountList() (items []*CygxIndustryFllowCountRep, err error) {
  156. o := orm.NewOrm()
  157. sql := `SELECT
  158. COUNT( 1 ) AS num,
  159. f.industrial_management_id
  160. FROM
  161. cygx_industry_fllow AS f
  162. INNER JOIN cygx_industrial_management AS m ON m.industrial_management_id = f.industrial_management_id
  163. WHERE
  164. 1 = 1
  165. GROUP BY
  166. f.industrial_management_id
  167. ORDER BY
  168. num DESC
  169. LIMIT 30 `
  170. _, err = o.Raw(sql).QueryRows(&items)
  171. return
  172. }
  173. // GetUserFllowIndustrialListByUserIdAndIndustrial 通过用户ID 跟产业ID获取用户关注的产业列表
  174. func GetUserFllowIndustrialListByUserIdAndIndustrial(userIds, industrials string) (items []*CygxIndustryFllow, err error) {
  175. o := orm.NewOrm()
  176. sql := `SELECT * FROM cygx_industry_fllow
  177. WHERE 1 = 1
  178. AND user_id IN ( ` + userIds + ` )
  179. AND industrial_management_id IN ( ` + industrials + `) `
  180. _, err = o.Raw(sql).QueryRows(&items)
  181. return
  182. }
  183. // 获取某个用户关注某个行业下的产业数量
  184. func GetCountCygxIndustryFllowByUidAndChartPermissionId(userId, ChartPermissionId int) (count int, err error) {
  185. sql := `SELECT
  186. COUNT( 1 ) AS count
  187. FROM
  188. cygx_industry_fllow AS f
  189. INNER JOIN cygx_industrial_management AS m ON m.industrial_management_id = f.industrial_management_id
  190. WHERE
  191. user_id = ?
  192. AND m.chart_permission_id = ? `
  193. err = orm.NewOrm().Raw(sql, userId, ChartPermissionId).QueryRow(&count)
  194. return
  195. }
  196. // GetTopIndustryFollowData 获取关注度最高的产业关注数据
  197. func GetTopIndustryFollowData(startSize, pageSize int, condition string, pars []interface{}) (list []*IndustrialManagement, err error) {
  198. sql := `SELECT
  199. man.*
  200. FROM
  201. cygx_industrial_management AS man
  202. WHERE 1 = 1 `
  203. if condition != "" {
  204. sql += condition
  205. }
  206. sql += ` ORDER BY
  207. one_month_follow_num DESC , man.industrial_management_id DESC
  208. LIMIT ?,?`
  209. _, err = orm.NewOrm().Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  210. return
  211. }
  212. // 列表
  213. func GetCygxIndustryFllowListByCon(condition string, pars []interface{}) (items []*CygxIndustryFllow, err error) {
  214. o := orm.NewOrm()
  215. sql := `SELECT * FROM cygx_industry_fllow as art WHERE 1= 1 `
  216. if condition != "" {
  217. sql += condition
  218. }
  219. //sql += ` LIMIT ?,? `
  220. _, err = o.Raw(sql, pars).QueryRows(&items)
  221. return
  222. }
  223. type CygxIndustryFllowNumResp struct {
  224. IndustrialManagementId int `description:"产业D"`
  225. Total int `description:"关注数量"`
  226. }
  227. // 获取用户关注的产业列表
  228. func GetIndustrialManagementOneMonthFollowNum() (items []*CygxIndustryFllowNumResp, err error) {
  229. o := orm.NewOrm()
  230. sql := `SELECT
  231. COUNT( 1 ) AS total,
  232. industrial_management_id
  233. FROM
  234. cygx_industry_fllow AS idf
  235. WHERE
  236. 1 = 1
  237. AND create_time > '%s'
  238. AND create_time < '%s'
  239. AND source != 3
  240. GROUP BY
  241. industrial_management_id `
  242. sql = fmt.Sprintf(sql, time.Now().AddDate(0, -1, 0).Format(utils.FormatDate), time.Now().Format(utils.FormatDate))
  243. _, err = o.Raw(sql).QueryRows(&items)
  244. return
  245. }