package models import ( "fmt" "github.com/beego/beego/v2/client/orm" "hongze/hongze_api/utils" "strings" "time" ) type WxUser struct { UserId int `orm:"column(user_id);pk"` OpenId string `description:"open_id"` UnionId string `description:"union_id"` Subscribe string `description:"是否关注"` CompanyId int `description:"客户id"` NickName string `description:"用户昵称"` RealName string `description:"用户实际名称"` UserCode string `description:"用户编码"` Mobile string `description:"手机号码"` BindAccount string `description:"绑定时的账号"` WxCode string `description:"微信号"` Profession string `description:"职业"` Email string `description:"邮箱"` Telephone string `description:"座机"` Sex int `description:"普通用户性别,1为男性,2为女性"` Province string `description:"普通用户个人资料填写的省份"` City string `description:"普通用户个人资料填写的城市"` Country string `description:"国家,如中国为CN"` SubscribeTime int `description:"关注时间"` Remark string `description:"备注"` Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"` Privilege string `description:"用户特权信息,json数组,如微信沃卡用户为(chinaunicom)"` Unionid string `description:"用户统一标识。针对一个微信开放平台帐号下的应用,同一用户的unionid是唯一的。"` FirstLogin int `description:"是否第一次登陆"` Enabled int `description:"是否可用"` CreatedTime time.Time `description:"创建时间"` LastUpdatedTime time.Time `description:"最新一次修改时间"` Seller string `description:"销售员"` Note string `description:"客户备份信息"` IsNote int `description:"是否备注过信息"` FromType string `description:"report' COMMENT 'report:研报,teleconference:电话会"` ApplyMethod int `description:"0:未申请,1:已付费客户申请试用,2:非客户申请试用"` RegisterTime time.Time `description:"注册时间"` RegisterPlatform int `description:"注册平台,1:微信端,2:PC网页端"` IsFreeLogin bool `description:"是否免登陆,true:免登陆,false:非免登陆"` LoginTime time.Time `description:"最近一次登录时间"` IsRegister int `description:"是否注册:1:已注册,0:未注册"` Source int `description:"绑定来源,1:微信端,2:pc网页端,3:查研观向小程序,4:每日咨询"` CountryCode string `description:"区号,86、852、886等"` OutboundMobile string `description:"外呼手机号"` OutboundCountryCode string `description:"外呼手机号区号,86、852、886等"` } type WxUserItem struct { UserId int `description:"用户id"` OpenId string `description:"open_id"` UnionId string `description:"union_id"` CompanyId int `description:"客户id"` NickName string `description:"用户昵称"` RealName string `description:"用户实际名称"` Mobile string `description:"手机号码"` BindAccount string `description:"绑定时的账号"` Email string `description:"邮箱"` Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"` ApplyMethod int `description:"0:未申请,1:已付费客户申请试用,2:非客户申请试用"` FirstLogin int `description:"是否第一次登陆"` IsFreeLogin int `description:"是否免登陆,true:免登陆,false:非免登陆"` LoginTime time.Time `description:"登录时间"` CreatedTime time.Time `description:"创建时间"` LastUpdatedTime time.Time `description:"最近一次修改时间"` IsRegister int `description:"是否注册:1:已注册,0:未注册"` Note string `description:"客户备份信息"` IsNote int8 `description:"是否备注过信息"` } func GetWxUserItemByUserId(userId int) (item *WxUserItem, err error) { sql := `SELECT * FROM wx_user WHERE user_id=? ` err = orm.NewOrm().Raw(sql, userId).QueryRow(&item) return } func GetWxUserItemByOpenId(openId string) (item *WxUserItem, err error) { sql := `SELECT * FROM wx_user WHERE open_id=? ` err = orm.NewOrm().Raw(sql, openId).QueryRow(&item) return } func GetWxUserItemByUnionid(unionid string) (item *WxUserItem, err error) { sql := `SELECT * FROM wx_user WHERE union_id=? ` err = orm.NewOrm().Raw(sql, unionid).QueryRow(&item) return } type PermissionSearchKeyWord struct { KeyWord string } func GetPermissionSearchKeyWord(userId int) (items []*PermissionSearchKeyWord, err error) { sql := "SELECT a.key_word FROM chart_permission_search_key_word_mapping AS a INNER JOIN company_report_permission AS crp ON a.chart_permission_id=crp.chart_permission_id INNER JOIN wx_user AS wu ON wu.company_id=crp.company_id WHERE wu.user_id=? AND `from`='rddp' GROUP BY a.key_word " o := orm.NewOrm() _, err = o.Raw(sql, userId).QueryRows(&items) return } // 判断客户权限总数 func GetUserIsMaxPermission(companyId int) (count int, err error) { sql := ` SELECT COUNT(DISTINCT b.chart_permission_id) AS COUNT FROM company AS a INNER JOIN company_product AS c ON a.company_id=c.company_id AND c.product_id=1 INNER JOIN company_report_permission AS b ON a.company_id=b.company_id WHERE b.company_id=? ` o := orm.NewOrm() err = o.Raw(sql, companyId).QueryRow(&count) return } // 添加用户信息 func AddWxUser(item *WxUser) (lastId int64, err error) { o := orm.NewOrm() lastId, err = o.Insert(item) return } type WxLoginResp struct { Code int Authorization string UserId int Expires time.Time FirstLogin int UserPermission int `description:"状态码"` Headimgurl string `description:"用户头像"` Mobile string `description:"手机号"` Email string `description:"邮箱"` CompanyName string `description:"客户名称"` Status string `description:"状态"` EndDate string `description:"到期日期"` ProductName string `description:"客户类型名称"` } type UserDetail struct { FirstLogin int `description:"是否第一次登陆"` Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"` Mobile string `description:"手机号码"` Email string `description:"邮箱"` UserPermission int `description:"用户权限状态:0:付费用户,可正常查看报告,40001:获取用户信息失败,40002:非付费用户"` } func GetUserDetailByUserId(userId int) (item *UserDetail, err error) { o := orm.NewOrm() sql := `SELECT first_login,headimgurl,mobile,email FROM wx_user WHERE user_id = ? ` err = o.Raw(sql, userId).QueryRow(&item) return } type CheckSmsCodeReq struct { Mobile string `description:"手机号"` SmsCode string `description:"验证码"` } type SmallLimitResp struct { IsMaxPermission int } type CheckEmailCodeReq struct { Email string `description:"邮箱"` SmsCode string `description:"验证码"` } type ApplyReq struct { ApplyMethod int `description:"申请方式:"` CompanyName string `description:"公司名称"` RealName string `description:"姓名"` } func Apply(userId, applyMethod int, mobile, email, companyName, realName, openId string) (err error) { sql := "INSERT INTO user_apply(user_id, mobile, email, company_name, real_name, apply_method) VALUES (?,?,?,?,?,?) " rddpOrm := orm.NewOrmUsingDB("rddp") _, err = rddpOrm.Raw(sql, userId, mobile, email, companyName, realName, applyMethod).Exec() if err != nil { return } o := orm.NewOrm() if realName == "" { msql := " UPDATE wx_user SET apply_method = ?,note=? WHERE open_id = ? " _, err = o.Raw(msql, applyMethod, companyName, openId).Exec() } else { msql := " UPDATE wx_user SET apply_method = ?,real_name=?,note=? WHERE user_id = ? " _, err = o.Raw(msql, applyMethod, realName, companyName, userId).Exec() } return } type LoginReq struct { LoginType int `description:"登录方式:1:手机,2:邮箱"` Mobile string `description:"手机号"` Email string `description:"邮箱"` AreaNum int `description:"国际区号"` } type LoginResp struct { UserId int `description:"用户id"` UserPermission int `description:"权限"` 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:"客户类型名称"` } func BindMobile(openId, mobile string, userId, loginType int) (wxUserId int, err error) { utils.FileLog.Info("BindMobile: openId: %s,mobile: %s,userId: %d,loginType:%d ", openId, mobile, userId, loginType) mobile = strings.Trim(mobile, " ") //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) fmt.Println("err:", err) if err != nil && err.Error() != utils.ErrNoRow() { return } fmt.Println(user) if user == nil || (err != nil && err.Error() == utils.ErrNoRow()) { utils.FileLog.Info("user is nil ") fmt.Println("line 210") msql := `` if loginType == 1 { msql = "UPDATE wx_user SET mobile = ?,bind_account = ? where open_id = ? " } else { msql = "UPDATE wx_user SET email = ?,bind_account = ? where open_id = ? " } fmt.Println("bind") fmt.Println(msql, mobile, mobile, openId) _, err = o.Raw(msql, mobile, mobile, openId).Exec() wxUserId = userId } else { utils.FileLog.Info("user is not nil ") fmt.Println("line 223") if openId != "" { sql = `SELECT * FROM wx_user WHERE open_id = ? ` openIdUser := new(WxUser) o := orm.NewOrm() err = o.Raw(sql, openId).QueryRow(&openIdUser) if openIdUser != nil { if openIdUser.UserId != user.UserId { utils.FileLog.Info("user id is not eq %d , %d", openIdUser.UserId, user.UserId) wxUserId = user.UserId dsql := ` DELETE FROM wx_user WHERE open_id = ? ` _, err = o.Raw(dsql, openId).Exec() if err != nil { return wxUserId, err } } } msql := `` ssql := `` if loginType == 1 { msql = ` UPDATE wx_user SET open_id = ?,bind_account = ?,created_time=NOW(),register_time=NOW() WHERE mobile = ? ` ssql = `SELECT * FROM wx_user WHERE mobile = ? ` } else { msql = ` UPDATE wx_user SET open_id = ?,bind_account = ?,created_time=NOW(),register_time=NOW() WHERE email = ? ` ssql = `SELECT * FROM wx_user WHERE email = ? ` } _, err = o.Raw(msql, openId, mobile, mobile).Exec() bindUser := new(WxUser) err = o.Raw(ssql, mobile).QueryRow(&bindUser) if bindUser != nil && bindUser.UserId > 0 { wxUserId = bindUser.UserId } } else { fmt.Println("line 239") wxUserId = userId } } if wxUserId <= 0 && (user != nil && err.Error() != utils.ErrNoRow()) { utils.FileLog.Info("wxUserId =0 %d , %d", user.UserId) wxUserId = user.UserId } return } func PcBindMobile(unionId, mobile string, userId, loginType int) (wxUserId int, err error) { //loginType 登录方式:1:手机,2:邮箱 utils.FileLog.Info("绑定参数:%s %s %d %d", unionId, mobile, userId, loginType) 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()) { utils.FileLog.Info("用户不存在,根据union_id绑定") 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 { utils.FileLog.Info("用户存在,user.UnionId:%s", user.UnionId) 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 } utils.FileLog.Info("user.RegisterTime %s", user.RegisterTime.Format(utils.FormatDateTime)) utils.FileLog.Info("userInfo.RegisterTime %s", userInfo.RegisterTime.Format(utils.FormatDateTime)) var maxRegisterTime time.Time if user.RegisterTime.Before(userInfo.RegisterTime) { maxRegisterTime = user.RegisterTime utils.FileLog.Info("after") } else { maxRegisterTime = userInfo.RegisterTime utils.FileLog.Info("not after") } utils.FileLog.Info("maxRegisterTime %s", maxRegisterTime.Format(utils.FormatDateTime)) 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 } dsql := ` DELETE FROM wx_user WHERE user_id = ? ` _, err = o.Raw(dsql, userId).Exec() if err != nil { return user.UserId, err } if user.Mobile == "" && loginType == 1 { msql := ` UPDATE wx_user SET mobile = ?,bind_account = ? WHERE user_id = ?` _, err = o.Raw(msql, mobile, mobile, user.UserId).Exec() wxUserId = user.UserId } if user.Email == "" && loginType == 2 { msql := ` UPDATE wx_user SET email = ?,bind_account = ? WHERE user_id = ?` _, err = o.Raw(msql, mobile, mobile, user.UserId).Exec() wxUserId = user.UserId } utils.FileLog.Info("用户存在,bind:%s,%d,%s", unionId, wxUserId) wxUserId = userId } } return } func ModifyFirstLogin(userId int) (err error) { o := orm.NewOrm() sql := `UPDATE wx_user SET first_login=0 WHERE user_id = ? ` _, err = o.Raw(sql, userId).Exec() return } func GetWxUserItemByEmail(email string) (item *WxUserItem, err error) { sql := `SELECT * FROM wx_user WHERE email=? ` err = orm.NewOrm().Raw(sql, email).QueryRow(&item) return } func GetWxUserItemByMobile(mobile string) (item *WxUserItem, err error) { sql := `SELECT * FROM wx_user WHERE mobile=? OR mobile_two=? ` err = orm.NewOrm().Raw(sql, mobile, mobile).QueryRow(&item) return } type PcLoginReq struct { LoginType int `description:"登录方式:1:手机,2:邮箱"` Mobile string `description:"手机号"` Email string `description:"邮箱"` SmsCode string `description:"短信/邮箱验证码"` IsFreeLogin bool `description:"是否免登陆,true:免登陆,false:非免登陆"` } type BindReq struct { BindType int `description:"绑定方式:1:手机,2:邮箱"` Mobile string `description:"手机号"` Email string `description:"邮箱"` AreaNum int `description:"国际区号"` VerifyCode string `description:"短信/邮箱 验证码"` } func ModifyLoginTime(userId int, isFreeLogin bool) (err error) { o := orm.NewOrm() sql := `UPDATE wx_user SET is_free_login=?,login_time=NOW() WHERE user_id = ? ` _, err = o.Raw(sql, isFreeLogin, userId).Exec() return } func GetCustomPermission(companyId int) (count int, err error) { o := orm.NewOrm() sql := `SELECT COUNT(1) AS count FROM company AS a INNER JOIN company_product AS b ON a.company_id=b.company_id WHERE b.company_id=? AND b.status IN('试用','正式') GROUP BY b.company_id` err = o.Raw(sql, companyId).QueryRow(&count) return } type CheckLoginResp struct { IsTips bool `description:"true:需要提示,false:不需要提示"` IsBind bool `description:"true:需要绑定邮箱或验证码,false:不需要绑定邮箱或验证码"` } func GetOpenIdAll() (items []*WxUserItem, err error) { sql := ` SELECT * FROM wx_user WHERE open_id<>'' AND union_id IS NULL ` o := orm.NewOrm() _, err = o.Raw(sql).QueryRows(&items) return } func ModifyWxUserUnionId(unionId string, userId int) (err error) { o := orm.NewOrm() sql := `UPDATE wx_user SET union_id=?,unionid=? WHERE user_id = ? ` _, err = o.Raw(sql, unionId, unionId, userId).Exec() return } func GetWxUserAll() (items []*WxUserItem, err error) { sql := ` SELECT a.* FROM wx_user AS a INNER JOIN company_product AS b ON a.company_id=b.company_id WHERE b.status IN('正式','试用') AND b.product_id=1 ` o := orm.NewOrm() _, err = o.Raw(sql).QueryRows(&items) return } // 变更联系人是否已注册状态 func ModifyWxUserRegisterStatus(userId, status, source int, registerTime time.Time) (err error) { o := orm.NewOrm() sql := `UPDATE wx_user SET is_register=?,source=?,register_time=? WHERE user_id = ? ` _, err = o.Raw(sql, status, source, registerTime, userId).Exec() return } // ModifyWxUserNameApplyMethod 更新用户名称等信息 func ModifyWxUserNameApplyMethod(userId int, realName, note string, isNote int8, applyMethod int) (err error) { o := orm.NewOrm() sql := `UPDATE wx_user SET real_name=?,note=?,is_note=?,apply_method=? WHERE user_id = ? ` _, err = o.Raw(sql, realName, note, isNote, applyMethod, userId).Exec() return } func UserSubscribe(subscribeType int, openId string) (err error) { o := orm.NewOrm() sql := `UPDATE user_record SET subscribe=?,subscribe_time=NOW() WHERE open_id = ? AND create_platform=1 ` _, err = o.Raw(sql, subscribeType, openId).Exec() return } // UpdateWxUserViewData 更新客户产品的阅读记录(阅读时间、总阅读次数) func UpdateWxUserViewData(userId, productId int, lastViewTime string) (err error) { sql := `` if productId == 2 { sql = ` update wx_user set rai_view_total=rai_view_total+1,rai_last_view_time=?,report_last_view_time = ? WHERE user_id=? ` } else { sql = ` update wx_user set ficc_view_total=ficc_view_total+1,ficc_last_view_time=?,report_last_view_time = ? WHERE user_id=? ` } o := orm.NewOrm() _, err = o.Raw(sql, lastViewTime, lastViewTime, userId).Exec() return }