user.go 16 KB

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