wx_user.go 23 KB

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