pushSet.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/init"
  5. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/type/bxsubscribe"
  6. "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXSubscribe/rpc/util"
  7. "bp.jydev.jianyu360.cn/BaseService/powerCheckCenter/rpc/pb"
  8. "fmt"
  9. "github.com/gogf/gf/v2/util/gconv"
  10. "regexp"
  11. "time"
  12. )
  13. type PushSetService struct {
  14. UserId string //mongodb 的用户id
  15. PositionType int64
  16. EntId int64
  17. EntUserId int64
  18. BaseUserId int64
  19. AccountId int64
  20. PositionId int64
  21. MgoUserId string
  22. }
  23. func (this *PushSetService) Update(item, setType, pushType string, ratemode, pushValue, interested int64, times []string) bool {
  24. set := map[string]interface{}{}
  25. switch setType {
  26. case "pushRoute":
  27. set = map[string]interface{}{
  28. fmt.Sprintf("o_pushset.%s.%s", item, pushType): common.IntAll(pushValue),
  29. }
  30. break
  31. case "a_times":
  32. if ratemode == 2 {
  33. if len(times) == 0 {
  34. return false
  35. }
  36. }
  37. set = map[string]interface{}{
  38. fmt.Sprintf("o_pushset.%s.a_times", item): times,
  39. fmt.Sprintf("o_pushset.%s.i_ratemode", item): common.IntAll(ratemode),
  40. }
  41. case "message":
  42. set = map[string]interface{}{
  43. fmt.Sprintf("o_pushset.%s.%s", item, pushType): common.IntAll(pushValue),
  44. }
  45. default:
  46. set = map[string]interface{}{
  47. "o_pushset.i_interested": common.IntAll(interested),
  48. }
  49. }
  50. update := false
  51. if (setType == "message" && this.PositionType == 1) || this.PositionType == 0 {
  52. update = IC.Mgo.UpdateById(util.USER, this.UserId, map[string]interface{}{
  53. "$set": set,
  54. })
  55. data, _ := IC.Mgo.FindById(util.USER, this.UserId, `{"o_pushset":1}`)
  56. (*data)["userid"] = this.UserId
  57. (*data)["createtime"] = time.Now().Unix()
  58. IC.MgoLog.Save("pushset_log", data)
  59. } else {
  60. update = IC.Mgo.Update(util.ENTUSER, map[string]interface{}{"i_entid": this.EntId, "i_userid": this.EntUserId}, map[string]interface{}{
  61. "$set": set,
  62. }, true, false)
  63. data, _ := IC.Mgo.FindOne(util.ENTUSER, map[string]interface{}{"i_entid": this.EntId, "i_userid": this.EntUserId})
  64. if data != nil {
  65. data1 := gconv.Map((*data)["o_pushset"])
  66. data1["i_userid"] = this.EntUserId
  67. data1["i_entid"] = this.EntId
  68. data1["createtime"] = time.Now().Unix()
  69. data1["userid"] = this.UserId
  70. IC.MgoLog.Save("pushset_log", data)
  71. }
  72. }
  73. return update
  74. }
  75. func (this *PushSetService) Find() map[string]*bxsubscribe.PushSet {
  76. pushSetMap := &map[string]interface{}{}
  77. o_msgPushSet := map[string]interface{}{}
  78. //
  79. ShowWx := false
  80. s_m_openid := ""
  81. pushSetMap, _ = IC.Mgo.FindById(util.USER, this.UserId, `{"o_pushset":":1,"s_m_openid":1,"l_registedate",1}`)
  82. if pushSetMap != nil && len(*pushSetMap) > 0 {
  83. s_m_openid = common.InterfaceToStr((*pushSetMap)["s_m_openid"])
  84. o_msgPushSet, _ = (*pushSetMap)["o_pushset"].(map[string]interface{})
  85. }
  86. registedate := int64(0)
  87. if s_m_openid != "" {
  88. //微信是否关注处理
  89. subscribeList := &[]map[string]interface{}{}
  90. subscribeList, _ = IC.Mgo.Find(util.SUBSCRIBE, map[string]interface{}{"s_m_openid": s_m_openid}, `{"l_date":-1}`, `{"s_event":1"}`, true, -1, -1)
  91. if subscribeList != nil && len(*subscribeList) > 0 {
  92. s_event := common.InterfaceToStr((*subscribeList)[0]["s_event"])
  93. if s_event == "subscribe" {
  94. ShowWx = true
  95. }
  96. }
  97. }
  98. registedate = common.Int64All((*pushSetMap)["l_registedate"])
  99. if this.PositionType == 1 {
  100. pushSetMap, _ = IC.Mgo.FindOne(util.ENTUSER, map[string]interface{}{"i_entid": this.EntId, "i_userid": this.EntUserId})
  101. }
  102. pushSet := map[string]*bxsubscribe.PushSet{}
  103. //用户权益获取
  104. powerData := IC.Middleground.PowerCheckCenter.Check("10000", this.MgoUserId, this.BaseUserId, this.AccountId, this.EntId, this.PositionType, this.PositionId)
  105. o_pushset := map[string]interface{}{}
  106. if pushSetMap != nil && len(*pushSetMap) > 0 {
  107. o_pushset, _ = (*pushSetMap)["o_pushset"].(map[string]interface{})
  108. }
  109. if o_pushset != nil {
  110. pushSet["interested"] = &bxsubscribe.PushSet{
  111. Interested: common.Int64All(common.If(o_pushset["i_interested"] == nil, 1, common.Int64All(o_pushset["i_interested"]))),
  112. }
  113. } else {
  114. pushSet["interested"] = &bxsubscribe.PushSet{
  115. Interested: 1,
  116. }
  117. }
  118. //推送设置
  119. fool, o_subset := pushSetMontage(o_pushset["o_subset"], "o_subset", powerData, ShowWx, registedate)
  120. if fool {
  121. pushSet["o_subset"] = o_subset
  122. }
  123. fool, o_week_report := pushSetMontage(o_pushset["o_week_report"], "o_week_report", powerData, ShowWx, registedate)
  124. if fool {
  125. pushSet["o_week_report"] = o_week_report
  126. }
  127. fool, o_month_report := pushSetMontage(o_pushset["o_month_report"], "o_month_report", powerData, ShowWx, registedate)
  128. if fool {
  129. pushSet["o_month_report"] = o_month_report
  130. }
  131. fool, o_newproject_forecast := pushSetMontage(o_pushset["o_newproject_forecast"], "o_newproject_forecast", powerData, ShowWx, registedate)
  132. if fool {
  133. pushSet["o_newproject_forecast"] = o_newproject_forecast
  134. }
  135. //企业信息
  136. fool, o_entinfo := pushSetMontage(o_pushset["o_entinfo"], "o_entinfo", powerData, ShowWx, registedate)
  137. if fool {
  138. pushSet["o_entinfo"] = o_entinfo
  139. }
  140. //项目关注
  141. fool, o_follow_project := pushSetMontage(o_pushset["o_follow_project"], "o_follow_project", powerData, ShowWx, registedate)
  142. if fool {
  143. pushSet["o_follow_project"] = o_follow_project
  144. }
  145. //业主监控 P303
  146. fool, o_owner_monitor := pushSetMontage(o_pushset["o_owner_monitor"], "o_owner_monitor", powerData, ShowWx, registedate)
  147. if fool {
  148. pushSet["o_owner_monitor"] = o_owner_monitor
  149. }
  150. //企业关注
  151. fool, o_follow_ent := pushSetMontage(o_pushset["o_follow_ent"], "o_follow_ent", powerData, ShowWx, registedate)
  152. if fool {
  153. pushSet["o_follow_ent"] = o_follow_ent
  154. }
  155. //消息
  156. fool, o_msg_active := pushSetMontage(o_msgPushSet["o_msg_active"], "o_msg_active", powerData, ShowWx, registedate)
  157. if fool {
  158. pushSet["o_msg_active"] = o_msg_active
  159. }
  160. //消息服务
  161. fool, o_msg_service := pushSetMontage(o_msgPushSet["o_msg_service"], "o_msg_service", powerData, ShowWx, registedate)
  162. if fool {
  163. pushSet["o_msg_service"] = o_msg_service
  164. }
  165. //剑鱼学堂
  166. fool, o_msg_jyschool := pushSetMontage(o_msgPushSet["o_msg_jyschool"], "o_msg_jyschool", powerData, ShowWx, registedate)
  167. if fool {
  168. pushSet["o_msg_jyschool"] = o_msg_jyschool
  169. }
  170. fool, o_msg_business := pushSetMontage(o_msgPushSet["o_msg_business"], "o_msg_business", powerData, ShowWx, registedate)
  171. if fool {
  172. pushSet["o_msg_business"] = o_msg_business
  173. }
  174. //私信
  175. fool, o_msg_pending := pushSetMontage(o_msgPushSet["o_msg_pending"], "o_msg_business", powerData, ShowWx, registedate)
  176. if fool {
  177. pushSet["o_msg_pending"] = o_msg_pending
  178. }
  179. fool, o_msg_privateletter := pushSetMontage(o_msgPushSet["o_msg_privateletter"], "o_msg_privateletter", powerData, ShowWx, registedate)
  180. if fool {
  181. pushSet["o_msg_privateletter"] = o_msg_privateletter
  182. }
  183. return pushSet
  184. }
  185. func pushSetMontage(in interface{}, name string, powerData *pb.CheckResp, ShowWx bool, registedate int64) (bool, *bxsubscribe.PushSet) {
  186. data := common.ObjToMap(in)
  187. a_times := []string{}
  188. isMailShow := int64(1)
  189. isReturnMailShow := true
  190. if !(data == nil || len(*data) == 0) {
  191. if (*data)["a_times"] != nil {
  192. a_times = common.ObjArrToStringArr((*data)["a_times"].([]interface{}))
  193. }
  194. }
  195. power := make(map[int]bool)
  196. for _, v := range powerData.Member.MemberPowerList {
  197. power[common.IntAll(v)] = true
  198. }
  199. returnData := &bxsubscribe.PushSet{}
  200. switch name {
  201. case "o_subset":
  202. if powerData.Free.IsFree {
  203. returnData = &bxsubscribe.PushSet{
  204. ATimes: []string{"每日上午、下午各推送1次"},
  205. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 0, (*data)["i_apppush"])),
  206. IApppushTip: common.Int64All(common.If((*data)["i_apppush_tip"] == nil, 0, (*data)["i_apppush_tip"])),
  207. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  208. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  209. IRatemode: 2,
  210. IsWxShow: 1,
  211. INomsgtip: common.Int64All(common.If((*data)["i_nomsgtip"] == nil, 1, (*data)["i_nomsgtip"])),
  212. }
  213. } else {
  214. if data == nil || len(*data) == 0 {
  215. returnData = &bxsubscribe.PushSet{
  216. ATimes: util.TimeMap[2],
  217. IApppush: 0,
  218. IApppushTip: 0,
  219. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  220. IMailpush: 0,
  221. IRatemode: 2,
  222. IsWxShow: 1,
  223. INomsgtip: 1,
  224. }
  225. } else {
  226. returnData = &bxsubscribe.PushSet{
  227. ATimes: common.If((*data)["i_ratemode"] == nil, util.TimeMap[2], a_times).([]string),
  228. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 0, (*data)["i_apppush"])),
  229. IApppushTip: common.Int64All(common.If((*data)["i_apppush_tip"] == nil, 0, (*data)["i_apppush_tip"])),
  230. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  231. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  232. IRatemode: common.Int64All(common.If((*data)["i_ratemode"] == nil, 2, (*data)["i_ratemode"])),
  233. IsWxShow: 1,
  234. INomsgtip: common.Int64All(common.If((*data)["i_nomsgtip"] == nil, 1, (*data)["i_nomsgtip"])),
  235. }
  236. if common.Int64All((*data)["i_ratemode"]) == 5 {
  237. returnData.ATimes = []string{"14:00"}
  238. }
  239. }
  240. }
  241. case "o_follow_project", "o_follow_ent":
  242. if powerData.Free.IsFree {
  243. returnData = &bxsubscribe.PushSet{
  244. ATimes: []string{"实时推送"},
  245. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  246. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  247. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  248. IRatemode: 1,
  249. IsWxShow: 1,
  250. }
  251. } else if data == nil || len(*data) == 0 {
  252. a_times = append(a_times, "实时推送")
  253. returnData = &bxsubscribe.PushSet{
  254. ATimes: a_times,
  255. IApppush: 1,
  256. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  257. IMailpush: 0,
  258. IRatemode: 1,
  259. IsWxShow: 1,
  260. }
  261. } else {
  262. returnData = &bxsubscribe.PushSet{
  263. ATimes: a_times,
  264. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  265. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  266. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  267. IRatemode: common.Int64All(common.If((*data)["i_ratemode"] == nil, 1, (*data)["i_ratemode"])),
  268. IsWxShow: 1,
  269. }
  270. }
  271. case "o_newproject_forecast":
  272. if powerData.Member.Status > 0 && power[9] {
  273. if data == nil || len(*data) == 0 {
  274. a_times = append(a_times, "实时推送")
  275. returnData = &bxsubscribe.PushSet{
  276. ATimes: a_times,
  277. IApppush: 1,
  278. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  279. IMailpush: 0,
  280. IRatemode: 1,
  281. IsWxShow: 1,
  282. }
  283. } else {
  284. returnData = &bxsubscribe.PushSet{
  285. ATimes: a_times,
  286. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  287. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  288. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  289. IRatemode: common.Int64All(common.If((*data)["i_ratemode"] == nil, 1, (*data)["i_ratemode"])),
  290. IsWxShow: 1,
  291. }
  292. }
  293. } else {
  294. return false, nil
  295. }
  296. case "o_owner_monitor":
  297. //付费用户
  298. if !powerData.Free.IsFree {
  299. if data == nil || len(*data) == 0 {
  300. a_times = append(a_times, "实时推送")
  301. returnData = &bxsubscribe.PushSet{
  302. ATimes: a_times,
  303. IApppush: 1,
  304. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  305. IMailpush: 0,
  306. IRatemode: 1,
  307. IsWxShow: 1,
  308. }
  309. } else {
  310. returnData = &bxsubscribe.PushSet{
  311. ATimes: a_times,
  312. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  313. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  314. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  315. IRatemode: common.Int64All(common.If((*data)["i_ratemode"] == nil, 1, (*data)["i_ratemode"])),
  316. IsWxShow: 1,
  317. }
  318. }
  319. } else {
  320. return false, nil
  321. }
  322. case "o_entinfo":
  323. if powerData.Member.Status > 0 && power[12] {
  324. if data == nil || len(*data) == 0 {
  325. a_times = append(a_times, "实时推送")
  326. returnData = &bxsubscribe.PushSet{
  327. ATimes: a_times,
  328. IApppush: 1,
  329. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  330. IMailpush: 0,
  331. IRatemode: 1,
  332. IsWxShow: 0,
  333. }
  334. } else {
  335. returnData = &bxsubscribe.PushSet{
  336. ATimes: a_times,
  337. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  338. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  339. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  340. IRatemode: common.Int64All(common.If((*data)["i_ratemode"] == nil, 1, (*data)["i_ratemode"])),
  341. IsWxShow: 0,
  342. }
  343. }
  344. } else {
  345. return false, nil
  346. }
  347. case "o_week_report", "o_month_report":
  348. if powerData.Vip.Status > 0 || power[10] {
  349. if name == "o_week_report" {
  350. a_times = append(a_times, "每周推送 (周一 08:00)")
  351. } else {
  352. a_times = append(a_times, "每月推送 (1日 08:00)")
  353. }
  354. IsWxShow := int64(0)
  355. if powerData.Vip.Status > 0 {
  356. IsWxShow = int64(1)
  357. }
  358. if !power[10] {
  359. isMailShow = int64(0)
  360. }
  361. if data == nil || len(*data) == 0 {
  362. returnData = &bxsubscribe.PushSet{
  363. ATimes: a_times,
  364. IApppush: 1,
  365. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  366. IMailpush: 0,
  367. IRatemode: 3,
  368. IsWxShow: IsWxShow,
  369. }
  370. } else {
  371. returnData = &bxsubscribe.PushSet{
  372. ATimes: a_times,
  373. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  374. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  375. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  376. IRatemode: 3,
  377. IsWxShow: IsWxShow,
  378. }
  379. }
  380. } else {
  381. return false, nil
  382. }
  383. case "o_msg_active", "o_msg_service", "o_msg_jyschool", "o_msg_privateletter", "o_msg_business", "o_msg_pending":
  384. isReturnMailShow = false
  385. var isWXShow int64
  386. isWXShow = 1
  387. if name == "o_msg_active" {
  388. isWXShow = 0
  389. }
  390. iApppush := int64(0)
  391. iWxpush := int64(0)
  392. if registedate > IC.C.Registedate {
  393. iApppush = 1
  394. if ShowWx {
  395. iWxpush = 1
  396. }
  397. }
  398. if data == nil || len(*data) == 0 {
  399. returnData = &bxsubscribe.PushSet{
  400. IApppush: iApppush,
  401. IWxpush: iWxpush,
  402. IsWxShow: isWXShow,
  403. }
  404. } else {
  405. returnData = &bxsubscribe.PushSet{
  406. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, iApppush, (*data)["i_apppush"])),
  407. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, iWxpush, (*data)["i_wxpush"])),
  408. IsWxShow: isWXShow,
  409. }
  410. }
  411. }
  412. if isReturnMailShow {
  413. returnData.IsMailShow = isMailShow
  414. }
  415. return true, returnData
  416. }
  417. func (this *PushSetService) SetUser(mail string) (bool, string) {
  418. var emailPattern = regexp.MustCompile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")
  419. if !emailPattern.MatchString(mail) {
  420. return false, "邮箱格式不正确"
  421. }
  422. set := map[string]interface{}{
  423. "o_pushset.s_email": mail,
  424. }
  425. update := false
  426. if this.PositionType == 0 {
  427. update = IC.Mgo.UpdateById(util.USER, this.UserId, map[string]interface{}{
  428. "$set": set,
  429. })
  430. } else {
  431. update = IC.Mgo.Update(util.ENTUSER, map[string]interface{}{"i_entid": this.EntId, "i_userid": this.EntUserId}, map[string]interface{}{
  432. "$set": set,
  433. }, true, false)
  434. IC.MainMysql.UpdateOrDeleteBySql(`update entniche_user set mail=? where id=? and ent_id=?`, mail, this.EntUserId, this.EntId)
  435. }
  436. return update, ""
  437. }