package service

import (
	"app.yhyue.com/moapp/jybase/common"
	"bp.jydev.jianyu360.cn/BaseService/powerCheckCenter/rpc/pb"
	"fmt"
	IC "jyBXSubscribe/rpc/init"
	"jyBXSubscribe/rpc/type/bxsubscribe"
	"jyBXSubscribe/rpc/util"
	"regexp"
)

type PushSetService struct {
	UserId       string //mongodb 的用户id
	PositionType int64
	EntId        int64
	EntUserId    int64
	BaseUserId   int64
	AccountId    int64
	PositionId   int64
	MgoUserId    string
}

func (this *PushSetService) Update(item, setType, pushType string, ratemode, pushValue, interested int64, times []string) bool {
	set := map[string]interface{}{}
	switch setType {
	case "pushRoute":
		set = map[string]interface{}{
			fmt.Sprintf("o_pushset.%s.%s", item, pushType): common.IntAll(pushValue),
		}
		break
	case "a_times":
		if ratemode == 2 {
			if len(times) == 0 {
				return false
			}
		}
		set = map[string]interface{}{
			fmt.Sprintf("o_pushset.%s.a_times", item):    times,
			fmt.Sprintf("o_pushset.%s.i_ratemode", item): common.IntAll(ratemode),
		}
	default:
		set = map[string]interface{}{
			"o_pushset.i_interested": common.IntAll(interested),
		}
	}
	update := false
	if this.PositionType == 0 {
		update = IC.Mgo.UpdateById(util.USER, this.UserId, map[string]interface{}{
			"$set": set,
		})
	} else {
		update = IC.Mgo.Update(util.ENTUSER, map[string]interface{}{"i_entid": this.EntId, "i_userid": this.EntUserId}, map[string]interface{}{
			"$set": set,
		}, true, false)
	}
	return update
}
func (this *PushSetService) Find() map[string]*bxsubscribe.PushSet {
	pushSetMap := &map[string]interface{}{}
	//
	ShowWx := false
	s_m_openid := ""
	pushSetMap, _ = IC.Mgo.FindById(util.USER, this.UserId, `{"o_pushset":":1,"s_m_openid":1}`)
	if pushSetMap != nil && len(*pushSetMap) > 0 {
		s_m_openid = common.InterfaceToStr((*pushSetMap)["s_m_openid"])
	}
	if s_m_openid != "" {
		//微信是否关注处理
		subscribeList := &[]map[string]interface{}{}
		subscribeList, _ = IC.Mgo.Find(util.SUBSCRIBE, map[string]interface{}{"s_m_openid": s_m_openid}, `{"l_date":-1}`, `{"s_event":1"}`, true, -1, -1)
		if subscribeList != nil && len(*subscribeList) > 0 {
			s_event := common.InterfaceToStr((*subscribeList)[0]["s_event"])
			if s_event == "subscribe" {
				ShowWx = true
			}
		}
	}

	if this.PositionType == 1 {
		pushSetMap, _ = IC.Mgo.FindOne(util.ENTUSER, map[string]interface{}{"i_entid": this.EntId, "i_userid": this.EntUserId})
	}
	pushSet := map[string]*bxsubscribe.PushSet{}
	//用户权益获取
	powerData := IC.Middleground.PowerCheckCenter.Check("10000", this.MgoUserId, this.BaseUserId, this.AccountId, this.EntId, this.PositionType, this.PositionId)
	o_pushset := map[string]interface{}{}
	if pushSetMap != nil && len(*pushSetMap) > 0 {
		o_pushset, _ = (*pushSetMap)["o_pushset"].(map[string]interface{})
	}
	if o_pushset != nil {
		pushSet["interested"] = &bxsubscribe.PushSet{
			Interested: common.Int64All(common.If(o_pushset["i_interested"] == nil, 1, common.Int64All(o_pushset["i_interested"]))),
		}
	} else {
		pushSet["interested"] = &bxsubscribe.PushSet{
			Interested: 1,
		}
	}
	fool, o_subset := pushSetMontage(o_pushset["o_subset"], "o_subset", powerData, ShowWx)
	if fool {
		pushSet["o_subset"] = o_subset
	}

	fool, o_week_report := pushSetMontage(o_pushset["o_week_report"], "o_week_report", powerData, ShowWx)
	if fool {
		pushSet["o_week_report"] = o_week_report
	}
	fool, o_month_report := pushSetMontage(o_pushset["o_month_report"], "o_month_report", powerData, ShowWx)
	if fool {
		pushSet["o_month_report"] = o_month_report
	}
	fool, o_newproject_forecast := pushSetMontage(o_pushset["o_newproject_forecast"], "o_newproject_forecast", powerData, ShowWx)
	if fool {
		pushSet["o_newproject_forecast"] = o_newproject_forecast
	}
	fool, o_entinfo := pushSetMontage(o_pushset["o_entinfo"], "o_entinfo", powerData, ShowWx)
	if fool {
		pushSet["o_entinfo"] = o_entinfo
	}
	fool, o_follow_project := pushSetMontage(o_pushset["o_follow_project"], "o_follow_project", powerData, ShowWx)
	if fool {
		pushSet["o_follow_project"] = o_follow_project
	}
	fool, o_follow_ent := pushSetMontage(o_pushset["o_follow_ent"], "o_follow_ent", powerData, ShowWx)
	if fool {
		pushSet["o_follow_ent"] = o_follow_ent
	}

	return pushSet
}
func pushSetMontage(in interface{}, name string, powerData *pb.CheckResp, ShowWx bool) (bool, *bxsubscribe.PushSet) {
	data := common.ObjToMap(in)
	a_times := []string{}
	isMailShow := int64(1)
	if !(data == nil || len(*data) == 0) {
		if (*data)["a_times"] != nil {
			a_times = common.ObjArrToStringArr((*data)["a_times"].([]interface{}))
		}
	}
	power := make(map[int]bool)
	for _, v := range powerData.Member.MemberPowerList {
		power[common.IntAll(v)] = true
	}
	returnData := &bxsubscribe.PushSet{}
	switch name {
	case "o_subset":
		if powerData.Free.IsFree {
			returnData = &bxsubscribe.PushSet{
				ATimes:    []string{"每日上午、下午各推送1次"},
				IApppush:  1,
				IWxpush:   common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
				IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
				IRatemode: 2,
				IsWxShow:  1,
				INomsgtip: common.Int64All(common.If((*data)["i_nomsgtip"] == nil, 1, (*data)["i_nomsgtip"])),
			}
		} else {
			if data == nil || len(*data) == 0 {
				returnData = &bxsubscribe.PushSet{
					ATimes:    util.TimeMap[2],
					IApppush:  1,
					IWxpush:   common.Int64All(common.If(ShowWx, 1, 0)),
					IMailpush: 0,
					IRatemode: 2,
					IsWxShow:  1,
					INomsgtip: 1,
				}
			} else {
				returnData = &bxsubscribe.PushSet{
					ATimes:    common.If((*data)["i_ratemode"] == nil, util.TimeMap[2], a_times).([]string),
					IApppush:  common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
					IWxpush:   common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
					IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
					IRatemode: common.Int64All(common.If((*data)["i_ratemode"] == nil, 2, (*data)["i_ratemode"])),
					IsWxShow:  1,
					INomsgtip: common.Int64All(common.If((*data)["i_nomsgtip"] == nil, 1, (*data)["i_nomsgtip"])),
				}
				if common.Int64All((*data)["i_ratemode"]) == 5 {
					returnData.ATimes = []string{"14:00"}
				}

			}
		}

	case "o_follow_project", "o_follow_ent":
		a_times = append(a_times, "实时推送")
		if data == nil || len(*data) == 0 {
			returnData = &bxsubscribe.PushSet{
				ATimes:    a_times,
				IApppush:  1,
				IWxpush:   common.Int64All(common.If(ShowWx, 1, 0)),
				IMailpush: 0,
				IRatemode: 1,
				IsWxShow:  1,
			}
		} else {
			returnData = &bxsubscribe.PushSet{
				ATimes:    a_times,
				IApppush:  common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
				IWxpush:   common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
				IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
				IRatemode: 1,
				IsWxShow:  1,
			}
		}
	case "o_newproject_forecast":
		a_times = append(a_times, "实时推送")
		if powerData.Member.Status > 0 && power[9] {
			if data == nil || len(*data) == 0 {
				returnData = &bxsubscribe.PushSet{
					ATimes:    a_times,
					IApppush:  1,
					IWxpush:   common.Int64All(common.If(ShowWx, 1, 0)),
					IMailpush: 0,
					IRatemode: 1,
					IsWxShow:  0,
				}
			} else {
				returnData = &bxsubscribe.PushSet{
					ATimes:    a_times,
					IApppush:  common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
					IWxpush:   common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
					IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
					IRatemode: 1,
					IsWxShow:  0,
				}
			}
		} else {
			return false, nil
		}
	case "o_entinfo":
		a_times = append(a_times, "实时推送")
		if powerData.Member.Status > 0 && power[12] {
			if data == nil || len(*data) == 0 {
				returnData = &bxsubscribe.PushSet{
					ATimes:    a_times,
					IApppush:  1,
					IWxpush:   common.Int64All(common.If(ShowWx, 1, 0)),
					IMailpush: 0,
					IRatemode: 1,
					IsWxShow:  0,
				}
			} else {
				returnData = &bxsubscribe.PushSet{
					ATimes:    a_times,
					IApppush:  common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
					IWxpush:   common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
					IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
					IRatemode: 1,
					IsWxShow:  0,
				}
			}
		} else {
			return false, nil
		}
	case "o_week_report", "o_month_report":
		if powerData.Vip.Status > 0 || power[10] {
			if name == "o_week_report" {
				a_times = append(a_times, "每周推送 (周五 09:00)")
			} else {
				a_times = append(a_times, "每月推送 (28日 09:00)")
			}
			IsWxShow := int64(0)
			if powerData.Vip.Status > 0 {
				IsWxShow = int64(1)
			}
			if !power[10] {
				isMailShow = int64(0)
			}
			if data == nil || len(*data) == 0 {
				returnData = &bxsubscribe.PushSet{
					ATimes:    a_times,
					IApppush:  1,
					IWxpush:   common.Int64All(common.If(ShowWx, 1, 0)),
					IMailpush: 0,
					IRatemode: 3,
					IsWxShow:  IsWxShow,
				}
			} else {
				returnData = &bxsubscribe.PushSet{
					ATimes:    a_times,
					IApppush:  common.Int64All(common.If((*data)["i_apppush"] == nil, 1, (*data)["i_apppush"])),
					IWxpush:   common.Int64All(common.If((*data)["i_wxpush"] == nil, common.If(ShowWx, 1, 0), common.If(ShowWx, (*data)["i_wxpush"], 0))),
					IMailpush: common.Int64All(common.If((*data)["i_mailpush"] == nil, 0, (*data)["i_mailpush"])),
					IRatemode: 3,
					IsWxShow:  IsWxShow,
				}
			}
		} else {
			return false, nil
		}
	}
	returnData.IsMailShow = isMailShow
	return true, returnData

}
func (this *PushSetService) SetUser(mail string) (bool, string) {
	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})(\\]?)$")
	if !emailPattern.MatchString(mail) {
		return false, "邮箱格式不正确"
	}
	set := map[string]interface{}{
		"o_pushset.s_email": mail,
	}
	update := false
	if this.PositionType == 0 {
		update = IC.Mgo.UpdateById(util.USER, this.UserId, map[string]interface{}{
			"$set": set,
		})
	} else {
		update = IC.Mgo.Update(util.ENTUSER, map[string]interface{}{"i_entid": this.EntId, "i_userid": this.EntUserId}, map[string]interface{}{
			"$set": set,
		}, true, false)
		IC.MainMysql.UpdateOrDeleteBySql(`update entniche_user set mail=? where id=? and ent_id=?`, mail, this.EntUserId, this.EntId)
	}
	return update, ""
}
