user.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. //手机号新增
  407. fieldStr = ` u.mobile,u.country_code,u.real_name,c.company_name,u.company_id,cp.seller_name,cp.status,`
  408. condition = ` AND cp.status IN ( '正式', '试用' ) AND u.mobile IN (` + mobileStr + `) `
  409. listMobile, err := models.GetFormalUserWhiteList(fieldStr, condition)
  410. if err != nil {
  411. msg = "获取失败,Err:" + err.Error()
  412. return
  413. }
  414. //外呼手机号新增
  415. outboundMobileStr, err := models.GetWxUserWhiteOutboundMobile()
  416. if outboundMobileStr == "" {
  417. outboundMobileStr = "1"
  418. }
  419. outboundMobileStr = strings.Replace(outboundMobileStr, " ", "", -1)
  420. fieldStr = ` u.outbound_mobile as mobile,u.outbound_country_code as country_code,u.real_name,c.company_name,u.company_id,cp.status,`
  421. condition = ` AND cp.status IN ( '正式', '试用' ) AND u.outbound_mobile IN (` + outboundMobileStr + `) `
  422. listOutboundMobile, err := models.GetFormalUserWhiteList(fieldStr, condition)
  423. if err != nil {
  424. msg = "获取失败,Err:" + err.Error()
  425. return
  426. }
  427. var rep models.UserWhiteListRep
  428. var repList []*models.UserWhiteList
  429. repList = listMobile
  430. if len(listOutboundMobile) > 0 {
  431. for _, v := range listOutboundMobile {
  432. repList = append(listMobile, v)
  433. }
  434. }
  435. rep.List = repList
  436. //创建excel
  437. dir, errFile := os.Executable()
  438. exPath := filepath.Dir(dir)
  439. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandDigit(5) + ".xlsx"
  440. xlsxFile := xlsx.NewFile()
  441. if errFile != nil {
  442. msg = "生成文件失败Err:" + errFile.Error()
  443. return
  444. }
  445. style := xlsx.NewStyle()
  446. alignment := xlsx.Alignment{
  447. Horizontal: "center",
  448. Vertical: "center",
  449. WrapText: true,
  450. }
  451. style.Alignment = alignment
  452. style.ApplyAlignment = true
  453. sheet, err := xlsxFile.AddSheet("白名单")
  454. if err != nil {
  455. msg = "新增Sheet失败,Err:" + err.Error()
  456. return
  457. }
  458. //设置宽度
  459. _ = sheet.SetColWidth(2, 2, 15)
  460. _ = sheet.SetColWidth(6, 6, 30)
  461. _ = sheet.SetColWidth(13, 13, 35)
  462. //标头
  463. rowTitle := sheet.AddRow()
  464. cellA := rowTitle.AddCell()
  465. cellA.Value = "姓名"
  466. cellB := rowTitle.AddCell()
  467. cellB.Value = "国际代码1"
  468. cellC := rowTitle.AddCell()
  469. cellC.Value = "手机号"
  470. cellD := rowTitle.AddCell()
  471. cellD.Value = "国际代码2"
  472. cellE := rowTitle.AddCell()
  473. cellE.Value = "备用号"
  474. cellF := rowTitle.AddCell()
  475. cellF.Value = "电子邮箱"
  476. cellG := rowTitle.AddCell()
  477. cellG.Value = "公司名称"
  478. cellH := rowTitle.AddCell()
  479. cellH.Value = "职位名称"
  480. cellI := rowTitle.AddCell()
  481. cellI.Value = "客户类型"
  482. cellJ := rowTitle.AddCell()
  483. cellJ.Value = "对口销售"
  484. cellK := rowTitle.AddCell()
  485. cellK.Value = "归属部门"
  486. cellL := rowTitle.AddCell()
  487. cellL.Value = "有效开始时间"
  488. cellM := rowTitle.AddCell()
  489. cellM.Value = "有效结束时间"
  490. cellN := rowTitle.AddCell()
  491. cellN.Value = "备注"
  492. cellO := rowTitle.AddCell()
  493. cellO.Value = "权限(消费,医药,智造,科技,策略)"
  494. if len(rep.List) > 0 {
  495. for _, item := range rep.List {
  496. row := sheet.AddRow()
  497. cellA := row.AddCell()
  498. cellA.Value = item.RealName
  499. cellB := row.AddCell()
  500. cellB.Value = item.CountryCode
  501. if len(item.Mobile) >= 11 && item.CountryCode == "" {
  502. cellB.Value = "86"
  503. }
  504. cellC := row.AddCell()
  505. cellC.Value = item.Mobile
  506. cellD := row.AddCell()
  507. cellD.Value = ""
  508. cellE := row.AddCell()
  509. cellE.Value = ""
  510. cellF := row.AddCell()
  511. cellF.Value = ""
  512. cellG := row.AddCell()
  513. cellG.Value = item.CompanyName
  514. cellH := row.AddCell()
  515. cellH.Value = ""
  516. cellI := row.AddCell()
  517. cellI.Value = ""
  518. cellJ := row.AddCell()
  519. cellJ.Value = item.SellerName
  520. cellK := row.AddCell()
  521. cellK.Value = ""
  522. cellL := row.AddCell()
  523. cellL.Value = ""
  524. cellM := row.AddCell()
  525. cellM.Value = ""
  526. cellN := row.AddCell()
  527. cellN.Value = ""
  528. cellO := row.AddCell()
  529. if item.Permission == "" {
  530. item.Permission = "专家/医药/智造/消费/研选/科技/策略/路演服务"
  531. }
  532. cellO.Value = item.Permission
  533. }
  534. }
  535. errFile = xlsxFile.Save(downLoadnFilePath)
  536. if errFile != nil {
  537. msg = "保存文件失败Err:" + errFile.Error()
  538. return
  539. }
  540. title := time.Now().Format("2006-01-02") + "新增白名单用户"
  541. content := time.Now().Format("2006-01-02") + "新增白名单用户"
  542. fileName := downLoadnFilePath
  543. var sendResult bool
  544. if len(rep.List) > 0 {
  545. sendResult = utils.SendEmailByHongze(title, content, utils.EmaiWhiteUserList, fileName, title+".xlsx")
  546. }
  547. os.Remove(downLoadnFilePath)
  548. //创建冻结excel
  549. dir, errFile = os.Executable()
  550. exPath = filepath.Dir(dir)
  551. downLoadnFilePaths := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandDigit(5) + ".xlsx"
  552. xlsxFile = xlsx.NewFile()
  553. if errFile != nil {
  554. msg = "生成文件失败Err:" + errFile.Error()
  555. return
  556. }
  557. style = xlsx.NewStyle()
  558. alignment = xlsx.Alignment{
  559. Horizontal: "center",
  560. Vertical: "center",
  561. WrapText: true,
  562. }
  563. style.Alignment = alignment
  564. style.ApplyAlignment = true
  565. sheet, err = xlsxFile.AddSheet("白名单")
  566. if err != nil {
  567. msg = "新增Sheet失败,Err:" + err.Error()
  568. return
  569. }
  570. //设置宽度
  571. _ = sheet.SetColWidth(2, 2, 15)
  572. _ = sheet.SetColWidth(6, 6, 30)
  573. _ = sheet.SetColWidth(13, 13, 35)
  574. //标头
  575. rowTitle = sheet.AddRow()
  576. cellA = rowTitle.AddCell()
  577. cellA.Value = "姓名"
  578. cellB = rowTitle.AddCell()
  579. cellB.Value = "国际代码1"
  580. cellC = rowTitle.AddCell()
  581. cellC.Value = "手机号"
  582. cellD = rowTitle.AddCell()
  583. cellD.Value = "国际代码2"
  584. cellE = rowTitle.AddCell()
  585. cellE.Value = "备用号"
  586. cellF = rowTitle.AddCell()
  587. cellF.Value = "电子邮箱"
  588. cellG = rowTitle.AddCell()
  589. cellG.Value = "公司名称"
  590. cellH = rowTitle.AddCell()
  591. cellH.Value = "职位名称"
  592. cellI = rowTitle.AddCell()
  593. cellI.Value = "客户类型"
  594. cellJ = rowTitle.AddCell()
  595. cellJ.Value = "对口销售"
  596. cellK = rowTitle.AddCell()
  597. cellK.Value = "归属部门"
  598. cellL = rowTitle.AddCell()
  599. cellL.Value = "有效开始时间"
  600. cellM = rowTitle.AddCell()
  601. cellM.Value = "有效结束时间"
  602. cellN = rowTitle.AddCell()
  603. cellN.Value = "备注"
  604. cellO = rowTitle.AddCell()
  605. cellO.Value = "权限(消费,医药,智造,科技,策略)"
  606. //手机号冻结
  607. listFrozen, err := models.GetFrozenUserWhiteList() //手机号用户修改
  608. listFrozenOutbound, err := models.GetFrozenUserWhiteListOutbound() //外呼手机号用户修改
  609. if len(listFrozenOutbound) > 0 {
  610. for _, v := range listFrozenOutbound {
  611. listFrozen = append(listFrozen, v)
  612. }
  613. }
  614. if err != nil {
  615. msg = "获取失败,Err:" + err.Error()
  616. return
  617. }
  618. if len(listFrozen) > 0 {
  619. for _, item := range listFrozen {
  620. row := sheet.AddRow()
  621. cellA := row.AddCell()
  622. cellA.Value = item.RealName
  623. cellB := row.AddCell()
  624. cellB.Value = item.CountryCode
  625. if len(item.Mobile) >= 11 && item.CountryCode == "" {
  626. cellB.Value = "86"
  627. }
  628. cellC := row.AddCell()
  629. cellC.Value = item.Mobile
  630. cellD := row.AddCell()
  631. cellD.Value = ""
  632. cellE := row.AddCell()
  633. cellE.Value = ""
  634. cellF := row.AddCell()
  635. cellF.Value = ""
  636. cellG := row.AddCell()
  637. cellG.Value = item.CompanyName
  638. cellH := row.AddCell()
  639. cellH.Value = ""
  640. cellI := row.AddCell()
  641. cellI.Value = ""
  642. cellJ := row.AddCell()
  643. cellJ.Value = item.SellerName
  644. cellK := row.AddCell()
  645. cellK.Value = ""
  646. cellL := row.AddCell()
  647. cellL.Value = ""
  648. cellM := row.AddCell()
  649. cellM.Value = ""
  650. cellN := row.AddCell()
  651. cellN.Value = ""
  652. cellO := row.AddCell()
  653. cellO.Value = item.PermissionName
  654. }
  655. }
  656. errFile = xlsxFile.Save(downLoadnFilePaths)
  657. if errFile != nil {
  658. msg = "保存文件失败Err:" + errFile.Error()
  659. return
  660. }
  661. title = time.Now().Format("2006-01-02") + "删除白名单用户"
  662. content = time.Now().Format("2006-01-02") + "删除白名单用户"
  663. fileName = downLoadnFilePaths
  664. var sendResult2 bool
  665. if len(listFrozen) > 0 {
  666. sendResult2 = utils.SendEmailByHongze(title, content, utils.EmaiWhiteUserList, fileName, title+".xlsx")
  667. }
  668. fmt.Println(sendResult2)
  669. fmt.Println(sendResult)
  670. os.Remove(downLoadnFilePaths)
  671. //更新名单表
  672. if sendResult {
  673. if len(listMobile) > 0 {
  674. for _, v := range listMobile {
  675. item := new(models.WxUserWhite)
  676. item.Mobile = v.Mobile
  677. item.CountryCode = v.CountryCode
  678. item.CreatedTime = time.Now()
  679. item.CompanyName = v.CompanyName
  680. item.PermissionName = v.Permission
  681. item.UserCreatedTime = v.CreatedTime
  682. item.RealName = v.RealName
  683. item.SellerName = v.SellerName
  684. item.Status = v.Status
  685. _, err = models.AddWxUserWhite(item)
  686. if err != nil {
  687. msg = "获取失败,Err:" + err.Error()
  688. return
  689. }
  690. }
  691. }
  692. if len(listOutboundMobile) > 0 {
  693. for _, v := range listOutboundMobile {
  694. item := new(models.WxUserWhite)
  695. item.OutboundMobile = v.Mobile
  696. item.OutboundCountryCode = v.CountryCode
  697. item.CreatedTime = time.Now()
  698. item.CompanyName = v.CompanyName
  699. item.PermissionName = v.Permission
  700. item.UserCreatedTime = v.CreatedTime
  701. item.RealName = v.RealName
  702. item.SellerName = v.SellerName
  703. item.Status = v.Status
  704. _, err = models.AddWxUserWhite(item)
  705. if err != nil {
  706. msg = "获取失败,Err:" + err.Error()
  707. return
  708. }
  709. }
  710. }
  711. }
  712. if sendResult2 {
  713. for _, v := range listFrozen {
  714. err = models.DeleteWxUserWhite(v)
  715. if err != nil {
  716. msg = "删除信息失败,Err:" + err.Error()
  717. return
  718. }
  719. }
  720. }
  721. fmt.Println("发送附件完成", len(listFrozen))
  722. return
  723. }
  724. //获取用户权限
  725. func GetUserhasPermission(user *models.WxUserItem) (hasPermission int, err error) {
  726. //判断是否已经申请过
  727. applyCount, err := models.GetApplyRecordCount(user.UserId)
  728. if err != nil && err.Error() != utils.ErrNoRow() {
  729. return
  730. }
  731. if applyCount > 0 {
  732. hasPermission = 3
  733. } else {
  734. hasPermission = 4
  735. }
  736. //HasPermission int `description:"1:有该行业权限,正常展示,2:无该行业权限,不存在权益客户下,3:无该品类权限,已提交过申请,4:无该行业权限,未提交过申请,5:潜在客户,未提交过申请,6:潜在客户,已提交过申请"`
  737. if user.CompanyId > 1 {
  738. companyPermission, errPer := models.GetCompanyPermission(user.CompanyId)
  739. if errPer != nil {
  740. err = errPer
  741. return
  742. }
  743. if companyPermission == "" {
  744. if applyCount > 0 {
  745. hasPermission = 3
  746. } else {
  747. hasPermission = 4
  748. }
  749. } else {
  750. if strings.Contains(companyPermission, "医药") || strings.Contains(companyPermission, "科技") || strings.Contains(companyPermission, "消费") || strings.Contains(companyPermission, "智造") {
  751. hasPermission = 1
  752. }
  753. }
  754. }
  755. return
  756. }
  757. //每周五发送当前所有的权益用户
  758. func SendEmailAllUserWithRAI() (err error) {
  759. defer func() {
  760. if err != nil {
  761. fmt.Println("err:", err, time.Now())
  762. go utils.SendEmail("发送权益用户邮件失败"+"【"+utils.APPNAME+"】"+time.Now().Format(utils.FormatDateTime), ";Err:"+err.Error(), utils.EmailSendToUsers)
  763. utils.FileLog.Info("发送权益用户邮件失败,Err:%s", err.Error())
  764. }
  765. }()
  766. list, err := models.GetSendEmailAllUserWithRAI()
  767. if err != nil {
  768. return
  769. }
  770. //创建excel
  771. dir, err := os.Executable()
  772. exPath := filepath.Dir(dir)
  773. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandDigit(5) + ".xlsx"
  774. xlsxFile := xlsx.NewFile()
  775. if err != nil {
  776. return
  777. }
  778. style := xlsx.NewStyle()
  779. alignment := xlsx.Alignment{
  780. Horizontal: "center",
  781. Vertical: "center",
  782. WrapText: true,
  783. }
  784. style.Alignment = alignment
  785. style.ApplyAlignment = true
  786. sheet, err := xlsxFile.AddSheet("权益用户名单")
  787. if err != nil {
  788. return
  789. }
  790. //设置宽度
  791. _ = sheet.SetColWidth(0, 0, 30)
  792. _ = sheet.SetColWidth(1, 1, 22)
  793. _ = sheet.SetColWidth(3, 3, 18)
  794. _ = sheet.SetColWidth(5, 5, 15)
  795. _ = sheet.SetColWidth(7, 8, 12)
  796. _ = sheet.SetColWidth(9, 9, 17)
  797. _ = sheet.SetColWidth(10, 10, 35)
  798. //标头
  799. rowTitle := sheet.AddRow()
  800. cellA := rowTitle.AddCell()
  801. cellA.Value = "客户名称"
  802. cellB := rowTitle.AddCell()
  803. cellB.Value = "社会信用码"
  804. cellC := rowTitle.AddCell()
  805. cellC.Value = "客户类型"
  806. cellD := rowTitle.AddCell()
  807. cellD.Value = "行业"
  808. cellE := rowTitle.AddCell()
  809. cellE.Value = "所属销售"
  810. cellF := rowTitle.AddCell()
  811. cellF.Value = "销售手机号"
  812. cellG := rowTitle.AddCell()
  813. cellG.Value = "状态"
  814. cellH := rowTitle.AddCell()
  815. cellH.Value = "服务起始期限"
  816. cellI := rowTitle.AddCell()
  817. cellI.Value = "服务结束期限"
  818. cellJ := rowTitle.AddCell()
  819. cellJ.Value = "创建时间"
  820. cellK := rowTitle.AddCell()
  821. cellK.Value = "权限"
  822. if len(list) > 0 {
  823. for _, item := range list {
  824. row := sheet.AddRow()
  825. cellA := row.AddCell()
  826. cellA.Value = item.CompanyName
  827. cellB := row.AddCell()
  828. cellB.Value = item.CreditCode
  829. cellC := row.AddCell()
  830. cellC.Value = item.ProductName
  831. cellD := row.AddCell()
  832. cellD.Value = item.IndustryName
  833. cellE := row.AddCell()
  834. cellE.Value = item.RealName
  835. cellF := row.AddCell()
  836. cellF.Value = item.Mobile
  837. cellG := row.AddCell()
  838. cellG.Value = item.Status
  839. cellH := row.AddCell()
  840. cellH.Value = item.StartDate
  841. cellI := row.AddCell()
  842. cellI.Value = item.EndDate
  843. cellJ := row.AddCell()
  844. cellJ.Value = item.CreatedTime
  845. cellK := row.AddCell()
  846. cellK.Value = item.Permission
  847. }
  848. }
  849. err = xlsxFile.Save(downLoadnFilePath)
  850. if err != nil {
  851. return
  852. }
  853. title := time.Now().Format(utils.FormatDate) + "权益用户名单"
  854. content := time.Now().Format(utils.FormatDate) + "权益用户名单"
  855. fileName := downLoadnFilePath
  856. if len(list) > 0 {
  857. utils.SendEmailByHongze(title, content, "cxzhang@hzinsights.com;tshen@hzinsights.com", fileName, title+".xlsx")
  858. }
  859. os.Remove(downLoadnFilePath)
  860. return
  861. }
  862. //每周五发送发送这些公司下的用户
  863. func SendEmailAllUserWithCompany() (err error) {
  864. defer func() {
  865. if err != nil {
  866. fmt.Println("err:", err, time.Now())
  867. go utils.SendEmail("发送权益用户邮件失败"+"【"+utils.APPNAME+"】"+time.Now().Format(utils.FormatDateTime), ";Err:"+err.Error(), utils.EmailSendToUsers)
  868. utils.FileLog.Info("发送权益用户邮件失败,Err:%s", err.Error())
  869. }
  870. }()
  871. list, err := models.GetSendEmailAllUserWithCompany()
  872. if err != nil {
  873. return
  874. }
  875. //创建excel
  876. dir, err := os.Executable()
  877. exPath := filepath.Dir(dir)
  878. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + utils.GetRandDigit(5) + ".xlsx"
  879. xlsxFile := xlsx.NewFile()
  880. if err != nil {
  881. return
  882. }
  883. style := xlsx.NewStyle()
  884. alignment := xlsx.Alignment{
  885. Horizontal: "center",
  886. Vertical: "center",
  887. WrapText: true,
  888. }
  889. style.Alignment = alignment
  890. style.ApplyAlignment = true
  891. sheet, err := xlsxFile.AddSheet("私募客户联系人名单")
  892. if err != nil {
  893. return
  894. }
  895. //设置宽度
  896. _ = sheet.SetColWidth(1, 1, 15)
  897. _ = sheet.SetColWidth(6, 6, 22)
  898. _ = sheet.SetColWidth(7, 7, 32)
  899. //标头
  900. rowTitle := sheet.AddRow()
  901. cellA := rowTitle.AddCell()
  902. cellA.Value = "*姓名"
  903. cellB := rowTitle.AddCell()
  904. cellB.Value = "*手机号1"
  905. cellC := rowTitle.AddCell()
  906. cellC.Value = "国家号1"
  907. cellD := rowTitle.AddCell()
  908. cellD.Value = "手机号2"
  909. cellE := rowTitle.AddCell()
  910. cellE.Value = "国家号2"
  911. cellF := rowTitle.AddCell()
  912. cellF.Value = "座机"
  913. cellG := rowTitle.AddCell()
  914. cellG.Value = "*邮箱"
  915. cellH := rowTitle.AddCell()
  916. cellH.Value = "*所属公司"
  917. cellI := rowTitle.AddCell()
  918. cellI.Value = "性别"
  919. cellJ := rowTitle.AddCell()
  920. cellJ.Value = "*是否决策人"
  921. cellK := rowTitle.AddCell()
  922. cellK.Value = "部门"
  923. cellL := rowTitle.AddCell()
  924. cellL.Value = "职位"
  925. cellM := rowTitle.AddCell()
  926. cellM.Value = "所属销售"
  927. cellN := rowTitle.AddCell()
  928. cellN.Value = "等级"
  929. cellO := rowTitle.AddCell()
  930. cellO.Value = "附件权限"
  931. cellP := rowTitle.AddCell()
  932. cellP.Value = "标签"
  933. cellQ := rowTitle.AddCell()
  934. cellQ.Value = "近期调研"
  935. cellR := rowTitle.AddCell()
  936. cellR.Value = "创建时间"
  937. if len(list) > 0 {
  938. for _, item := range list {
  939. row := sheet.AddRow()
  940. cellA := row.AddCell()
  941. cellA.Value = item.RealName
  942. cellB := row.AddCell()
  943. cellB.Value = item.Mobile
  944. cellC := row.AddCell()
  945. if item.CountryCode != "" && item.Mobile != "" {
  946. cellC.Value = "+" + item.CountryCode
  947. }
  948. if item.CountryCode == "" && item.Mobile != "" {
  949. cellC.Value = "+86"
  950. }
  951. cellD := row.AddCell()
  952. cellD.Value = ""
  953. cellE := row.AddCell()
  954. cellE.Value = ""
  955. cellF := row.AddCell()
  956. cellF.Value = ""
  957. cellG := row.AddCell()
  958. cellG.Value = item.Email
  959. cellH := row.AddCell()
  960. cellH.Value = item.CompanyName
  961. cellI := row.AddCell()
  962. cellI.Value = ""
  963. cellJ := row.AddCell()
  964. if item.IsMaker == "1" {
  965. cellJ.Value = "是"
  966. } else {
  967. cellJ.Value = "否"
  968. }
  969. }
  970. }
  971. err = xlsxFile.Save(downLoadnFilePath)
  972. if err != nil {
  973. return
  974. }
  975. title := time.Now().Format(utils.FormatDate) + "私募客户联系人名单"
  976. content := time.Now().Format(utils.FormatDate) + "私募客户联系人名单"
  977. fileName := downLoadnFilePath
  978. if len(list) > 0 {
  979. utils.SendEmailByHongze(title, content, "cxzhang@hzinsights.com;tshen@hzinsights.com", fileName, title+".xlsx")
  980. }
  981. os.Remove(downLoadnFilePath)
  982. return
  983. }