cygx_user_record.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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_user_record AS cr
  186. INNER JOIN cygx_industry_fllow AS f ON f.user_id = cr.cygx_user_id
  187. INNER JOIN cygx_industrial_activity_group_management AS mg ON mg.industrial_management_id = f.industrial_management_id
  188. WHERE
  189. 1 = 1
  190. AND mg.activity_id = ?
  191. AND mg.source = 1
  192. AND f.follow_type = 1 `
  193. _, err = o.Raw(sql, activityId).QueryRows(&items)
  194. return
  195. }
  196. //// 根据销售规模,获取所有有权限的用户的opid
  197. //func GetCygxUserRecordPowerByScale(chartPermissionIds, scale string) (items []*OpenIdList, err error) {
  198. // o := orm.NewOrm()
  199. // sql := `SELECT
  200. // cr.open_id,
  201. // u.user_id,
  202. // u.company_id,
  203. // u.real_name,
  204. // u.mobile
  205. // FROM
  206. // company_report_permission AS p
  207. // INNER JOIN wx_user AS u ON u.company_id = p.company_id
  208. // INNER JOIN user_record AS r ON r.user_id = u.user_id
  209. // INNER JOIN cygx_user_record AS cr ON cr.union_id = r.union_id
  210. // INNER JOIN company_product AS cp ON cp.company_id = p.company_id
  211. // WHERE
  212. // p.chart_permission_id IN (` + chartPermissionIds + `)
  213. // AND cp.scale IN (` + scale + `)
  214. // AND r.create_platform = 4
  215. // AND p.STATUS IN ('正式','试用','永续') GROUP BY u.company_id `
  216. // _, err = o.Raw(sql).QueryRows(&items)
  217. // return
  218. //}
  219. // GetCygxUserRecordPowerByActivitySet 根据活动设置,获取所有有权限的用户的opid
  220. func GetCygxUserRecordPowerByActivitySet(chartPermissionIds, condition string) (items []*OpenIdList, itemsYx []*OpenIdList, err error) {
  221. //o := orm.NewOrm()
  222. //sql := `SELECT
  223. // cr.open_id,
  224. // u.user_id,
  225. // u.company_id,
  226. // u.real_name,
  227. // u.mobile
  228. // FROM
  229. // company_report_permission AS p
  230. // INNER JOIN wx_user AS u ON u.company_id = p.company_id
  231. // INNER JOIN user_record AS r ON r.user_id = u.user_id
  232. // INNER JOIN cygx_user_record AS cr ON cr.union_id = r.union_id
  233. // WHERE
  234. // p.chart_permission_id IN (` + chartPermissionIds + `) ` + condition + ` AND r.create_platform = 4
  235. // AND p.STATUS IN ('正式','试用','永续') GROUP BY cr.open_id `
  236. //_, err = o.Raw(sql).QueryRows(&items)
  237. listMobile, err := GetCygxUserRecordPowerByActivitySetListMobile(chartPermissionIds, condition)
  238. if err != nil {
  239. return
  240. }
  241. var mobileArr []string
  242. for _, v := range listMobile {
  243. if v.Mobile != "" {
  244. mobileArr = append(mobileArr, v.Mobile)
  245. }
  246. }
  247. mobileLen := len(mobileArr)
  248. if mobileLen == 0 {
  249. return
  250. }
  251. openIdList, err := GetUserRecordListByMobileArr(mobileArr)
  252. if err != nil {
  253. return
  254. }
  255. items = openIdList
  256. openIdListYx, err := GetMfyxUserRecordListByMobileArr(mobileArr)
  257. if err != nil {
  258. return
  259. }
  260. itemsYx = openIdListYx
  261. return
  262. }
  263. // GetCygxUserRecordPowerByActivitySetListMobile 根据活动设置,获取所有有权限的用户的手机号
  264. func GetCygxUserRecordPowerByActivitySetListMobile(chartPermissionIds, condition string) (items []*OpenIdMobileList, err error) {
  265. o := orm.NewOrm()
  266. sql := `SELECT
  267. u.user_id,
  268. u.company_id,
  269. u.real_name,
  270. u.mobile
  271. FROM
  272. company_report_permission AS p
  273. INNER JOIN wx_user AS u ON u.company_id = p.company_id
  274. INNER JOIN user_record AS r ON r.user_id = u.user_id
  275. WHERE
  276. p.chart_permission_id IN (` + chartPermissionIds + `) ` + condition + `
  277. AND p.STATUS IN ('正式','试用','永续') GROUP BY u.user_id `
  278. _, err = o.Raw(sql).QueryRows(&items)
  279. return
  280. }
  281. // 获取关注了查研观向小助手公众号的所有公司ID
  282. func GetCygxUserRecordCompany() (items []*OpenIdList, err error) {
  283. listUser, err := GetCygxUserRecordCompanyUserId()
  284. if err != nil {
  285. return
  286. }
  287. var userIds []int
  288. for _, v := range listUser {
  289. userIds = append(userIds, v.UserId)
  290. }
  291. lenarr := len(userIds)
  292. if lenarr == 0 {
  293. return
  294. }
  295. var condition string
  296. var pars []interface{}
  297. condition = ` AND w.user_id IN (` + utils.GetOrmInReplace(lenarr) + `)`
  298. pars = append(pars, userIds)
  299. o := orm.NewOrm()
  300. sql := `SELECT
  301. w.company_id
  302. FROM
  303. wx_user AS w
  304. WHERE
  305. 1 = 1` + condition + `GROUP BY w.company_id `
  306. _, err = o.Raw(sql, pars).QueryRows(&items)
  307. return
  308. }
  309. // 获取关注了查研观向小助手公众号的所有用户ID
  310. func GetCygxUserRecordCompanyUserId() (items []*OpenIdList, err error) {
  311. o := orm.NewOrmUsingDB("hz_cygx")
  312. sql := `SELECT
  313. u.open_id,
  314. u.cygx_user_id AS user_id
  315. FROM
  316. cygx_user_record AS u
  317. WHERE
  318. 1 = 1
  319. AND subscribe = 1
  320. GROUP BY
  321. u.cygx_user_id `
  322. _, err = o.Raw(sql).QueryRows(&items)
  323. return
  324. }
  325. type CompanyReportPermissionItem struct {
  326. CompanyId int
  327. ChartPermissionId int
  328. IsUpgrade int `description:"是否升级,1是,0否"`
  329. Remark string `description:"备注,权限名称带有主观客观这种"`
  330. PermissionName string `description:"权限名称"`
  331. }
  332. // GetCompanyReportPermissionItem 获取客户对应的权限
  333. func GetCompanyReportPermissionItem(condition string, pars []interface{}) (items []*CompanyReportPermissionItem, err error) {
  334. o := orm.NewOrm()
  335. sql := `SELECT
  336. r.company_id,
  337. r.chart_permission_id,
  338. r.is_upgrade,
  339. c.remark,
  340. c.permission_name
  341. FROM
  342. company_report_permission AS r
  343. INNER JOIN chart_permission AS c ON r.chart_permission_id = c.chart_permission_id
  344. WHERE
  345. 1 = 1
  346. AND r.product_id = 2
  347. AND r.STATUS = '正式' ` + condition
  348. _, err = o.Raw(sql, pars).QueryRows(&items)
  349. return
  350. }
  351. // 根据手机号获取用户的openid
  352. func GetUserRecordListByMobileArr(bindAccount []string) (items []*OpenIdList, err error) {
  353. o := orm.NewOrmUsingDB("hz_cygx")
  354. lenarr := len(bindAccount)
  355. if lenarr == 0 {
  356. return
  357. }
  358. var condition string
  359. var pars []interface{}
  360. condition = ` AND u.cygx_bind_account IN (` + utils.GetOrmInReplace(lenarr) + `)`
  361. pars = append(pars, bindAccount)
  362. sql := `SELECT
  363. u.open_id,
  364. u.cygx_user_id AS user_id
  365. FROM
  366. cygx_user_record AS u
  367. WHERE
  368. 1 = 1 ` + condition
  369. _, err = o.Raw(sql, pars).QueryRows(&items)
  370. return
  371. }
  372. // 获取单个用户openid
  373. func GetUserRecordOpenidByMobile(bindAccount string) (item *OpenIdList, err error) {
  374. o := orm.NewOrmUsingDB("hz_cygx")
  375. var sql string
  376. sql = `SELECT cr.open_id,cr.cygx_user_id as user_id
  377. FROM cygx_user_record AS cr
  378. WHERE cr.cygx_bind_account = ?`
  379. err = o.Raw(sql, bindAccount).QueryRow(&item)
  380. return
  381. }
  382. // 获取预约活动的用户的openID
  383. func GetActivityOpenIdList(activityId int) (items []*OpenIdList, err error) {
  384. o := orm.NewOrmUsingDB("hz_cygx")
  385. sql := `SELECT DISTINCT cr.open_id,cr.cygx_user_id as user_id
  386. FROM
  387. cygx_my_schedule AS m
  388. INNER JOIN cygx_user_record AS cr ON cr.cygx_bind_account = m.mobile
  389. WHERE m.activity_id = ? `
  390. _, err = o.Raw(sql, activityId).QueryRows(&items)
  391. return
  392. }
  393. // 获取预约活动的用户的openID
  394. func GetActivitySpecialOpenIdList() (items []*OpenIdList, err error) {
  395. o := orm.NewOrmUsingDB("hz_cygx")
  396. sql := `SELECT DISTINCT cr.open_id,cr.cygx_user_id as user_id
  397. FROM
  398. cygx_user_follow_special AS m
  399. INNER JOIN cygx_user_record AS cr ON cr.cygx_bind_account = m.mobile `
  400. _, err = o.Raw(sql).QueryRows(&items)
  401. return
  402. }
  403. // 获取关注作者的用户的openID
  404. func GetFollowDepartmentOpenIdList(departmentId int) (items []*OpenIdList, err error) {
  405. o := orm.NewOrmUsingDB("hz_cygx")
  406. sql := `SELECT DISTINCT cr.open_id,cr.cygx_user_id as user_id
  407. FROM
  408. cygx_article_department_follow AS f
  409. INNER JOIN cygx_user_record AS cr ON cr.cygx_bind_account = f.mobile
  410. WHERE
  411. f.department_id = ? `
  412. _, err = o.Raw(sql, departmentId).QueryRows(&items)
  413. return
  414. }
  415. // 获取关注产业的用户的openID
  416. func GetFollowindustrialOpenIdList(industrialManagementId int) (items []*OpenIdList, err error) {
  417. o := orm.NewOrmUsingDB("hz_cygx")
  418. sql := `SELECT DISTINCT cr.open_id,cr.cygx_user_id as user_id
  419. FROM
  420. cygx_industry_fllow AS f
  421. INNER JOIN cygx_user_record AS cr ON cr.cygx_bind_account = f.mobile
  422. WHERE
  423. f.industrial_management_id = ? `
  424. _, err = o.Raw(sql, industrialManagementId).QueryRows(&items)
  425. return
  426. }
  427. // 获取所有关注了该产业用户的openid
  428. func GetCygxUserIndustryFllowOpneidByActivityIds(activityId int) (items []*OpenIdList, err error) {
  429. o := orm.NewOrm()
  430. sql := `SELECT
  431. cr.open_id,
  432. cr.cygx_bind_account as mobile,
  433. cr.cygx_user_id AS user_id
  434. FROM
  435. cygx_industrial_activity_group_management AS agm
  436. INNER JOIN cygx_industry_fllow AS f ON f.industrial_management_id = agm.industrial_management_id
  437. INNER JOIN cygx_user_record AS cr ON cr.cygx_user_id = f.user_id
  438. WHERE
  439. agm.activity_id = ? `
  440. _, err = o.Raw(sql, activityId).QueryRows(&items)
  441. return
  442. }
  443. // 获取预约活动的用户的openID
  444. func GetMfyxActivityOpenIdList(activityId int) (items []*OpenIdList, err error) {
  445. o := orm.NewOrmUsingDB("hz_cygx")
  446. sql := `SELECT DISTINCT cr.open_id,cr.cygx_user_id as user_id
  447. FROM
  448. cygx_my_schedule AS m
  449. INNER JOIN cygx_mfyx_gzh_user_record AS cr ON cr.cygx_bind_account = m.mobile
  450. WHERE m.activity_id = ? `
  451. _, err = o.Raw(sql, activityId).QueryRows(&items)
  452. return
  453. }
  454. // 根据手机号获取用户的openid
  455. func GetMfyxUserRecordListByMobileArr(bindAccount []string) (items []*OpenIdList, err error) {
  456. o := orm.NewOrmUsingDB("hz_cygx")
  457. lenarr := len(bindAccount)
  458. if lenarr == 0 {
  459. return
  460. }
  461. var condition string
  462. var pars []interface{}
  463. condition = ` AND u.cygx_bind_account IN (` + utils.GetOrmInReplace(lenarr) + `)`
  464. pars = append(pars, bindAccount)
  465. sql := `SELECT
  466. u.open_id,
  467. u.cygx_user_id AS user_id
  468. FROM
  469. cygx_mfyx_gzh_user_record AS u
  470. WHERE
  471. 1 = 1 ` + condition
  472. _, err = o.Raw(sql, pars).QueryRows(&items)
  473. return
  474. }
  475. // 获取单个用户openid
  476. func GetMfyxUserRecordOpenidByMobile(bindAccount string) (item *OpenIdList, err error) {
  477. o := orm.NewOrmUsingDB("hz_cygx")
  478. var sql string
  479. sql = `SELECT cr.open_id,cr.cygx_user_id as user_id
  480. FROM cygx_mfyx_gzh_user_record AS cr
  481. WHERE cr.cygx_bind_account = ?`
  482. err = o.Raw(sql, bindAccount).QueryRow(&item)
  483. return
  484. }