pushSet.go 9.9 KB

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