package util

import (
	MC "app.yhyue.com/moapp/jybase/common"
	"app.yhyue.com/moapp/jybase/encrypt"
	"app.yhyue.com/moapp/jybase/mongodb"
	"app.yhyue.com/moapp/jybase/mysql"
)

const (
	USER      = "user"
	ENTUSER   = "ent_user"
	SUBSCRIBE = "jy_subscribe"
)

var ServiceMap = map[string]string{
	"app": "85",
	"pc":  "84",
	"wx":  "86",
}
var TimeMap = map[int][]string{
	1: []string{},
	2: []string{"09:00", "14:00"},
	3: []string{"09:00"},
	4: []string{"09:00"},
}

// 加密
func EncodeId(sid string) string {
	if sid == "" {
		return ""
	}
	return encrypt.EncodeArticleId2ByCheck(sid)
}

// 解密
func DecodeId(eid string) string {
	if eid == "" {
		return ""
	}
	return encrypt.DecodeArticleId2ByCheck(eid)[0]
}

//是否是付费用户
type VipState struct {
	VipState     int64 //超级订阅状态(1普通 2升级版)
	BigMember    int64 //大会员状态
	EntMember    int64 //商机管理用户状态
	registerData int64 //注册时间
}

func GetVipState(mysql *mysql.Mysql, mg mongodb.MongodbSim, userId string) (vs *VipState) {
	vs = &VipState{}
	if userId == "" {
		return
	}
	phone := ""
	data, ok := mg.FindById("user", userId, `"i_member_status":1,"i_vip_status":1,"s_m_phone":1,"s_phone":1,"o_vipjy":1,"l_registedate":1`)
	if data != nil && len(*data) > 0 && ok {
		i_vip_status := MC.Int64All((*data)["i_vip_status"])
		if i_vip_status > 1 {
			vs.VipState = 1
			ovipjy, _ := (*data)["o_vipjy"].(map[string]interface{})
			if ovipjy["o_buyset"] != nil {
				o_buyset := ovipjy["o_buyset"].(map[string]interface{})
				if o_buyset["upgrade"] != nil {
					vs.VipState = 2
				}
			}
		}
		if i_member_status := MC.Int64All((*data)["i_member_status"]); i_member_status > 0 {
			vs.BigMember = i_member_status
		}
		if s_phone, _ := (*data)["s_phone"].(string); s_phone != "" {
			phone = s_phone
		} else if s_m_phone, _ := (*data)["s_m_phone"].(string); s_m_phone != "" {
			phone = s_m_phone
		}
		if phone != "" {
			if mysql.CountBySql(`select count(1) from entniche_user where phone = ? and power =1`, phone) > 0 {
				vs.EntMember = 1
			}
		}
		vs.registerData, _ = ((*data)["l_registedate"]).(int64)
	}
	return
}

//是否是付费账户
func (vs *VipState) IsPayedUser() bool {
	return vs.VipState > 0 || vs.BigMember > 0 || vs.EntMember > 0
}
