user.go 36 KB

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