cygx_user_record.go 16 KB

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