user.go 34 KB

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