pushSet.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 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. //
  63. ShowWx := false
  64. s_m_openid := ""
  65. pushSetMap, _ = IC.Mgo.FindById(util.USER, this.UserId, `{"o_pushset":":1,"s_m_openid":1}`)
  66. if pushSetMap != nil && len(*pushSetMap) > 0 {
  67. s_m_openid = common.InterfaceToStr((*pushSetMap)["s_m_openid"])
  68. }
  69. if s_m_openid != "" {
  70. //微信是否关注处理
  71. subscribeList := &[]map[string]interface{}{}
  72. subscribeList, _ = IC.Mgo.Find(util.SUBSCRIBE, map[string]interface{}{"s_m_openid": s_m_openid}, `{"l_date":-1}`, `{"s_event":1"}`, true, -1, -1)
  73. if subscribeList != nil && len(*subscribeList) > 0 {
  74. s_event := common.InterfaceToStr((*subscribeList)[0]["s_event"])
  75. if s_event == "subscribe" {
  76. ShowWx = true
  77. }
  78. }
  79. }
  80. if this.PositionType == 1 {
  81. pushSetMap, _ = IC.Mgo.FindOne(util.ENTUSER, map[string]interface{}{"i_entid": this.EntId, "i_userid": this.EntUserId})
  82. }
  83. pushSet := map[string]*bxsubscribe.PushSet{}
  84. //用户权益获取
  85. powerData := IC.Middleground.PowerCheckCenter.Check("10000", this.MgoUserId, this.BaseUserId, this.AccountId, this.EntId, this.PositionType, this.PositionId)
  86. o_pushset := map[string]interface{}{}
  87. if pushSetMap != nil && len(*pushSetMap) > 0 {
  88. o_pushset, _ = (*pushSetMap)["o_pushset"].(map[string]interface{})
  89. }
  90. if o_pushset != nil {
  91. pushSet["interested"] = &bxsubscribe.PushSet{
  92. Interested: common.Int64All(common.If(o_pushset["i_interested"] == nil, 1, common.Int64All(o_pushset["i_interested"]))),
  93. }
  94. } else {
  95. pushSet["interested"] = &bxsubscribe.PushSet{
  96. Interested: 1,
  97. }
  98. }
  99. fool, o_subset := pushSetMontage(o_pushset["o_subset"], "o_subset", powerData, ShowWx)
  100. if fool {
  101. pushSet["o_subset"] = o_subset
  102. }
  103. fool, o_week_report := pushSetMontage(o_pushset["o_week_report"], "o_week_report", powerData, ShowWx)
  104. if fool {
  105. pushSet["o_week_report"] = o_week_report
  106. }
  107. fool, o_month_report := pushSetMontage(o_pushset["o_month_report"], "o_month_report", powerData, ShowWx)
  108. if fool {
  109. pushSet["o_month_report"] = o_month_report
  110. }
  111. fool, o_newproject_forecast := pushSetMontage(o_pushset["o_newproject_forecast"], "o_newproject_forecast", powerData, ShowWx)
  112. if fool {
  113. pushSet["o_newproject_forecast"] = o_newproject_forecast
  114. }
  115. fool, o_entinfo := pushSetMontage(o_pushset["o_entinfo"], "o_entinfo", powerData, ShowWx)
  116. if fool {
  117. pushSet["o_entinfo"] = o_entinfo
  118. }
  119. fool, o_follow_project := pushSetMontage(o_pushset["o_follow_project"], "o_follow_project", powerData, ShowWx)
  120. if fool {
  121. pushSet["o_follow_project"] = o_follow_project
  122. }
  123. fool, o_follow_ent := pushSetMontage(o_pushset["o_follow_ent"], "o_follow_ent", powerData, ShowWx)
  124. if fool {
  125. pushSet["o_follow_ent"] = o_follow_ent
  126. }
  127. //消息
  128. fool, o_msg_active := pushSetMontage(o_pushset["o_msg_active"], "o_msg_active", powerData, ShowWx)
  129. if fool {
  130. pushSet["o_msg_active"] = o_msg_active
  131. }
  132. fool, o_msg_service := pushSetMontage(o_pushset["o_msg_service"], "o_msg_service", powerData, ShowWx)
  133. if fool {
  134. pushSet["o_msg_service"] = o_msg_service
  135. }
  136. fool, o_msg_jyschool := pushSetMontage(o_pushset["o_msg_jyschool"], "o_msg_jyschool", powerData, ShowWx)
  137. if fool {
  138. pushSet["o_msg_jyschool"] = o_msg_jyschool
  139. }
  140. fool, o_msg_privateletter := pushSetMontage(o_pushset["o_msg_privateletter"], "o_msg_privateletter", powerData, ShowWx)
  141. if fool {
  142. pushSet["o_msg_privateletter"] = o_msg_privateletter
  143. }
  144. return pushSet
  145. }
  146. func pushSetMontage(in interface{}, name string, powerData *pb.CheckResp, ShowWx bool) (bool, *bxsubscribe.PushSet) {
  147. data := common.ObjToMap(in)
  148. a_times := []string{}
  149. isMailShow := int64(1)
  150. if !(data == nil || len(*data) == 0) {
  151. if (*data)["a_times"] != nil {
  152. a_times = common.ObjArrToStringArr((*data)["a_times"].([]interface{}))
  153. }
  154. }
  155. power := make(map[int]bool)
  156. for _, v := range powerData.Member.MemberPowerList {
  157. power[common.IntAll(v)] = true
  158. }
  159. returnData := &bxsubscribe.PushSet{}
  160. switch name {
  161. case "o_subset":
  162. if powerData.Free.IsFree {
  163. returnData = &bxsubscribe.PushSet{
  164. ATimes: []string{"每日上午、下午各推送1次"},
  165. IApppush: 1,
  166. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  167. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  168. IRatemode: 2,
  169. IsWxShow: 1,
  170. INomsgtip: common.Int64All(common.If((*data)["i_nomsgtip"] == nil, 1, (*data)["i_nomsgtip"])),
  171. }
  172. } else {
  173. if data == nil || len(*data) == 0 {
  174. returnData = &bxsubscribe.PushSet{
  175. ATimes: util.TimeMap[2],
  176. IApppush: 1,
  177. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  178. IMailpush: 0,
  179. IRatemode: 2,
  180. IsWxShow: 1,
  181. INomsgtip: 1,
  182. }
  183. } else {
  184. returnData = &bxsubscribe.PushSet{
  185. ATimes: common.If((*data)["i_ratemode"] == nil, util.TimeMap[2], a_times).([]string),
  186. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  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: common.Int64All(common.If((*data)["i_ratemode"] == nil, 2, (*data)["i_ratemode"])),
  190. IsWxShow: 1,
  191. INomsgtip: common.Int64All(common.If((*data)["i_nomsgtip"] == nil, 1, (*data)["i_nomsgtip"])),
  192. }
  193. if common.Int64All((*data)["i_ratemode"]) == 5 {
  194. returnData.ATimes = []string{"14:00"}
  195. }
  196. }
  197. }
  198. case "o_follow_project", "o_follow_ent":
  199. a_times = append(a_times, "实时推送")
  200. if data == nil || len(*data) == 0 {
  201. returnData = &bxsubscribe.PushSet{
  202. ATimes: a_times,
  203. IApppush: 1,
  204. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  205. IMailpush: 0,
  206. IRatemode: 1,
  207. IsWxShow: 1,
  208. }
  209. } else {
  210. returnData = &bxsubscribe.PushSet{
  211. ATimes: a_times,
  212. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  213. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  214. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  215. IRatemode: 1,
  216. IsWxShow: 1,
  217. }
  218. }
  219. case "o_newproject_forecast":
  220. a_times = append(a_times, "实时推送")
  221. if powerData.Member.Status > 0 && power[9] {
  222. if data == nil || len(*data) == 0 {
  223. returnData = &bxsubscribe.PushSet{
  224. ATimes: a_times,
  225. IApppush: 1,
  226. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  227. IMailpush: 0,
  228. IRatemode: 1,
  229. IsWxShow: 0,
  230. }
  231. } else {
  232. returnData = &bxsubscribe.PushSet{
  233. ATimes: a_times,
  234. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  235. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  236. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  237. IRatemode: 1,
  238. IsWxShow: 0,
  239. }
  240. }
  241. } else {
  242. return false, nil
  243. }
  244. case "o_entinfo":
  245. a_times = append(a_times, "实时推送")
  246. if powerData.Member.Status > 0 && power[12] {
  247. if data == nil || len(*data) == 0 {
  248. returnData = &bxsubscribe.PushSet{
  249. ATimes: a_times,
  250. IApppush: 1,
  251. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  252. IMailpush: 0,
  253. IRatemode: 1,
  254. IsWxShow: 0,
  255. }
  256. } else {
  257. returnData = &bxsubscribe.PushSet{
  258. ATimes: a_times,
  259. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  260. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  261. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  262. IRatemode: 1,
  263. IsWxShow: 0,
  264. }
  265. }
  266. } else {
  267. return false, nil
  268. }
  269. case "o_week_report", "o_month_report":
  270. if powerData.Vip.Status > 0 || power[10] {
  271. if name == "o_week_report" {
  272. a_times = append(a_times, "每周推送 (周五 09:00)")
  273. } else {
  274. a_times = append(a_times, "每月推送 (28日 09:00)")
  275. }
  276. IsWxShow := int64(0)
  277. if powerData.Vip.Status > 0 {
  278. IsWxShow = int64(1)
  279. }
  280. if !power[10] {
  281. isMailShow = int64(0)
  282. }
  283. if data == nil || len(*data) == 0 {
  284. returnData = &bxsubscribe.PushSet{
  285. ATimes: a_times,
  286. IApppush: 1,
  287. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  288. IMailpush: 0,
  289. IRatemode: 3,
  290. IsWxShow: IsWxShow,
  291. }
  292. } else {
  293. returnData = &bxsubscribe.PushSet{
  294. ATimes: a_times,
  295. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  296. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  297. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  298. IRatemode: 3,
  299. IsWxShow: IsWxShow,
  300. }
  301. }
  302. } else {
  303. return false, nil
  304. }
  305. case "o_msg_active", "o_msg_service", "o_msg_jyschool", "o_msg_privateletter":
  306. if data == nil || len(*data) == 0 {
  307. returnData = &bxsubscribe.PushSet{
  308. IApppush: 0,
  309. IWxpush: 0,
  310. }
  311. } else {
  312. returnData = &bxsubscribe.PushSet{
  313. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 0, (*data)["i_apppush"])),
  314. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, 0, (*data)["i_wxpush"])),
  315. }
  316. }
  317. }
  318. returnData.IsMailShow = isMailShow
  319. return true, returnData
  320. }
  321. func (this *PushSetService) SetUser(mail string) (bool, string) {
  322. 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})(\\]?)$")
  323. if !emailPattern.MatchString(mail) {
  324. return false, "邮箱格式不正确"
  325. }
  326. set := map[string]interface{}{
  327. "o_pushset.s_email": mail,
  328. }
  329. update := false
  330. if this.PositionType == 0 {
  331. update = IC.Mgo.UpdateById(util.USER, this.UserId, map[string]interface{}{
  332. "$set": set,
  333. })
  334. } else {
  335. update = IC.Mgo.Update(util.ENTUSER, map[string]interface{}{"i_entid": this.EntId, "i_userid": this.EntUserId}, map[string]interface{}{
  336. "$set": set,
  337. }, true, false)
  338. IC.MainMysql.UpdateOrDeleteBySql(`update entniche_user set mail=? where id=? and ent_id=?`, mail, this.EntUserId, this.EntId)
  339. }
  340. return update, ""
  341. }