industry_fllow.go 9.0 KB

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