user.go 16 KB

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