pushSet.go 14 KB

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