user.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hongze_cygx/utils"
  7. "time"
  8. )
  9. type UserDetail struct {
  10. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  11. Mobile string `description:"手机号码"`
  12. Email string `description:"邮箱"`
  13. NickName string `description:"用户昵称"`
  14. RealName string `description:"用户实际名称"`
  15. CompanyName string `description:"公司名称"`
  16. PermissionName string `description:"拥有权限分类,多个用英文逗号分隔"`
  17. HasPermission int `description:"1:无该行业权限,不存在权益客户下,2:潜在客户,未提交过申请,3:潜在客户,已提交过申请"`
  18. SellerMobile string `description:"销售手机号"`
  19. SellerName string `description:"销售名称"`
  20. Note string `json:"-" description:"申请提交时,公司名称"`
  21. CountryCode string `description:"区号"`
  22. OutboundMobile string `description:"外呼手机号"`
  23. OutboundCountryCode string `description:"外呼手机号区号"`
  24. ConNum int `description:"收藏"`
  25. HistoryNum int `description:"足迹"`
  26. ScheduleNum int `description:"活动日程"`
  27. }
  28. func GetUserDetailByUserId(userId int) (item *UserDetail, err error) {
  29. o := orm.NewOrm()
  30. sql := `SELECT *,
  31. ( SELECT COUNT( 1 ) AS count FROM cygx_article_collect AS a INNER JOIN cygx_article AS art ON art.article_id = a.article_id WHERE
  32. a.user_id = ? AND art.publish_status = 1 ) AS con_num,
  33. (SELECT COUNT( 1 ) AS count FROM
  34. ( SELECT count(*) FROM cygx_article_history_record AS a WHERE a.user_id = ? GROUP BY a.article_id ) b ) AS history_num ,
  35. ( SELECT COUNT( 1 ) AS count FROM cygx_my_schedule AS a INNER JOIN cygx_activity AS art ON art.activity_id = a.activity_id WHERE
  36. a.user_id = ? AND art.publish_status = 1 AND art.active_state != 3 ) AS schedule_num
  37. FROM wx_user WHERE user_id = ? `
  38. err = o.Raw(sql, userId, userId, userId, userId).QueryRow(&item)
  39. return
  40. }
  41. type UserPermission struct {
  42. CompanyName string `description:"公司名称"`
  43. ChartPermissionName string `description:"权限"`
  44. }
  45. type LoginReq struct {
  46. LoginType int `description:"登录方式:1:微信手机,2:邮箱,3:自定义手机登录"`
  47. Mobile string `description:"手机号"`
  48. Email string `description:"邮箱"`
  49. VCode string `description:"验证码"`
  50. CountryCode string `description:"区号"`
  51. ShareUserCode string `description:"分享人的分享码"`
  52. }
  53. func PcBindMobile(unionId, mobile string, userId, loginType int) (wxUserId int, err error) {
  54. //loginType 登录方式:1:手机,2:邮箱
  55. sql := ``
  56. if loginType == 1 {
  57. sql = `SELECT * FROM wx_user WHERE mobile = ? `
  58. } else {
  59. sql = "SELECT * FROM wx_user WHERE email = ? "
  60. }
  61. user := new(WxUser)
  62. o := orm.NewOrm()
  63. err = o.Raw(sql, mobile).QueryRow(&user)
  64. if err != nil && err.Error() != utils.ErrNoRow() {
  65. return
  66. }
  67. if user == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  68. msql := ``
  69. if loginType == 1 {
  70. msql = "UPDATE wx_user SET mobile = ?,bind_account = ? WHERE union_id = ? "
  71. } else {
  72. msql = "UPDATE wx_user SET email = ?,bind_account = ? WHERE union_id = ? "
  73. }
  74. _, err = o.Raw(msql, mobile, mobile, unionId).Exec()
  75. wxUserId = userId
  76. } else {
  77. if user.UnionId == "" {
  78. sql = `SELECT * FROM wx_user WHERE union_id = ? `
  79. userInfo := new(WxUser)
  80. o := orm.NewOrm()
  81. err = o.Raw(sql, unionId).QueryRow(&userInfo)
  82. if err != nil {
  83. return
  84. }
  85. var maxRegisterTime time.Time
  86. if user.RegisterTime.Before(userInfo.RegisterTime) {
  87. maxRegisterTime = user.RegisterTime
  88. } else {
  89. maxRegisterTime = userInfo.RegisterTime
  90. }
  91. wxUserId = user.UserId
  92. dsql := ` DELETE FROM wx_user WHERE union_id = ? `
  93. _, err = o.Raw(dsql, unionId).Exec()
  94. if err != nil {
  95. return wxUserId, err
  96. }
  97. msql := ` UPDATE wx_user SET union_id=?,register_time=?,province=?,city=?,country=?,headimgurl=?,unionid=?,sex=? WHERE user_id = ? `
  98. _, err = o.Raw(msql, unionId, maxRegisterTime, userInfo.Province, userInfo.City, userInfo.Country, userInfo.Headimgurl, unionId, userInfo.Sex, user.UserId).Exec()
  99. wxUserId = user.UserId
  100. } else {
  101. sql = `SELECT * FROM wx_user WHERE user_id = ? `
  102. userInfo := new(WxUser)
  103. o := orm.NewOrm()
  104. err = o.Raw(sql, userId).QueryRow(&userInfo)
  105. if err != nil && err.Error() != utils.ErrNoRow() {
  106. return
  107. }
  108. if user.UserId != userId {
  109. dsql := ` DELETE FROM wx_user WHERE user_id = ? `
  110. _, err = o.Raw(dsql, userId).Exec()
  111. if err != nil {
  112. return user.UserId, err
  113. }
  114. }
  115. msql := ` UPDATE wx_user SET union_id=?,province=?,city=?,country=?,headimgurl=?,unionid=?,sex=? WHERE user_id = ? `
  116. _, err = o.Raw(msql, unionId, userInfo.Province, userInfo.City, userInfo.Country, userInfo.Headimgurl, unionId, userInfo.Sex, user.UserId).Exec()
  117. wxUserId = userId
  118. }
  119. }
  120. return
  121. }
  122. type LoginResp struct {
  123. UserId int `description:"用户id"`
  124. Authorization string `description:"Token"`
  125. Headimgurl string `description:"用户头像"`
  126. Mobile string `description:"手机号"`
  127. Email string `description:"邮箱"`
  128. CompanyName string `description:"客户名称"`
  129. Status string `description:"状态"`
  130. EndDate string `description:"到期日期"`
  131. ProductName string `description:"客户类型名称"`
  132. IsPotential bool `description:"是否是潜在"`
  133. }
  134. type CheckStatusResp struct {
  135. IsBind bool `description:"true:需要绑定手机号或邮箱,false:不需要绑定手机号或邮箱"`
  136. IsAuth bool `description:"true:需要授权,false:不需要授权"`
  137. PermissionName string `description:"拥有权限分类,多个用英文逗号分隔"`
  138. }
  139. func GetArticleUserCollectCount(userId int) (count int, err error) {
  140. sql := `SELECT COUNT(1) AS count FROM cygx_article_collect AS a INNER JOIN cygx_article as art ON art.article_id = a.article_id WHERE a.user_id=? AND art.publish_status = 1 `
  141. err = orm.NewOrm().Raw(sql, userId).QueryRow(&count)
  142. return
  143. }
  144. func GetArticleUserCollectList(startSize, pageSize, userId int) (items []*ArticleCollectList, err error) {
  145. sql := `SELECT a.* FROM cygx_article_collect AS a INNER JOIN cygx_article as art ON art.article_id = a.article_id
  146. WHERE a.user_id=? AND art.publish_status = 1
  147. ORDER BY a.create_time DESC LIMIT ?,? `
  148. _, err = orm.NewOrm().Raw(sql, userId, startSize, pageSize).QueryRows(&items)
  149. return
  150. }
  151. type ArticleCollectListResp struct {
  152. List []*ArticleCollectList
  153. Paging *paging.PagingItem
  154. }
  155. func GetArticleUserInterviewApplyCount(userId int) (count int, err error) {
  156. sql := `SELECT COUNT(1) AS count FROM cygx_interview_apply AS a WHERE a.user_id=? `
  157. err = orm.NewOrm().Raw(sql, userId).QueryRow(&count)
  158. return
  159. }
  160. func GetArticleUserInterviewApplyList(startSize, pageSize, userId int) (items []*ArticleInterviewApplyList, err error) {
  161. sql := `SELECT a.* FROM cygx_interview_apply AS a
  162. WHERE a.user_id=?
  163. ORDER BY a.status ASC LIMIT ?,? `
  164. _, err = orm.NewOrm().Raw(sql, userId, startSize, pageSize).QueryRows(&items)
  165. return
  166. }
  167. type ArticleInterviewApplyListResp struct {
  168. List []*ArticleInterviewApplyList
  169. Paging *paging.PagingItem
  170. }
  171. func GetArticleUserBrowseHistoryCount(userId int, endDate string) (count int, err error) {
  172. sql := `SELECT COUNT( 1 ) as count
  173. FROM
  174. ( SELECT count(*) FROM cygx_article_history_record AS a WHERE a.user_id = ? AND a.create_time >= ? GROUP BY a.article_id ) b `
  175. err = orm.NewOrm().Raw(sql, userId, endDate).QueryRow(&count)
  176. return
  177. }
  178. func GetArticleUserBrowseHistoryList(startSize, pageSize, userId int, endDate string) (items []*ArticleInterviewApplyList, err error) {
  179. sql := `SELECT a.* FROM cygx_article_history_record AS a
  180. WHERE a.user_id=? AND a.create_time>=? GROUP BY a.article_id
  181. ORDER BY a.id DESC LIMIT ?,? `
  182. _, err = orm.NewOrm().Raw(sql, userId, endDate, startSize, pageSize).QueryRows(&items)
  183. return
  184. }
  185. type ArticleBrowseHistoryListResp struct {
  186. List []*ArticleInterviewApplyList
  187. Paging *paging.PagingItem
  188. }
  189. type ApplyTryReq struct {
  190. BusinessCardUrl string `description:"名片地址"`
  191. RealName string `description:"姓名"`
  192. CompanyName string `description:"公司名称"`
  193. ApplyMethod int `description:"1:已付费客户申请试用,2:非客户申请试用,3:非客户申请试用(ficc下,不需要进行数据校验)"`
  194. TryType string `description:"提交类型,Article:文章、Activity:活动"`
  195. DetailId int `description:"详情ID"`
  196. }
  197. type CountryCode struct {
  198. IsNeedAddCountryCode bool `description:"是否需要填写区号:需要填写,false:不需要填写"`
  199. }
  200. type OutboundMobile struct {
  201. IsNeedAddOutboundMobile bool `description:"是否需要填写外呼手机号:需要填写,false:不需要填写"`
  202. }
  203. type CountryCodeItem struct {
  204. CountryCode string `description:"区号"`
  205. }
  206. func AddCountryCode(CountryCode string, user *WxUserItem) (err error) {
  207. o := orm.NewOrm()
  208. if user.OutboundCountryCode == "" {
  209. sql := `UPDATE wx_user SET country_code=?,outbound_mobile=?,outbound_country_code=? WHERE user_id=? `
  210. _, err = o.Raw(sql, CountryCode, user.OutboundMobile, user.OutboundCountryCode, user.UserId).Exec()
  211. } else {
  212. sql := `UPDATE wx_user SET country_code=? WHERE user_id=? `
  213. _, err = o.Raw(sql, CountryCode, user.UserId).Exec()
  214. }
  215. return
  216. }
  217. //修改外呼手机号
  218. type OutboundMobileItem struct {
  219. OutboundMobile string `description:"外呼手机号"`
  220. OutboundCountryCode string `description:"外呼手机号区号"`
  221. ActivityId int `description:"活动ID"`
  222. }
  223. func AddOutboundMobile(item *OutboundMobileItem, userId int) (err error) {
  224. o, err := orm.NewOrm().Begin()
  225. if err != nil {
  226. return
  227. }
  228. defer func() {
  229. fmt.Println(err)
  230. if err == nil {
  231. o.Commit()
  232. } else {
  233. o.Rollback()
  234. }
  235. }()
  236. sql := `UPDATE wx_user SET outbound_mobile=? ,outbound_country_code = ?, is_msg_outbound_mobile= 1 WHERE user_id=? `
  237. _, err = o.Raw(sql, item.OutboundMobile, item.OutboundCountryCode, userId).Exec()
  238. if err != nil {
  239. return
  240. }
  241. if item.ActivityId > 0 {
  242. sql = `UPDATE cygx_activity_signup SET outbound_mobile=? ,country_code = ? WHERE user_id=? AND activity_id = ?`
  243. _, err = o.Raw(sql, item.OutboundMobile, item.OutboundCountryCode, userId, item.ActivityId).Exec()
  244. }
  245. return
  246. }
  247. //用户绑定手机号时同时绑定外呼手机号
  248. func BindUserOutboundMobile(mobile, countryCode string, userId int) (err error) {
  249. o := orm.NewOrm()
  250. sql := `UPDATE wx_user SET outbound_mobile=? ,outbound_country_code = ?, country_code= ? WHERE user_id=? `
  251. _, err = o.Raw(sql, mobile, countryCode, countryCode, userId).Exec()
  252. return
  253. }
  254. //已经绑定手机号,没有绑定外呼手机号的的用户,预约外呼的时候,将外呼手机号同步成手机号
  255. func BindUserOutboundMobileByMobile(mobile, countryCode string, userId int) (err error) {
  256. o := orm.NewOrm()
  257. sql := `UPDATE wx_user SET outbound_mobile=? ,outbound_country_code = ? WHERE user_id=? `
  258. _, err = o.Raw(sql, mobile, countryCode, userId).Exec()
  259. return
  260. }
  261. //将手机号为11位的用户,区号默认设置为86
  262. func ChangeUserOutboundMobileByMobile(userId int) (err error) {
  263. o := orm.NewOrm()
  264. sql := `UPDATE wx_user SET country_code = 86 WHERE user_id=? `
  265. _, err = o.Raw(sql, userId).Exec()
  266. return
  267. }
  268. type UserWhiteList struct {
  269. Mobile string `description:"手机号码"`
  270. RealName string `description:"用户实际名称"`
  271. CompanyName string `description:"公司名称"`
  272. Permission string `description:"拥有权限分类,多个用英文逗号分隔"`
  273. CountryCode string `description:"区号"`
  274. SellerName string `description:"销售姓名"`
  275. CreatedTime time.Time
  276. Status string `description:"客户状态'试用','永续','冻结','流失','正式','潜在'"`
  277. }
  278. type UserWhiteListRep struct {
  279. List []*UserWhiteList
  280. }
  281. //获取正式试用用户白名单
  282. func GetFormalUserWhiteList(fieldStr, condition string) (items []*UserWhiteList, err error) {
  283. sql := `SELECT ` + fieldStr + `
  284. (SELECT cp.seller_name FROM company_product AS cp WHERE cp.company_id = u.company_id ORDER BY cp.product_id DESC LIMIT 0,1 ) as seller_name,
  285. GROUP_CONCAT( DISTINCT b.chart_permission_name SEPARATOR '/' ) AS permission
  286. FROM wx_user AS u
  287. INNER JOIN company as c ON c.company_id = u.company_id
  288. INNER JOIN company_report_permission AS p ON p.company_id = u.company_id
  289. INNER JOIN chart_permission AS b ON b.chart_permission_id=p.chart_permission_id
  290. INNER JOIN company_product AS cp ON cp.company_id = u.company_id
  291. WHERE 1= 1
  292. AND cp.product_id = 2
  293. AND b.product_id = 2
  294. AND u.company_id >1 ` + condition + `
  295. GROUP BY u.user_id`
  296. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  297. return
  298. }
  299. //获取永续用户白名单
  300. func GetSustainableUserWhiteList(fieldStr, condition string) (items []*UserWhiteList, err error) {
  301. sql := `SELECT ` + fieldStr + `
  302. (SELECT cp.seller_name FROM company_product AS cp WHERE cp.company_id = u.company_id ORDER BY cp.product_id DESC LIMIT 0,1 ) as seller_name,
  303. (SELECT
  304. GROUP_CONCAT( DISTINCT b.chart_permission_name SEPARATOR '/' )
  305. FROM
  306. company_report_permission AS p
  307. INNER JOIN chart_permission AS b ON b.chart_permission_id = p.chart_permission_id
  308. WHERE
  309. p.company_id = u.company_id
  310. AND p.status IN ( '永续' )
  311. AND p.product_id = 2
  312. ) AS permission
  313. FROM wx_user AS u
  314. INNER JOIN company AS c ON c.company_id = u.company_id
  315. INNER JOIN company_report_permission AS p ON p.company_id = u.company_id
  316. INNER JOIN chart_permission AS b ON b.chart_permission_id = p.chart_permission_id
  317. INNER JOIN company_product AS cp ON cp.company_id = u.company_id
  318. WHERE 1 = 1
  319. AND u.company_id > 1 ` + condition + `
  320. GROUP BY u.user_id `
  321. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  322. return
  323. }
  324. //权益用户
  325. type GetSendEmailAllUserWithRAIRep struct {
  326. CompanyName string `description:"客户名称"`
  327. CreditCode string `description:"社会统一信用码"`
  328. ProductName string `description:"客户类型"`
  329. IndustryName string `description:"行业"`
  330. RealName string `description:"姓名"`
  331. Mobile string `description:"手机号"`
  332. Status string `description:"手机号"`
  333. StartDate string `description:"合同开始日期"`
  334. EndDate string `description:"合同结束日期"`
  335. CreatedTime string `description:"创建时间"`
  336. Permission string `description:"权限"`
  337. }
  338. func GetSendEmailAllUserWithRAI() (items []*GetSendEmailAllUserWithRAIRep, err error) {
  339. sql := `SELECT
  340. c.company_name,
  341. c.credit_code,
  342. cp.product_name,
  343. cp.industry_name,
  344. a.real_name,
  345. a.mobile,
  346. cp.status,
  347. cp.start_date,
  348. cp.end_date,
  349. c.created_time,
  350. GROUP_CONCAT( DISTINCT b.chart_permission_name SEPARATOR '/' ) AS permission
  351. FROM
  352. company AS c
  353. INNER JOIN company_report_permission AS p ON p.company_id = c.company_id
  354. INNER JOIN chart_permission AS b ON b.chart_permission_id = p.chart_permission_id
  355. INNER JOIN company_product AS cp ON cp.company_id = c.company_id
  356. INNER JOIN admin AS a ON a.admin_id = cp.seller_id
  357. WHERE
  358. 1 = 1
  359. AND cp.product_id = 2
  360. AND b.product_id = 2
  361. AND cp.status IN ( '正式', '试用' )
  362. GROUP BY
  363. c.company_id
  364. ORDER BY
  365. c.company_id DESC`
  366. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  367. return
  368. }
  369. //权益用户
  370. type GetSendEmailAllUserWithCompanyRep struct {
  371. RealName string `description:"姓名"`
  372. Mobile string `description:"手机号"`
  373. CountryCode string `description:"国家号"`
  374. Email string `description:"邮箱"`
  375. CompanyName string `description:"客户名称"`
  376. CreditCode string `description:"社会统一信用码"`
  377. IsMaker string `description:"是否是决策人,1是,0否"`
  378. }
  379. func GetSendEmailAllUserWithCompany() (items []*GetSendEmailAllUserWithCompanyRep, err error) {
  380. sql := `SELECT
  381. u.real_name,
  382. u.mobile,
  383. u.country_code,
  384. u.email,
  385. c.company_name,
  386. u.is_maker
  387. FROM
  388. no_use AS n
  389. INNER JOIN company AS c ON c.company_name = n.company_name
  390. INNER JOIN company_report_permission AS p ON p.company_id = c.company_id
  391. INNER JOIN chart_permission AS b ON b.chart_permission_id = p.chart_permission_id
  392. INNER JOIN company_product AS cp ON cp.company_id = c.company_id
  393. INNER JOIN wx_user AS u ON u.company_id = c.company_id
  394. GROUP BY
  395. u.user_id
  396. ORDER BY
  397. c.company_id ASC`
  398. _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
  399. return
  400. }
  401. type Headimgurl struct {
  402. Headimgurl string `description:"用户头像"`
  403. }
  404. //更改用户手机号
  405. func UpdateUserHeadimgurl(headimgurl string, userId int) (err error) {
  406. o := orm.NewOrm()
  407. sql := `UPDATE wx_user SET headimgurl = ? WHERE user_id=? `
  408. _, err = o.Raw(sql, headimgurl, userId).Exec()
  409. return
  410. }