pushSet.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 powerData.Free.IsFree {
  141. returnData = &bxsubscribe.PushSet{
  142. ATimes: []string{"每日上午、下午各推送1次"},
  143. IApppush: 1,
  144. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  145. IMailpush: 0,
  146. IRatemode: 2,
  147. IsWxShow: 1,
  148. }
  149. } else {
  150. if data == nil || len(*data) == 0 {
  151. returnData = &bxsubscribe.PushSet{
  152. ATimes: util.TimeMap[2],
  153. IApppush: 1,
  154. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  155. IMailpush: 0,
  156. IRatemode: 2,
  157. IsWxShow: 1,
  158. }
  159. } else {
  160. returnData = &bxsubscribe.PushSet{
  161. ATimes: common.If((*data)["i_ratemode"] == nil, util.TimeMap[2], a_times).([]string),
  162. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  163. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  164. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  165. IRatemode: common.Int64All(common.If((*data)["i_ratemode"] == nil, 2, (*data)["i_ratemode"])),
  166. IsWxShow: 1,
  167. }
  168. if common.Int64All((*data)["i_ratemode"]) == 5 {
  169. returnData.ATimes = []string{"14:00"}
  170. }
  171. }
  172. }
  173. case "o_follow_project", "o_follow_ent":
  174. a_times = append(a_times, "实时推送")
  175. if data == nil || len(*data) == 0 {
  176. returnData = &bxsubscribe.PushSet{
  177. ATimes: a_times,
  178. IApppush: 1,
  179. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  180. IMailpush: 0,
  181. IRatemode: 1,
  182. IsWxShow: 1,
  183. }
  184. } else {
  185. returnData = &bxsubscribe.PushSet{
  186. ATimes: a_times,
  187. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  188. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  189. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  190. IRatemode: 1,
  191. IsWxShow: 1,
  192. }
  193. }
  194. case "o_newproject_forecast":
  195. a_times = append(a_times, "实时推送")
  196. if powerData.Member.Status > 0 && power[9] {
  197. if data == nil || len(*data) == 0 {
  198. returnData = &bxsubscribe.PushSet{
  199. ATimes: a_times,
  200. IApppush: 1,
  201. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  202. IMailpush: 0,
  203. IRatemode: 1,
  204. IsWxShow: 0,
  205. }
  206. } else {
  207. returnData = &bxsubscribe.PushSet{
  208. ATimes: a_times,
  209. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  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: 1,
  213. IsWxShow: 0,
  214. }
  215. }
  216. } else {
  217. return false, nil
  218. }
  219. case "o_entinfo":
  220. a_times = append(a_times, "实时推送")
  221. if powerData.Member.Status > 0 && power[12] {
  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_week_report", "o_month_report":
  245. if powerData.Vip.Status > 0 || power[10] {
  246. if name == "o_week_report" {
  247. a_times = append(a_times, "每周推送 (周五 09:00)")
  248. } else {
  249. a_times = append(a_times, "每月推送 (28日 09:00)")
  250. }
  251. IsWxShow := int64(0)
  252. if powerData.Vip.Status > 0 {
  253. IsWxShow = int64(1)
  254. }
  255. if !power[10] {
  256. isMailShow = int64(0)
  257. }
  258. if data == nil || len(*data) == 0 {
  259. returnData = &bxsubscribe.PushSet{
  260. ATimes: a_times,
  261. IApppush: 1,
  262. IWxpush: common.Int64All(common.If(ShowWx, 1, 0)),
  263. IMailpush: 0,
  264. IRatemode: 3,
  265. IsWxShow: IsWxShow,
  266. }
  267. } else {
  268. returnData = &bxsubscribe.PushSet{
  269. ATimes: a_times,
  270. IApppush: common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
  271. IWxpush: common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
  272. IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
  273. IRatemode: 3,
  274. IsWxShow: IsWxShow,
  275. }
  276. }
  277. } else {
  278. return false, nil
  279. }
  280. }
  281. returnData.IsMailShow = isMailShow
  282. return true, returnData
  283. }
  284. func (this *PushSetService) SetUser(mail string) (bool, string) {
  285. 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})(\\]?)$")
  286. if !emailPattern.MatchString(mail) {
  287. return false, "邮箱格式不正确"
  288. }
  289. set := map[string]interface{}{
  290. "o_pushset.s_email": mail,
  291. }
  292. update := false
  293. if this.PositionType == 0 {
  294. update = IC.Mgo.UpdateById(util.USER, this.UserId, map[string]interface{}{
  295. "$set": set,
  296. })
  297. } else {
  298. update = IC.Mgo.Update(util.ENTUSER, map[string]interface{}{"i_entid": this.EntId, "i_userid": this.EntUserId}, map[string]interface{}{
  299. "$set": set,
  300. }, true, false)
  301. IC.MainMysql.UpdateOrDeleteBySql(`update entniche_user set mail=? where id=? and ent_id=?`, mail, this.EntUserId, this.EntId)
  302. }
  303. return update, ""
  304. }