user.go 33 KB

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