user.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_clpt/utils"
  6. "time"
  7. )
  8. type UserDetail struct {
  9. Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
  10. Mobile string `description:"手机号码"`
  11. Email string `description:"邮箱"`
  12. NickName string `description:"用户昵称"`
  13. RealName string `description:"用户实际名称"`
  14. CompanyName string `description:"公司名称"`
  15. PermissionName string `description:"拥有权限分类,多个用英文逗号分隔"`
  16. HasPermission int `description:"1:无该行业权限,不存在权益客户下,2:潜在客户,未提交过申请,3:潜在客户,已提交过申请"`
  17. SellerMobile string `description:"销售手机号"`
  18. SellerName string `description:"销售名称"`
  19. Note string `json:"-" description:"申请提交时,公司名称"`
  20. CountryCode string `description:"区号"`
  21. OutboundMobile string `description:"外呼手机号"`
  22. OutboundCountryCode string `description:"外呼手机号区号"`
  23. }
  24. func GetUserDetailByUserId(userId int) (item *UserDetail, err error) {
  25. o := orm.NewOrmUsingDB("weekly_report")
  26. sql := `SELECT * FROM wx_user WHERE user_id = ? `
  27. err = o.Raw(sql, userId).QueryRow(&item)
  28. return
  29. }
  30. func GetUserDetailByMobile(mobile string) (item *UserDetail, err error) {
  31. o := orm.NewOrmUsingDB("weekly_report")
  32. sql := `SELECT * FROM wx_user WHERE mobile = ? `
  33. err = o.Raw(sql, mobile).QueryRow(&item)
  34. return
  35. }
  36. type UserPermission struct {
  37. CompanyName string `description:"公司名称"`
  38. ChartPermissionName string `description:"权限"`
  39. }
  40. type LoginReq struct {
  41. Mobile string `description:"手机号"`
  42. VCode string `description:"验证码"`
  43. Token string `description:"微信扫码登录之后返回的Token"`
  44. }
  45. type WxBindMobileReq struct {
  46. Mobile string `description:"手机号"`
  47. VCode string `description:"验证码"`
  48. //Token string `description:"Token"`
  49. }
  50. func PcBindMobile(unionId, mobile string, userId, loginType int) (wxUserId int, err error) {
  51. //loginType 登录方式:1:手机,2:邮箱
  52. sql := ``
  53. if loginType == 1 {
  54. sql = `SELECT * FROM wx_user WHERE mobile = ? `
  55. } else {
  56. sql = "SELECT * FROM wx_user WHERE email = ? "
  57. }
  58. user := new(WxUser)
  59. o := orm.NewOrmUsingDB("weekly_report")
  60. err = o.Raw(sql, mobile).QueryRow(&user)
  61. if err != nil && err.Error() != utils.ErrNoRow() {
  62. return
  63. }
  64. if user == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  65. msql := ``
  66. if loginType == 1 {
  67. msql = "UPDATE wx_user SET mobile = ?,bind_account = ? WHERE union_id = ? "
  68. } else {
  69. msql = "UPDATE wx_user SET email = ?,bind_account = ? WHERE union_id = ? "
  70. }
  71. _, err = o.Raw(msql, mobile, mobile, unionId).Exec()
  72. wxUserId = userId
  73. } else {
  74. if user.UnionId == "" {
  75. sql = `SELECT * FROM wx_user WHERE union_id = ? `
  76. userInfo := new(WxUser)
  77. o := orm.NewOrm()
  78. err = o.Raw(sql, unionId).QueryRow(&userInfo)
  79. if err != nil {
  80. return
  81. }
  82. var maxRegisterTime time.Time
  83. if user.RegisterTime.Before(userInfo.RegisterTime) {
  84. maxRegisterTime = user.RegisterTime
  85. } else {
  86. maxRegisterTime = userInfo.RegisterTime
  87. }
  88. wxUserId = user.UserId
  89. dsql := ` DELETE FROM wx_user WHERE union_id = ? `
  90. _, err = o.Raw(dsql, unionId).Exec()
  91. if err != nil {
  92. return wxUserId, err
  93. }
  94. msql := ` UPDATE wx_user SET union_id=?,register_time=?,province=?,city=?,country=?,headimgurl=?,unionid=?,sex=? WHERE user_id = ? `
  95. _, err = o.Raw(msql, unionId, maxRegisterTime, userInfo.Province, userInfo.City, userInfo.Country, userInfo.Headimgurl, unionId, userInfo.Sex, user.UserId).Exec()
  96. wxUserId = user.UserId
  97. } else {
  98. sql = `SELECT * FROM wx_user WHERE user_id = ? `
  99. userInfo := new(WxUser)
  100. o := orm.NewOrm()
  101. err = o.Raw(sql, userId).QueryRow(&userInfo)
  102. if err != nil && err.Error() != utils.ErrNoRow() {
  103. return
  104. }
  105. if user.UserId != userId {
  106. dsql := ` DELETE FROM wx_user WHERE user_id = ? `
  107. _, err = o.Raw(dsql, userId).Exec()
  108. if err != nil {
  109. return user.UserId, err
  110. }
  111. }
  112. msql := ` UPDATE wx_user SET union_id=?,province=?,city=?,country=?,headimgurl=?,unionid=?,sex=? WHERE user_id = ? `
  113. _, err = o.Raw(msql, unionId, userInfo.Province, userInfo.City, userInfo.Country, userInfo.Headimgurl, unionId, userInfo.Sex, user.UserId).Exec()
  114. wxUserId = userId
  115. }
  116. }
  117. return
  118. }
  119. type LoginResp struct {
  120. UserId int `description:"用户id"`
  121. Authorization string `description:"Token"`
  122. Headimgurl string `description:"用户头像"`
  123. Mobile string `description:"手机号"`
  124. Email string `description:"邮箱"`
  125. CompanyName string `description:"客户名称"`
  126. }
  127. type UserDetailResp struct {
  128. UserId int `description:"用户id"`
  129. UserName string `description:"用户名称"`
  130. Headimgurl string `description:"用户头像"`
  131. Mobile string `description:"手机号"`
  132. Email string `description:"邮箱"`
  133. CompanyName string `description:"客户名称"`
  134. OutboundMobile string `description:"外呼手机号"`
  135. OutboundCountryCode string `description:"外呼手机号区号"`
  136. PermissionName []string `description:"拥有权限分类"`
  137. }
  138. type CheckStatusResp struct {
  139. IsBind bool `description:"true:需要绑定手机号或邮箱,false:不需要绑定手机号或邮箱"`
  140. IsAuth bool `description:"true:需要授权,false:不需要授权"`
  141. PermissionName string `description:"拥有权限分类,多个用英文逗号分隔"`
  142. }
  143. type ApplyTryReq struct {
  144. BusinessCardUrl string `description:"名片地址"`
  145. RealName string `description:"姓名"`
  146. CompanyName string `description:"公司名称"`
  147. ApplyMethod int `description:"1:已付费客户申请试用,2:非客户申请试用,3:非客户申请试用(ficc下,不需要进行数据校验)"`
  148. TryType string `description:"提交类型,Article:文章、Activity:活动、MicroAudio:微路演音频、MicroVideo:微路演视频、Researchsummary:本周研究汇总、Minutessummary:上周纪要汇总、ReportSelection:报告精选、ProductInterior:产品内测"`
  149. DetailId int `description:"详情ID"`
  150. }
  151. type CountryCode struct {
  152. IsNeedAddCountryCode bool `description:"是否需要填写区号:需要填写,false:不需要填写"`
  153. }
  154. type OutboundMobile struct {
  155. IsNeedAddOutboundMobile bool `description:"是否需要填写外呼手机号:需要填写,false:不需要填写"`
  156. }
  157. type CountryCodeItem struct {
  158. CountryCode string `description:"区号"`
  159. }
  160. // 修改外呼手机号
  161. type OutboundMobileItem struct {
  162. OutboundMobile string `description:"外呼手机号"`
  163. OutboundCountryCode string `description:"外呼手机号区号"`
  164. }
  165. type UserWhiteList struct {
  166. Mobile string `description:"手机号码"`
  167. RealName string `description:"用户实际名称"`
  168. CompanyName string `description:"公司名称"`
  169. Permission string `description:"拥有权限分类,多个用英文逗号分隔"`
  170. CountryCode string `description:"区号"`
  171. SellerName string `description:"销售姓名"`
  172. CreatedTime time.Time
  173. Status string `description:"客户状态'试用','永续','冻结','流失','正式','潜在'"`
  174. }
  175. type UserWhiteListRep struct {
  176. List []*UserWhiteList
  177. }
  178. type UserDetailByUserLogin struct {
  179. Headimgurl string `description:"头像"`
  180. Mobile string `description:"手机号码"`
  181. RealName string `description:"用户实际名称"`
  182. CompanyName string `description:"公司名称"`
  183. Permission string `description:"拥有权限分类,多个用英文逗号分隔"`
  184. HasPermission int `description:"1:有该行业权限,正常展示,2:无权限,非潜在客户,3:潜在客户"`
  185. Token string `description:"Token"`
  186. }
  187. func GetCompanyPermission(companyId int) (permission string, err error) {
  188. sql := ` SELECT GROUP_CONCAT(DISTINCT b.remark ORDER BY b.sort ASC SEPARATOR ',') AS permission
  189. FROM company_report_permission AS a
  190. INNER JOIN chart_permission AS b ON a.chart_permission_id=b.chart_permission_id
  191. INNER JOIN company_product AS c ON a.company_id=c.company_id AND a.product_id=c.product_id
  192. WHERE a.company_id=?
  193. AND c.is_suspend=0
  194. AND b.cygx_auth=1
  195. AND c.status IN('正式','试用','永续')
  196. AND a.status IN('正式','试用','永续') `
  197. o := orm.NewOrmUsingDB("weekly_report")
  198. err = o.Raw(sql, companyId).QueryRow(&permission)
  199. return
  200. }
  201. func GetCompanyPermissionName(companyId int) (permission string, err error) {
  202. sql := ` SELECT GROUP_CONCAT(DISTINCT b.chart_permission_name ORDER BY b.sort ASC SEPARATOR ',') AS permission
  203. FROM company_report_permission AS a
  204. INNER JOIN chart_permission AS b ON a.chart_permission_id=b.chart_permission_id
  205. INNER JOIN company_product AS c ON a.company_id=c.company_id AND a.product_id=c.product_id
  206. WHERE a.company_id=?
  207. AND c.is_suspend=0
  208. AND b.cygx_auth=1
  209. AND c.status IN('正式','试用','永续')
  210. AND a.status IN('正式','试用','永续') `
  211. o := orm.NewOrmUsingDB("weekly_report")
  212. err = o.Raw(sql, companyId).QueryRow(&permission)
  213. return
  214. }
  215. func GetCompanyPermissionId(companyId int) (permissionId string, err error) {
  216. sql := ` SELECT GROUP_CONCAT(DISTINCT b.chart_permission_id ORDER BY b.sort ASC SEPARATOR ',') AS permissionId
  217. FROM company_report_permission AS a
  218. INNER JOIN chart_permission AS b ON a.chart_permission_id=b.chart_permission_id
  219. INNER JOIN company_product AS c ON a.company_id=c.company_id AND a.product_id=c.product_id
  220. WHERE a.company_id=?
  221. AND c.is_suspend=0
  222. AND b.cygx_auth=1
  223. AND c.status IN('正式','试用','永续')
  224. AND a.status IN('正式','试用','永续') `
  225. o := orm.NewOrmUsingDB("weekly_report")
  226. err = o.Raw(sql, companyId).QueryRow(&permissionId)
  227. return
  228. }
  229. // 用户绑定手机号时同时绑定外呼手机号
  230. func BindUserOutboundMobile(mobile, countryCode string, userId int) (err error) {
  231. o := orm.NewOrmUsingDB("weekly_report")
  232. sql := `UPDATE wx_user SET outbound_mobile=? ,outbound_country_code = ?, country_code= ? WHERE user_id=? `
  233. _, err = o.Raw(sql, mobile, countryCode, countryCode, userId).Exec()
  234. return
  235. }
  236. type UserPermissionAuthInfo struct {
  237. HasPermission int `description:"是否有权限:1-有权限; 2-无权限; 3-潜在客户未提交申请; 4-潜在客户已提交申请 5-仅有FICC权限"`
  238. SellerMobile string `description:"销售手机号"`
  239. SellerName string `description:"销售名称"`
  240. OperationMode string `description:"操作方式:Apply-立即申请; Call-拨号"`
  241. PopupMsg string `description:"权限弹窗信息"`
  242. }
  243. // 修改用户是否绑定外呼手机号弹窗
  244. func ModifyWxUserIsMsgOutboundMobile(userId int) (err error) {
  245. o := orm.NewOrmUsingDB("weekly_report")
  246. sql := `UPDATE wx_user SET is_msg_outbound_mobile=1 WHERE user_id=? `
  247. _, err = o.Raw(sql, userId).Exec()
  248. return
  249. }
  250. // 已经绑定手机号,没有绑定外呼手机号的的用户,预约外呼的时候,将外呼手机号同步成手机号
  251. func BindUserOutboundMobileByMobile(mobile, countryCode string, userId int) (err error) {
  252. o := orm.NewOrmUsingDB("weekly_report")
  253. sql := `UPDATE wx_user SET outbound_mobile=? ,outbound_country_code = ? WHERE user_id=? `
  254. _, err = o.Raw(sql, mobile, countryCode, userId).Exec()
  255. return
  256. }
  257. // GetWxUserByMobiles 根据用户手机号获取用户详情
  258. func GetWxUserByMobiles(mobiles []string) (items []*WxUser, err error) {
  259. lenmobiles := len(mobiles)
  260. if lenmobiles == 0 {
  261. return
  262. }
  263. sql := `SELECT* FROM wx_user WHERE mobile in (` + utils.GetOrmInReplace(lenmobiles) + `) `
  264. o := orm.NewOrmUsingDB("weekly_report")
  265. _, err = o.Raw(sql, mobiles).QueryRows(&items)
  266. return
  267. }
  268. func GetUserCompanyPermissionSand(companyId int) (items []*ChartPermissionResp, err error) {
  269. sql := ` SELECT b.*
  270. FROM company_report_permission AS a
  271. INNER JOIN chart_permission AS b ON a.chart_permission_id=b.chart_permission_id
  272. INNER JOIN company_product AS c ON a.company_id=c.company_id AND a.product_id=c.product_id
  273. WHERE a.company_id=?
  274. AND a.product_id=2
  275. AND c.is_suspend=0
  276. AND b.cygx_auth=1
  277. AND b.is_other=0
  278. AND c.status IN('正式','试用','永续')
  279. AND a.status IN('正式','试用','永续')
  280. AND b.chart_permission_name != '专家'
  281. GROUP BY b.chart_permission_name
  282. ORDER BY b.sort ASC `
  283. o := orm.NewOrmUsingDB("weekly_report")
  284. _, err = o.Raw(sql, companyId).QueryRows(&items)
  285. return
  286. }
  287. func AddOutboundMobile(item *OutboundMobileItem, userId int) (err error) {
  288. o, err := orm.NewOrmUsingDB("weekly_report").Begin()
  289. if err != nil {
  290. return
  291. }
  292. defer func() {
  293. fmt.Println(err)
  294. if err == nil {
  295. o.Commit()
  296. } else {
  297. o.Rollback()
  298. }
  299. }()
  300. sql := `UPDATE wx_user SET outbound_mobile=? ,outbound_country_code = ?, is_msg_outbound_mobile= 1 WHERE user_id=? `
  301. _, err = o.Raw(sql, item.OutboundMobile, item.OutboundCountryCode, userId).Exec()
  302. if err != nil {
  303. return
  304. }
  305. //if item.ActivityId > 0 {
  306. // sql = `UPDATE cygx_activity_signup SET outbound_mobile=? ,country_code = ? WHERE user_id=? AND activity_id = ?`
  307. // _, err = o.Raw(sql, item.OutboundMobile, item.OutboundCountryCode, userId, item.ActivityId).Exec()
  308. //}
  309. return
  310. }
  311. type Headimgurl struct {
  312. Headimgurl string `description:"用户头像"`
  313. }
  314. // 更改用户头像
  315. func UpdateUserHeadimgurl(headimgurl string, userId int) (err error) {
  316. o := orm.NewOrmUsingDB("weekly_report")
  317. sql := `UPDATE wx_user SET headimgurl = ? WHERE user_id=? `
  318. _, err = o.Raw(sql, headimgurl, userId).Exec()
  319. return
  320. }
  321. // 更改用户邮箱号
  322. func UpdateUserEmail(email string, userId int) (err error) {
  323. o := orm.NewOrmUsingDB("weekly_report")
  324. sql := `UPDATE wx_user SET email = ? WHERE user_id=? `
  325. _, err = o.Raw(sql, email, userId).Exec()
  326. return
  327. }
  328. func GetArticleUserCollectCount(userId int) (count int, err error) {
  329. sql := `SELECT SUM(count) AS count FROM (
  330. SELECT COUNT(1) AS count FROM cygx_article_collect AS a
  331. INNER JOIN cygx_article as art ON art.article_id = a.article_id
  332. WHERE a.user_id=? AND art.publish_status = 1
  333. UNION ALL
  334. SELECT COUNT(1) AS count FROM cygx_yanxuan_special_collect AS a
  335. INNER JOIN cygx_yanxuan_special as b ON b.id = a.yanxuan_special_id
  336. WHERE a.user_id=? AND b.status = 3) AS c
  337. `
  338. err = orm.NewOrm().Raw(sql, userId, userId).QueryRow(&count)
  339. return
  340. }
  341. func GetArticleUserCollectList(startSize, pageSize, userId int) (items []*ArticleReportBillboardResp, err error) {
  342. sql := `SELECT
  343. a.article_id,
  344. art.category_id,
  345. '' AS title,
  346. '' AS publish_date,
  347. '' AS nick_name,
  348. 0 AS article_type_id,
  349. '' AS article_type_name,
  350. 0 AS is_special_int,
  351. '' AS special_tags,
  352. 0 AS pv,
  353. 0 AS collect_num,
  354. 0 AS my_collect_num,
  355. 0 AS special_type,
  356. a.user_id AS user_id,
  357. '' AS company_tag_str,
  358. '' AS industry_tag_str,
  359. 0 AS special_column_id,
  360. a.create_time AS create_time
  361. FROM cygx_article_collect AS a
  362. INNER JOIN cygx_article as art ON art.article_id = a.article_id
  363. WHERE a.user_id=?
  364. UNION ALL
  365. SELECT
  366. a.id AS article_id,
  367. 0 AS category_id,
  368. a.title AS title,
  369. date_format( a.publish_time, '%Y-%m-%d' ) AS publish_date,
  370. b.nick_name AS nick_name,
  371. -1 AS article_type_id,
  372. '' AS article_type_name,
  373. 1 AS is_special_int,
  374. a.tags AS special_tags,
  375. ( SELECT count( 1 ) FROM cygx_yanxuan_special_record AS h WHERE h.yanxuan_special_id = a.id ) AS pv,
  376. ( SELECT count( 1 ) FROM cygx_yanxuan_special_collect AS ac WHERE ac.yanxuan_special_id = a.id ) AS collect_num,
  377. ( SELECT count( 1 ) FROM cygx_yanxuan_special_collect AS ac WHERE ac.yanxuan_special_id = a.id AND user_id = ? ) AS my_collect_num,
  378. a.type AS special_type,
  379. a.user_id AS user_id,
  380. a.company_tags AS company_tag_str,
  381. a.industry_tags AS industry_tags_str,
  382. b.id AS special_column_id,
  383. c.create_time AS create_time
  384. FROM
  385. cygx_yanxuan_special AS a
  386. JOIN cygx_yanxuan_special_author AS b ON a.user_id = b.user_id
  387. JOIN cygx_yanxuan_special_collect AS c ON a.id = c.yanxuan_special_id
  388. WHERE
  389. 1 = 1 AND a.status = 3 AND c.user_id=?
  390. ORDER BY create_time DESC
  391. LIMIT ?,? `
  392. _, err = orm.NewOrm().Raw(sql, userId, userId,userId, startSize, pageSize).QueryRows(&items)
  393. return
  394. }
  395. func GetArticleUserBrowseHistoryCount(userId int, endDate string) (count int, err error) {
  396. sql := `SELECT COUNT( 1 ) as count
  397. FROM
  398. ( SELECT count(*) FROM cygx_article_history_record AS a WHERE a.user_id = ? AND a.create_time >= ? GROUP BY a.article_id ) b `
  399. err = orm.NewOrm().Raw(sql, userId, endDate).QueryRow(&count)
  400. return
  401. }
  402. func GetArticleUserBrowseHistoryList(startSize, pageSize, userId int, endDate string) (items []*ArticleReportBillboardResp, err error) {
  403. sql := `SELECT a.* , MAX( a.id ) AS mid FROM cygx_article_history_record AS a
  404. WHERE a.user_id=? AND a.create_time>=? GROUP BY a.article_id
  405. ORDER BY mid DESC LIMIT ?,? `
  406. _, err = orm.NewOrm().Raw(sql, userId, endDate, startSize, pageSize).QueryRows(&items)
  407. return
  408. }
  409. func GetUserMicroRoadshowCollectList(userId int) (items []*CygxArticleCollect, err error) {
  410. sql := `SELECT a.* FROM cygx_article_collect AS a
  411. WHERE a.user_id=?
  412. AND a.article_id =0
  413. ORDER BY a.create_time DESC `
  414. _, err = orm.NewOrm().Raw(sql, userId).QueryRows(&items)
  415. return
  416. }