1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189 |
- package services
- import (
- "context"
- "errors"
- "fmt"
- "github.com/tealeg/xlsx"
- "hongze/hongze_mfyx/models"
- "hongze/hongze_mfyx/utils"
- "os"
- "path/filepath"
- "strconv"
- "strings"
- "time"
- )
- var ERR_NO_USER_RECORD = errors.New("用户关系没有入库")
- var ERR_USER_NOT_BIND = errors.New("用户没有绑定")
- // 通过openid获取用户信息
- func GetWxUserItemByOpenId(openid string) (item *models.WxUserItem, err error) {
- //通过openid获取用户关联信息
- userRecord, userRecordErr := models.GetUserRecordByOpenId(openid)
- fmt.Println(userRecordErr)
- //fmt.Println("userRecordErr", userRecordErr)
- if userRecordErr != nil {
- if userRecordErr.Error() == utils.ErrNoRow() {
- err = ERR_NO_USER_RECORD
- return
- } else {
- err = userRecordErr
- return
- }
- }
- //该openid在系统中没有关联关系
- if userRecord == nil {
- err = ERR_NO_USER_RECORD
- return
- }
- //该openid没有绑定用户
- if userRecord.UserId <= 0 {
- err = ERR_USER_NOT_BIND
- item = new(models.WxUserItem)
- //格式化返回用户数据
- formatWxUserAndUserRecord(item, userRecord)
- return
- }
- //获取用户信息
- item, wxUserErr := models.GetWxUserItemByUserId(userRecord.UserId)
- //fmt.Println("wxUserErr", wxUserErr)
- if wxUserErr != nil {
- err = wxUserErr
- //如果是找不到数据,那么可能是该用户被删除了,但是user_record没有删除对应的关系
- if wxUserErr.Error() == utils.ErrNoRow() {
- //用户被删除了,但是user_record没有删除对应的关系,那么去解除绑定
- userUnbindErr := models.UnBindUserRecordByOpenid(openid)
- if userUnbindErr != nil {
- err = userUnbindErr
- return
- }
- //返回状态为 用户未绑定 逻辑代码
- err = ERR_USER_NOT_BIND
- item = new(models.WxUserItem)
- //格式化返回用户数据
- formatWxUserAndUserRecord(item, userRecord)
- return
- }
- return
- }
- if item.RealName == "" {
- item.RealName = userRecord.RealName
- }
- //格式化返回用户数据
- formatWxUserAndUserRecord(item, userRecord)
- return
- }
- // 根据用户id和平台id获取用户信息
- func GetWxUserItemByUserId(userId, platform int) (wxUserItem *models.WxUserItem, err error) {
- //获取用户信息
- wxUserItem, wxUserErr := models.GetWxUserItemByUserId(userId)
- if wxUserErr != nil {
- err = wxUserErr
- return
- }
- //格式化返回用户数据
- formatWxUser(wxUserItem, platform)
- return
- }
- // 根据用户邮箱和平台id获取用户信息
- func GetWxUserItemByEmail(email string, platform int) (wxUserItem *models.WxUserItem, err error) {
- //获取用户信息
- wxUserItem, wxUserErr := models.GetWxUserItemByEmail(email)
- if wxUserErr != nil {
- err = wxUserErr
- return
- }
- //格式化返回用户数据
- formatWxUser(wxUserItem, platform)
- return
- }
- // 根据用户手机号和平台id获取用户信息
- func GetWxUserItemByMobile(mobile string, platform int) (wxUserItem *models.WxUserItem, err error) {
- //获取用户信息
- wxUserItem, wxUserErr := models.GetWxUserItemByMobile(mobile)
- if wxUserErr != nil {
- err = wxUserErr
- return
- }
- //格式化返回用户数据
- formatWxUser(wxUserItem, platform)
- return
- }
- // 根据用户unionid和平台id获取用户信息
- func GetWxUserItemByUnionId(unionId string, platform int) (wxUserItem *models.WxUserItem, err error) {
- //获取用户信息
- wxUserItem, wxUserErr := models.GetWxUserItemByUnionid(unionId)
- if wxUserErr != nil {
- err = wxUserErr
- return
- }
- //格式化返回用户数据
- formatWxUser(wxUserItem, platform)
- return
- }
- // 通过用户 关系表记录 和 用户记录 格式化返回 用户数据
- func formatWxUserAndUserRecord(wxUser *models.WxUserItem, userRecord *models.UserRecord) {
- wxUser.OpenId = userRecord.OpenId
- wxUser.UnionId = userRecord.UnionId
- wxUser.NickName = userRecord.NickName
- //wxUser.RealName = userRecord.RealName
- //wxUser.BindAccount = userRecord.BindAccount
- wxUser.Headimgurl = userRecord.Headimgurl
- wxUser.SessionKey = userRecord.SessionKey
- }
- // 通过用户 用户记录 和 来源平台 格式化返回 用户数据
- func formatWxUser(wxUser *models.WxUserItem, platform int) {
- //根据用户id和平台id获取用户关系
- userRecord, userRecordErr := models.GetUserRecordByUserId(wxUser.UserId, platform)
- if userRecordErr != nil {
- if userRecordErr.Error() != utils.ErrNoRow() {
- return
- }
- if userRecordErr.Error() == utils.ErrNoRow() {
- return
- }
- }
- //该openid在系统中没有关联关系
- if userRecord == nil {
- return
- }
- wxUser.OpenId = userRecord.OpenId
- wxUser.UnionId = userRecord.UnionId
- wxUser.NickName = userRecord.NickName
- //wxUser.RealName = userRecord.RealName
- //wxUser.BindAccount = userRecord.BindAccount
- wxUser.Headimgurl = userRecord.Headimgurl
- wxUser.SessionKey = userRecord.SessionKey
- return
- }
- // 用户绑定
- func BindWxUser(openid, mobile, email, countryCode, inviteShareCode string) (wxUser *models.WxUserItem, err error) {
- if mobile == "" && email == "" {
- err = errors.New("手机号或邮箱必填一个")
- return
- }
- var bindAccount string
- //根据手机号获取用户信息
- if mobile != "" {
- tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile)
- if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
- err = wxUserErr
- return
- }
- wxUser = tmpWxUser
- bindAccount = mobile
- }
- //根据邮箱获取用户信息
- if wxUser == nil && email != "" {
- tmpWxUser, wxUserErr := models.GetWxUserItemByEmail(email)
- if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
- err = wxUserErr
- return
- }
- wxUser = tmpWxUser
- bindAccount = email
- }
- //查询openid的第三方(微信)信息
- userRecord, err := models.GetUserRecordByOpenId(openid)
- if err != nil {
- return
- }
- var userId int
- //如果查询出来的用户是nil,那么需要新增用户
- if wxUser == nil {
- user := &models.WxUser{
- CompanyId: 1,
- CreatedTime: time.Now(),
- FirstLogin: 1,
- Enabled: 1,
- RegisterPlatform: 4,
- RegisterTime: time.Now(),
- Mobile: mobile,
- Email: email,
- IsRegister: 1,
- Source: 3,
- CountryCode: countryCode,
- OutboundMobile: mobile,
- OutboundCountryCode: countryCode,
- }
- tmpUserId, addUserErr := models.AddWxUser(user)
- if addUserErr != nil {
- err = addUserErr
- return
- }
- user.UserId = int(tmpUserId)
- userId = int(tmpUserId)
- wxUser, err = models.GetWxUserItemByUserId(userId)
- if inviteShareCode != "" { //记录分享来源
- go AddCygxUserAdminShareHistory(wxUser, "login", "", inviteShareCode, 0) //记录分享来源
- }
- } else {
- userId = wxUser.UserId
- err = models.BindUserOutboundMobile(mobile, countryCode, userId)
- if err != nil {
- return
- }
- if wxUser.IsRegister == 0 {
- models.ModifyWxUserRegisterStatus(userId)
- }
- }
- //如果存在该手机号/邮箱,那么需要校验
- if userRecord.UserId > 0 && userRecord.UserId != userId {
- err = errors.New("用户已绑定,不允许重复绑定")
- return
- }
- err = models.BindUserRecordByOpenid(userId, openid, bindAccount)
- if err != nil {
- return
- }
- userRecord.UserId = userId
- //如果当前该第三方用户信息的昵称为空串的话,那么需要去查询该用户的第一个绑定信息的数据作为来源做数据修复
- if userRecord.NickName == "" {
- oldUserRecord, err := models.GetUserThirdRecordByUserId(userId)
- if err == nil && oldUserRecord != nil {
- //如果该用户绑定的第一条数据的头像信息不为空串,那么就去做新数据的修复
- if oldUserRecord.NickName != "" {
- _ = models.ModifyUserRecordByDetail(userRecord.OpenId, userRecord.UnionId, oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex, userId)
- }
- }
- }
- //格式化用户数据
- formatWxUserAndUserRecord(wxUser, userRecord)
- return
- }
- //func init() {
- // fmt.Println(GetWxUserItemByOpenId("ouw2U62cUWNe97e96AZ5jxeAgrJM"))
- //}
- // 微信登录
- func WxLogin(code, openId, unionId string, wxUserInfo *WxUserInfo) (token string, userId, firstLogin, permission int, err error) {
- if unionId == "" {
- unionId = wxUserInfo.Unionid
- }
- //firstLogin==1,强制绑定手机号或者邮箱
- firstLogin = 1
- fmt.Println("GetWxUserItemByOpenId ", openId)
- QUERY_WX_USER:
- wxUser, wxUserErr := GetWxUserItemByOpenId(openId)
- fmt.Println("wxUserErr", wxUserErr)
- if wxUserErr == ERR_NO_USER_RECORD { //没有用户openid记录
- // 如果查研观向小程序已经绑定过相关信息了,那么拿过来复用
- userRecordCygx, userRecordErrCygx := models.GetUserRecordByUnionId(unionId, 4)
- if userRecordErrCygx != nil && userRecordErrCygx.Error() != utils.ErrNoRow() {
- err = userRecordErrCygx
- return
- }
- if userRecordCygx != nil {
- _, recordErr := AddUserRecordByCygx(openId, unionId, wxUserInfo.Nickname, userRecordCygx.RealName, wxUserInfo.Province, wxUserInfo.City, wxUserInfo.Country, wxUserInfo.Headimgurl, wxUserInfo.SessionKey, utils.WxPlatform, wxUserInfo.Sex, 0, userRecordCygx.UserId)
- //如果插入失败,那么直接将错误信息返回
- if recordErr != nil {
- err = recordErr
- return
- }
- } else {
- //先添加第三方信息(openid等信息)
- _, recordErr := AddUserRecord(openId, unionId, wxUserInfo.Nickname, "", wxUserInfo.Province, wxUserInfo.City, wxUserInfo.Country, wxUserInfo.Headimgurl, wxUserInfo.SessionKey, utils.WxPlatform, wxUserInfo.Sex, 0)
- //如果插入失败,那么直接将错误信息返回
- if recordErr != nil {
- err = recordErr
- return
- }
- }
- //插入成功后,需要重新查询该用户,并进入下面的逻辑
- goto QUERY_WX_USER
- } else if wxUserErr == ERR_USER_NOT_BIND {
- //没有用户信息
- //wxUser.FirstLogin = 1
- } else if wxUserErr != nil {
- err = wxUserErr
- return
- }
- fmt.Println("wxUserInfo", wxUserInfo)
- fmt.Println("wxUserInfo.Nickname", wxUserInfo.Nickname)
- fmt.Println("SessionKey", wxUserInfo.SessionKey)
- if wxUserInfo != nil {
- fmt.Println("ModifyUserRecordSessionKey")
- err = models.ModifyUserRecordSessionKey(openId, wxUserInfo.SessionKey)
- fmt.Println("ModifyUserRecordSessionKey Err", err)
- }
- //如果已经登录注册绑定的情况下
- if wxUser != nil && wxUserErr == nil {
- //获取用户权限
- firstLogin = wxUser.FirstLogin
- userId = wxUser.UserId
- {
- codeLog := new(models.WxUserCode)
- codeLog.WxCode = code
- codeLog.UserId = userId
- codeLog.Code = 0
- codeLog.FirstLogin = firstLogin
- codeLog.Authorization = token
- codeLog.UserPermission = permission
- codeLog.CreateTime = time.Now()
- go models.AddWxUserCode(codeLog)
- }
- if wxUser.Mobile == "" && wxUser.Email == "" {
- firstLogin = 1
- }
- }
- //获取登录token
- tokenItem, tokenErr := models.GetTokenByOpenId(openId)
- if tokenErr != nil && tokenErr.Error() != utils.ErrNoRow() {
- err = errors.New("登录失败,获取token失败:" + tokenErr.Error())
- return
- }
- fmt.Println("line 271 ", openId)
- if tokenItem == nil || (tokenErr != nil && tokenErr.Error() == utils.ErrNoRow()) {
- timeUnix := time.Now().Unix()
- timeUnixStr := strconv.FormatInt(timeUnix, 10)
- token = utils.MD5(openId) + utils.MD5(timeUnixStr)
- //新增session
- {
- session := new(models.CygxSession)
- session.OpenId = openId
- session.UserId = userId
- session.CreatedTime = time.Now()
- session.LastUpdatedTime = time.Now()
- session.ExpireTime = time.Now().AddDate(0, 3, 0)
- session.AccessToken = token
- sessionErr := models.AddSession(session)
- if err != nil {
- err = errors.New("登录失败,新增用户session信息失败:" + sessionErr.Error())
- return
- }
- }
- } else {
- token = tokenItem.AccessToken
- }
- fmt.Println("line 294 ", token)
- //新增登录日志
- {
- loginLog := new(models.WxUserLog)
- loginLog.UserId = userId
- loginLog.OpenId = openId
- loginLog.UnionId = unionId
- loginLog.CreateTime = time.Now()
- loginLog.Handle = "wechat_login_mfyx"
- loginLog.Remark = token
- go models.AddWxUserLog(loginLog)
- }
- return
- }
- func UserLogin() {
- }
- // 添加第三方用户(微信)记录
- func AddUserRecord(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe int) (userRecord *models.UserRecord, err error) {
- find, err := models.GetUserRecordByOpenId(openId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- if find != nil {
- userRecord = find
- return
- }
- userRecord = &models.UserRecord{
- OpenId: openId, //用户open_id
- UnionId: unionId, //用户union_id
- Subscribe: subscribe,
- NickName: nickName, //用户昵称,最大长度:32
- RealName: realName, //用户实际名称,最大长度:32
- Sex: sex, //普通用户性别,1为男性,2为女性
- Province: province, //普通用户个人资料填写的省份,最大长度:30
- City: city, //普通用户个人资料填写的城市,最大长度:30
- Country: country, //国家,如中国为CN,最大长度:30
- Headimgurl: headimgurl, //用户第三方(微信)头像,最大长度:512
- CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间
- CreatePlatform: platform, //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1
- SessionKey: sessionKey, //微信小程序会话密钥,最大长度:255
- }
- recordId, err := models.AddUserRecord(userRecord)
- if err != nil {
- return
- }
- userRecord.UserRecordId = int(recordId)
- return
- }
- // 添加第三方用户(微信)记录
- func AddUserRecordByCygx(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe, userId int) (userRecord *models.UserRecord, err error) {
- find, err := models.GetUserRecordByOpenId(openId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- if find != nil {
- userRecord = find
- return
- }
- userRecord = &models.UserRecord{
- OpenId: openId, //用户open_id
- UnionId: unionId, //用户union_id
- Subscribe: subscribe,
- NickName: nickName, //用户昵称,最大长度:32
- RealName: realName, //用户实际名称,最大长度:32
- Sex: sex, //普通用户性别,1为男性,2为女性
- Province: province, //普通用户个人资料填写的省份,最大长度:30
- City: city, //普通用户个人资料填写的城市,最大长度:30
- Country: country, //国家,如中国为CN,最大长度:30
- Headimgurl: headimgurl, //用户第三方(微信)头像,最大长度:512
- CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间
- CreatePlatform: platform, //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1
- SessionKey: sessionKey, //微信小程序会话密钥,最大长度:255
- UserId: userId, //微信小程序会话密钥,最大长度:255
- }
- recordId, err := models.AddUserRecord(userRecord)
- if err != nil {
- return
- }
- userRecord.UserRecordId = int(recordId)
- return
- }
- // 每天新增,删除的白名单
- func SendEmailUserWhiteListChange(cont context.Context) (err error) {
- var msg string
- var fieldStr string
- var condition string
- defer func() {
- if err != nil {
- go utils.SendAlarmMsg("发送附件模版消息失败", 2)
- fmt.Println("err:", err, time.Now())
- go utils.SendEmail("发送附件模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format(utils.FormatDateTime), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
- utils.FileLog.Info("发送附件模版消息失败,Err:%s", err.Error())
- }
- if msg != "" {
- utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
- }
- }()
- mobileStr, err := models.GetWxUserWhiteMobile()
- if err != nil {
- msg = "获取失败,Err:" + err.Error()
- return
- }
- if mobileStr == "" {
- mobileStr = "1"
- }
- mobileStr = strings.Replace(mobileStr, " ", "", -1)
- mobileStr = strings.Replace(mobileStr, ",", "','", -1)
- mobileStr = "'" + mobileStr + "'"
- //手机号新增
- fieldStr = ` u.mobile,u.country_code,u.real_name,c.company_name,u.company_id,cp.seller_name,cp.status,`
- condition = ` AND cp.status IN ( '正式', '试用' ) AND u.mobile IN (` + mobileStr + `) `
- condition += ` AND u.mobile !='' AND DATE_SUB(CURDATE(), INTERVAL 2 DAY) <= DATE(u.created_time )`
- listMobile, err := models.GetFormalUserWhiteList(fieldStr, condition)
- if err != nil {
- msg = "获取失败,Err:" + err.Error()
- return
- }
- //外呼手机号新增
- outboundMobileStr, err := models.GetWxUserWhiteOutboundMobile()
- if outboundMobileStr == "" {
- outboundMobileStr = "1"
- }
- outboundMobileStr = strings.Replace(outboundMobileStr, " ", "", -1)
- fieldStr = ` u.outbound_mobile as mobile,u.outbound_country_code as country_code,u.real_name,c.company_name,u.company_id,cp.status,`
- condition = ` AND cp.status IN ( '正式', '试用' ) AND u.outbound_mobile IN (` + outboundMobileStr + `) `
- condition += ` AND DATE_SUB(CURDATE(), INTERVAL 2 DAY) <= DATE(u.created_time )`
- listOutboundMobile, err := models.GetFormalUserWhiteList(fieldStr, condition)
- if err != nil {
- msg = "获取失败,Err:" + err.Error()
- return
- }
- var rep models.UserWhiteListRep
- var repList []*models.UserWhiteList
- repList = listMobile
- if len(listOutboundMobile) > 0 {
- for _, v := range listOutboundMobile {
- repList = append(listMobile, v)
- }
- }
- //新增用户,过滤手机号为空的
- for _, v := range repList {
- if v.Mobile == "" {
- continue
- }
- rep.List = append(rep.List, v)
- }
- //rep.List = repList
- //创建excel
- dir, errFile := os.Executable()
- exPath := filepath.Dir(dir)
- downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandDigit(5) + ".xlsx"
- xlsxFile := xlsx.NewFile()
- if errFile != nil {
- msg = "生成文件失败Err:" + errFile.Error()
- return
- }
- style := xlsx.NewStyle()
- alignment := xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- WrapText: true,
- }
- style.Alignment = alignment
- style.ApplyAlignment = true
- sheet, err := xlsxFile.AddSheet("白名单")
- if err != nil {
- msg = "新增Sheet失败,Err:" + err.Error()
- return
- }
- //设置宽度
- _ = sheet.SetColWidth(2, 2, 15)
- _ = sheet.SetColWidth(6, 6, 30)
- _ = sheet.SetColWidth(13, 13, 35)
- //标头
- rowTitle := sheet.AddRow()
- cellA := rowTitle.AddCell()
- cellA.Value = "姓名"
- cellB := rowTitle.AddCell()
- cellB.Value = "国际代码1"
- cellC := rowTitle.AddCell()
- cellC.Value = "手机号"
- cellD := rowTitle.AddCell()
- cellD.Value = "国际代码2"
- cellE := rowTitle.AddCell()
- cellE.Value = "备用号"
- cellF := rowTitle.AddCell()
- cellF.Value = "电子邮箱"
- cellG := rowTitle.AddCell()
- cellG.Value = "公司名称"
- cellH := rowTitle.AddCell()
- cellH.Value = "职位名称"
- cellI := rowTitle.AddCell()
- cellI.Value = "客户类型"
- cellJ := rowTitle.AddCell()
- cellJ.Value = "对口销售"
- cellK := rowTitle.AddCell()
- cellK.Value = "归属部门"
- cellL := rowTitle.AddCell()
- cellL.Value = "有效开始时间"
- cellM := rowTitle.AddCell()
- cellM.Value = "有效结束时间"
- cellN := rowTitle.AddCell()
- cellN.Value = "备注"
- cellO := rowTitle.AddCell()
- cellO.Value = "权限(消费,医药,智造,科技,策略)"
- if len(rep.List) > 0 {
- for _, item := range rep.List {
- row := sheet.AddRow()
- cellA := row.AddCell()
- cellA.Value = item.RealName
- cellB := row.AddCell()
- cellB.Value = item.CountryCode
- if len(item.Mobile) >= 11 && item.CountryCode == "" {
- cellB.Value = "86"
- }
- cellC := row.AddCell()
- cellC.Value = item.Mobile
- cellD := row.AddCell()
- cellD.Value = ""
- cellE := row.AddCell()
- cellE.Value = ""
- cellF := row.AddCell()
- cellF.Value = ""
- cellG := row.AddCell()
- cellG.Value = item.CompanyName
- cellH := row.AddCell()
- cellH.Value = ""
- cellI := row.AddCell()
- cellI.Value = ""
- cellJ := row.AddCell()
- cellJ.Value = item.SellerName
- cellK := row.AddCell()
- cellK.Value = ""
- cellL := row.AddCell()
- cellL.Value = ""
- cellM := row.AddCell()
- cellM.Value = ""
- cellN := row.AddCell()
- cellN.Value = ""
- cellO := row.AddCell()
- if item.Permission == "" {
- item.Permission = "专家/医药/智造/消费/研选/科技/策略/路演服务"
- }
- cellO.Value = item.Permission
- }
- }
- errFile = xlsxFile.Save(downLoadnFilePath)
- if errFile != nil {
- msg = "保存文件失败Err:" + errFile.Error()
- return
- }
- title := time.Now().Format("2006-01-02") + "新增白名单用户"
- content := time.Now().Format("2006-01-02") + "新增白名单用户"
- fileName := downLoadnFilePath
- var sendResult bool
- if len(rep.List) > 0 {
- sendResult = utils.SendEmailByHongze(title, content, utils.EmaiWhiteUserList, fileName, title+".xlsx")
- }
- os.Remove(downLoadnFilePath)
- //创建冻结excel
- dir, errFile = os.Executable()
- exPath = filepath.Dir(dir)
- downLoadnFilePaths := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandDigit(5) + ".xlsx"
- xlsxFile = xlsx.NewFile()
- if errFile != nil {
- msg = "生成文件失败Err:" + errFile.Error()
- return
- }
- style = xlsx.NewStyle()
- alignment = xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- WrapText: true,
- }
- style.Alignment = alignment
- style.ApplyAlignment = true
- sheet, err = xlsxFile.AddSheet("白名单")
- if err != nil {
- msg = "新增Sheet失败,Err:" + err.Error()
- return
- }
- //设置宽度
- _ = sheet.SetColWidth(2, 2, 15)
- _ = sheet.SetColWidth(6, 6, 30)
- _ = sheet.SetColWidth(13, 13, 35)
- //标头
- rowTitle = sheet.AddRow()
- cellA = rowTitle.AddCell()
- cellA.Value = "姓名"
- cellB = rowTitle.AddCell()
- cellB.Value = "国际代码1"
- cellC = rowTitle.AddCell()
- cellC.Value = "手机号"
- cellD = rowTitle.AddCell()
- cellD.Value = "国际代码2"
- cellE = rowTitle.AddCell()
- cellE.Value = "备用号"
- cellF = rowTitle.AddCell()
- cellF.Value = "电子邮箱"
- cellG = rowTitle.AddCell()
- cellG.Value = "公司名称"
- cellH = rowTitle.AddCell()
- cellH.Value = "职位名称"
- cellI = rowTitle.AddCell()
- cellI.Value = "客户类型"
- cellJ = rowTitle.AddCell()
- cellJ.Value = "对口销售"
- cellK = rowTitle.AddCell()
- cellK.Value = "归属部门"
- cellL = rowTitle.AddCell()
- cellL.Value = "有效开始时间"
- cellM = rowTitle.AddCell()
- cellM.Value = "有效结束时间"
- cellN = rowTitle.AddCell()
- cellN.Value = "备注"
- cellO = rowTitle.AddCell()
- cellO.Value = "权限(消费,医药,智造,科技,策略)"
- //手机号冻结
- listFrozen, err := models.GetFrozenUserWhiteList() //手机号用户修改
- listFrozenOutbound, err := models.GetFrozenUserWhiteListOutbound() //外呼手机号用户修改
- if err != nil {
- msg = "获取失败,Err:" + err.Error()
- return
- }
- if len(listFrozenOutbound) > 0 {
- for _, v := range listFrozenOutbound {
- listFrozen = append(listFrozen, v)
- }
- }
- var listFrozenUser []*models.WxUserWhite
- for _, v := range listFrozen {
- if v.Mobile == "" {
- continue
- }
- listFrozenUser = append(listFrozenUser, v)
- }
- if len(listFrozenUser) > 0 {
- for _, item := range listFrozenUser {
- row := sheet.AddRow()
- cellA := row.AddCell()
- cellA.Value = item.RealName
- cellB := row.AddCell()
- cellB.Value = item.CountryCode
- if len(item.Mobile) >= 11 && item.CountryCode == "" {
- cellB.Value = "86"
- }
- cellC := row.AddCell()
- cellC.Value = item.Mobile
- cellD := row.AddCell()
- cellD.Value = ""
- cellE := row.AddCell()
- cellE.Value = ""
- cellF := row.AddCell()
- cellF.Value = ""
- cellG := row.AddCell()
- cellG.Value = item.CompanyName
- cellH := row.AddCell()
- cellH.Value = ""
- cellI := row.AddCell()
- cellI.Value = ""
- cellJ := row.AddCell()
- cellJ.Value = item.SellerName
- cellK := row.AddCell()
- cellK.Value = ""
- cellL := row.AddCell()
- cellL.Value = ""
- cellM := row.AddCell()
- cellM.Value = ""
- cellN := row.AddCell()
- cellN.Value = ""
- cellO := row.AddCell()
- cellO.Value = item.PermissionName
- }
- }
- errFile = xlsxFile.Save(downLoadnFilePaths)
- if errFile != nil {
- msg = "保存文件失败Err:" + errFile.Error()
- return
- }
- title = time.Now().Format("2006-01-02") + "删除白名单用户"
- content = time.Now().Format("2006-01-02") + "删除白名单用户"
- fileName = downLoadnFilePaths
- var sendResult2 bool
- if len(listFrozenOutbound) > 0 {
- sendResult2 = utils.SendEmailByHongze(title, content, utils.EmaiWhiteUserList, fileName, title+".xlsx")
- }
- os.Remove(downLoadnFilePaths)
- //更新名单表
- if sendResult {
- if len(listMobile) > 0 {
- for _, v := range listMobile {
- item := new(models.WxUserWhite)
- item.Mobile = v.Mobile
- item.CountryCode = v.CountryCode
- item.CreatedTime = time.Now()
- item.CompanyName = v.CompanyName
- item.PermissionName = v.Permission
- item.UserCreatedTime = v.CreatedTime
- item.RealName = v.RealName
- item.SellerName = v.SellerName
- item.Status = v.Status
- _, err = models.AddWxUserWhite(item)
- if err != nil {
- msg = "获取失败,Err:" + err.Error()
- return
- }
- }
- }
- if len(listOutboundMobile) > 0 {
- for _, v := range listOutboundMobile {
- item := new(models.WxUserWhite)
- item.Mobile = v.Mobile
- item.OutboundMobile = v.Mobile
- item.OutboundCountryCode = v.CountryCode
- item.CreatedTime = time.Now()
- item.CompanyName = v.CompanyName
- item.PermissionName = v.Permission
- item.UserCreatedTime = v.CreatedTime
- item.RealName = v.RealName
- item.SellerName = v.SellerName
- item.Status = v.Status
- _, err = models.AddWxUserWhite(item)
- if err != nil {
- msg = "获取失败,Err:" + err.Error()
- return
- }
- }
- }
- }
- if sendResult2 {
- for _, v := range listFrozen {
- err = models.DeleteWxUserWhite(v)
- if err != nil {
- msg = "删除信息失败,Err:" + err.Error()
- return
- }
- }
- }
- fmt.Println("发送附件完成", len(listFrozen))
- return
- }
- // 获取用户权限
- func GetUserhasPermission(user *models.WxUserItem) (hasPermission int, err error) {
- //判断是否已经申请过
- applyCount, err := models.GetApplyRecordCount(user.UserId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- if applyCount > 0 {
- hasPermission = 3
- } else {
- hasPermission = 4
- }
- //HasPermission int `description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,已提交过申请,4:无该行业权限,未提交过申请,5:潜在客户,未提交过申请,6:潜在客户,已提交过申请"`
- if user.CompanyId > 1 {
- companyPermission, errPer := models.GetCompanyPermission(user.CompanyId)
- if errPer != nil {
- err = errPer
- return
- }
- if companyPermission == "" {
- if applyCount > 0 {
- hasPermission = 3
- } else {
- hasPermission = 4
- }
- } else {
- if strings.Contains(companyPermission, "医药") || strings.Contains(companyPermission, "科技") || strings.Contains(companyPermission, "消费") || strings.Contains(companyPermission, "智造") || strings.Contains(companyPermission, "策略") {
- hasPermission = 1
- }
- }
- }
- return
- }
- // 获取用户有没有开通任意一个行业权限
- func GetUserhasPermissionOne(user *models.WxUserItem) (hasPermission int, err error) {
- //判断是否已经申请过
- applyCount, err := models.GetApplyRecordCount(user.UserId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- if applyCount > 0 {
- hasPermission = 3
- } else {
- hasPermission = 4
- }
- //HasPermission int `description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,已提交过申请,4:无该行业权限,未提交过申请,5:潜在客户,未提交过申请,6:潜在客户,已提交过申请"`
- if user.CompanyId > 1 {
- companyPermission, errPer := models.GetCompanyPermission(user.CompanyId)
- if errPer != nil {
- err = errPer
- return
- }
- if companyPermission == "" {
- if applyCount > 0 {
- hasPermission = 3
- } else {
- hasPermission = 4
- }
- } else {
- hasPermission = 1
- }
- }
- return
- }
- // 每周五发送当前所有的权益用户
- func SendEmailAllUserWithRAI() (err error) {
- defer func() {
- if err != nil {
- fmt.Println("err:", err, time.Now())
- go utils.SendEmail("发送权益用户邮件失败"+"【"+utils.APPNAME+"】"+time.Now().Format(utils.FormatDateTime), ";Err:"+err.Error(), utils.EmailSendToUsers)
- utils.FileLog.Info("发送权益用户邮件失败,Err:%s", err.Error())
- }
- }()
- list, err := models.GetSendEmailAllUserWithRAI()
- if err != nil {
- return
- }
- //创建excel
- dir, err := os.Executable()
- exPath := filepath.Dir(dir)
- downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandDigit(5) + ".xlsx"
- xlsxFile := xlsx.NewFile()
- if err != nil {
- return
- }
- style := xlsx.NewStyle()
- alignment := xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- WrapText: true,
- }
- style.Alignment = alignment
- style.ApplyAlignment = true
- sheet, err := xlsxFile.AddSheet("权益用户名单")
- if err != nil {
- return
- }
- //设置宽度
- _ = sheet.SetColWidth(0, 0, 30)
- _ = sheet.SetColWidth(1, 1, 22)
- _ = sheet.SetColWidth(3, 3, 18)
- _ = sheet.SetColWidth(5, 5, 15)
- _ = sheet.SetColWidth(7, 8, 12)
- _ = sheet.SetColWidth(9, 9, 17)
- _ = sheet.SetColWidth(10, 10, 35)
- //标头
- rowTitle := sheet.AddRow()
- cellA := rowTitle.AddCell()
- cellA.Value = "客户名称"
- cellB := rowTitle.AddCell()
- cellB.Value = "社会信用码"
- cellC := rowTitle.AddCell()
- cellC.Value = "客户类型"
- cellD := rowTitle.AddCell()
- cellD.Value = "行业"
- cellE := rowTitle.AddCell()
- cellE.Value = "所属销售"
- cellF := rowTitle.AddCell()
- cellF.Value = "销售手机号"
- cellG := rowTitle.AddCell()
- cellG.Value = "状态"
- cellH := rowTitle.AddCell()
- cellH.Value = "服务起始期限"
- cellI := rowTitle.AddCell()
- cellI.Value = "服务结束期限"
- cellJ := rowTitle.AddCell()
- cellJ.Value = "创建时间"
- cellK := rowTitle.AddCell()
- cellK.Value = "权限"
- if len(list) > 0 {
- for _, item := range list {
- row := sheet.AddRow()
- cellA := row.AddCell()
- cellA.Value = item.CompanyName
- cellB := row.AddCell()
- cellB.Value = item.CreditCode
- cellC := row.AddCell()
- cellC.Value = item.ProductName
- cellD := row.AddCell()
- cellD.Value = item.IndustryName
- cellE := row.AddCell()
- cellE.Value = item.RealName
- cellF := row.AddCell()
- cellF.Value = item.Mobile
- cellG := row.AddCell()
- cellG.Value = item.Status
- cellH := row.AddCell()
- cellH.Value = item.StartDate
- cellI := row.AddCell()
- cellI.Value = item.EndDate
- cellJ := row.AddCell()
- cellJ.Value = item.CreatedTime
- cellK := row.AddCell()
- cellK.Value = item.Permission
- }
- }
- err = xlsxFile.Save(downLoadnFilePath)
- if err != nil {
- return
- }
- title := time.Now().Format(utils.FormatDate) + "权益用户名单"
- content := time.Now().Format(utils.FormatDate) + "权益用户名单"
- fileName := downLoadnFilePath
- if len(list) > 0 {
- utils.SendEmailByHongze(title, content, "cxzhang@hzinsights.com;tshen@hzinsights.com", fileName, title+".xlsx")
- }
- os.Remove(downLoadnFilePath)
- return
- }
- // 每周五发送发送这些公司下的用户
- func SendEmailAllUserWithCompany() (err error) {
- defer func() {
- if err != nil {
- fmt.Println("err:", err, time.Now())
- go utils.SendEmail("发送权益用户邮件失败"+"【"+utils.APPNAME+"】"+time.Now().Format(utils.FormatDateTime), ";Err:"+err.Error(), utils.EmailSendToUsers)
- utils.FileLog.Info("发送权益用户邮件失败,Err:%s", err.Error())
- }
- }()
- list, err := models.GetSendEmailAllUserWithCompany()
- if err != nil {
- return
- }
- //创建excel
- dir, err := os.Executable()
- exPath := filepath.Dir(dir)
- downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandDigit(5) + ".xlsx"
- xlsxFile := xlsx.NewFile()
- if err != nil {
- return
- }
- style := xlsx.NewStyle()
- alignment := xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- WrapText: true,
- }
- style.Alignment = alignment
- style.ApplyAlignment = true
- sheet, err := xlsxFile.AddSheet("私募客户联系人名单")
- if err != nil {
- return
- }
- //设置宽度
- _ = sheet.SetColWidth(1, 1, 15)
- _ = sheet.SetColWidth(6, 6, 22)
- _ = sheet.SetColWidth(7, 7, 32)
- //标头
- rowTitle := sheet.AddRow()
- cellA := rowTitle.AddCell()
- cellA.Value = "*姓名"
- cellB := rowTitle.AddCell()
- cellB.Value = "*手机号1"
- cellC := rowTitle.AddCell()
- cellC.Value = "国家号1"
- cellD := rowTitle.AddCell()
- cellD.Value = "手机号2"
- cellE := rowTitle.AddCell()
- cellE.Value = "国家号2"
- cellF := rowTitle.AddCell()
- cellF.Value = "座机"
- cellG := rowTitle.AddCell()
- cellG.Value = "*邮箱"
- cellH := rowTitle.AddCell()
- cellH.Value = "*所属公司"
- cellI := rowTitle.AddCell()
- cellI.Value = "性别"
- cellJ := rowTitle.AddCell()
- cellJ.Value = "*是否决策人"
- cellK := rowTitle.AddCell()
- cellK.Value = "部门"
- cellL := rowTitle.AddCell()
- cellL.Value = "职位"
- cellM := rowTitle.AddCell()
- cellM.Value = "所属销售"
- cellN := rowTitle.AddCell()
- cellN.Value = "等级"
- cellO := rowTitle.AddCell()
- cellO.Value = "附件权限"
- cellP := rowTitle.AddCell()
- cellP.Value = "标签"
- cellQ := rowTitle.AddCell()
- cellQ.Value = "近期调研"
- cellR := rowTitle.AddCell()
- cellR.Value = "创建时间"
- if len(list) > 0 {
- for _, item := range list {
- row := sheet.AddRow()
- cellA := row.AddCell()
- cellA.Value = item.RealName
- cellB := row.AddCell()
- cellB.Value = item.Mobile
- cellC := row.AddCell()
- if item.CountryCode != "" && item.Mobile != "" {
- cellC.Value = "+" + item.CountryCode
- }
- if item.CountryCode == "" && item.Mobile != "" {
- cellC.Value = "+86"
- }
- cellD := row.AddCell()
- cellD.Value = ""
- cellE := row.AddCell()
- cellE.Value = ""
- cellF := row.AddCell()
- cellF.Value = ""
- cellG := row.AddCell()
- cellG.Value = item.Email
- cellH := row.AddCell()
- cellH.Value = item.CompanyName
- cellI := row.AddCell()
- cellI.Value = ""
- cellJ := row.AddCell()
- if item.IsMaker == "1" {
- cellJ.Value = "是"
- } else {
- cellJ.Value = "否"
- }
- }
- }
- err = xlsxFile.Save(downLoadnFilePath)
- if err != nil {
- return
- }
- title := time.Now().Format(utils.FormatDate) + "私募客户联系人名单"
- content := time.Now().Format(utils.FormatDate) + "私募客户联系人名单"
- fileName := downLoadnFilePath
- if len(list) > 0 {
- utils.SendEmailByHongze(title, content, "cxzhang@hzinsights.com;tshen@hzinsights.com", fileName, title+".xlsx")
- }
- os.Remove(downLoadnFilePath)
- return
- }
- // 先关注后登录,更新用户是否关注过查研观向小助手公众号
- func UpdateCygxSubscribe(uid int, unionId string) (err error) {
- defer func() {
- if err != nil && err.Error() != utils.ErrNoRow() {
- go utils.SendAlarmMsg("先关注后登录,更新用户是否关注过查研观向小助手公众号失败"+err.Error()+"uid:"+strconv.Itoa(uid)+"unionId:"+unionId, 2)
- }
- }()
- if unionId == "" {
- err = errors.New("unionId为空,用户ID:" + strconv.Itoa(uid))
- return
- }
- detail, err := models.GetCygxUserRecordSubscribe(unionId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- if detail != nil {
- err = models.UserSubscribe(detail.SubscribeTime, uid)
- }
- return
- }
- // SendPermissionApplyTemplateMsgAdmin 处理试用申请给王芳,汪洋发消息
- func SendPermissionApplyTemplateMsgAdmin(req models.ApplyTryReq, usermobile, applyMethod, categoryApplyMethod ,redirectUrl string, isResearch bool) (err error) {
- defer func() {
- if err != nil {
- go utils.SendAlarmMsg("处理试用申请给王芳,汪洋发消息失败, ErrMsg: "+err.Error(), 3)
- }
- }()
- var configCode string
- //如果是研选的就推送给汪洋跟王芳,否则就推送给王芳
- configCode = utils.TPL_MSG_WANG_FANG_WANG_YANG
- cnf, e := models.GetConfigByCode(configCode)
- if e != nil {
- err = errors.New("GetConfigByCode, Err: " + e.Error() + configCode)
- return
- }
- openIdList, e := models.GetUserRecordListByMobile(12, cnf.ConfigValue)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = errors.New("GetUserRecordListByMobile, Err: " + e.Error() + cnf.ConfigValue)
- return err
- }
- for _, v := range openIdList {
- //go SendPermissionApplyTemplateMsg(req.RealName, req.CompanyName, usermobile, applyMethod, v)
- go SendPermissionApplyCategoryTemplateMsg(req.RealName, req.CompanyName, usermobile, categoryApplyMethod, v, redirectUrl)
- }
- //openIpItem, e := models.GetUserRecordByMobile(4, cnf.ConfigValue)
- //if e != nil {
- // err = errors.New("GetUserRecordByMobile, Err: " + e.Error() + cnf.ConfigValue)
- // return
- //}
- return
- }
|