user.go 32 KB

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