user.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/tealeg/xlsx"
  7. "hongze/hongze_cygx/models"
  8. "hongze/hongze_cygx/utils"
  9. "os"
  10. "path/filepath"
  11. "strconv"
  12. "time"
  13. )
  14. var ERR_NO_USER_RECORD = errors.New("用户关系没有入库")
  15. var ERR_USER_NOT_BIND = errors.New("用户没有绑定")
  16. //通过openid获取用户信息
  17. func GetWxUserItemByOpenId(openid string) (item *models.WxUserItem, err error) {
  18. //通过openid获取用户关联信息
  19. userRecord, userRecordErr := models.GetUserRecordByOpenId(openid)
  20. fmt.Println("userRecordErr", userRecordErr)
  21. if userRecordErr != nil {
  22. if userRecordErr.Error() == utils.ErrNoRow() {
  23. err = ERR_NO_USER_RECORD
  24. return
  25. } else {
  26. err = userRecordErr
  27. return
  28. }
  29. }
  30. //该openid在系统中没有关联关系
  31. if userRecord == nil {
  32. err = ERR_NO_USER_RECORD
  33. return
  34. }
  35. //该openid没有绑定用户
  36. if userRecord.UserId <= 0 {
  37. err = ERR_USER_NOT_BIND
  38. item = new(models.WxUserItem)
  39. //格式化返回用户数据
  40. formatWxUserAndUserRecord(item, userRecord)
  41. return
  42. }
  43. //获取用户信息
  44. item, wxUserErr := models.GetWxUserItemByUserId(userRecord.UserId)
  45. fmt.Println("wxUserErr", wxUserErr)
  46. if wxUserErr != nil {
  47. err = wxUserErr
  48. //如果是找不到数据,那么可能是该用户被删除了,但是user_record没有删除对应的关系
  49. if wxUserErr.Error() == utils.ErrNoRow() {
  50. //用户被删除了,但是user_record没有删除对应的关系,那么去解除绑定
  51. userUnbindErr := models.UnBindUserRecordByOpenid(openid)
  52. if userUnbindErr != nil {
  53. err = userUnbindErr
  54. return
  55. }
  56. //返回状态为 用户未绑定 逻辑代码
  57. err = ERR_USER_NOT_BIND
  58. item = new(models.WxUserItem)
  59. //格式化返回用户数据
  60. formatWxUserAndUserRecord(item, userRecord)
  61. return
  62. }
  63. return
  64. }
  65. if item.RealName == "" {
  66. item.RealName = userRecord.RealName
  67. }
  68. //格式化返回用户数据
  69. formatWxUserAndUserRecord(item, userRecord)
  70. return
  71. }
  72. //根据用户id和平台id获取用户信息
  73. func GetWxUserItemByUserId(userId, platform int) (wxUserItem *models.WxUserItem, err error) {
  74. //获取用户信息
  75. wxUserItem, wxUserErr := models.GetWxUserItemByUserId(userId)
  76. if wxUserErr != nil {
  77. err = wxUserErr
  78. return
  79. }
  80. //格式化返回用户数据
  81. formatWxUser(wxUserItem, platform)
  82. return
  83. }
  84. //根据用户邮箱和平台id获取用户信息
  85. func GetWxUserItemByEmail(email string, platform int) (wxUserItem *models.WxUserItem, err error) {
  86. //获取用户信息
  87. wxUserItem, wxUserErr := models.GetWxUserItemByEmail(email)
  88. if wxUserErr != nil {
  89. err = wxUserErr
  90. return
  91. }
  92. //格式化返回用户数据
  93. formatWxUser(wxUserItem, platform)
  94. return
  95. }
  96. //根据用户手机号和平台id获取用户信息
  97. func GetWxUserItemByMobile(mobile string, platform int) (wxUserItem *models.WxUserItem, err error) {
  98. //获取用户信息
  99. wxUserItem, wxUserErr := models.GetWxUserItemByMobile(mobile)
  100. if wxUserErr != nil {
  101. err = wxUserErr
  102. return
  103. }
  104. //格式化返回用户数据
  105. formatWxUser(wxUserItem, platform)
  106. return
  107. }
  108. //根据用户unionid和平台id获取用户信息
  109. func GetWxUserItemByUnionId(unionId string, platform int) (wxUserItem *models.WxUserItem, err error) {
  110. //获取用户信息
  111. wxUserItem, wxUserErr := models.GetWxUserItemByUnionid(unionId)
  112. if wxUserErr != nil {
  113. err = wxUserErr
  114. return
  115. }
  116. //格式化返回用户数据
  117. formatWxUser(wxUserItem, platform)
  118. return
  119. }
  120. //通过用户 关系表记录 和 用户记录 格式化返回 用户数据
  121. func formatWxUserAndUserRecord(wxUser *models.WxUserItem, userRecord *models.UserRecord) {
  122. wxUser.OpenId = userRecord.OpenId
  123. wxUser.UnionId = userRecord.UnionId
  124. wxUser.NickName = userRecord.NickName
  125. //wxUser.RealName = userRecord.RealName
  126. //wxUser.BindAccount = userRecord.BindAccount
  127. wxUser.Headimgurl = userRecord.Headimgurl
  128. wxUser.SessionKey = userRecord.SessionKey
  129. }
  130. //通过用户 用户记录 和 来源平台 格式化返回 用户数据
  131. func formatWxUser(wxUser *models.WxUserItem, platform int) {
  132. //根据用户id和平台id获取用户关系
  133. userRecord, userRecordErr := models.GetUserRecordByUserId(wxUser.UserId, platform)
  134. if userRecordErr != nil {
  135. if userRecordErr.Error() != utils.ErrNoRow() {
  136. return
  137. }
  138. if userRecordErr.Error() == utils.ErrNoRow() {
  139. return
  140. }
  141. }
  142. //该openid在系统中没有关联关系
  143. if userRecord == nil {
  144. return
  145. }
  146. wxUser.OpenId = userRecord.OpenId
  147. wxUser.UnionId = userRecord.UnionId
  148. wxUser.NickName = userRecord.NickName
  149. //wxUser.RealName = userRecord.RealName
  150. //wxUser.BindAccount = userRecord.BindAccount
  151. wxUser.Headimgurl = userRecord.Headimgurl
  152. wxUser.SessionKey = userRecord.SessionKey
  153. return
  154. }
  155. //用户绑定
  156. func BindWxUser(openid, mobile, email, countryCode string) (wxUser *models.WxUserItem, err error) {
  157. if mobile == "" && email == "" {
  158. err = errors.New("手机号或邮箱必填一个")
  159. return
  160. }
  161. var bindAccount string
  162. //根据手机号获取用户信息
  163. if mobile != "" {
  164. tmpWxUser, wxUserErr := models.GetWxUserItemByMobile(mobile)
  165. if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
  166. err = wxUserErr
  167. return
  168. }
  169. wxUser = tmpWxUser
  170. bindAccount = mobile
  171. }
  172. //根据邮箱获取用户信息
  173. if wxUser == nil && email != "" {
  174. tmpWxUser, wxUserErr := models.GetWxUserItemByEmail(email)
  175. if wxUserErr != nil && wxUserErr.Error() != utils.ErrNoRow() {
  176. err = wxUserErr
  177. return
  178. }
  179. wxUser = tmpWxUser
  180. bindAccount = email
  181. }
  182. //查询openid的第三方(微信)信息
  183. userRecord, err := models.GetUserRecordByOpenId(openid)
  184. if err != nil {
  185. return
  186. }
  187. var userId int
  188. //如果查询出来的用户是nil,那么需要新增用户
  189. if wxUser == nil {
  190. user := &models.WxUser{
  191. CompanyId: 1,
  192. CreatedTime: time.Now(),
  193. FirstLogin: 1,
  194. Enabled: 1,
  195. RegisterPlatform: 4,
  196. RegisterTime: time.Now(),
  197. Mobile: mobile,
  198. Email: email,
  199. IsRegister: 1,
  200. Source: 3,
  201. CountryCode: countryCode,
  202. OutboundMobile: mobile,
  203. OutboundCountryCode: countryCode,
  204. }
  205. tmpUserId, addUserErr := models.AddWxUser(user)
  206. if err != nil {
  207. err = addUserErr
  208. return
  209. }
  210. user.UserId = int(tmpUserId)
  211. userId = int(tmpUserId)
  212. wxUser, err = models.GetWxUserItemByUserId(userId)
  213. } else {
  214. userId = wxUser.UserId
  215. err = models.BindUserOutboundMobile(mobile, countryCode, userId)
  216. if err != nil {
  217. return
  218. }
  219. if wxUser.IsRegister == 0 {
  220. models.ModifyWxUserRegisterStatus(userId)
  221. }
  222. }
  223. //如果存在该手机号/邮箱,那么需要校验
  224. if userRecord.UserId > 0 && userRecord.UserId != userId {
  225. err = errors.New("用户已绑定,不允许重复绑定")
  226. return
  227. }
  228. err = models.BindUserRecordByOpenid(userId, openid, bindAccount)
  229. if err != nil {
  230. return
  231. }
  232. userRecord.UserId = userId
  233. //如果当前该第三方用户信息的昵称为空串的话,那么需要去查询该用户的第一个绑定信息的数据作为来源做数据修复
  234. if userRecord.NickName == "" {
  235. oldUserRecord, err := models.GetUserThirdRecordByUserId(userId)
  236. if err == nil && oldUserRecord != nil {
  237. //如果该用户绑定的第一条数据的头像信息不为空串,那么就去做新数据的修复
  238. if oldUserRecord.NickName != "" {
  239. _ = models.ModifyUserRecordByDetail(userRecord.OpenId, userRecord.UnionId, oldUserRecord.NickName, oldUserRecord.Headimgurl, oldUserRecord.City, oldUserRecord.Province, oldUserRecord.Country, oldUserRecord.Sex, userId)
  240. }
  241. }
  242. }
  243. //格式化用户数据
  244. formatWxUserAndUserRecord(wxUser, userRecord)
  245. return
  246. }
  247. //微信登录
  248. func WxLogin(code, openId, unionId string, wxUserInfo *WxUserInfo) (token string, userId, firstLogin, permission int, err error) {
  249. if unionId == "" {
  250. unionId = wxUserInfo.Unionid
  251. }
  252. //firstLogin==1,强制绑定手机号或者邮箱
  253. firstLogin = 1
  254. fmt.Println("GetWxUserItemByOpenId ", openId)
  255. QUERY_WX_USER:
  256. wxUser, wxUserErr := GetWxUserItemByOpenId(openId)
  257. fmt.Println("wxUserErr", wxUserErr)
  258. if wxUserErr == ERR_NO_USER_RECORD { //没有用户openid记录
  259. //先添加第三方信息(openid等信息)
  260. _, recordErr := AddUserRecord(openId, unionId, wxUserInfo.Nickname, "", wxUserInfo.Province, wxUserInfo.City, wxUserInfo.Country, wxUserInfo.Headimgurl, wxUserInfo.SessionKey, utils.WxPlatform, wxUserInfo.Sex, 0)
  261. //如果插入失败,那么直接将错误信息返回
  262. if recordErr != nil {
  263. err = recordErr
  264. return
  265. }
  266. //插入成功后,需要重新查询该用户,并进入下面的逻辑
  267. goto QUERY_WX_USER
  268. } else if wxUserErr == ERR_USER_NOT_BIND {
  269. //没有用户信息
  270. //wxUser.FirstLogin = 1
  271. } else if wxUserErr != nil {
  272. err = wxUserErr
  273. return
  274. }
  275. fmt.Println("wxUserInfo", wxUserInfo)
  276. fmt.Println("wxUserInfo.Nickname", wxUserInfo.Nickname)
  277. fmt.Println("SessionKey", wxUserInfo.SessionKey)
  278. if wxUserInfo != nil {
  279. fmt.Println("ModifyUserRecordSessionKey")
  280. err = models.ModifyUserRecordSessionKey(openId, wxUserInfo.SessionKey)
  281. fmt.Println("ModifyUserRecordSessionKey Err", err)
  282. }
  283. //如果已经登录注册绑定的情况下
  284. if wxUser != nil && wxUserErr == nil {
  285. //获取用户权限
  286. firstLogin = wxUser.FirstLogin
  287. userId = wxUser.UserId
  288. {
  289. codeLog := new(models.WxUserCode)
  290. codeLog.WxCode = code
  291. codeLog.UserId = userId
  292. codeLog.Code = 0
  293. codeLog.FirstLogin = firstLogin
  294. codeLog.Authorization = token
  295. codeLog.UserPermission = permission
  296. codeLog.CreateTime = time.Now()
  297. go models.AddWxUserCode(codeLog)
  298. }
  299. if wxUser.Mobile == "" && wxUser.Email == "" {
  300. firstLogin = 1
  301. }
  302. }
  303. //获取登录token
  304. tokenItem, tokenErr := models.GetTokenByOpenId(openId)
  305. if tokenErr != nil && tokenErr.Error() != utils.ErrNoRow() {
  306. err = errors.New("登录失败,获取token失败:" + tokenErr.Error())
  307. return
  308. }
  309. fmt.Println("line 271 ", openId)
  310. if tokenItem == nil || (tokenErr != nil && tokenErr.Error() == utils.ErrNoRow()) {
  311. timeUnix := time.Now().Unix()
  312. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  313. token = utils.MD5(openId) + utils.MD5(timeUnixStr)
  314. //新增session
  315. {
  316. session := new(models.CygxSession)
  317. session.OpenId = openId
  318. session.UserId = userId
  319. session.CreatedTime = time.Now()
  320. session.LastUpdatedTime = time.Now()
  321. session.ExpireTime = time.Now().AddDate(0, 3, 0)
  322. session.AccessToken = token
  323. sessionErr := models.AddSession(session)
  324. if err != nil {
  325. err = errors.New("登录失败,新增用户session信息失败:" + sessionErr.Error())
  326. return
  327. }
  328. }
  329. } else {
  330. token = tokenItem.AccessToken
  331. }
  332. fmt.Println("line 294 ", token)
  333. //新增登录日志
  334. {
  335. loginLog := new(models.WxUserLog)
  336. loginLog.UserId = userId
  337. loginLog.OpenId = openId
  338. loginLog.UnionId = unionId
  339. loginLog.CreateTime = time.Now()
  340. loginLog.Handle = "wechat_login_cygx"
  341. loginLog.Remark = token
  342. go models.AddWxUserLog(loginLog)
  343. }
  344. return
  345. }
  346. func UserLogin() {
  347. }
  348. //添加第三方用户(微信)记录
  349. func AddUserRecord(openId, unionId, nickName, realName, province, city, country, headimgurl, sessionKey string, platform, sex, subscribe int) (userRecord *models.UserRecord, err error) {
  350. find, err := models.GetUserRecordByOpenId(openId)
  351. if err != nil && err.Error() != utils.ErrNoRow() {
  352. return
  353. }
  354. if find != nil {
  355. userRecord = find
  356. return
  357. }
  358. userRecord = &models.UserRecord{
  359. OpenId: openId, //用户open_id
  360. UnionId: unionId, //用户union_id
  361. Subscribe: subscribe,
  362. NickName: nickName, //用户昵称,最大长度:32
  363. RealName: realName, //用户实际名称,最大长度:32
  364. Sex: sex, //普通用户性别,1为男性,2为女性
  365. Province: province, //普通用户个人资料填写的省份,最大长度:30
  366. City: city, //普通用户个人资料填写的城市,最大长度:30
  367. Country: country, //国家,如中国为CN,最大长度:30
  368. Headimgurl: headimgurl, //用户第三方(微信)头像,最大长度:512
  369. CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间
  370. CreatePlatform: platform, //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1
  371. SessionKey: sessionKey, //微信小程序会话密钥,最大长度:255
  372. }
  373. recordId, err := models.AddUserRecord(userRecord)
  374. if err != nil {
  375. return
  376. }
  377. userRecord.UserRecordId = int(recordId)
  378. return
  379. }
  380. //预约外呼名单,会前1小时自动发送邮件给专家组
  381. func SendEmailUserWhiteList(cont context.Context) (err error) {
  382. var msg string
  383. var fieldStr string
  384. var condition string
  385. //var touser string
  386. defer func() {
  387. if err != nil {
  388. fmt.Println("err:", err)
  389. go utils.SendEmail("发送附件模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  390. utils.FileLog.Info("发送附件模版消息失败,Err:%s", err.Error())
  391. }
  392. if msg != "" {
  393. fmt.Println(msg)
  394. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  395. }
  396. }()
  397. fmt.Println("发送附件")
  398. fieldStr = ` u.mobile,u.country_code,u.real_name,c.company_name,u.company_id,cp.seller_name,`
  399. //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' `
  400. 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) `
  401. list1, err := models.GetFormalUserWhiteList(fieldStr, condition)
  402. if err != nil {
  403. msg = "获取失败,Err:" + err.Error()
  404. return
  405. }
  406. fieldStr = ` u.outbound_mobile as mobile,u.outbound_country_code as country_code,u.real_name,c.company_name,u.company_id,`
  407. //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' `
  408. 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) `
  409. list2, err := models.GetFormalUserWhiteList(fieldStr, condition)
  410. if err != nil {
  411. msg = "获取失败,Err:" + err.Error()
  412. return
  413. }
  414. //fieldStr = `u.mobile,u.country_code,u.real_name,c.company_name,u.company_id,`
  415. //condition = ` AND u.mobile = u.outbound_mobile AND cp.status IN ( '永续' ) AND u.mobile != '' `
  416. //list3, err := models.GetSustainableUserWhiteList(fieldStr, condition)
  417. //if err != nil {
  418. // msg = "获取失败,Err:" + err.Error()
  419. // return
  420. //}
  421. //fieldStr = ` u.outbound_mobile as mobile,u.outbound_country_code as country_code,u.real_name,c.company_name,u.company_id,`
  422. //condition = ` AND u.mobile != u.outbound_mobile AND cp.status IN ( '永续') AND u.outbound_mobile != '' `
  423. //list4, err := models.GetSustainableUserWhiteList(fieldStr, condition)
  424. //if err != nil {
  425. // msg = "获取失败,Err:" + err.Error()
  426. // return
  427. //}
  428. var rep models.UserWhiteListRep
  429. for _, v := range list1 {
  430. rep.List = append(rep.List, v)
  431. }
  432. for _, v := range list2 {
  433. rep.List = append(rep.List, v)
  434. }
  435. //for _, v := range list3 {
  436. // rep.List = append(rep.List, v)
  437. //}
  438. //for _, v := range list4 {
  439. // rep.List = append(rep.List, v)
  440. //}
  441. //创建excel
  442. dir, errFile := os.Executable()
  443. exPath := filepath.Dir(dir)
  444. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  445. xlsxFile := xlsx.NewFile()
  446. if errFile != nil {
  447. msg = "生成文件失败Err:" + errFile.Error()
  448. return
  449. }
  450. style := xlsx.NewStyle()
  451. alignment := xlsx.Alignment{
  452. Horizontal: "center",
  453. Vertical: "center",
  454. WrapText: true,
  455. }
  456. style.Alignment = alignment
  457. style.ApplyAlignment = true
  458. sheet, err := xlsxFile.AddSheet("白名单")
  459. if err != nil {
  460. msg = "新增Sheet失败,Err:" + err.Error()
  461. return
  462. }
  463. //标头
  464. rowTitle := sheet.AddRow()
  465. cellA := rowTitle.AddCell()
  466. cellA.Value = "姓名"
  467. cellB := rowTitle.AddCell()
  468. cellB.Value = "手机号"
  469. cellC := rowTitle.AddCell()
  470. cellC.Value = "国际代码"
  471. cellD := rowTitle.AddCell()
  472. cellD.Value = "公司"
  473. cellE := rowTitle.AddCell()
  474. cellE.Value = "职位"
  475. cellF := rowTitle.AddCell()
  476. cellF.Value = "邮箱"
  477. cellG := rowTitle.AddCell()
  478. cellG.Value = "客户类型"
  479. cellH := rowTitle.AddCell()
  480. cellH.Value = "对口销售"
  481. cellI := rowTitle.AddCell()
  482. cellI.Value = "有效开始时间"
  483. cellJ := rowTitle.AddCell()
  484. cellJ.Value = "有效结束时间"
  485. cellK := rowTitle.AddCell()
  486. cellK.Value = "归属部门"
  487. cellL := rowTitle.AddCell()
  488. cellL.Value = "备注"
  489. cellM := rowTitle.AddCell()
  490. cellM.Value = "权限(消费,医药,智造,科技,策略)"
  491. for _, item := range rep.List {
  492. row := sheet.AddRow()
  493. cellA := row.AddCell()
  494. cellA.Value = item.RealName
  495. cellB := row.AddCell()
  496. cellB.Value = item.Mobile
  497. cellC := row.AddCell()
  498. cellC.Value = item.CountryCode
  499. if len(item.Mobile) >= 11 && item.CountryCode == "" {
  500. cellC.Value = "86"
  501. }
  502. cellD := row.AddCell()
  503. cellD.Value = item.CompanyName
  504. cellE := row.AddCell()
  505. cellE.Value = ""
  506. cellF := row.AddCell()
  507. cellF.Value = ""
  508. cellG := row.AddCell()
  509. cellG.Value = ""
  510. cellH := row.AddCell()
  511. cellH.Value = item.SellerName
  512. cellI := row.AddCell()
  513. cellI.Value = ""
  514. cellJ := row.AddCell()
  515. cellJ.Value = ""
  516. cellK := row.AddCell()
  517. cellK.Value = ""
  518. cellL := row.AddCell()
  519. cellL.Value = ""
  520. cellM := row.AddCell()
  521. if item.Permission == "" {
  522. item.Permission = "专家/医药/智造/消费/研选/科技/策略/路演服务"
  523. }
  524. cellM.Value = item.Permission
  525. }
  526. errFile = xlsxFile.Save(downLoadnFilePath)
  527. if errFile != nil {
  528. msg = "保存文件失败Err:" + errFile.Error()
  529. return
  530. }
  531. title := "用户白名单"
  532. content := "用户白名单"
  533. fileName := downLoadnFilePath
  534. //if activityInfo.ChartPermissionName == "科技" {
  535. // touser = utils.EmailTechnology
  536. //} else if activityInfo.ChartPermissionName == "医药" {
  537. // touser = utils.EmailMedicine
  538. //} else if activityInfo.ChartPermissionName == "消费" {
  539. // touser = utils.EmailConsumption
  540. //} else if activityInfo.ChartPermissionName == "智造" {
  541. // touser = utils.EmailZhizao
  542. //} else if activityInfo.ChartPermissionName == "策略" {
  543. // touser = utils.EmailStrategy
  544. //}
  545. //go utils.SendEmailHaveFile(title, content, fileName, "cxzhang@hzinsights.com;tshen@hzinsights.com")
  546. go utils.SendEmailHaveFile(title, content, fileName, "cxzhang@hzinsights.com")
  547. //go utils.SendEmailHaveFile(title, content, fileName, "tshen@hzinsights.com")
  548. time.Sleep(time.Duration(10) * time.Second) //延迟两秒,避免过多活动的时候邮件发送没有内容
  549. //errFile = models.UPdateActivityIdToSendFile(v.ActivityId)
  550. //if errFile != nil {
  551. // msg = "获取失败,Err:" + errFile.Error()
  552. // return
  553. //}
  554. defer func() {
  555. os.Remove(downLoadnFilePath)
  556. }()
  557. fmt.Println("发送附件完成", len(rep.List))
  558. return
  559. }
  560. //预约外呼名单,会前1小时自动发送邮件给专家组
  561. func SendEmailUserWhiteListChange(cont context.Context) (err error) {
  562. var msg string
  563. var fieldStr string
  564. var condition string
  565. //var mobileStr string
  566. //var touser string
  567. defer func() {
  568. if err != nil {
  569. fmt.Println("err:", err, time.Now())
  570. go utils.SendEmail("发送附件模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
  571. utils.FileLog.Info("发送附件模版消息失败,Err:%s", err.Error())
  572. }
  573. if msg != "" {
  574. fmt.Println(msg)
  575. utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
  576. }
  577. }()
  578. mobileStr, err := models.GetWxUserWhiteMobile()
  579. if err != nil {
  580. msg = "获取失败,Err:" + err.Error()
  581. return
  582. }
  583. if mobileStr == "" {
  584. mobileStr = "1"
  585. }
  586. fmt.Println("发送附件")
  587. //手机号新增
  588. fieldStr = ` u.mobile,u.country_code,u.real_name,c.company_name,u.company_id,cp.seller_name,cp.status,`
  589. condition = ` AND cp.status IN ( '正式', '试用' ) AND u.mobile IN (` + mobileStr + `) `
  590. list1, err := models.GetFormalUserWhiteList(fieldStr, condition)
  591. if err != nil {
  592. msg = "获取失败,Err:" + err.Error()
  593. return
  594. }
  595. //外呼手机号新增
  596. outboundMobileStr, err := models.GetWxUserWhiteOutboundMobile()
  597. if outboundMobileStr == "" {
  598. outboundMobileStr = "1"
  599. }
  600. fieldStr = ` u.outbound_mobile as mobile,u.outbound_country_code as country_code,u.real_name,c.company_name,u.company_id,cp.status,`
  601. condition = ` AND cp.status IN ( '正式', '试用' ) AND u.outbound_mobile IN (` + outboundMobileStr + `) `
  602. list2, err := models.GetFormalUserWhiteList(fieldStr, condition)
  603. if err != nil {
  604. msg = "获取失败,Err:" + err.Error()
  605. return
  606. }
  607. var rep models.UserWhiteListRep
  608. for _, v := range list1 {
  609. rep.List = append(rep.List, v)
  610. }
  611. for _, v := range list2 {
  612. rep.List = append(rep.List, v)
  613. }
  614. //创建excel
  615. dir, errFile := os.Executable()
  616. exPath := filepath.Dir(dir)
  617. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  618. xlsxFile := xlsx.NewFile()
  619. if errFile != nil {
  620. msg = "生成文件失败Err:" + errFile.Error()
  621. return
  622. }
  623. style := xlsx.NewStyle()
  624. alignment := xlsx.Alignment{
  625. Horizontal: "center",
  626. Vertical: "center",
  627. WrapText: true,
  628. }
  629. style.Alignment = alignment
  630. style.ApplyAlignment = true
  631. sheet, err := xlsxFile.AddSheet("白名单")
  632. if err != nil {
  633. msg = "新增Sheet失败,Err:" + err.Error()
  634. return
  635. }
  636. //标头
  637. rowTitle := sheet.AddRow()
  638. cellA := rowTitle.AddCell()
  639. cellA.Value = "姓名"
  640. cellB := rowTitle.AddCell()
  641. cellB.Value = "手机号"
  642. cellC := rowTitle.AddCell()
  643. cellC.Value = "国际代码"
  644. cellD := rowTitle.AddCell()
  645. cellD.Value = "公司"
  646. cellE := rowTitle.AddCell()
  647. cellE.Value = "职位"
  648. cellF := rowTitle.AddCell()
  649. cellF.Value = "邮箱"
  650. cellG := rowTitle.AddCell()
  651. cellG.Value = "客户类型"
  652. cellH := rowTitle.AddCell()
  653. cellH.Value = "对口销售"
  654. cellI := rowTitle.AddCell()
  655. cellI.Value = "有效开始时间"
  656. cellJ := rowTitle.AddCell()
  657. cellJ.Value = "有效结束时间"
  658. cellK := rowTitle.AddCell()
  659. cellK.Value = "归属部门"
  660. cellL := rowTitle.AddCell()
  661. cellL.Value = "备注"
  662. cellM := rowTitle.AddCell()
  663. cellM.Value = "权限(消费,医药,智造,科技,策略)"
  664. for _, item := range rep.List {
  665. row := sheet.AddRow()
  666. cellA := row.AddCell()
  667. cellA.Value = item.RealName
  668. cellB := row.AddCell()
  669. cellB.Value = item.Mobile
  670. cellC := row.AddCell()
  671. cellC.Value = item.CountryCode
  672. if len(item.Mobile) >= 11 && item.CountryCode == "" {
  673. cellC.Value = "86"
  674. }
  675. cellD := row.AddCell()
  676. cellD.Value = item.CompanyName
  677. cellE := row.AddCell()
  678. cellE.Value = ""
  679. cellF := row.AddCell()
  680. cellF.Value = ""
  681. cellG := row.AddCell()
  682. cellG.Value = ""
  683. cellH := row.AddCell()
  684. cellH.Value = item.SellerName
  685. cellI := row.AddCell()
  686. cellI.Value = ""
  687. cellJ := row.AddCell()
  688. cellJ.Value = ""
  689. cellK := row.AddCell()
  690. cellK.Value = ""
  691. cellL := row.AddCell()
  692. cellL.Value = ""
  693. cellM := row.AddCell()
  694. if item.Permission == "" {
  695. item.Permission = "专家/医药/智造/消费/研选/科技/策略/路演服务"
  696. }
  697. cellM.Value = item.Permission
  698. }
  699. errFile = xlsxFile.Save(downLoadnFilePath)
  700. if errFile != nil {
  701. msg = "保存文件失败Err:" + errFile.Error()
  702. return
  703. }
  704. title := "新增用户白名单"
  705. content := "新增用户白名单"
  706. fileName := downLoadnFilePath
  707. //if activityInfo.ChartPermissionName == "科技" {
  708. // touser = utils.EmailTechnology
  709. //} else if activityInfo.ChartPermissionName == "医药" {
  710. // touser = utils.EmailMedicine
  711. //} else if activityInfo.ChartPermissionName == "消费" {
  712. // touser = utils.EmailConsumption
  713. //} else if activityInfo.ChartPermissionName == "智造" {
  714. // touser = utils.EmailZhizao
  715. //} else if activityInfo.ChartPermissionName == "策略" {
  716. // touser = utils.EmailStrategy
  717. //}
  718. go utils.SendEmailHaveFile(title, content, fileName, "cxzhang@hzinsights.com;tshen@hzinsights.com")
  719. //go utils.SendEmailHaveFile(title, content, fileName, "cxzhang@hzinsights.com")
  720. //go utils.SendEmailHaveFile(title, content, fileName, "tshen@hzinsights.com")
  721. time.Sleep(time.Duration(2) * time.Second) //延迟两秒,避免过多活动的时候邮件发送没有内容
  722. //errFile = models.UPdateActivityIdToSendFile(v.ActivityId)
  723. //if errFile != nil {
  724. // msg = "获取失败,Err:" + errFile.Error()
  725. // return
  726. //}
  727. defer func() {
  728. os.Remove(downLoadnFilePath)
  729. }()
  730. //创建冻结excel
  731. dir, errFile = os.Executable()
  732. exPath = filepath.Dir(dir)
  733. downLoadnFilePath = exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  734. xlsxFile = xlsx.NewFile()
  735. if errFile != nil {
  736. msg = "生成文件失败Err:" + errFile.Error()
  737. return
  738. }
  739. style = xlsx.NewStyle()
  740. alignment = xlsx.Alignment{
  741. Horizontal: "center",
  742. Vertical: "center",
  743. WrapText: true,
  744. }
  745. style.Alignment = alignment
  746. style.ApplyAlignment = true
  747. sheet, err = xlsxFile.AddSheet("白名单")
  748. if err != nil {
  749. msg = "新增Sheet失败,Err:" + err.Error()
  750. return
  751. }
  752. //标头
  753. rowTitle = sheet.AddRow()
  754. cellA = rowTitle.AddCell()
  755. cellA.Value = "姓名"
  756. cellB = rowTitle.AddCell()
  757. cellB.Value = "手机号"
  758. cellC = rowTitle.AddCell()
  759. cellC.Value = "国际代码"
  760. cellD = rowTitle.AddCell()
  761. cellD.Value = "公司"
  762. cellE = rowTitle.AddCell()
  763. cellE.Value = "职位"
  764. cellF = rowTitle.AddCell()
  765. cellF.Value = "邮箱"
  766. cellG = rowTitle.AddCell()
  767. cellG.Value = "客户类型"
  768. cellH = rowTitle.AddCell()
  769. cellH.Value = "对口销售"
  770. cellI = rowTitle.AddCell()
  771. cellI.Value = "有效开始时间"
  772. cellJ = rowTitle.AddCell()
  773. cellJ.Value = "有效结束时间"
  774. cellK = rowTitle.AddCell()
  775. cellK.Value = "归属部门"
  776. cellL = rowTitle.AddCell()
  777. cellL.Value = "备注"
  778. cellM = rowTitle.AddCell()
  779. cellM.Value = "权限(消费,医药,智造,科技,策略)"
  780. //手机号冻结
  781. list3, err := models.GetFrozenUserWhiteList()
  782. if err != nil {
  783. msg = "获取失败,Err:" + err.Error()
  784. return
  785. }
  786. for _, item := range list3 {
  787. row := sheet.AddRow()
  788. cellA := row.AddCell()
  789. cellA.Value = item.RealName
  790. cellB := row.AddCell()
  791. if item.Mobile != "" {
  792. cellB.Value = item.Mobile
  793. } else {
  794. cellB.Value = item.OutboundMobile
  795. }
  796. cellC := row.AddCell()
  797. if item.CountryCode != "" {
  798. cellC.Value = item.CountryCode
  799. } else {
  800. cellC.Value = item.OutboundCountryCode
  801. }
  802. cellD := row.AddCell()
  803. cellD.Value = item.CompanyName
  804. cellE := row.AddCell()
  805. cellE.Value = ""
  806. cellF := row.AddCell()
  807. cellF.Value = ""
  808. cellG := row.AddCell()
  809. cellG.Value = ""
  810. cellH := row.AddCell()
  811. cellH.Value = item.SellerName
  812. cellI := row.AddCell()
  813. cellI.Value = ""
  814. cellJ := row.AddCell()
  815. cellJ.Value = ""
  816. cellK := row.AddCell()
  817. cellK.Value = ""
  818. cellL := row.AddCell()
  819. cellL.Value = ""
  820. cellM := row.AddCell()
  821. cellM.Value = item.PermissionName
  822. }
  823. errFile = xlsxFile.Save(downLoadnFilePath)
  824. if errFile != nil {
  825. msg = "保存文件失败Err:" + errFile.Error()
  826. return
  827. }
  828. title = "冻结用户白名单"
  829. content = "冻结用户白名单"
  830. fileName = downLoadnFilePath
  831. go utils.SendEmailHaveFile(title, content, fileName, "cxzhang@hzinsights.com;tshen@hzinsights.com")
  832. time.Sleep(time.Duration(2) * time.Second) //延迟两秒,避免过多活动的时候邮件发送没有内容
  833. defer func() {
  834. os.Remove(downLoadnFilePath)
  835. }()
  836. fmt.Println(len(list2))
  837. if len(list2) > 0 {
  838. for _, v := range list1 {
  839. item := new(models.WxUserWhite)
  840. item.Mobile = v.Mobile
  841. item.CountryCode = v.CountryCode
  842. item.CreatedTime = time.Now()
  843. item.CompanyName = v.CompanyName
  844. item.PermissionName = v.Permission
  845. item.UserCreatedTime = v.CreatedTime
  846. item.RealName = v.RealName
  847. item.SellerName = v.SellerName
  848. item.Status = v.Status
  849. _, err = models.AddWxUserWhite(item)
  850. if err != nil {
  851. msg = "获取失败,Err:" + err.Error()
  852. return
  853. }
  854. }
  855. }
  856. if len(list2) > 0 {
  857. for _, v := range list2 {
  858. item := new(models.WxUserWhite)
  859. item.OutboundMobile = v.Mobile
  860. item.OutboundCountryCode = v.CountryCode
  861. item.CreatedTime = time.Now()
  862. item.CompanyName = v.CompanyName
  863. item.PermissionName = v.Permission
  864. item.UserCreatedTime = v.CreatedTime
  865. item.RealName = v.RealName
  866. item.SellerName = v.SellerName
  867. item.Status = v.Status
  868. _, err = models.AddWxUserWhite(item)
  869. if err != nil {
  870. msg = "获取失败,Err:" + err.Error()
  871. return
  872. }
  873. }
  874. }
  875. for _, v := range list3 {
  876. err = models.DeleteWxUserWhite(v)
  877. if err != nil {
  878. msg = "删除信息失败,Err:" + err.Error()
  879. return
  880. }
  881. }
  882. fmt.Println("发送附件完成", len(rep.List))
  883. return
  884. }