user.go 30 KB

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