cygx_user_record.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. package cygx
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hz_crm_api/utils"
  5. "time"
  6. )
  7. type CygxUserRecord struct {
  8. UserRecordId int `orm:"column(user_record_id);pk"`
  9. OpenId string `description:"用户openid,最大长度:32"`
  10. UnionId string `description:"用户unionid,最大长度:64"`
  11. NickName string `descritpion:"用户昵称,最大长度:32"`
  12. Sex int `descritpion:"普通用户性别,1为男性,2为女性"`
  13. Province string `description:"普通用户个人资料填写的省份,最大长度:30"`
  14. City string `description:"普通用户个人资料填写的城市,最大长度:30"`
  15. Country string `description:"国家,如中国为CN,最大长度:30"`
  16. Headimgurl string `description:"用户第三方(微信)头像,最大长度:512"`
  17. CreateTime time.Time `description:"创建时间,关系添加时间、用户授权时间"`
  18. }
  19. type OpenIdList struct {
  20. OpenId string
  21. UserId int
  22. CompanyId int `description:"公司ID"`
  23. Mobile string `description:"手机号"`
  24. }
  25. type OpenIdMobileList struct {
  26. OpenId string
  27. UserId int
  28. Mobile string `description:"手机号"`
  29. CompanyId int `description:"公司ID"`
  30. }
  31. func GetCygxUserRecord() (items []*CygxUserRecord, err error) {
  32. o := orm.NewOrmUsingDB("hz_cygx")
  33. sql := `SELECT union_id,open_id FROM cygx_user_record `
  34. _, err = o.Raw(sql).QueryRows(&items)
  35. return
  36. }
  37. // 获取所有有权限的用户的opid
  38. func GetCygxUserRecordPower(chartPermissionIds string) (items []*OpenIdList, err error) {
  39. listMobile, err := GetCygxUserRecordPowerMobileList(chartPermissionIds)
  40. if err != nil {
  41. return
  42. }
  43. var mobileArr []string
  44. for _, v := range listMobile {
  45. if v.Mobile != "" {
  46. mobileArr = append(mobileArr, v.Mobile)
  47. }
  48. }
  49. mobileLen := len(mobileArr)
  50. if mobileLen == 0 {
  51. return
  52. }
  53. openIdList, err := GetUserRecordListByMobileArr(mobileArr)
  54. if err != nil {
  55. return
  56. }
  57. items = openIdList
  58. return
  59. }
  60. // 获取所有有权限的用户的opid
  61. func GetCygxUserRecordPowerArticle(chartPermissionIds string) (items []*OpenIdList, err error) {
  62. listMobile, err := GetCygxUserRecordPowerMobileList(chartPermissionIds)
  63. if err != nil {
  64. return
  65. }
  66. var mobileArr []string
  67. for _, v := range listMobile {
  68. if v.CompanyId == utils.JMCJ_COMPANY_ID {
  69. continue //需求池 1043:深圳进门财经科技股份有限公司下所有的账号,所有活动的新增修改都做推送,所有的报告的新增修改都不做推送。
  70. }
  71. if v.Mobile != "" {
  72. mobileArr = append(mobileArr, v.Mobile)
  73. }
  74. }
  75. mobileLen := len(mobileArr)
  76. if mobileLen == 0 {
  77. return
  78. }
  79. openIdList, err := GetUserRecordListByMobileArr(mobileArr)
  80. if err != nil {
  81. return
  82. }
  83. items = openIdList
  84. return
  85. }
  86. // 获取所有有权限的用户的opid
  87. func GetCygxUserRecordPowerMobileList(chartPermissionIds string) (items []*OpenIdMobileList, err error) {
  88. o := orm.NewOrm()
  89. sql := `SELECT
  90. u.user_id,
  91. u.company_id,
  92. u.real_name,
  93. u.mobile
  94. FROM
  95. company_report_permission AS p
  96. INNER JOIN wx_user AS u ON u.company_id = p.company_id
  97. INNER JOIN user_record AS r ON r.user_id = u.user_id
  98. WHERE
  99. p.chart_permission_id IN (` + chartPermissionIds + `)
  100. AND r.create_platform = 4
  101. AND p.STATUS IN ('正式','试用','永续') GROUP BY u.user_id `
  102. _, err = o.Raw(sql).QueryRows(&items)
  103. return
  104. }
  105. // 获取关注这个文章对应产业的用户的 openid
  106. func GetCygxUserFllowOpenid(articleId int) (items []*OpenIdList, err error) {
  107. o := orm.NewOrmUsingDB("hz_cygx")
  108. sql := `SELECT
  109. cr.open_id,
  110. cr.cygx_user_id as user_id
  111. FROM
  112. cygx_xzs_choose_send AS s
  113. INNER JOIN cygx_industry_fllow AS f ON f.user_id = s.user_id
  114. INNER JOIN cygx_user_record AS cr ON cr.cygx_user_id = s.user_id
  115. INNER JOIN cygx_industrial_article_group_management as mg ON mg.industrial_management_id = f.industrial_management_id
  116. WHERE 1=1
  117. AND mg.article_id = ?`
  118. _, err = o.Raw(sql, articleId).QueryRows(&items)
  119. return
  120. }
  121. // 获取关注这个产业的用户的 openid
  122. func GetCygxUserindustryFllowOpenid(industrialId int) (items []*OpenIdList, err error) {
  123. o := orm.NewOrmUsingDB("hz_cygx")
  124. sql := `SELECT
  125. cr.open_id,
  126. cr.cygx_user_id as user_id
  127. FROM
  128. cygx_xzs_choose_send AS s
  129. INNER JOIN cygx_industry_fllow AS f ON f.user_id = s.user_id
  130. INNER JOIN cygx_user_record AS cr ON cr.cygx_user_id = s.user_id
  131. WHERE
  132. 1=1
  133. AND f.industrial_management_id = ?`
  134. _, err = o.Raw(sql, industrialId).QueryRows(&items)
  135. return
  136. }
  137. // 获取关注这个文章对应作者的用户的 openid
  138. func GetCygxUserFllowDepartmentOpenid(articleId int) (items []*OpenIdList, err error) {
  139. o := orm.NewOrmUsingDB("hz_cygx")
  140. sql := `SELECT
  141. cr.open_id,
  142. cr.cygx_user_id as user_id
  143. FROM
  144. cygx_article_department_follow AS f
  145. INNER JOIN cygx_user_record AS cr ON cr.cygx_bind_account = f.mobile
  146. INNER JOIN cygx_article AS a ON a.department_id = f.department_id
  147. WHERE
  148. 1=1
  149. AND a.article_id = ?`
  150. _, err = o.Raw(sql, articleId).QueryRows(&items)
  151. return
  152. }
  153. // 获取拒绝接收推送的的用户的 openid
  154. func GetCygxUserRefusetOpenid() (items []*OpenIdList, err error) {
  155. o := orm.NewOrmUsingDB("hz_cygx")
  156. sql := `SELECT
  157. cr.open_id,
  158. cr.cygx_user_id as user_id
  159. FROM
  160. cygx_xzs_choose_send AS s
  161. INNER JOIN cygx_user_record AS cr ON cr.cygx_user_id = s.user_id
  162. WHERE
  163. 1=1
  164. AND s.is_refuse = 1`
  165. _, err = o.Raw(sql).QueryRows(&items)
  166. return
  167. }
  168. // 获取选择策略推送的用户的openid openid
  169. func GetCygxUserFllowCeLueOpenid(categoryId int) (items []*OpenIdList, err error) {
  170. o := orm.NewOrmUsingDB("hz_cygx")
  171. sql := `SELECT
  172. cr.open_id,
  173. cr.cygx_user_id AS user_id
  174. FROM
  175. cygx_xzs_choose_send AS s
  176. INNER JOIN cygx_xzs_choose_category AS f ON f.user_id = s.user_id
  177. INNER JOIN cygx_user_record AS cr ON cr.cygx_user_id = s.user_id
  178. WHERE
  179. 1 = 1
  180. AND s.is_refuse = 0
  181. AND s.is_subjective = 1
  182. AND s.is_objective = 1
  183. AND f.category_id = ?`
  184. _, err = o.Raw(sql, categoryId).QueryRows(&items)
  185. return
  186. }
  187. // 获取关注这个活动对应产业的用户的 openid
  188. func GetCygxActiviyUserFllowOpenid(activityId int) (items []*OpenIdList, err error) {
  189. o := orm.NewOrmUsingDB("hz_cygx")
  190. sql := `SELECT
  191. cr.open_id,
  192. cr.cygx_user_id AS user_id
  193. FROM
  194. cygx_xzs_choose_send AS s
  195. INNER JOIN cygx_industry_fllow AS f ON f.user_id = s.user_id
  196. INNER JOIN cygx_user_record AS cr ON cr.cygx_user_id = s.user_id
  197. INNER JOIN cygx_industrial_activity_group_management AS mg ON mg.industrial_management_id = f.industrial_management_id
  198. WHERE
  199. 1=1
  200. AND mg.activity_id = ? AND mg.source = 1 `
  201. _, err = o.Raw(sql, activityId).QueryRows(&items)
  202. return
  203. }
  204. //// 根据销售规模,获取所有有权限的用户的opid
  205. //func GetCygxUserRecordPowerByScale(chartPermissionIds, scale string) (items []*OpenIdList, err error) {
  206. // o := orm.NewOrm()
  207. // sql := `SELECT
  208. // cr.open_id,
  209. // u.user_id,
  210. // u.company_id,
  211. // u.real_name,
  212. // u.mobile
  213. // FROM
  214. // company_report_permission AS p
  215. // INNER JOIN wx_user AS u ON u.company_id = p.company_id
  216. // INNER JOIN user_record AS r ON r.user_id = u.user_id
  217. // INNER JOIN cygx_user_record AS cr ON cr.union_id = r.union_id
  218. // INNER JOIN company_product AS cp ON cp.company_id = p.company_id
  219. // WHERE
  220. // p.chart_permission_id IN (` + chartPermissionIds + `)
  221. // AND cp.scale IN (` + scale + `)
  222. // AND r.create_platform = 4
  223. // AND p.STATUS IN ('正式','试用','永续') GROUP BY u.company_id `
  224. // _, err = o.Raw(sql).QueryRows(&items)
  225. // return
  226. //}
  227. // GetCygxUserRecordPowerByActivitySet 根据活动设置,获取所有有权限的用户的opid
  228. func GetCygxUserRecordPowerByActivitySet(chartPermissionIds, condition string, mobileArrJmcj []string) (items []*OpenIdList, itemsYx []*OpenIdList, err error) {
  229. listMobile, err := GetCygxUserRecordPowerByActivitySetListMobile(chartPermissionIds, condition)
  230. if err != nil {
  231. return
  232. }
  233. var mobileArr []string
  234. var mobileArrYx []string
  235. //需求池 1043:深圳进门财经科技股份有限公司下所有的账号,所有活动的新增修改都做推送,所有的报告的新增修改都不做推送。
  236. for _, v := range mobileArrJmcj {
  237. if v != "" {
  238. mobileArr = append(mobileArr, v)
  239. }
  240. }
  241. for _, v := range listMobile {
  242. if v.Mobile != "" {
  243. if !utils.InArrayByStr(mobileArrJmcj, v.Mobile) {
  244. mobileArrYx = append(mobileArrYx, v.Mobile)
  245. }
  246. mobileArr = append(mobileArr, v.Mobile)
  247. }
  248. }
  249. mobileLen := len(mobileArr)
  250. if mobileLen == 0 {
  251. return
  252. }
  253. openIdList, err := GetUserRecordListByMobileArr(mobileArr)
  254. if err != nil {
  255. return
  256. }
  257. items = openIdList
  258. openIdListYx, err := GetMfyxUserRecordListByMobileArr(mobileArrYx)
  259. if err != nil {
  260. return
  261. }
  262. itemsYx = openIdListYx
  263. return
  264. }
  265. // GetCygxUserRecordPowerByActivitySetListMobile 根据活动设置,获取所有有权限的用户的手机号
  266. func GetCygxUserRecordPowerByActivitySetListMobile(chartPermissionIds, condition string) (items []*OpenIdMobileList, err error) {
  267. o := orm.NewOrm()
  268. sql := `SELECT
  269. u.user_id,
  270. u.company_id,
  271. u.real_name,
  272. u.mobile
  273. FROM
  274. company_report_permission AS p
  275. INNER JOIN wx_user AS u ON u.company_id = p.company_id
  276. INNER JOIN user_record AS r ON r.user_id = u.user_id
  277. WHERE
  278. p.chart_permission_id IN (` + chartPermissionIds + `) ` + condition + `
  279. AND p.STATUS IN ('正式','试用','永续') GROUP BY u.user_id `
  280. _, err = o.Raw(sql).QueryRows(&items)
  281. return
  282. }
  283. // 获取关注了查研观向小助手公众号的所有公司ID
  284. func GetCygxUserRecordCompany() (items []*OpenIdList, err error) {
  285. listUser, err := GetCygxUserRecordCompanyUserId()
  286. if err != nil {
  287. return
  288. }
  289. var userIds []int
  290. for _, v := range listUser {
  291. userIds = append(userIds, v.UserId)
  292. }
  293. lenarr := len(userIds)
  294. if lenarr == 0 {
  295. return
  296. }
  297. var condition string
  298. var pars []interface{}
  299. condition = ` AND w.user_id IN (` + utils.GetOrmInReplace(lenarr) + `)`
  300. pars = append(pars, userIds)
  301. o := orm.NewOrm()
  302. sql := `SELECT
  303. w.company_id
  304. FROM
  305. wx_user AS w
  306. WHERE
  307. 1 = 1` + condition + `GROUP BY w.company_id `
  308. _, err = o.Raw(sql, pars).QueryRows(&items)
  309. return
  310. }
  311. // 获取关注了查研观向小助手公众号的所有用户ID
  312. func GetCygxUserRecordCompanyUserId() (items []*OpenIdList, err error) {
  313. o := orm.NewOrmUsingDB("hz_cygx")
  314. sql := `SELECT
  315. u.open_id,
  316. u.cygx_user_id AS user_id
  317. FROM
  318. cygx_user_record AS u
  319. WHERE
  320. 1 = 1
  321. AND subscribe = 1
  322. GROUP BY
  323. u.cygx_user_id `
  324. _, err = o.Raw(sql).QueryRows(&items)
  325. return
  326. }
  327. type CompanyReportPermissionItem struct {
  328. CompanyId int
  329. ChartPermissionId int
  330. IsUpgrade int `description:"是否升级,1是,0否"`
  331. Remark string `description:"备注,权限名称带有主观客观这种"`
  332. PermissionName string `description:"权限名称"`
  333. }
  334. // GetCompanyReportPermissionItem 获取客户对应的权限
  335. func GetCompanyReportPermissionItem(condition string, pars []interface{}) (items []*CompanyReportPermissionItem, err error) {
  336. o := orm.NewOrm()
  337. sql := `SELECT
  338. r.company_id,
  339. r.chart_permission_id,
  340. r.is_upgrade,
  341. c.remark,
  342. c.permission_name
  343. FROM
  344. company_report_permission AS r
  345. INNER JOIN chart_permission AS c ON r.chart_permission_id = c.chart_permission_id
  346. WHERE
  347. 1 = 1
  348. AND r.product_id = 2
  349. AND r.STATUS = '正式' ` + condition
  350. _, err = o.Raw(sql, pars).QueryRows(&items)
  351. return
  352. }
  353. // 根据手机号获取用户的openid
  354. func GetUserRecordListByMobileArr(bindAccount []string) (items []*OpenIdList, err error) {
  355. o := orm.NewOrmUsingDB("hz_cygx")
  356. lenarr := len(bindAccount)
  357. if lenarr == 0 {
  358. return
  359. }
  360. var condition string
  361. var pars []interface{}
  362. condition = ` AND u.cygx_bind_account IN (` + utils.GetOrmInReplace(lenarr) + `)`
  363. pars = append(pars, bindAccount)
  364. sql := `SELECT
  365. u.open_id,
  366. u.cygx_user_id AS user_id
  367. FROM
  368. cygx_user_record AS u
  369. WHERE
  370. 1 = 1 ` + condition
  371. _, err = o.Raw(sql, pars).QueryRows(&items)
  372. return
  373. }
  374. // 获取单个用户openid
  375. func GetUserRecordOpenidByMobile(bindAccount string) (item *OpenIdList, err error) {
  376. o := orm.NewOrmUsingDB("hz_cygx")
  377. var sql string
  378. sql = `SELECT cr.open_id,cr.cygx_user_id as user_id
  379. FROM cygx_user_record AS cr
  380. WHERE cr.cygx_bind_account = ?`
  381. err = o.Raw(sql, bindAccount).QueryRow(&item)
  382. return
  383. }
  384. // 获取预约活动的用户的openID
  385. func GetActivityOpenIdList(activityId int) (items []*OpenIdList, err error) {
  386. o := orm.NewOrmUsingDB("hz_cygx")
  387. sql := `SELECT DISTINCT cr.open_id,cr.cygx_user_id as user_id
  388. FROM
  389. cygx_my_schedule AS m
  390. INNER JOIN cygx_user_record AS cr ON cr.cygx_bind_account = m.mobile
  391. WHERE m.activity_id = ? `
  392. _, err = o.Raw(sql, activityId).QueryRows(&items)
  393. return
  394. }
  395. // 获取预约活动的用户的openID
  396. func GetActivitySpecialOpenIdList() (items []*OpenIdList, err error) {
  397. o := orm.NewOrmUsingDB("hz_cygx")
  398. sql := `SELECT DISTINCT cr.open_id,cr.cygx_user_id as user_id
  399. FROM
  400. cygx_user_follow_special AS m
  401. INNER JOIN cygx_user_record AS cr ON cr.cygx_bind_account = m.mobile `
  402. _, err = o.Raw(sql).QueryRows(&items)
  403. return
  404. }
  405. // 获取关注作者的用户的openID
  406. func GetFollowDepartmentOpenIdList(departmentId int) (items []*OpenIdList, err error) {
  407. o := orm.NewOrmUsingDB("hz_cygx")
  408. sql := `SELECT DISTINCT cr.open_id,cr.cygx_user_id as user_id
  409. FROM
  410. cygx_article_department_follow AS f
  411. INNER JOIN cygx_user_record AS cr ON cr.cygx_bind_account = f.mobile
  412. WHERE
  413. f.department_id = ? `
  414. _, err = o.Raw(sql, departmentId).QueryRows(&items)
  415. return
  416. }
  417. // 获取关注产业的用户的openID
  418. func GetFollowindustrialOpenIdList(industrialManagementId int) (items []*OpenIdList, err error) {
  419. o := orm.NewOrmUsingDB("hz_cygx")
  420. sql := `SELECT DISTINCT cr.open_id,cr.cygx_user_id as user_id
  421. FROM
  422. cygx_industry_fllow AS f
  423. INNER JOIN cygx_user_record AS cr ON cr.cygx_bind_account = f.mobile
  424. WHERE
  425. f.industrial_management_id = ? `
  426. _, err = o.Raw(sql, industrialManagementId).QueryRows(&items)
  427. return
  428. }
  429. // 获取所有关注了该产业用户的openid
  430. func GetCygxUserIndustryFllowOpneidByActivityIds(activityId int) (items []*OpenIdList, err error) {
  431. o := orm.NewOrm()
  432. sql := `SELECT
  433. cr.open_id,
  434. cr.cygx_bind_account as mobile,
  435. cr.cygx_user_id AS user_id
  436. FROM
  437. cygx_industrial_activity_group_management AS agm
  438. INNER JOIN cygx_industry_fllow AS f ON f.industrial_management_id = agm.industrial_management_id
  439. INNER JOIN cygx_user_record AS cr ON cr.cygx_user_id = f.user_id
  440. WHERE
  441. agm.activity_id = ? `
  442. _, err = o.Raw(sql, activityId).QueryRows(&items)
  443. return
  444. }
  445. // 获取预约活动的用户的openID
  446. func GetMfyxActivityOpenIdList(activityId int) (items []*OpenIdList, err error) {
  447. o := orm.NewOrmUsingDB("hz_cygx")
  448. sql := `SELECT DISTINCT cr.open_id,cr.cygx_user_id as user_id
  449. FROM
  450. cygx_my_schedule AS m
  451. INNER JOIN cygx_mfyx_gzh_user_record AS cr ON cr.cygx_bind_account = m.mobile
  452. WHERE m.activity_id = ? `
  453. _, err = o.Raw(sql, activityId).QueryRows(&items)
  454. return
  455. }
  456. // 根据手机号获取用户的openid
  457. func GetMfyxUserRecordListByMobileArr(bindAccount []string) (items []*OpenIdList, err error) {
  458. o := orm.NewOrmUsingDB("hz_cygx")
  459. lenarr := len(bindAccount)
  460. if lenarr == 0 {
  461. return
  462. }
  463. var condition string
  464. var pars []interface{}
  465. condition = ` AND u.cygx_bind_account IN (` + utils.GetOrmInReplace(lenarr) + `)`
  466. pars = append(pars, bindAccount)
  467. sql := `SELECT
  468. u.open_id,
  469. u.cygx_user_id AS user_id
  470. FROM
  471. cygx_mfyx_gzh_user_record AS u
  472. WHERE
  473. 1 = 1 ` + condition
  474. _, err = o.Raw(sql, pars).QueryRows(&items)
  475. return
  476. }
  477. // 获取单个用户openid
  478. func GetMfyxUserRecordOpenidByMobile(bindAccount string) (item *OpenIdList, err error) {
  479. o := orm.NewOrmUsingDB("hz_cygx")
  480. var sql string
  481. sql = `SELECT cr.open_id,cr.cygx_user_id as user_id
  482. FROM cygx_mfyx_gzh_user_record AS cr
  483. WHERE cr.cygx_bind_account = ?`
  484. err = o.Raw(sql, bindAccount).QueryRow(&item)
  485. return
  486. }