|
@@ -3,7 +3,7 @@ package entity
|
|
|
import (
|
|
|
"app.yhyue.com/moapp/jybase/common"
|
|
|
"app.yhyue.com/moapp/jybase/date"
|
|
|
- "app.yhyue.com/moapp/jybase/mongodb"
|
|
|
+ "app.yhyue.com/moapp/jybase/redis"
|
|
|
"app.yhyue.com/moapp/jybase/sms"
|
|
|
"fmt"
|
|
|
"gopkg.in/mgo.v2/bson"
|
|
@@ -34,7 +34,9 @@ const (
|
|
|
var equityStockLock = sync.Mutex{} // 赠送视频权益码库存锁
|
|
|
// EquityActive 赠送视频会员活动
|
|
|
type EquityActive struct {
|
|
|
- UserId string // id 这边取到的id可能是职位id也可能是mongo库id 发消息时需要判断一下
|
|
|
+ UserId string // id 这边取到的id可能是职位id也可能是mongo库id
|
|
|
+ mgoId string // mongo库id
|
|
|
+ positionId string // 职位id
|
|
|
Phone string // 手机号 用于发短信
|
|
|
OrderCode string // 订单号
|
|
|
OrderType int // 1购买 2升级 3续费
|
|
@@ -52,6 +54,12 @@ func (e *EquityActive) GiftVip() {
|
|
|
if !open || activeId <= 0 {
|
|
|
return
|
|
|
}
|
|
|
+ // 获取 缓存中的用户信息
|
|
|
+ e.setUserID()
|
|
|
+ if e.mgoId == "" {
|
|
|
+ log.Println("equityActive 未获取到有效的mgoId", e.OrderCode)
|
|
|
+ return
|
|
|
+ }
|
|
|
//2. 判断是否符合赠送条件
|
|
|
ok, mold := e.matchOrder()
|
|
|
if !ok {
|
|
@@ -75,19 +83,14 @@ func (e *EquityActive) GiftVip() {
|
|
|
eId := common.IntAll((*eInfo)[0]["id"])
|
|
|
//4. 更新库存
|
|
|
if !e.updateVipStock(eId) {
|
|
|
- log.Println("更新库存信息失败:", eId, e.OrderCode)
|
|
|
+ log.Println("equityActive 更新库存信息失败:", eId, e.OrderCode)
|
|
|
return
|
|
|
}
|
|
|
- // 判断id是职位id还是mongoid 处理
|
|
|
- mgoId, positionId, phone := getUserID(e.UserId)
|
|
|
- if phone != "" {
|
|
|
- e.Phone = phone
|
|
|
- }
|
|
|
//6. 发送短信、站内信
|
|
|
eName := common.ObjToString((*eInfo)[0]["name"])
|
|
|
code := common.ObjToString((*eInfo)[0]["code"])
|
|
|
exEndTime := common.ObjToString((*eInfo)[0]["ex_end_time"])
|
|
|
- go e.sendVipMsg(mgoId, positionId, eName, code, exEndTime)
|
|
|
+ go e.sendVipMsg(eName, code, exEndTime)
|
|
|
}
|
|
|
|
|
|
// txActivityStart 判断活动是否开启 开启则返回活动id
|
|
@@ -136,53 +139,33 @@ func (e *EquityActive) matchOrder() (ok bool, mold int) {
|
|
|
|
|
|
// 查看兑换权益码信息
|
|
|
func (e *EquityActive) findVipStock(activeId, mold int) (data *[]map[string]interface{}) {
|
|
|
- query := fmt.Sprintf("SELECT * FROM %s where state= 0 and mold=? and active_id=? ;", TableEquityInfo)
|
|
|
- data = util.Mysql.SelectBySql(query, activeId, mold)
|
|
|
+ query := fmt.Sprintf("SELECT id,name,code,date_format(ex_end_time,'%%Y-%%m-%%d') as ex_end_time FROM %s where state= 0 and mold=? and active_id=? ;", TableEquityInfo)
|
|
|
+ data = util.Mysql.SelectBySql(query, mold, activeId)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-// 根据用户mongoid或者职位id 返回mgoId 和 职位id
|
|
|
-func getUserID(userId string) (mgoId string, positionId string, phone string) {
|
|
|
- // 判断是mgoId 还是职位id
|
|
|
- if bson.IsObjectIdHex(userId) {
|
|
|
- mgoId = userId
|
|
|
- // 如果是mgoId 去mgo库查base_user_id
|
|
|
- rs, b := util.MQFW.FindById("user", userId, `{"base_user_id":1,"s_phone":1,"s_m_phone":1}`)
|
|
|
- if !b || rs == nil || len(*rs) == 0 {
|
|
|
- return
|
|
|
- }
|
|
|
- phone = common.ObjToString((*rs)["s_phone"])
|
|
|
- if phone == "" {
|
|
|
- phone = common.ObjToString((*rs)["s_m_phone"])
|
|
|
- }
|
|
|
- // 根据base_user_id 去查 base_position 获取职位id 条件是user_id
|
|
|
- baseUserId := (*rs)["base_user_id"]
|
|
|
- q := fmt.Sprintf("select id from %s where user_id=? ", TablebasePosition)
|
|
|
- positionRs := util.Mysql.SelectBySql(q, baseUserId)
|
|
|
- if positionRs != nil && len(*positionRs) > 0 {
|
|
|
- positionId = common.InterfaceToStr((*positionRs)[0]["id"])
|
|
|
- }
|
|
|
- } else {
|
|
|
- positionId = userId
|
|
|
- // 如果是职位id
|
|
|
- //去查 base_position 获取user_id(base_user_id)
|
|
|
- q := fmt.Sprintf("select user_id from %s where id=? ", TablebasePosition)
|
|
|
- positionRs := util.Mysql.SelectBySql(q, userId)
|
|
|
- if positionRs == nil || len(*positionRs) == 0 {
|
|
|
- return
|
|
|
- }
|
|
|
- base_user_id := common.InterfaceToStr((*positionRs)[0]["id"])
|
|
|
- //去mgo 库查base_user_id
|
|
|
- rs, b := util.MQFW.FindOneByField("user", map[string]interface{}{
|
|
|
- "base_user_id": base_user_id,
|
|
|
- }, `{"_id":1,"s_phone":1,"s_m_phone":1}`)
|
|
|
- if !b || rs == nil || len(*rs) == 0 {
|
|
|
- return
|
|
|
- }
|
|
|
- mgoId = mongodb.BsonIdToSId((*rs)["_id"])
|
|
|
- phone = common.ObjToString((*rs)["s_phone"])
|
|
|
- if phone == "" {
|
|
|
- phone = common.ObjToString((*rs)["s_m_phone"])
|
|
|
+// 根据订单号获取缓存里面的用户id等信息
|
|
|
+func (e *EquityActive) setUserID() (mgoId string, positionId string) {
|
|
|
+ orderKey := fmt.Sprintf("order_%s", e.OrderCode)
|
|
|
+ rs := redis.Get("other", orderKey)
|
|
|
+ info := common.ObjToMap(rs)
|
|
|
+ if info == nil || len(*info) == 0 {
|
|
|
+ log.Println("equityActive 未获取到redis保存的用户身份信息:", orderKey, info)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ mgoId = common.ObjToString((*info)["mgoId"])
|
|
|
+ positionId = fmt.Sprintf("%v", common.IntAll((*info)["positionId"]))
|
|
|
+ phone := common.ObjToString((*info)["phone"])
|
|
|
+ if phone != "" {
|
|
|
+ e.Phone = phone
|
|
|
+ }
|
|
|
+ e.mgoId = mgoId
|
|
|
+ e.positionId = positionId
|
|
|
+ if mgoId == "" && bson.IsObjectIdHex(e.UserId) {
|
|
|
+ // 如果没有redis取到说明是代用户下单的订单
|
|
|
+ // 代用户下单的现在只有个人版的 所以可以直接从订单里面取用户mgoId
|
|
|
+ if bson.IsObjectIdHex(e.UserId) {
|
|
|
+ mgoId = e.UserId
|
|
|
}
|
|
|
}
|
|
|
return
|
|
@@ -195,12 +178,12 @@ func (e *EquityActive) updateVipStock(eInfoId int) bool {
|
|
|
}
|
|
|
|
|
|
// 发送消息
|
|
|
-func (e *EquityActive) sendVipMsg(mgoId string, positionId, eName, code, ex_end_time string) {
|
|
|
+func (e *EquityActive) sendVipMsg(eName, code, ex_end_time string) {
|
|
|
// 发送短信
|
|
|
// 04 短信模板:用户{1}您好,购买{2}产品赠送您{3},请前往{4}进行兑换解锁福利。
|
|
|
// 手机号处理
|
|
|
- phone := ""
|
|
|
if e.Phone != "" {
|
|
|
+ phone := e.Phone
|
|
|
var PhoneReg = regexp.MustCompile(`^(100\d{8}|1[3-9]\d{9})$`)
|
|
|
if PhoneReg.MatchString(e.Phone) {
|
|
|
phone = string(phone[0:3]) + "****" + string(phone[(len(phone)-4):])
|
|
@@ -208,25 +191,21 @@ func (e *EquityActive) sendVipMsg(mgoId string, positionId, eName, code, ex_end_
|
|
|
smsconfig := config.Config.EquityActive.Sms
|
|
|
args3, args4 := "", ""
|
|
|
if len(smsconfig.Args) == 2 {
|
|
|
- args3 = fmt.Sprintf(smsconfig.Args[0], eName, code)
|
|
|
+ args3 = fmt.Sprintf(smsconfig.Args[0], eName, code, ex_end_time)
|
|
|
} else {
|
|
|
- log.Println("视频会员活动短信模板内容参数配置异常,请检查配置文件。")
|
|
|
+ log.Println("equityActive 视频会员活动短信模板内容参数配置异常,请检查配置文件。")
|
|
|
return
|
|
|
}
|
|
|
params := []string{phone, e.ProductType, args3, args4}
|
|
|
sms.SendSms(config.Config.SmsServiceRpc, smsconfig.Tid, e.Phone, params...)
|
|
|
} else {
|
|
|
- log.Println("未获取到用户手机号,不再发送短信:", e.OrderCode, e.UserId, e.Phone)
|
|
|
- }
|
|
|
- if mgoId == "" {
|
|
|
- log.Println("equityActive 获取用户mgoId失败,不再发送消息:", e.OrderCode, e.Phone, e.UserId)
|
|
|
- return
|
|
|
+ log.Println("equityActive 未获取到用户手机号,不再发送短信:", e.OrderCode, e.UserId, e.Phone)
|
|
|
}
|
|
|
siteMsg := config.Config.EquityActive.SiteMsg
|
|
|
// 发送站内信
|
|
|
p := util.MessageParam{
|
|
|
- UserIds: mgoId,
|
|
|
- PositionIds: positionId,
|
|
|
+ UserIds: e.mgoId,
|
|
|
+ PositionIds: e.positionId,
|
|
|
Title: fmt.Sprintf(siteMsg.Title, eName),
|
|
|
Content: fmt.Sprintf(siteMsg.Content, e.ProductType, eName, code, ex_end_time),
|
|
|
MsgType: siteMsg.MsgType,
|
|
@@ -250,14 +229,14 @@ func (e *EquityActive) sendAlarmMail(activeName string, activeId, mold, count in
|
|
|
} else {
|
|
|
m = "月度"
|
|
|
}
|
|
|
- content := fmt.Sprintf("活动:%s(id:%d),%s权益当前库存:%d", activeName, activeId, m, count)
|
|
|
+ content := fmt.Sprintf("活动:%s(活动id:%d),%s权益当前库存:%d", activeName, activeId, m, count)
|
|
|
util.SendRetryMailMany(ma.ReTry, to, ma.Title, content, "", "", config.GmailAuth)
|
|
|
}
|
|
|
|
|
|
// 判断是否年度会员
|
|
|
func (e *EquityActive) isYearVip() bool {
|
|
|
- query := fmt.Sprintf("select * from %s where user_id=? and order_status=1 and product_type =? and (timestampdiff(day,vip_starttime,vip_endtime)>=365) order by create_time desc limit 1", TableOrder)
|
|
|
+ query := fmt.Sprintf("select count(*) from %s where user_id=? and order_status=1 and product_type =? and (timestampdiff(day,vip_starttime,vip_endtime)>=365) order by create_time desc limit 1", TableOrder)
|
|
|
count := util.Mysql.CountBySql(query, e.UserId, e.OrderProductType)
|
|
|
- log.Println("判断是否为年度订单:", query, e.UserId, count)
|
|
|
+ log.Println("isYearVip 判断是否为年度订单:", query, e.UserId, count)
|
|
|
return count > 0
|
|
|
}
|