123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- package models
- import (
- "fmt"
- "github.com/rdlucklib/rdluck_tools/orm"
- "github.com/rdlucklib/rdluck_tools/paging"
- "hongze/hongze_cygx/utils"
- "time"
- )
- type UserDetail struct {
- Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
- Mobile string `description:"手机号码"`
- Email string `description:"邮箱"`
- NickName string `description:"用户昵称"`
- RealName string `description:"用户实际名称"`
- CompanyName string `description:"公司名称"`
- PermissionName string `description:"拥有权限分类,多个用英文逗号分隔"`
- HasPermission int `description:"1:无该行业权限,不存在权益客户下,2:潜在客户,未提交过申请,3:潜在客户,已提交过申请"`
- SellerMobile string `description:"销售手机号"`
- SellerName string `description:"销售名称"`
- Note string `json:"-" description:"申请提交时,公司名称"`
- CountryCode string `description:"区号"`
- OutboundMobile string `description:"外呼手机号"`
- OutboundCountryCode string `description:"外呼手机号区号"`
- }
- func GetUserDetailByUserId(userId int) (item *UserDetail, err error) {
- o := orm.NewOrm()
- sql := `SELECT * FROM wx_user WHERE user_id = ? `
- err = o.Raw(sql, userId).QueryRow(&item)
- return
- }
- type UserPermission struct {
- CompanyName string `description:"公司名称"`
- ChartPermissionName string `description:"权限"`
- }
- type LoginReq struct {
- LoginType int `description:"登录方式:1:微信手机,2:邮箱,3:自定义手机登录"`
- Mobile string `description:"手机号"`
- Email string `description:"邮箱"`
- VCode string `description:"验证码"`
- CountryCode string `description:"区号"`
- }
- func PcBindMobile(unionId, mobile string, userId, loginType int) (wxUserId int, err error) {
- //loginType 登录方式:1:手机,2:邮箱
- sql := ``
- if loginType == 1 {
- sql = `SELECT * FROM wx_user WHERE mobile = ? `
- } else {
- sql = "SELECT * FROM wx_user WHERE email = ? "
- }
- user := new(WxUser)
- o := orm.NewOrm()
- err = o.Raw(sql, mobile).QueryRow(&user)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- if user == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
- msql := ``
- if loginType == 1 {
- msql = "UPDATE wx_user SET mobile = ?,bind_account = ? WHERE union_id = ? "
- } else {
- msql = "UPDATE wx_user SET email = ?,bind_account = ? WHERE union_id = ? "
- }
- _, err = o.Raw(msql, mobile, mobile, unionId).Exec()
- wxUserId = userId
- } else {
- if user.UnionId == "" {
- sql = `SELECT * FROM wx_user WHERE union_id = ? `
- userInfo := new(WxUser)
- o := orm.NewOrm()
- err = o.Raw(sql, unionId).QueryRow(&userInfo)
- if err != nil {
- return
- }
- var maxRegisterTime time.Time
- if user.RegisterTime.Before(userInfo.RegisterTime) {
- maxRegisterTime = user.RegisterTime
- } else {
- maxRegisterTime = userInfo.RegisterTime
- }
- wxUserId = user.UserId
- dsql := ` DELETE FROM wx_user WHERE union_id = ? `
- _, err = o.Raw(dsql, unionId).Exec()
- if err != nil {
- return wxUserId, err
- }
- msql := ` UPDATE wx_user SET union_id=?,register_time=?,province=?,city=?,country=?,headimgurl=?,unionid=?,sex=? WHERE user_id = ? `
- _, err = o.Raw(msql, unionId, maxRegisterTime, userInfo.Province, userInfo.City, userInfo.Country, userInfo.Headimgurl, unionId, userInfo.Sex, user.UserId).Exec()
- wxUserId = user.UserId
- } else {
- sql = `SELECT * FROM wx_user WHERE user_id = ? `
- userInfo := new(WxUser)
- o := orm.NewOrm()
- err = o.Raw(sql, userId).QueryRow(&userInfo)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- if user.UserId != userId {
- dsql := ` DELETE FROM wx_user WHERE user_id = ? `
- _, err = o.Raw(dsql, userId).Exec()
- if err != nil {
- return user.UserId, err
- }
- }
- msql := ` UPDATE wx_user SET union_id=?,province=?,city=?,country=?,headimgurl=?,unionid=?,sex=? WHERE user_id = ? `
- _, err = o.Raw(msql, unionId, userInfo.Province, userInfo.City, userInfo.Country, userInfo.Headimgurl, unionId, userInfo.Sex, user.UserId).Exec()
- wxUserId = userId
- }
- }
- return
- }
- type LoginResp struct {
- UserId int `description:"用户id"`
- Authorization string `description:"Token"`
- Headimgurl string `description:"用户头像"`
- Mobile string `description:"手机号"`
- Email string `description:"邮箱"`
- CompanyName string `description:"客户名称"`
- Status string `description:"状态"`
- EndDate string `description:"到期日期"`
- ProductName string `description:"客户类型名称"`
- }
- type CheckStatusResp struct {
- IsBind bool `description:"true:需要绑定手机号或邮箱,false:不需要绑定手机号或邮箱"`
- IsAuth bool `description:"true:需要授权,false:不需要授权"`
- PermissionName string `description:"拥有权限分类,多个用英文逗号分隔"`
- }
- func GetArticleUserCollectCount(userId int) (count int, err error) {
- 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=? `
- err = orm.NewOrm().Raw(sql, userId).QueryRow(&count)
- return
- }
- func GetArticleUserCollectList(startSize, pageSize, userId int) (items []*ArticleCollectList, err error) {
- sql := `SELECT a.* FROM cygx_article_collect AS a INNER JOIN cygx_article as art ON art.article_id = a.article_id
- WHERE a.user_id=?
- ORDER BY a.create_time DESC LIMIT ?,? `
- _, err = orm.NewOrm().Raw(sql, userId, startSize, pageSize).QueryRows(&items)
- return
- }
- type ArticleCollectListResp struct {
- List []*ArticleCollectList
- Paging *paging.PagingItem
- }
- func GetArticleUserInterviewApplyCount(userId int) (count int, err error) {
- sql := `SELECT COUNT(1) AS count FROM cygx_interview_apply AS a WHERE a.user_id=? `
- err = orm.NewOrm().Raw(sql, userId).QueryRow(&count)
- return
- }
- func GetArticleUserInterviewApplyList(startSize, pageSize, userId int) (items []*ArticleInterviewApplyList, err error) {
- sql := `SELECT a.* FROM cygx_interview_apply AS a
- WHERE a.user_id=?
- ORDER BY a.status ASC LIMIT ?,? `
- _, err = orm.NewOrm().Raw(sql, userId, startSize, pageSize).QueryRows(&items)
- return
- }
- type ArticleInterviewApplyListResp struct {
- List []*ArticleInterviewApplyList
- Paging *paging.PagingItem
- }
- func GetArticleUserBrowseHistoryCount(userId int, endDate string) (count int, err error) {
- sql := `SELECT COUNT( 1 ) as count
- FROM
- ( SELECT count(*) FROM cygx_article_history_record AS a WHERE a.user_id = ? AND a.create_time >= ? GROUP BY a.article_id ) b `
- err = orm.NewOrm().Raw(sql, userId, endDate).QueryRow(&count)
- return
- }
- func GetArticleUserBrowseHistoryList(startSize, pageSize, userId int, endDate string) (items []*ArticleInterviewApplyList, err error) {
- sql := `SELECT a.* FROM cygx_article_history_record AS a
- WHERE a.user_id=? AND a.create_time>=? GROUP BY a.article_id
- ORDER BY a.id DESC LIMIT ?,? `
- _, err = orm.NewOrm().Raw(sql, userId, endDate, startSize, pageSize).QueryRows(&items)
- return
- }
- type ArticleBrowseHistoryListResp struct {
- List []*ArticleInterviewApplyList
- Paging *paging.PagingItem
- }
- type ApplyTryReq struct {
- BusinessCardUrl string `description:"名片地址"`
- RealName string `description:"姓名"`
- CompanyName string `description:"公司名称"`
- ApplyMethod int `description:"1:已付费客户申请试用,2:非客户申请试用,3:非客户申请试用(ficc下,不需要进行数据校验)"`
- }
- type CountryCode struct {
- IsNeedAddCountryCode bool `description:"是否需要填写区号:需要填写,false:不需要填写"`
- }
- type OutboundMobile struct {
- IsNeedAddOutboundMobile bool `description:"是否需要填写外呼手机号:需要填写,false:不需要填写"`
- }
- type CountryCodeItem struct {
- CountryCode string `description:"区号"`
- }
- func AddCountryCode(CountryCode string, user *WxUserItem) (err error) {
- o := orm.NewOrm()
- if user.OutboundCountryCode == "" {
- sql := `UPDATE wx_user SET country_code=?,outbound_mobile=?,outbound_country_code=? WHERE user_id=? `
- _, err = o.Raw(sql, CountryCode, user.OutboundMobile, user.OutboundCountryCode, user.UserId).Exec()
- } else {
- sql := `UPDATE wx_user SET country_code=? WHERE user_id=? `
- _, err = o.Raw(sql, CountryCode, user.UserId).Exec()
- }
- return
- }
- //修改外呼手机号
- type OutboundMobileItem struct {
- OutboundMobile string `description:"外呼手机号"`
- OutboundCountryCode string `description:"外呼手机号区号"`
- ActivityId int `description:"活动ID"`
- }
- func AddOutboundMobile(item *OutboundMobileItem, userId int) (err error) {
- o := orm.NewOrm()
- o.Begin()
- defer func() {
- fmt.Println(err)
- if err == nil {
- o.Commit()
- } else {
- o.Rollback()
- }
- }()
- sql := `UPDATE wx_user SET outbound_mobile=? ,outbound_country_code = ?, is_msg_outbound_mobile= 1 WHERE user_id=? `
- _, err = o.Raw(sql, item.OutboundMobile, item.OutboundCountryCode, userId).Exec()
- if err != nil {
- return
- }
- if item.ActivityId > 0 {
- sql = `UPDATE cygx_activity_signup SET outbound_mobile=? ,country_code = ? WHERE user_id=? AND activity_id = ?`
- _, err = o.Raw(sql, item.OutboundMobile, item.OutboundCountryCode, userId, item.ActivityId).Exec()
- }
- return
- }
- //用户绑定手机号时同时绑定外呼手机号
- func BindUserOutboundMobile(mobile, countryCode string, userId int) (err error) {
- o := orm.NewOrm()
- sql := `UPDATE wx_user SET outbound_mobile=? ,outbound_country_code = ?, country_code= ? WHERE user_id=? `
- _, err = o.Raw(sql, mobile, countryCode, countryCode, userId).Exec()
- return
- }
- //已经绑定手机号,没有绑定外呼手机号的的用户,预约外呼的时候,将外呼手机号同步成手机号
- func BindUserOutboundMobileByMobile(mobile, countryCode string, userId int) (err error) {
- o := orm.NewOrm()
- sql := `UPDATE wx_user SET outbound_mobile=? ,outbound_country_code = ? WHERE user_id=? `
- _, err = o.Raw(sql, mobile, countryCode, userId).Exec()
- return
- }
- //将手机号为11位的用户,区号默认设置为86
- func ChangeUserOutboundMobileByMobile(userId int) (err error) {
- o := orm.NewOrm()
- sql := `UPDATE wx_user SET country_code = 86 WHERE user_id=? `
- _, err = o.Raw(sql, userId).Exec()
- return
- }
- type UserWhiteList struct {
- Mobile string `description:"手机号码"`
- RealName string `description:"用户实际名称"`
- CompanyName string `description:"公司名称"`
- Permission string `description:"拥有权限分类,多个用英文逗号分隔"`
- CountryCode string `description:"区号"`
- SellerName string `description:"销售姓名"`
- CreatedTime time.Time
- Status string `description:"客户状态'试用','永续','冻结','流失','正式','潜在'"`
- }
- type UserWhiteListRep struct {
- List []*UserWhiteList
- }
- //获取正式试用用户白名单
- func GetFormalUserWhiteList(fieldStr, condition string) (items []*UserWhiteList, err error) {
- sql := `SELECT ` + fieldStr + `
- (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,
- GROUP_CONCAT( DISTINCT b.chart_permission_name SEPARATOR '/' ) AS permission
- FROM wx_user AS u
- INNER JOIN company as c ON c.company_id = u.company_id
- INNER JOIN company_report_permission AS p ON p.company_id = u.company_id
- INNER JOIN chart_permission AS b ON b.chart_permission_id=p.chart_permission_id
- INNER JOIN company_product AS cp ON cp.company_id = u.company_id
- WHERE 1= 1
- AND cp.product_id = 2
- AND b.product_id = 2
- AND u.company_id >1 ` + condition + `
- GROUP BY u.user_id`
- _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
- return
- }
- //获取永续用户白名单
- func GetSustainableUserWhiteList(fieldStr, condition string) (items []*UserWhiteList, err error) {
- sql := `SELECT ` + fieldStr + `
- (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,
- (SELECT
- GROUP_CONCAT( DISTINCT b.chart_permission_name SEPARATOR '/' )
- FROM
- company_report_permission AS p
- INNER JOIN chart_permission AS b ON b.chart_permission_id = p.chart_permission_id
- WHERE
- p.company_id = u.company_id
- AND p.status IN ( '永续' )
- AND p.product_id = 2
- ) AS permission
- FROM wx_user AS u
- INNER JOIN company AS c ON c.company_id = u.company_id
- INNER JOIN company_report_permission AS p ON p.company_id = u.company_id
- INNER JOIN chart_permission AS b ON b.chart_permission_id = p.chart_permission_id
- INNER JOIN company_product AS cp ON cp.company_id = u.company_id
- WHERE 1 = 1
- AND u.company_id > 1 ` + condition + `
- GROUP BY u.user_id `
- _, err = orm.NewOrm().Raw(sql).QueryRows(&items)
- return
- }
|