pushSet.go 12 KB

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