package services import ( "context" "errors" "fmt" "github.com/tealeg/xlsx" "hongze/hongze_cygx/models" "hongze/hongze_cygx/utils" "os" "path/filepath" "strconv" "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 err != 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 } //预约外呼名单,会前1小时自动发送邮件给专家组 func SendEmailUserWhiteList(cont context.Context) (err error) { var msg string var fieldStr string var condition string //var touser string defer func() { if err != nil { fmt.Println("err:", err) go utils.SendEmail("发送附件模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers) utils.FileLog.Info("发送附件模版消息失败,Err:%s", err.Error()) } if msg != "" { fmt.Println(msg) utils.FileLog.Info("发送模版消息失败,msg:%s", msg) } }() fmt.Println("发送附件") fieldStr = ` u.mobile,u.country_code,u.real_name,c.company_name,u.company_id,cp.seller_name,` //condition = ` AND u.mobile = u.outbound_mobile AND cp.status IN ( '正式', '试用' ) AND u.mobile != '' AND u.created_time > '2021-08-17 00:00:01' AND u.created_time < '2021-08-17 23:59:59' ` condition = ` AND u.mobile = u.outbound_mobile AND cp.status IN ( '冻结' ) AND u.mobile != '' AND u.company_id IN (6965,6965,5461,6167,6167,5461) ` list1, err := models.GetFormalUserWhiteList(fieldStr, condition) if err != nil { msg = "获取失败,Err:" + err.Error() return } fieldStr = ` u.outbound_mobile as mobile,u.outbound_country_code as country_code,u.real_name,c.company_name,u.company_id,` //condition = ` AND u.mobile != u.outbound_mobile AND cp.status IN ( '正式', '试用' ) AND u.outbound_mobile != '' AND u.created_time > '2021-08-17 00:00:01' AND u.created_time < '2021-08-17 23:59:59' ` condition = ` AND u.mobile != u.outbound_mobile AND cp.status IN ( '冻结' ) AND u.outbound_mobile != '' AND u.company_id IN (6965,6965,5461,6167,6167,5461) ` list2, err := models.GetFormalUserWhiteList(fieldStr, condition) if err != nil { msg = "获取失败,Err:" + err.Error() return } //fieldStr = `u.mobile,u.country_code,u.real_name,c.company_name,u.company_id,` //condition = ` AND u.mobile = u.outbound_mobile AND cp.status IN ( '永续' ) AND u.mobile != '' ` //list3, err := models.GetSustainableUserWhiteList(fieldStr, condition) //if err != nil { // msg = "获取失败,Err:" + err.Error() // return //} //fieldStr = ` u.outbound_mobile as mobile,u.outbound_country_code as country_code,u.real_name,c.company_name,u.company_id,` //condition = ` AND u.mobile != u.outbound_mobile AND cp.status IN ( '永续') AND u.outbound_mobile != '' ` //list4, err := models.GetSustainableUserWhiteList(fieldStr, condition) //if err != nil { // msg = "获取失败,Err:" + err.Error() // return //} var rep models.UserWhiteListRep for _, v := range list1 { rep.List = append(rep.List, v) } for _, v := range list2 { rep.List = append(rep.List, v) } //for _, v := range list3 { // rep.List = append(rep.List, v) //} //for _, v := range list4 { // rep.List = append(rep.List, v) //} //创建excel dir, errFile := os.Executable() exPath := filepath.Dir(dir) downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".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 } //标头 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 = "归属部门" cellL := rowTitle.AddCell() cellL.Value = "备注" cellM := rowTitle.AddCell() cellM.Value = "权限(消费,医药,智造,科技,策略)" for _, item := range rep.List { row := sheet.AddRow() cellA := row.AddCell() cellA.Value = item.RealName cellB := row.AddCell() cellB.Value = item.Mobile cellC := row.AddCell() cellC.Value = item.CountryCode if len(item.Mobile) >= 11 && item.CountryCode == "" { cellC.Value = "86" } cellD := row.AddCell() cellD.Value = item.CompanyName cellE := row.AddCell() cellE.Value = "" cellF := row.AddCell() cellF.Value = "" cellG := row.AddCell() cellG.Value = "" cellH := row.AddCell() cellH.Value = item.SellerName cellI := row.AddCell() cellI.Value = "" cellJ := row.AddCell() cellJ.Value = "" cellK := row.AddCell() cellK.Value = "" cellL := row.AddCell() cellL.Value = "" cellM := row.AddCell() if item.Permission == "" { item.Permission = "专家/医药/智造/消费/研选/科技/策略/路演服务" } cellM.Value = item.Permission } errFile = xlsxFile.Save(downLoadnFilePath) if errFile != nil { msg = "保存文件失败Err:" + errFile.Error() return } title := "用户白名单" content := "用户白名单" fileName := downLoadnFilePath //if activityInfo.ChartPermissionName == "科技" { // touser = utils.EmailTechnology //} else if activityInfo.ChartPermissionName == "医药" { // touser = utils.EmailMedicine //} else if activityInfo.ChartPermissionName == "消费" { // touser = utils.EmailConsumption //} else if activityInfo.ChartPermissionName == "智造" { // touser = utils.EmailZhizao //} else if activityInfo.ChartPermissionName == "策略" { // touser = utils.EmailStrategy //} //go utils.SendEmailHaveFile(title, content, fileName, "cxzhang@hzinsights.com;tshen@hzinsights.com") go utils.SendEmailHaveFile(title, content, fileName, "cxzhang@hzinsights.com") //go utils.SendEmailHaveFile(title, content, fileName, "tshen@hzinsights.com") time.Sleep(time.Duration(10) * time.Second) //延迟两秒,避免过多活动的时候邮件发送没有内容 //errFile = models.UPdateActivityIdToSendFile(v.ActivityId) //if errFile != nil { // msg = "获取失败,Err:" + errFile.Error() // return //} defer func() { os.Remove(downLoadnFilePath) }() fmt.Println("发送附件完成", len(rep.List)) return } //预约外呼名单,会前1小时自动发送邮件给专家组 func SendEmailUserWhiteListChange(cont context.Context) (err error) { var msg string var fieldStr string var condition string //var mobileStr string //var touser string defer func() { if err != nil { fmt.Println("err:", err, time.Now()) go utils.SendEmail("发送附件模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers) utils.FileLog.Info("发送附件模版消息失败,Err:%s", err.Error()) } if msg != "" { fmt.Println(msg) utils.FileLog.Info("发送模版消息失败,msg:%s", msg) } }() mobileStr, err := models.GetWxUserWhiteMobile() if err != nil { msg = "获取失败,Err:" + err.Error() return } if mobileStr == "" { mobileStr = "1" } fmt.Println("发送附件") //手机号新增 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 + `) ` list1, err := models.GetFormalUserWhiteList(fieldStr, condition) if err != nil { msg = "获取失败,Err:" + err.Error() return } //外呼手机号新增 outboundMobileStr, err := models.GetWxUserWhiteOutboundMobile() if outboundMobileStr == "" { 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 + `) ` list2, err := models.GetFormalUserWhiteList(fieldStr, condition) if err != nil { msg = "获取失败,Err:" + err.Error() return } var rep models.UserWhiteListRep for _, v := range list1 { rep.List = append(rep.List, v) } for _, v := range list2 { rep.List = append(rep.List, v) } //创建excel dir, errFile := os.Executable() exPath := filepath.Dir(dir) downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".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 } //标头 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 = "归属部门" cellL := rowTitle.AddCell() cellL.Value = "备注" cellM := rowTitle.AddCell() cellM.Value = "权限(消费,医药,智造,科技,策略)" for _, item := range rep.List { row := sheet.AddRow() cellA := row.AddCell() cellA.Value = item.RealName cellB := row.AddCell() cellB.Value = item.Mobile cellC := row.AddCell() cellC.Value = item.CountryCode if len(item.Mobile) >= 11 && item.CountryCode == "" { cellC.Value = "86" } cellD := row.AddCell() cellD.Value = item.CompanyName cellE := row.AddCell() cellE.Value = "" cellF := row.AddCell() cellF.Value = "" cellG := row.AddCell() cellG.Value = "" cellH := row.AddCell() cellH.Value = item.SellerName cellI := row.AddCell() cellI.Value = "" cellJ := row.AddCell() cellJ.Value = "" cellK := row.AddCell() cellK.Value = "" cellL := row.AddCell() cellL.Value = "" cellM := row.AddCell() if item.Permission == "" { item.Permission = "专家/医药/智造/消费/研选/科技/策略/路演服务" } cellM.Value = item.Permission } errFile = xlsxFile.Save(downLoadnFilePath) if errFile != nil { msg = "保存文件失败Err:" + errFile.Error() return } title := "新增用户白名单" content := "新增用户白名单" fileName := downLoadnFilePath //if activityInfo.ChartPermissionName == "科技" { // touser = utils.EmailTechnology //} else if activityInfo.ChartPermissionName == "医药" { // touser = utils.EmailMedicine //} else if activityInfo.ChartPermissionName == "消费" { // touser = utils.EmailConsumption //} else if activityInfo.ChartPermissionName == "智造" { // touser = utils.EmailZhizao //} else if activityInfo.ChartPermissionName == "策略" { // touser = utils.EmailStrategy //} go utils.SendEmailHaveFile(title, content, fileName, "cxzhang@hzinsights.com;tshen@hzinsights.com") //go utils.SendEmailHaveFile(title, content, fileName, "cxzhang@hzinsights.com") //go utils.SendEmailHaveFile(title, content, fileName, "tshen@hzinsights.com") time.Sleep(time.Duration(2) * time.Second) //延迟两秒,避免过多活动的时候邮件发送没有内容 //errFile = models.UPdateActivityIdToSendFile(v.ActivityId) //if errFile != nil { // msg = "获取失败,Err:" + errFile.Error() // return //} defer func() { os.Remove(downLoadnFilePath) }() //创建冻结excel dir, errFile = os.Executable() exPath = filepath.Dir(dir) downLoadnFilePath = exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".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 } //标头 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 = "归属部门" cellL = rowTitle.AddCell() cellL.Value = "备注" cellM = rowTitle.AddCell() cellM.Value = "权限(消费,医药,智造,科技,策略)" //手机号冻结 list3, err := models.GetFrozenUserWhiteList() if err != nil { msg = "获取失败,Err:" + err.Error() return } for _, item := range list3 { row := sheet.AddRow() cellA := row.AddCell() cellA.Value = item.RealName cellB := row.AddCell() if item.Mobile != "" { cellB.Value = item.Mobile } else { cellB.Value = item.OutboundMobile } cellC := row.AddCell() if item.CountryCode != "" { cellC.Value = item.CountryCode } else { cellC.Value = item.OutboundCountryCode } cellD := row.AddCell() cellD.Value = item.CompanyName cellE := row.AddCell() cellE.Value = "" cellF := row.AddCell() cellF.Value = "" cellG := row.AddCell() cellG.Value = "" cellH := row.AddCell() cellH.Value = item.SellerName cellI := row.AddCell() cellI.Value = "" cellJ := row.AddCell() cellJ.Value = "" cellK := row.AddCell() cellK.Value = "" cellL := row.AddCell() cellL.Value = "" cellM := row.AddCell() cellM.Value = item.PermissionName } errFile = xlsxFile.Save(downLoadnFilePath) if errFile != nil { msg = "保存文件失败Err:" + errFile.Error() return } title = "冻结用户白名单" content = "冻结用户白名单" fileName = downLoadnFilePath go utils.SendEmailHaveFile(title, content, fileName, "cxzhang@hzinsights.com;tshen@hzinsights.com") time.Sleep(time.Duration(2) * time.Second) //延迟两秒,避免过多活动的时候邮件发送没有内容 defer func() { os.Remove(downLoadnFilePath) }() fmt.Println(len(list2)) if len(list2) > 0 { for _, v := range list1 { 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(list2) > 0 { for _, v := range list2 { item := new(models.WxUserWhite) 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 } } } for _, v := range list3 { err = models.DeleteWxUserWhite(v) if err != nil { msg = "删除信息失败,Err:" + err.Error() return } } fmt.Println("发送附件完成", len(rep.List)) return }