user.go 30 KB

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