user.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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.NewOrm()
  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.NewOrm()
  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.NewOrm()
  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:活动"`
  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.chart_permission_name 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.NewOrm()
  198. err = o.Raw(sql, companyId).QueryRow(&permission)
  199. return
  200. }
  201. func GetCompanyPermissionId(companyId int) (permissionId string, err error) {
  202. sql := ` SELECT GROUP_CONCAT(DISTINCT b.chart_permission_id ORDER BY b.sort ASC SEPARATOR ',') AS permissionId
  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.NewOrm()
  212. err = o.Raw(sql, companyId).QueryRow(&permissionId)
  213. return
  214. }
  215. //用户绑定手机号时同时绑定外呼手机号
  216. func BindUserOutboundMobile(mobile, countryCode string, userId int) (err error) {
  217. o := orm.NewOrm()
  218. sql := `UPDATE wx_user SET outbound_mobile=? ,outbound_country_code = ?, country_code= ? WHERE user_id=? `
  219. _, err = o.Raw(sql, mobile, countryCode, countryCode, userId).Exec()
  220. return
  221. }
  222. type UserPermissionAuthInfo struct {
  223. HasPermission int `description:"是否有权限:1-有权限; 2-无权限; 3-潜在客户未提交申请; 4-潜在客户已提交申请 5-仅有FICC权限"`
  224. SellerMobile string `description:"销售手机号"`
  225. SellerName string `description:"销售名称"`
  226. OperationMode string `description:"操作方式:Apply-立即申请; Call-拨号"`
  227. PopupMsg string `description:"权限弹窗信息"`
  228. }
  229. //修改用户是否绑定外呼手机号弹窗
  230. func ModifyWxUserIsMsgOutboundMobile(userId int) (err error) {
  231. o := orm.NewOrm()
  232. sql := `UPDATE wx_user SET is_msg_outbound_mobile=1 WHERE user_id=? `
  233. _, err = o.Raw(sql, userId).Exec()
  234. return
  235. }
  236. //已经绑定手机号,没有绑定外呼手机号的的用户,预约外呼的时候,将外呼手机号同步成手机号
  237. func BindUserOutboundMobileByMobile(mobile, countryCode string, userId int) (err error) {
  238. o := orm.NewOrm()
  239. sql := `UPDATE wx_user SET outbound_mobile=? ,outbound_country_code = ? WHERE user_id=? `
  240. _, err = o.Raw(sql, mobile, countryCode, userId).Exec()
  241. return
  242. }
  243. // GetWxUserByMobiles 根据用户手机号获取用户详情
  244. func GetWxUserByMobiles(mobiles []string) (items []*WxUser, err error) {
  245. lenmobiles := len(mobiles)
  246. if lenmobiles == 0 {
  247. return
  248. }
  249. sql := `SELECT* FROM wx_user WHERE mobile in (` + utils.GetOrmInReplace(lenmobiles) + `) `
  250. o := orm.NewOrm()
  251. _, err = o.Raw(sql, mobiles).QueryRows(&items)
  252. return
  253. }
  254. func GetUserCompanyPermissionSand(companyId int) (items []*ChartPermissionResp, err error) {
  255. sql := ` SELECT b.*
  256. FROM company_report_permission AS a
  257. INNER JOIN chart_permission AS b ON a.chart_permission_id=b.chart_permission_id
  258. INNER JOIN company_product AS c ON a.company_id=c.company_id AND a.product_id=c.product_id
  259. WHERE a.company_id=?
  260. AND a.product_id=2
  261. AND c.is_suspend=0
  262. AND b.cygx_auth=1
  263. AND b.is_other=0
  264. AND c.status IN('正式','试用','永续')
  265. AND a.status IN('正式','试用','永续')
  266. AND b.chart_permission_name != '专家'
  267. GROUP BY b.chart_permission_name
  268. ORDER BY b.sort ASC `
  269. o := orm.NewOrm()
  270. _, err = o.Raw(sql, companyId).QueryRows(&items)
  271. return
  272. }
  273. func AddOutboundMobile(item *OutboundMobileItem, userId int) (err error) {
  274. o, err := orm.NewOrm().Begin()
  275. if err != nil {
  276. return
  277. }
  278. defer func() {
  279. fmt.Println(err)
  280. if err == nil {
  281. o.Commit()
  282. } else {
  283. o.Rollback()
  284. }
  285. }()
  286. sql := `UPDATE wx_user SET outbound_mobile=? ,outbound_country_code = ?, is_msg_outbound_mobile= 1 WHERE user_id=? `
  287. _, err = o.Raw(sql, item.OutboundMobile, item.OutboundCountryCode, userId).Exec()
  288. if err != nil {
  289. return
  290. }
  291. //if item.ActivityId > 0 {
  292. // sql = `UPDATE cygx_activity_signup SET outbound_mobile=? ,country_code = ? WHERE user_id=? AND activity_id = ?`
  293. // _, err = o.Raw(sql, item.OutboundMobile, item.OutboundCountryCode, userId, item.ActivityId).Exec()
  294. //}
  295. return
  296. }
  297. type Headimgurl struct {
  298. Headimgurl string `description:"用户头像"`
  299. }
  300. //更改用户头像
  301. func UpdateUserHeadimgurl(headimgurl string, userId int) (err error) {
  302. o := orm.NewOrm()
  303. sql := `UPDATE wx_user SET headimgurl = ? WHERE user_id=? `
  304. _, err = o.Raw(sql, headimgurl, userId).Exec()
  305. return
  306. }
  307. func GetArticleUserCollectCount(userId int) (count int, err error) {
  308. 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 `
  309. err = orm.NewOrm().Raw(sql, userId).QueryRow(&count)
  310. return
  311. }
  312. func GetArticleUserCollectList(startSize, pageSize, userId int) (items []*ArticleReportBillboardResp, err error) {
  313. sql := `SELECT a.* FROM cygx_article_collect AS a INNER JOIN cygx_article as art ON art.article_id = a.article_id
  314. WHERE a.user_id=?
  315. ORDER BY a.create_time DESC LIMIT ?,? `
  316. _, err = orm.NewOrm().Raw(sql, userId, startSize, pageSize).QueryRows(&items)
  317. return
  318. }
  319. func GetArticleUserBrowseHistoryCount(userId int, endDate string) (count int, err error) {
  320. sql := `SELECT COUNT( 1 ) as count
  321. FROM
  322. ( SELECT count(*) FROM cygx_article_history_record AS a WHERE a.user_id = ? AND a.create_time >= ? GROUP BY a.article_id ) b `
  323. err = orm.NewOrm().Raw(sql, userId, endDate).QueryRow(&count)
  324. return
  325. }
  326. func GetArticleUserBrowseHistoryList(startSize, pageSize, userId int, endDate string) (items []*ArticleReportBillboardResp, err error) {
  327. sql := `SELECT a.* , MAX( a.id ) AS mid FROM cygx_article_history_record AS a
  328. WHERE a.user_id=? AND a.create_time>=? GROUP BY a.article_id
  329. ORDER BY mid DESC LIMIT ?,? `
  330. _, err = orm.NewOrm().Raw(sql, userId, endDate, startSize, pageSize).QueryRows(&items)
  331. return
  332. }
  333. func GetUserMicroRoadshowCollectList(userId int) (items []*CygxArticleCollect, err error) {
  334. sql := `SELECT a.* FROM cygx_article_collect AS a
  335. WHERE a.user_id=?
  336. AND a.article_id =0
  337. ORDER BY a.create_time DESC `
  338. _, err = orm.NewOrm().Raw(sql, userId).QueryRows(&items)
  339. return
  340. }