user.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. }