pushSet.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. }
  20. func (this *PushSetService) Update(item, setType, pushType string, ratemode, pushValue, interested int64, times []string) bool {
  21. set := map[string]interface{}{}
  22. switch setType {
  23. case "pushRoute":
  24. set = map[string]interface{}{
  25. fmt.Sprintf("o_pushset.%s.%s", item, pushType): common.IntAll(pushValue),
  26. }
  27. break
  28. case "a_times":
  29. set = map[string]interface{}{
  30. fmt.Sprintf("o_pushset.%s.a_times", item): times,
  31. fmt.Sprintf("o_pushset.%s.i_ratemode", item): common.IntAll(ratemode),
  32. }
  33. default:
  34. set = map[string]interface{}{
  35. "o_pushset.i_interested": common.IntAll(interested),
  36. }
  37. }
  38. update := false
  39. if this.PositionType == 0 {
  40. update = IC.Mgo.UpdateById(util.USER, this.UserId, map[string]interface{}{
  41. "$set": set,
  42. })
  43. } else {
  44. update = IC.Mgo.Update(util.ENTUSER, map[string]interface{}{"i_entid": this.EntId, "i_userid": this.EntUserId}, map[string]interface{}{
  45. "$set": set,
  46. }, true, false)
  47. }
  48. return update
  49. }
  50. func (this *PushSetService) Find() map[string]*bxsubscribe.PushSet {
  51. pushSetMap := &map[string]interface{}{}
  52. if this.PositionType == 0 {
  53. pushSetMap, _ = IC.Mgo.FindById(util.USER, this.UserId, `{"o_pushset":":1}`)
  54. } else {
  55. pushSetMap, _ = IC.Mgo.FindOne(util.ENTUSER, map[string]interface{}{"i_entid": this.EntId, "i_userid": this.EntUserId})
  56. }
  57. pushSet := map[string]*bxsubscribe.PushSet{}
  58. //用户权益获取
  59. powerData := IC.Middleground.PowerCheckCenter.Check("10000", this.UserId, this.BaseUserId, this.AccountId, this.EntId, this.PositionType, this.PositionId)
  60. o_pushset := map[string]interface{}{}
  61. if pushSetMap != nil && len(*pushSetMap) > 0 {
  62. o_pushset, _ = (*pushSetMap)["o_pushset"].(map[string]interface{})
  63. }
  64. if o_pushset != nil {
  65. pushSet["interested"] = &bxsubscribe.PushSet{
  66. Interested: common.Int64All(o_pushset["i_interested"]),
  67. }
  68. }
  69. fool, o_subset := pushSetMontage(o_pushset["o_subset"], "o_subset", powerData)
  70. if fool {
  71. pushSet["o_subset"] = o_subset
  72. }
  73. fool, o_week_report := pushSetMontage(o_pushset["o_week_report"], "o_week_report", powerData)
  74. if fool {
  75. pushSet["o_week_report"] = o_week_report
  76. }
  77. fool, o_month_report := pushSetMontage(o_pushset["o_month_report"], "o_month_report", powerData)
  78. if fool {
  79. pushSet["o_month_report"] = o_month_report
  80. }
  81. fool, o_newproject_forecast := pushSetMontage(o_pushset["o_newproject_forecast"], "o_newproject_forecast", powerData)
  82. if fool {
  83. pushSet["o_newproject_forecast"] = o_newproject_forecast
  84. }
  85. fool, o_entinfo := pushSetMontage(o_pushset["o_entinfo"], "o_entinfo", powerData)
  86. if fool {
  87. pushSet["o_entinfo"] = o_entinfo
  88. }
  89. fool, o_follow_project := pushSetMontage(o_pushset["o_follow_project"], "o_follow_project", powerData)
  90. if fool {
  91. pushSet["o_follow_project"] = o_follow_project
  92. }
  93. fool, o_follow_ent := pushSetMontage(o_pushset["o_follow_ent"], "o_follow_ent", powerData)
  94. if fool {
  95. pushSet["o_follow_ent"] = o_follow_ent
  96. }
  97. return pushSet
  98. }
  99. func pushSetMontage(in interface{}, name string, powerData *pb.CheckResp) (bool, *bxsubscribe.PushSet) {
  100. data := common.ObjToMap(in)
  101. a_times := []string{}
  102. if !(data == nil || len(*data) == 0) {
  103. if (*data)["a_times"] != nil {
  104. a_times = common.ObjArrToStringArr((*data)["a_times"].([]interface{}))
  105. }
  106. }
  107. returnData := &bxsubscribe.PushSet{}
  108. switch name {
  109. case "o_subset":
  110. if data == nil || len(*data) == 0 {
  111. returnData = &bxsubscribe.PushSet{
  112. ATimes: util.TimeMap[2],
  113. IApppush: 1,
  114. IWxpush: 1,
  115. IMailpush: 0,
  116. IRatemode: 2,
  117. IsWxShow: 1,
  118. }
  119. } else {
  120. returnData = &bxsubscribe.PushSet{
  121. ATimes: common.If((*data)["i_ratemode"] == nil, util.TimeMap[2], a_times).([]string),
  122. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  123. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, 1, (*data)["i_wxpush"])),
  124. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  125. IRatemode: common.Int64All(common.If((*data)["i_ratemode"] == nil, 2, (*data)["i_ratemode"])),
  126. IsWxShow: 1,
  127. }
  128. }
  129. case "o_follow_project", "o_follow_ent":
  130. a_times = append(a_times, "实时推送")
  131. if data == nil || len(*data) == 0 {
  132. returnData = &bxsubscribe.PushSet{
  133. ATimes: a_times,
  134. IApppush: 1,
  135. IWxpush: 1,
  136. IMailpush: 0,
  137. IRatemode: 1,
  138. IsWxShow: 1,
  139. }
  140. } else {
  141. returnData = &bxsubscribe.PushSet{
  142. ATimes: a_times,
  143. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  144. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, 1, (*data)["i_wxpush"])),
  145. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  146. IRatemode: 1,
  147. IsWxShow: 1,
  148. }
  149. }
  150. case "o_entinfo", "o_newproject_forecast":
  151. a_times = append(a_times, "实时推送")
  152. if powerData.Member.Status > 0 {
  153. if data == nil || len(*data) == 0 {
  154. returnData = &bxsubscribe.PushSet{
  155. ATimes: a_times,
  156. IApppush: 1,
  157. IWxpush: 1,
  158. IMailpush: 0,
  159. IRatemode: 1,
  160. IsWxShow: 0,
  161. }
  162. } else {
  163. returnData = &bxsubscribe.PushSet{
  164. ATimes: a_times,
  165. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  166. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, 1, (*data)["i_wxpush"])),
  167. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  168. IRatemode: 1,
  169. IsWxShow: 0,
  170. }
  171. }
  172. } else {
  173. return false, nil
  174. }
  175. case "o_week_report", "o_month_report":
  176. power := make(map[int]bool)
  177. for _, v := range powerData.Member.MemberPowerList {
  178. power[common.IntAll(v)] = true
  179. }
  180. if powerData.Vip.Status > 0 || power[10] {
  181. if name == "o_week_report" {
  182. a_times = append(a_times, "每周推送 (周五:09:00)")
  183. } else {
  184. a_times = append(a_times, "每月推送 (28日09:00)")
  185. }
  186. IsWxShow := int64(0)
  187. if powerData.Vip.Status > 0 {
  188. IsWxShow = int64(1)
  189. }
  190. if data == nil || len(*data) == 0 {
  191. returnData = &bxsubscribe.PushSet{
  192. ATimes: a_times,
  193. IApppush: 1,
  194. IWxpush: 1,
  195. IMailpush: 0,
  196. IRatemode: 3,
  197. IsWxShow: IsWxShow,
  198. }
  199. } else {
  200. returnData = &bxsubscribe.PushSet{
  201. ATimes: a_times,
  202. IApppush: common.Int64All((*data)["i_apppush"]),
  203. IWxpush: common.Int64All((*data)["i_wxpush"]),
  204. IMailpush: common.Int64All((*data)["i_mailpush"]),
  205. IRatemode: 3,
  206. IsWxShow: IsWxShow,
  207. }
  208. }
  209. } else {
  210. return false, nil
  211. }
  212. }
  213. return true, returnData
  214. }
  215. func (this *PushSetService) SetUser(mail string) (bool, string) {
  216. 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})(\\]?)$")
  217. if !emailPattern.MatchString(mail) {
  218. return false, "邮箱格式不正确"
  219. }
  220. set := map[string]interface{}{
  221. "o_pushset.s_email": mail,
  222. }
  223. update := false
  224. if this.PositionType == 0 {
  225. update = IC.Mgo.UpdateById(util.USER, this.UserId, map[string]interface{}{
  226. "$set": set,
  227. })
  228. } else {
  229. update = IC.Mgo.Update(util.ENTUSER, map[string]interface{}{"i_entid": this.EntId, "i_userid": this.EntUserId}, map[string]interface{}{
  230. "$set": set,
  231. }, true, false)
  232. IC.MainMysql.UpdateOrDeleteBySql(`update entniche_user set mail=? where id=? and ent_id=?`, mail, this.EntUserId, this.EntId)
  233. }
  234. return update, ""
  235. }