cygx_user_record.go 16 KB

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