123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095 |
- package services
- import (
- "context"
- "errors"
- "fmt"
- "github.com/tealeg/xlsx"
- "hongze/hongze_cygx/models"
- "hongze/hongze_cygx/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", 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 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)
- } 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 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记录
- //先添加第三方信息(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_cygx"
- 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 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
- }
|