wx_user.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "hongze/hz_crm_api/utils"
  6. "time"
  7. )
  8. type WxUser struct {
  9. UserId int64 `orm:"column(user_id);pk"`
  10. Mobile string
  11. Email string
  12. CompanyId int
  13. RealName string `description:"姓名"`
  14. NickName string `description:"昵称"`
  15. CreatedTime time.Time
  16. MobileTwo string `description:"备用手机号"`
  17. BusinessCardUrl string `description:"名片"`
  18. IsMaker int `description:"是否决策人,1:是,0:否"`
  19. Position string `description:"职位"`
  20. Sex int `description:"普通用户性别,1为男性,2为女性"`
  21. DepartmentName string `description:"联系人部门"`
  22. RegisterTime time.Time
  23. RegisterPlatform int
  24. Remark string `description:"备注"`
  25. CountryCode string `description:"区号,86、852、886等"`
  26. OutboundMobile string `description:"外呼手机号"`
  27. OutboundCountryCode string `description:"外呼手机号区号,86、852、886等"`
  28. LastUpdatedTime time.Time `description:"最近一次更新时间"`
  29. IsDeal int `description:"是否标记处理 0-未处理 1-已处理"`
  30. OpenId string `orm:"column(open_id)" description:"微信openid"`
  31. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  32. UserLabel string `description:"查研观向用户标签"`
  33. MarkGroup string `description:"标记分组"`
  34. FiccViewTotal int `description:"ficc报告的阅读次数"`
  35. FiccLastViewTime string `description:"ficc报告最近一次阅读时间"`
  36. RaiViewTotal int `description:"权益报告的阅读次数"`
  37. RaiLastViewTime string `description:"权益报告的最近一次阅读时间"`
  38. }
  39. func AddWxUser(item *WxUser) (lastId int64, err error) {
  40. o := orm.NewOrm()
  41. lastId, err = o.Insert(item)
  42. return
  43. }
  44. func GetWxUserByMobile(mobile string) (item *WxUser, err error) {
  45. o := orm.NewOrm()
  46. sql := `SELECT * FROM wx_user WHERE mobile = ? LIMIT 1`
  47. err = o.Raw(sql, mobile).QueryRow(&item)
  48. return
  49. }
  50. // GetWxUserByMobileCountryCode 根据手机号和区号获取用户信息
  51. func GetWxUserByMobileCountryCode(mobile, countryCode string) (item *WxUser, err error) {
  52. o := orm.NewOrm()
  53. sql := `SELECT * FROM wx_user WHERE mobile = ? `
  54. sql += ` and country_code in ("","` + countryCode + `") `
  55. sql += ` LIMIT 1 `
  56. err = o.Raw(sql, mobile).QueryRow(&item)
  57. return
  58. }
  59. func GetWxUserByUserId(userId int) (item *WxUser, err error) {
  60. o := orm.NewOrm()
  61. sql := `SELECT * FROM wx_user WHERE user_id=? `
  62. err = o.Raw(sql, userId).QueryRow(&item)
  63. return
  64. }
  65. // 更新wxUser信息
  66. func (wxUser *WxUser) Update(cols []string) (err error) {
  67. o := orm.NewOrm()
  68. _, err = o.Update(wxUser, cols...)
  69. return
  70. }
  71. type PotentialUserItem struct {
  72. UserId int `description:"用户id"`
  73. RealName string `description:"姓名"`
  74. CountryCode string `description:"区号,86、852、886等"`
  75. Mobile string `description:"手机号"`
  76. Email string `description:"邮箱"`
  77. CreatedTime string `description:"注册时间"`
  78. ApplyMethod int `description:"0:未申请,1:已付费客户申请试用,2:非客户申请试用"`
  79. CompanyName string `description:"客户名称"`
  80. ViewTotal int `description:"累计阅读次数"`
  81. LastViewTime time.Time `json:"-" description:"最后一次阅读时间"`
  82. LastViewTimeStr string `description:"最后一次阅读时间"`
  83. FromType string `description:"report:研报,teleconference:电话会"`
  84. BusinessCardUrl string `description:"名片"`
  85. Source int `description:"来源,1:微信端,2:pc网页端,3:查研观向小程序,4:每日咨询,5:电话会"`
  86. IsDeal int `description:"是否标记处理,0是未处理,1是已处理"`
  87. }
  88. type PotentialUserListResp struct {
  89. List []*PotentialUserItem
  90. Paging *paging.PagingItem `description:"分页数据"`
  91. }
  92. // 联系人导入预览数据返回
  93. type ImportListResp struct {
  94. ValidUser []*WxUser `description:"有效客户数据"`
  95. RepeatUser []*WxUser `description:"重复客户数据"`
  96. }
  97. func GetPotentialUserListCount(condition string, pars []interface{}) (count int, err error) {
  98. o := orm.NewOrm()
  99. sql := `SELECT COUNT(1) AS count
  100. FROM wx_user AS a
  101. LEFT JOIN company AS b ON a.company_id = b.company_id
  102. LEFT JOIN user_record AS c ON a.user_id=c.user_id
  103. WHERE b.enabled = 1 AND b.company_id = 1
  104. AND (a.mobile IS NOT NULL || a.email IS NOT NULL)
  105. AND (a.mobile<>'' OR a.email<>'')
  106. AND (c.create_platform<>4 or c.create_platform is null)`
  107. if condition != "" {
  108. sql += condition
  109. }
  110. err = o.Raw(sql, pars).QueryRow(&count)
  111. return
  112. }
  113. func GetPotentialUserList(condition string, pars []interface{}, startSize, pageSize int) (items []*PotentialUserItem, err error) {
  114. o := orm.NewOrm()
  115. sql := `SELECT a.*,a.note AS company_name,
  116. IF(b.type IN (1,2),1,0) AS is_fee_customer,
  117. (SELECT count(1) FROM user_view_history AS uvh WHERE uvh.user_id=a.user_id GROUP BY a.user_id) AS view_total,
  118. a.report_last_view_time AS last_view_time_str,
  119. c.create_platform
  120. FROM wx_user AS a
  121. LEFT JOIN company AS b ON a.company_id = b.company_id
  122. LEFT JOIN user_record AS c ON a.user_id=c.user_id
  123. WHERE b.enabled = 1 AND b.company_id = 1
  124. AND (a.mobile IS NOT NULL || a.email IS NOT NULL)
  125. AND (a.mobile<>'' OR a.email<>'')
  126. AND (c.create_platform<>4 or c.create_platform is null) `
  127. if condition != "" {
  128. sql += condition
  129. }
  130. sql += ` GROUP BY a.user_id `
  131. sql += `ORDER BY a.created_time DESC LIMIT ?,? `
  132. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  133. return
  134. }
  135. type PotentialUserDeleteReq struct {
  136. UserId int `description:"用户id"`
  137. }
  138. // 标记潜在用户请求
  139. type PotentialUserDealReq struct {
  140. UserId int `description:"用户id"`
  141. }
  142. func DeletePotentialUser(userId int) (err error) {
  143. sql := ` DELETE FROM wx_user WHERE user_id=? `
  144. _, err = orm.NewOrm().Raw(sql, userId).Exec()
  145. return
  146. }
  147. // DeleteWxUserByUserIds 根据用户id字符串批量删除用户信息
  148. func DeleteWxUserByUserIds(userIds string) (err error) {
  149. sql := ` DELETE FROM wx_user WHERE user_id in (` + userIds + `) `
  150. _, err = orm.NewOrm().Raw(sql).Exec()
  151. return
  152. }
  153. // 移动潜在客户名下的联系人 请求数据
  154. type PotentialUserMoveReq struct {
  155. UserId int `description:"用户id"`
  156. CompanyId int `description:"移动到的客户id"`
  157. SellerId int `description:"需要移动到的销售id"`
  158. RealName string `description:"姓名"`
  159. Mobile string `description:"手机号"`
  160. Email string `description:"邮箱"`
  161. Remark string `description:"备注信息"`
  162. //crm4.0新增校验字段
  163. Sex int `description:"用户性别,1为男性,2为女性"`
  164. RegionType string `description:"区域,枚举值:国内、海外"`
  165. Position string `description:"职位"`
  166. IsMaker int `description:"是否决策人,1:是,0:否"`
  167. BusinessCardUrl string `description:"名片地址"`
  168. DepartmentName string `description:"联系人部门"`
  169. CountryCode string `description:"区号,86、852、886等"`
  170. }
  171. // 移动联系人请求数据
  172. type UserMoveReq struct {
  173. UserId int `description:"用户id"`
  174. CompanyId int `description:"移动到的客户id"`
  175. SellerId int `description:"需要移动到的销售id"`
  176. }
  177. // FICC申请单-移动潜在客户名下的联系人 请求数据
  178. type ApplyRecordPotentialUserMoveReq struct {
  179. UserId int `description:"用户id"`
  180. CompanyId int `description:"移动到的客户id"`
  181. SellerId int `description:"需要移动到的销售id"`
  182. RealName string `description:"姓名"`
  183. Mobile string `description:"手机号"`
  184. Email string `description:"邮箱"`
  185. Remark string `description:"备注信息"`
  186. ApplyRecordId int `description:"申请单ID"`
  187. Sex int `description:"用户性别,1为男性,2为女性"`
  188. RegionType string `description:"区域,枚举值:国内、海外"`
  189. Position string `description:"职位"`
  190. IsMaker int `description:"是否决策人,1:是,0:否"`
  191. BusinessCardUrl string `description:"名片地址"`
  192. DepartmentName string `description:"联系人部门"`
  193. CountryCode string `description:"区号,86、852、886等"`
  194. }
  195. func CheckUserMobileIsRegister(userId int, mobile string) (count int, err error) {
  196. sql := `SELECT COUNT(1) AS count FROM wx_user AS a
  197. INNER JOIN company AS b ON a.company_id = b.company_id
  198. WHERE a.mobile = ? AND a.user_id<> ? `
  199. o := orm.NewOrm()
  200. err = o.Raw(sql, mobile, userId).QueryRow(&count)
  201. return
  202. }
  203. func CheckUserEmailIsRegister(userId int, email string) (count int, err error) {
  204. sql := `SELECT COUNT(1) AS count FROM wx_user AS a
  205. INNER JOIN company AS b ON a.company_id = b.company_id
  206. WHERE a.email = ? AND a.user_id<> ? `
  207. o := orm.NewOrm()
  208. err = o.Raw(sql, email, userId).QueryRow(&count)
  209. return
  210. }
  211. // 移动潜在客户名下的联系人
  212. func MovePotentialUser(userId, companyId int, realName, mobile, email, remark string) (err error) {
  213. if realName != "" {
  214. sql := ` UPDATE wx_user
  215. SET
  216. company_id = ?,
  217. real_name = ?,
  218. mobile = ?,
  219. email = ?,
  220. remark = ?,
  221. last_updated_time = NOW(),
  222. is_note = 1
  223. WHERE user_id = ? `
  224. _, err = orm.NewOrm().Raw(sql, companyId, realName, mobile, email, remark, userId).Exec()
  225. } else {
  226. sql := ` UPDATE wx_user
  227. SET
  228. company_id = ?,
  229. mobile = ?,
  230. email = ?,
  231. remark = ?,
  232. last_updated_time = NOW(),
  233. is_note = 1
  234. WHERE user_id = ? `
  235. _, err = orm.NewOrm().Raw(sql, companyId, mobile, email, remark, userId).Exec()
  236. }
  237. return
  238. }
  239. // 移动联系人
  240. func MoveUser(userId, companyId int) (err error) {
  241. sql := ` UPDATE wx_user
  242. SET
  243. company_id = ?,
  244. last_updated_time = NOW(),
  245. is_note = 1
  246. WHERE user_id = ? `
  247. _, err = orm.NewOrm().Raw(sql, companyId, userId).Exec()
  248. return
  249. }
  250. type ReportViewRecordTotal struct {
  251. ViewTotal int
  252. LastViewTime time.Time
  253. }
  254. func GetReportViewMaxTime(uid int) (item *ReportViewRecordTotal, err error) {
  255. o := orm.NewOrmUsingDB("rddp")
  256. rddpSql := "SELECT MAX(create_time) AS last_view_time,COUNT(1) AS view_total FROM report_view_record WHERE user_id=? "
  257. err = o.Raw(rddpSql, uid).QueryRow(&item)
  258. return
  259. }
  260. // 根据用户id字符串获取用户的浏览数
  261. func GetReportViewMaxTimeByUserIds(userIds string) (items []*ReportViewRecord, err error) {
  262. o := orm.NewOrmUsingDB("rddp")
  263. rddpSql := `SELECT user_id,MAX(create_time) AS last_view_time,COUNT(1) AS view_total FROM report_view_record WHERE user_id in (` + userIds + `) group by user_id`
  264. _, err = o.Raw(rddpSql).QueryRows(&items)
  265. return
  266. }
  267. // 根据用户手机号字符串获取用户的浏览数
  268. type ReportViewMobileRecord struct {
  269. Mobile string
  270. ViewTotal int
  271. LastViewTime time.Time
  272. }
  273. func GetReportViewMaxTimeByMobiles(mobiles string) (items []*ReportViewMobileRecord, err error) {
  274. o := orm.NewOrmUsingDB("rddp")
  275. rddpSql := `SELECT mobile,MAX(create_time) AS last_view_time,COUNT(1) AS view_total FROM report_view_record WHERE mobile in (` + mobiles + `) group by mobile`
  276. _, err = o.Raw(rddpSql).QueryRows(&items)
  277. return
  278. }
  279. // 根据用户邮箱字符串获取用户的浏览数
  280. type ReportViewEmailRecord struct {
  281. Email string
  282. ViewTotal int
  283. LastViewTime time.Time
  284. }
  285. func GetReportViewMaxTimeByEmails(emails string) (items []*ReportViewEmailRecord, err error) {
  286. o := orm.NewOrmUsingDB("rddp")
  287. rddpSql := `SELECT email,MAX(create_time) AS last_view_time,COUNT(1) AS view_total FROM report_view_record WHERE email in (` + emails + `) group by email`
  288. _, err = o.Raw(rddpSql).QueryRows(&items)
  289. return
  290. }
  291. func GetPotentialUserListExport(condition string, pars []interface{}) (items []*PotentialUserItem, err error) {
  292. o := orm.NewOrm()
  293. sql := `SELECT DISTINCT a.*,b.company_name,
  294. IF(b.type IN (1,2),1,0) AS is_fee_customer,
  295. (SELECT count(1) FROM user_view_history AS uvh WHERE uvh.user_id=a.user_id GROUP BY a.user_id) AS view_total,
  296. (SELECT max(uvh.created_time) FROM user_view_history AS uvh WHERE uvh.user_id=a.user_id GROUP BY a.user_id) AS last_view_time
  297. FROM wx_user AS a
  298. LEFT JOIN company AS b ON a.company_id = b.company_id
  299. INNER JOIN user_record AS c ON a.user_id=c.user_id
  300. WHERE b.enabled = 1 AND b.company_id = 1
  301. AND (a.mobile IS NOT NULL || a.email IS NOT NULL)
  302. AND (a.mobile<>'' OR a.email<>'')
  303. AND c.create_platform<>4 `
  304. if condition != "" {
  305. sql += condition
  306. }
  307. sql += `ORDER BY a.last_updated_time DESC `
  308. _, err = o.Raw(sql, pars).QueryRows(&items)
  309. return
  310. }
  311. type WxUserDetail struct {
  312. UserId int64 `orm:"column(user_id);pk"`
  313. CountryCode string `description:"区号,86、852、886等"`
  314. Mobile string
  315. Email string
  316. CompanyId int
  317. RealName string `description:"姓名"`
  318. CreatedTime time.Time
  319. MobileTwo string `description:"备用手机号"`
  320. BusinessCardUrl string `description:"名片"`
  321. IsMaker int `description:"是否决策人,1:是,0:否"`
  322. Position string `description:"职位"`
  323. Sex int `description:"普通用户性别,1为男性,2为女性"`
  324. CompanyName string `description:"公司名称"`
  325. DepartmentName string `description:"联系人部门"`
  326. SellerRealName string `description:"销售"`
  327. }
  328. // GetWxUserListByUserIds 根据用户ID集合获取用户
  329. func GetWxUserListByUserIds(userIds string) (list []*WxUser, err error) {
  330. o := orm.NewOrm()
  331. sql := ` SELECT * FROM wx_user WHERE user_id IN (` + userIds + `) `
  332. _, err = o.Raw(sql).QueryRows(&list)
  333. return
  334. }
  335. // GetWxUserByCompanyIdAndMobile 根据客户ID及手机号获取用户
  336. func GetWxUserByCompanyIdAndMobile(companyId int, mobile string) (item *WxUser, err error) {
  337. o := orm.NewOrm()
  338. sql := ` SELECT * FROM wx_user WHERE company_id = ? AND mobile = ? LIMIT 1 `
  339. err = o.Raw(sql, companyId, mobile).QueryRow(&item)
  340. return
  341. }
  342. // DeleteWxUserAndRecordByUserId 删除用户及第三方信息
  343. func DeleteWxUserAndRecordByUserId(userId int) (err error) {
  344. o := orm.NewOrm()
  345. to, err := o.Begin()
  346. if err != nil {
  347. return
  348. }
  349. defer func() {
  350. if err != nil {
  351. _ = to.Rollback()
  352. } else {
  353. _ = to.Commit()
  354. }
  355. }()
  356. // 删除wx_user
  357. userSql := ` DELETE FROM wx_user WHERE user_id = ? LIMIT 1 `
  358. _, err = to.Raw(userSql, userId).Exec()
  359. // 删除user_record
  360. if err == nil {
  361. recordSql := ` DELETE FROM user_record WHERE user_id = ? `
  362. _, err = to.Raw(recordSql, userId).Exec()
  363. }
  364. return
  365. }
  366. // 获取这个公司下面所有用户的手机号
  367. func GetCompanyUserMobilesByCompanyId(companyId int) (mobiles string, err error) {
  368. o := orm.NewOrm()
  369. sql := ` SELECT GROUP_CONCAT( DISTINCT a.mobile SEPARATOR ',' ) AS mobiles FROM
  370. wx_user AS a WHERE company_id = ? AND mobile != '' `
  371. err = o.Raw(sql, companyId).QueryRow(&mobiles)
  372. return
  373. }
  374. // 获取这个公司下面所有用户的邮箱号
  375. func GetCompanyUserEmailsByCompanyId(companyId int) (emails string, err error) {
  376. o := orm.NewOrm()
  377. sql := ` SELECT GROUP_CONCAT( DISTINCT a.email SEPARATOR ',' ) AS emails FROM
  378. wx_user AS a WHERE company_id = ? AND email != '' `
  379. err = o.Raw(sql, companyId).QueryRow(&emails)
  380. return
  381. }
  382. // 获取这个公司下面所有用户的手机号
  383. func SetUserSubscribe(openId string) (err error) {
  384. o := orm.NewOrm()
  385. sql := ` UPDATE user_record SET subscribe=1,subscribe_time=NOW() WHERE open_id=? `
  386. _, err = o.Raw(sql, openId).Exec()
  387. return
  388. }
  389. type WxUserItem struct {
  390. UserId int `description:"用户id"`
  391. OpenId string `description:"open_id"`
  392. UnionId string `description:"union_id"`
  393. CompanyId int `description:"客户id"`
  394. NickName string `description:"用户昵称"`
  395. RealName string `description:"用户实际名称"`
  396. Mobile string `description:"手机号码"`
  397. BindAccount string `description:"绑定时的账号"`
  398. Email string `description:"邮箱"`
  399. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  400. ApplyMethod int `description:"0:未申请,1:已付费客户申请试用,2:非客户申请试用"`
  401. FirstLogin int `description:"是否第一次登陆"`
  402. IsFreeLogin int `description:"是否免登陆,true:免登陆,false:非免登陆"`
  403. LoginTime time.Time `description:"登录时间"`
  404. CreatedTime time.Time `description:"创建时间"`
  405. LastUpdatedTime time.Time `description:"最近一次修改时间"`
  406. SessionKey string `description:"微信小程序会话密钥"`
  407. CompanyName string `description:"公司名称"`
  408. IsRegister int `description:"是否注册:1:已注册,0:未注册"`
  409. CountryCode string `description:"手机国家区号"`
  410. OutboundMobile string `description:"外呼手机号"`
  411. OutboundCountryCode string `description:"外呼手机号区号"`
  412. IsMsgOutboundMobile int `description:"是否弹窗过绑定外呼手机号区号"`
  413. IsMaker int `description:"是否是决策人"`
  414. Source int
  415. Position string `description:"职位"`
  416. }
  417. // GetWxUserListByUserIds 根据用户ID集合获取用户有公司名称
  418. func GetWxUserListByUserIdsHaveCompany(userIds string) (list []*WxUserItem, err error) {
  419. o := orm.NewOrm()
  420. sql := ` SELECT u.*, c.company_name FROM wx_user AS u
  421. INNER JOIN company AS c ON c.company_id = u.company_id
  422. WHERE user_id IN (` + userIds + `) `
  423. _, err = o.Raw(sql).QueryRows(&list)
  424. return
  425. }
  426. // GetWxUserByAdminId 通过后台用户ID及微信平台获取对应的微信用户
  427. func GetWxUserByAdminId(platform, adminId int) (item *WxUser, err error) {
  428. o := orm.NewOrm()
  429. sql := `SELECT
  430. wu.user_id
  431. FROM
  432. wx_user AS wu
  433. JOIN user_record AS ur ON wu.user_id = ur.user_id AND ur.create_platform = ?
  434. JOIN admin AS ad ON wu.mobile = ad.mobile
  435. WHERE
  436. ad.admin_id = ?`
  437. err = o.Raw(sql, platform, adminId).QueryRow(&item)
  438. return
  439. }
  440. // GetWxUserByOutboundMobiles 根据用户手机号获取用户详情
  441. func GetWxUserByOutboundMobiles(mobiles []string) (items []*WxUser, err error) {
  442. lenmobiles := len(mobiles)
  443. if lenmobiles == 0 {
  444. return
  445. }
  446. sql := `SELECT* FROM wx_user WHERE outbound_mobile in (` + utils.GetOrmInReplace(lenmobiles) + `) `
  447. o := orm.NewOrm()
  448. _, err = o.Raw(sql, mobiles).QueryRows(&items)
  449. return
  450. }
  451. // GetWxUserByUserIds 根据用户ID获取用户详情
  452. func GetWxUserByUserIds(userIds []int) (items []*WxUser, err error) {
  453. lenuserIds := len(userIds)
  454. if lenuserIds == 0 {
  455. return
  456. }
  457. sql := `SELECT* FROM wx_user WHERE user_id in (` + utils.GetOrmInReplace(lenuserIds) + `) `
  458. o := orm.NewOrm()
  459. _, err = o.Raw(sql, userIds).QueryRows(&items)
  460. return
  461. }
  462. // GetWxUserListByUserMobileHaveCompany 根据用户手机号集合获取用户有公司名称
  463. func GetWxUserListByUserMobileHaveCompany(mobiles []string) (list []*WxUserItem, err error) {
  464. lenmobiles := len(mobiles)
  465. if lenmobiles == 0 {
  466. return
  467. }
  468. o := orm.NewOrm()
  469. sql := ` SELECT u.*, c.company_name FROM wx_user AS u
  470. INNER JOIN company AS c ON c.company_id = u.company_id
  471. WHERE mobile in (` + utils.GetOrmInReplace(lenmobiles) + `) `
  472. _, err = o.Raw(sql, mobiles).QueryRows(&list)
  473. return
  474. }
  475. // GetWxUserListCompanyId 根据公司ID获取所有用户信息
  476. func GetWxUserListCompanyId(companyId int) (list []*WxUserItem, err error) {
  477. o := orm.NewOrm()
  478. sql := ` SELECT* FROM wx_user WHERE company_id = ? `
  479. _, err = o.Raw(sql, companyId).QueryRows(&list)
  480. return
  481. }
  482. type WxUserSller struct {
  483. UserId int `description:"用户id"`
  484. RealName string `description:"姓名"`
  485. Mobile string `description:"手机号"`
  486. CompanyId int `description:"公司id"`
  487. CompanyName string `description:"公司名称"`
  488. SellerName string `description:"所属销售"`
  489. }
  490. // GetWxUserRaiSllerListByUserIds 根据用户id获取权益相关用户信息
  491. func GetWxUserRaiSllerListByUserIds(userIds []int) (list []*WxUserSller, err error) {
  492. lenarr := len(userIds)
  493. if lenarr == 0 {
  494. return
  495. }
  496. o := orm.NewOrm()
  497. sql := ` SELECT
  498. w.user_id,
  499. w.real_name,
  500. w.mobile,
  501. c.company_name,
  502. p.seller_name
  503. FROM
  504. wx_user AS w
  505. INNER JOIN company_product AS p ON p.company_id = w.company_id
  506. INNER JOIN company AS c ON c.company_id = w.company_id
  507. WHERE
  508. 1 = 1
  509. AND p.product_id = 2
  510. AND w.user_id IN (` + utils.GetOrmInReplace(lenarr) + `) `
  511. _, err = o.Raw(sql, userIds).QueryRows(&list)
  512. return
  513. }
  514. // GetWxUserRaiSllerListByUserMobile 根据用户手机号获取权益相关用户信息
  515. func GetWxUserRaiSllerListByUserMobile(mobiles []string) (list []*WxUserSller, err error) {
  516. lenarr := len(mobiles)
  517. if lenarr == 0 {
  518. return
  519. }
  520. o := orm.NewOrm()
  521. sql := ` SELECT
  522. w.user_id,
  523. w.real_name,
  524. w.mobile,
  525. c.company_name,
  526. c.company_id,
  527. p.seller_name
  528. FROM
  529. wx_user AS w
  530. INNER JOIN company_product AS p ON p.company_id = w.company_id
  531. INNER JOIN company AS c ON c.company_id = w.company_id
  532. WHERE
  533. 1 = 1
  534. AND p.product_id = 2
  535. AND w.mobile IN (` + utils.GetOrmInReplace(lenarr) + `) `
  536. _, err = o.Raw(sql, mobiles).QueryRows(&list)
  537. return
  538. }
  539. // GetWxUserByCompanyIds 根据客户ID集合获取用户列表
  540. func GetWxUserByCompanyIds(companyIds []int) (items []*WxUser, err error) {
  541. num := len(companyIds)
  542. if num == 0 {
  543. return
  544. }
  545. sql := `SELECT * FROM wx_user WHERE company_id in (` + utils.GetOrmInReplace(num) + `) `
  546. o := orm.NewOrm()
  547. _, err = o.Raw(sql, companyIds).QueryRows(&items)
  548. return
  549. }
  550. // GetWxUserList 用户列表查询
  551. func GetWxUserList(condition string, pars []interface{}) (items []*WxUser, err error) {
  552. o := orm.NewOrm()
  553. sql := `SELECT *
  554. FROM wx_user WHERE 1 = 1 `
  555. if condition != "" {
  556. sql += condition
  557. }
  558. _, err = o.Raw(sql, pars).QueryRows(&items)
  559. return
  560. }
  561. func SetUserFollow(userId, follow int) (err error) {
  562. o := orm.NewOrm()
  563. sql := ` UPDATE wx_user SET is_follow=? WHERE user_id=? `
  564. _, err = o.Raw(sql, follow, userId).Exec()
  565. return
  566. }
  567. func GetCompanyFollowCount(companyId int) (count int, err error) {
  568. o := orm.NewOrm()
  569. sql := ` SELECT COUNT(1) FROM wx_user WHERE company_id=? AND is_follow = 1 `
  570. err = o.Raw(sql, companyId).QueryRow(&count)
  571. return
  572. }
  573. // 根据用户ID获取相关信息
  574. func GetWxUserItemByUserId(userId int) (item *WxUserItem, err error) {
  575. o := orm.NewOrm()
  576. sql := `SELECT a.*,b.company_name FROM wx_user AS a
  577. LEFT JOIN company AS b on a.company_id=b.company_id
  578. WHERE a.user_id=? `
  579. err = o.Raw(sql, userId).QueryRow(&item)
  580. return
  581. }
  582. // 修改用户外呼号请求
  583. type EditUserOutboundMobileReq struct {
  584. UserId int `description:"用户id"`
  585. OutboundMobile string `description:"外呼手机号"`
  586. OutboundCountryCode string `description:"外呼手机号区号"`
  587. }
  588. // 修改用户外呼手机号
  589. func UpdateUserOutboundMobile(outboundMobile, outboundCountryCode string, userId int) (err error) {
  590. o := orm.NewOrm()
  591. sql := ` UPDATE wx_user SET outbound_mobile=? , outbound_country_code = ? WHERE user_id=? `
  592. _, err = o.Raw(sql, outboundMobile, outboundCountryCode, userId).Exec()
  593. return
  594. }