Sfoglia il codice sorgente

feat: 免费权益

zhangxinlei1996 2 anni fa
parent
commit
7b32c3b8c7
3 ha cambiato i file con 338 aggiunte e 294 eliminazioni
  1. 3 0
      entity/power.go
  2. 328 292
      service/power.go
  3. 7 2
      service/power_test.go

+ 3 - 0
entity/power.go

@@ -12,6 +12,9 @@ const (
 	PowerCacheBuyerPortKey = "free_buyer_portrait_%s"
 	PowerCacheFileKey      = "free_article_attach_%s"
 	PowerCachePortraitKey  = "free_portrait_%s_%s"
+	VipFileUploadNumKey    = "vip_file_num_%s_%s"  //超级订阅附件本月一下载次数 %s:userid ; %s:当前月份-fmt.Sprint(time.Now().Month())
+	FilePackNumKey         = "file_pack_num_%s_%s" //附件下载包本月  剩余次数  %s:userid   %s 当前月份-fmt.Sprint(time.Now().Month())
+
 )
 
 //免费权益相关

+ 328 - 292
service/power.go

@@ -3,8 +3,8 @@ package service
 import (
 	"encoding/json"
 	"fmt"
-	"log"
 	"math/rand"
+	"strconv"
 	"strings"
 	"time"
 
@@ -53,322 +53,322 @@ func (this *PowerService) Power(userid string, baseUserId, accountId, entId, pos
 	member := &entity.Member{}
 	free := &entity.Free{}
 	userPower := entity.Power{}
+	cacheBl := false
 	if bytes, err := redis.GetBytes("newother", GetRedisName(positionId)); err == nil && bytes != nil {
 		if err := json.Unmarshal(*bytes, &userPower); err == nil {
-			return &userPower
+			cacheBl = true
 		}
 	}
-
-	//获取用户本身的注册时间和邮箱、这个与个人、企业无关
-	mgoUserFields := map[string]interface{}{
-		"s_myemail":     1,
-		"l_registedate": 1,
-		"s_phone":       1,
-		"s_m_phone":     1,
-		"base_user_id":  1,
-	}
-	//大会员权益表查询条件
-	bigmemberServiceUserQuery := map[string]interface{}{"s_userid": userid, "i_status": 0}
-	//个人身份 取user表中权益
-	if positionType == 0 {
-		mgoUserFields["i_vip_status"] = 1
-		mgoUserFields["l_vip_starttime"] = 1
-		mgoUserFields["l_vip_endtime"] = 1
-		mgoUserFields["o_vipjy"] = 1
-		mgoUserFields["i_member_status"] = 1
-		mgoUserFields["i_member_give"] = 1
-		mgoUserFields["s_member_mainid"] = 1
-		mgoUserFields["i_member_sub_status"] = 1
-		mgoUserFields["i_member_trial"] = 1
-		mgoUserFields["o_member_jy"] = 1
-		mgoUserFields["o_jy"] = 1
-		mgoUserFields["i_ts_guide"] = 1
-		mgoUserFields["i_member_apppushunread"] = 1
-		mgoUserFields["i_entniche_apppushunread"] = 1
-		mgoUserFields["i_apppushunread"] = 1
-	} else {
-		bigmemberServiceUserQuery["s_userid"] = positionId
-	}
-	data, ok := this.Conn.MgoJy.FindById("user", userid, mgoUserFields)
-	if ok && data != nil && len(*data) > 0 {
-		//基本信息
-		registeDate := common.Int64All((*data)["l_registedate"])                                            //注册时间
-		phone, _ := common.If((*data)["s_phone"] != nil, (*data)["s_phone"], (*data)["s_m_phone"]).(string) //
-		mail := common.ObjToString((*data)["s_myemail"])
-		i_ts_guide := common.Int64All((*data)["i_ts_guide"])
-		i_member_apppushunread := common.Int64All((*data)["i_member_apppushunread"])
-		i_entniche_apppushunread := common.Int64All((*data)["i_entniche_apppushunread"])
-		i_apppushunread := common.Int64All((*data)["i_apppushunread"])
-		free.Mail = mail
-		free.Registedate = registeDate
-		free.Phone = phone
-		free.TsGuide = i_ts_guide
-		free.Apppushunread = i_apppushunread
-		free.EntnicheApppushunread = i_entniche_apppushunread
-		free.MemberApppushunread = i_member_apppushunread
-		//个人权益
+	if !cacheBl {
+		//获取用户本身的注册时间和邮箱、这个与个人、企业无关
+		mgoUserFields := map[string]interface{}{
+			"s_myemail":     1,
+			"l_registedate": 1,
+			"s_phone":       1,
+			"s_m_phone":     1,
+			"base_user_id":  1,
+		}
+		//大会员权益表查询条件
+		bigmemberServiceUserQuery := map[string]interface{}{"s_userid": userid, "i_status": 0}
+		//个人身份 取user表中权益
 		if positionType == 0 {
-			o_jy, _ := (*data)["o_jy"].(map[string]interface{})
-			a_key, _ := o_jy["a_key"].([]interface{})
-			free.FreeHasKey = len(a_key) > 0
-			if common.IntAll((o_jy)["i_newfree"]) > 0 || entity.IsNewFreeTimeCell < registeDate {
-				//IsNewFreeTimeCell dev3.6.4版本之前发了个紧急版本处理老用户订阅问题,i_newfree字段必须用户选择地区才能生成,不能作为判断是否是新用户得唯一标识,在此版本添加了常量:IsNewFreeTimeCell作为判断标准;--ws
-				free.IsUpgrade = true //新免费用户
-			}
-			//超级订阅
-			vipStatus := common.Int64All((*data)["i_vip_status"])
-			vipStartTime := common.Int64All((*data)["l_vip_starttime"])
-			vipEndTime := common.Int64All((*data)["l_vip_endtime"])
-			ovipjy := common.ObjToMap((*data)["o_vipjy"])
-			oBuyset := common.ObjToMap((*ovipjy)["o_buyset"])
-			upgrade := common.Int64All((*oBuyset)["upgrade"])
-			areacount := common.Int64All((*oBuyset)["areacount"])
-			buyerclasscount := common.Int64All((*oBuyset)["buyerclasscount"])
-			newcitys, _ := (*oBuyset)["newcitys"].([]interface{})
-			newcitysArr := common.InterfaceArrToint64Arr(newcitys)
-			vip = &entity.Vip{
-				Status:          vipStatus,
-				StartTime:       vipStartTime,
-				EndTime:         vipEndTime,
-				Upgrade:         upgrade,
-				Areacount:       areacount,
-				Buyerclasscount: buyerclasscount,
-				MaxKeyLength:    300,
-				PowerType:       common.Int64All(common.If(vipStatus > 0, 1, 0)),
-				NewCitys:        newcitysArr,
-				//TODO 附件下载次数
-			}
-			if (*ovipjy)["a_items"] != nil {
-				a_items := common.ObjArrToMapArr((*ovipjy)["a_items"].([]interface{}))
-				vip.HasKey = HasKey(a_items)
-			}
-			//个人member
-			i_member_sub_status := common.IntAllDef((*data)["i_member_sub_status"], 0)
+			mgoUserFields["i_vip_status"] = 1
+			mgoUserFields["l_vip_starttime"] = 1
+			mgoUserFields["l_vip_endtime"] = 1
+			mgoUserFields["o_vipjy"] = 1
+			mgoUserFields["i_member_status"] = 1
+			mgoUserFields["i_member_give"] = 1
+			mgoUserFields["s_member_mainid"] = 1
+			mgoUserFields["i_member_sub_status"] = 1
+			mgoUserFields["i_member_trial"] = 1
+			mgoUserFields["o_member_jy"] = 1
+			mgoUserFields["o_jy"] = 1
+			mgoUserFields["i_ts_guide"] = 1
+			mgoUserFields["i_member_apppushunread"] = 1
+			mgoUserFields["i_entniche_apppushunread"] = 1
+			mgoUserFields["i_apppushunread"] = 1
+		} else {
+			bigmemberServiceUserQuery["s_userid"] = positionId
+		}
+		data, ok := this.Conn.MgoJy.FindById("user", userid, mgoUserFields)
+		if ok && data != nil && len(*data) > 0 {
+			//基本信息
+			registeDate := common.Int64All((*data)["l_registedate"])                                            //注册时间
+			phone, _ := common.If((*data)["s_phone"] != nil, (*data)["s_phone"], (*data)["s_m_phone"]).(string) //
+			mail := common.ObjToString((*data)["s_myemail"])
+			i_ts_guide := common.Int64All((*data)["i_ts_guide"])
+			i_member_apppushunread := common.Int64All((*data)["i_member_apppushunread"])
+			i_entniche_apppushunread := common.Int64All((*data)["i_entniche_apppushunread"])
+			i_apppushunread := common.Int64All((*data)["i_apppushunread"])
+			free.Mail = mail
+			free.Registedate = registeDate
+			free.Phone = phone
+			free.TsGuide = i_ts_guide
+			free.Apppushunread = i_apppushunread
+			free.EntnicheApppushunread = i_entniche_apppushunread
+			free.MemberApppushunread = i_member_apppushunread
+			//个人权益
+			if positionType == 0 {
+				o_jy, _ := (*data)["o_jy"].(map[string]interface{})
+				a_key, _ := o_jy["a_key"].([]interface{})
+				free.FreeHasKey = len(a_key) > 0
+				if common.IntAll((o_jy)["i_newfree"]) > 0 || entity.IsNewFreeTimeCell < registeDate {
+					//IsNewFreeTimeCell dev3.6.4版本之前发了个紧急版本处理老用户订阅问题,i_newfree字段必须用户选择地区才能生成,不能作为判断是否是新用户得唯一标识,在此版本添加了常量:IsNewFreeTimeCell作为判断标准;--ws
+					free.IsUpgrade = true //新免费用户
+				}
+				//超级订阅
+				vipStatus := common.Int64All((*data)["i_vip_status"])
+				vipStartTime := common.Int64All((*data)["l_vip_starttime"])
+				vipEndTime := common.Int64All((*data)["l_vip_endtime"])
+				ovipjy := common.ObjToMap((*data)["o_vipjy"])
+				oBuyset := common.ObjToMap((*ovipjy)["o_buyset"])
+				upgrade := common.Int64All((*oBuyset)["upgrade"])
+				areacount := common.Int64All((*oBuyset)["areacount"])
+				buyerclasscount := common.Int64All((*oBuyset)["buyerclasscount"])
+				newcitys, _ := (*oBuyset)["newcitys"].([]interface{})
+				newcitysArr := common.InterfaceArrToint64Arr(newcitys)
+				vip = &entity.Vip{
+					Status:          vipStatus,
+					StartTime:       vipStartTime,
+					EndTime:         vipEndTime,
+					Upgrade:         upgrade,
+					Areacount:       areacount,
+					Buyerclasscount: buyerclasscount,
+					MaxKeyLength:    300,
+					PowerType:       common.Int64All(common.If(vipStatus > 0, 1, 0)),
+					NewCitys:        newcitysArr,
+				}
+				if (*ovipjy)["a_items"] != nil {
+					a_items := common.ObjArrToMapArr((*ovipjy)["a_items"].([]interface{}))
+					vip.HasKey = HasKey(a_items)
+				}
+				//个人member
+				i_member_sub_status := common.IntAllDef((*data)["i_member_sub_status"], 0)
 
-			memberStatus := common.Int64All((*data)["i_member_status"])
-			memberStarttime := common.Int64All((*data)["i_member_starttime"])
-			memberEndtime := common.Int64All((*data)["i_member_endtime"])
-			member = &entity.Member{
-				Status:       memberStatus,
-				StartTime:    memberStarttime,
-				EndTime:      memberEndtime,
-				MaxKeyLength: 300, //最大关键词数量限制
-				PowerType:    common.Int64All(common.If(memberStatus > 0, 1, 0)),
-			}
-			omemberjy := common.ObjToMap((*data)["o_member_jy"])
-			if (*omemberjy)["a_items"] != nil {
-				if arr, ok := (*omemberjy)["a_items"].([]interface{}); ok {
-					a_items := common.ObjArrToMapArr(arr)
-					member.HasKey = HasKey(a_items)
+				memberStatus := common.Int64All((*data)["i_member_status"])
+				memberStarttime := common.Int64All((*data)["i_member_starttime"])
+				memberEndtime := common.Int64All((*data)["i_member_endtime"])
+				member = &entity.Member{
+					Status:       memberStatus,
+					StartTime:    memberStarttime,
+					EndTime:      memberEndtime,
+					MaxKeyLength: 300, //最大关键词数量限制
+					PowerType:    common.Int64All(common.If(memberStatus > 0, 1, 0)),
 				}
-			}
-			if memberStatus > 0 {
-				member.MemberPower = 1 //是否分配了大会员;0:否 1:是
-			}
-			if (*data)["s_member_mainid"] != nil && common.ObjToString((*data)["s_member_mainid"]) != "" && i_member_sub_status > 0 {
-				member.Pid = common.ObjToString((*data)["s_member_mainid"])
-				member.IsSubCount = 1
-			}
-			if (member.Pid != "" && common.IntAllDef((*data)["i_member_sub_status"], 0) == 1) || this.Conn.MgoJy.Count("member", map[string]interface{}{"userid": userid}) > 0 {
-				member.Used = true
-			}
-			if (*data)["i_member_trial"] != nil {
-				member.IsMemberTrial = 1
-			}
-			//获取大会员版本名称
-			member.MemberName = level_map[memberStatus]
-		} else if positionType == 1 {
-			entnicheUserId := this.GetEntnicheUserId(entId, phone)
-			//免费
-			fdata, _ := this.Conn.MgoJy.FindOne("entniche_rule", map[string]interface{}{
-				"i_ent":    entId,
-				"i_userid": entnicheUserId,
-				"i_type":   2, //0:商机管理 1:超级订阅/大会员 2:免费
-			})
-			o_jy, _ := (*fdata)["o_entniche"].(map[string]interface{})
-			a_key, _ := o_jy["a_key"].([]interface{})
-			free.FreeHasKey = len(a_key) > 0
-			if common.IntAll((o_jy)["i_newfree"]) > 0 || entity.IsNewFreeTimeCell < registeDate { //IsNewFreeTimeCell dev3.6.4版本之前发了个紧急版本处理老用户订阅问题,i_newfree字段必须用户选择地区才能生成,不能作为判断是否是新用户得唯一标识,在此版本添加了常量:IsNewFreeTimeCell作为判断标准;--ws
-				free.IsUpgrade = true //新免费用户
-			}
-			//企业
-			edata, _ := this.Conn.MgoJy.FindOne("entniche_rule", map[string]interface{}{
-				"i_ent":    entId,
-				"i_userid": entnicheUserId,
-				"i_type":   1, //0:商机管理 1:超级订阅/大会员
-				"l_endtime": map[string]interface{}{
-					"$gt": time.Now().Unix(),
-				},
-			})
-			eudata, _ := this.Conn.MgoJy.FindOne("ent_user", map[string]interface{}{
-				"i_entid":  entId,
-				"i_userid": entnicheUserId,
-			})
-			if eudata != nil && len(*eudata) > 0 {
-				haskey := false
-				o_entniche := &map[string]interface{}{}
-				if edata != nil && len(*edata) > 0 {
-					o_entniche = common.ObjToMap((*edata)["o_entniche"])
-					if (*o_entniche)["a_items"] != nil {
-						if arr, ok := (*o_entniche)["a_items"].([]interface{}); ok {
-							a_items := common.ObjArrToMapArr(arr)
-							haskey = HasKey(a_items)
-						}
+				omemberjy := common.ObjToMap((*data)["o_member_jy"])
+				if (*omemberjy)["a_items"] != nil {
+					if arr, ok := (*omemberjy)["a_items"].([]interface{}); ok {
+						a_items := common.ObjArrToMapArr(arr)
+						member.HasKey = HasKey(a_items)
 					}
 				}
-				//获取用户是什么版本权益
-				//用于判断是否含有企业主体的超级订阅、大会员服务
-				epowerData := this.Conn.Mysql.SelectBySql(`SELECT c.product_type FROM entniche_user a INNER JOIN entniche_power b 
+				if memberStatus > 0 {
+					member.MemberPower = 1 //是否分配了大会员;0:否 1:是
+				}
+				if (*data)["s_member_mainid"] != nil && common.ObjToString((*data)["s_member_mainid"]) != "" && i_member_sub_status > 0 {
+					member.Pid = common.ObjToString((*data)["s_member_mainid"])
+					member.IsSubCount = 1
+				}
+				if (member.Pid != "" && common.IntAllDef((*data)["i_member_sub_status"], 0) == 1) || this.Conn.MgoJy.Count("member", map[string]interface{}{"userid": userid}) > 0 {
+					member.Used = true
+				}
+				if (*data)["i_member_trial"] != nil {
+					member.IsMemberTrial = 1
+				}
+				//获取大会员版本名称
+				member.MemberName = level_map[memberStatus]
+			} else if positionType == 1 {
+				entnicheUserId := this.GetEntnicheUserId(entId, phone)
+				//免费
+				fdata, _ := this.Conn.MgoJy.FindOne("entniche_rule", map[string]interface{}{
+					"i_ent":    entId,
+					"i_userid": entnicheUserId,
+					"i_type":   2, //0:商机管理 1:超级订阅/大会员 2:免费
+				})
+				o_jy, _ := (*fdata)["o_entniche"].(map[string]interface{})
+				a_key, _ := o_jy["a_key"].([]interface{})
+				free.FreeHasKey = len(a_key) > 0
+				if common.IntAll((o_jy)["i_newfree"]) > 0 || entity.IsNewFreeTimeCell < registeDate { //IsNewFreeTimeCell dev3.6.4版本之前发了个紧急版本处理老用户订阅问题,i_newfree字段必须用户选择地区才能生成,不能作为判断是否是新用户得唯一标识,在此版本添加了常量:IsNewFreeTimeCell作为判断标准;--ws
+					free.IsUpgrade = true //新免费用户
+				}
+				//企业
+				edata, _ := this.Conn.MgoJy.FindOne("entniche_rule", map[string]interface{}{
+					"i_ent":    entId,
+					"i_userid": entnicheUserId,
+					"i_type":   1, //0:商机管理 1:超级订阅/大会员
+					"l_endtime": map[string]interface{}{
+						"$gt": time.Now().Unix(),
+					},
+				})
+				eudata, _ := this.Conn.MgoJy.FindOne("ent_user", map[string]interface{}{
+					"i_entid":  entId,
+					"i_userid": entnicheUserId,
+				})
+				if eudata != nil && len(*eudata) > 0 {
+					haskey := false
+					o_entniche := &map[string]interface{}{}
+					if edata != nil && len(*edata) > 0 {
+						o_entniche = common.ObjToMap((*edata)["o_entniche"])
+						if (*o_entniche)["a_items"] != nil {
+							if arr, ok := (*o_entniche)["a_items"].([]interface{}); ok {
+								a_items := common.ObjArrToMapArr(arr)
+								haskey = HasKey(a_items)
+							}
+						}
+					}
+					//获取用户是什么版本权益
+					//用于判断是否含有企业主体的超级订阅、大会员服务
+					epowerData := this.Conn.Mysql.SelectBySql(`SELECT c.product_type FROM entniche_user a INNER JOIN entniche_power b 
 		 									INNER JOIN entniche_wait_empower c 
 		 									ON a.id =b.ent_user_id  AND c.id = b.wait_empower_id WHERE a.phone =? AND b.ent_id=? AND c.end_time >?`, phone, entId, time.Now().Format(date.Date_Full_Layout))
 
-				if epowerData != nil && len(*epowerData) > 0 {
-					for _, v := range *epowerData {
-						product_type := common.ObjToString(v["product_type"])
-						if strings.Contains(product_type, entity.ProductType_vip) {
-							oBuyset := common.ObjToMap((*o_entniche)["o_buyset"])
-							upgrade := common.Int64All((*oBuyset)["oBuyset"])
-							areacount := common.Int64All((*oBuyset)["areacount"])
-							buyerclasscount := common.Int64All((*oBuyset)["buyerclasscount"])
-							newcitys, _ := (*oBuyset)["newcitys"].([]interface{})
-							newcitysArr := common.InterfaceArrToint64Arr(newcitys)
-							status := common.Int64All((*eudata)["i_vip_status"])
-							starttime := common.Int64All((*eudata)["l_vip_starttime"])
-							endtime := common.Int64All((*eudata)["l_vip_endtime"])
-							vip = &entity.Vip{
-								Status:          status,
-								StartTime:       starttime,
-								EndTime:         endtime,
-								Upgrade:         upgrade,
-								Areacount:       areacount,
-								Buyerclasscount: buyerclasscount,
-								MaxKeyLength:    300,
-								PowerType:       2,
-								HasKey:          haskey,
-								NewCitys:        newcitysArr,
-							}
-						} else if strings.Contains(product_type, entity.ProductType_member) {
-							member.PowerType = 2
-							status := common.Int64All((*eudata)["i_member_status"])
-							starttime := common.Int64All((*eudata)["l_member_starttime"])
-							endtime := common.Int64All((*eudata)["l_member_endtime"])
-							member = &entity.Member{
-								Status:     status,
-								StartTime:  starttime,
-								EndTime:    endtime,
-								MemberName: level_map[status],
-								PowerType:  2,
-								HasKey:     haskey,
-							}
-							//是否试用
-							if this.Conn.MgoJy.Count("member", map[string]interface{}{"userid": positionId}) > 0 {
-								member.Used = true
+					if epowerData != nil && len(*epowerData) > 0 {
+						for _, v := range *epowerData {
+							product_type := common.ObjToString(v["product_type"])
+							if strings.Contains(product_type, entity.ProductType_vip) {
+								oBuyset := common.ObjToMap((*o_entniche)["o_buyset"])
+								upgrade := common.Int64All((*oBuyset)["oBuyset"])
+								areacount := common.Int64All((*oBuyset)["areacount"])
+								buyerclasscount := common.Int64All((*oBuyset)["buyerclasscount"])
+								newcitys, _ := (*oBuyset)["newcitys"].([]interface{})
+								newcitysArr := common.InterfaceArrToint64Arr(newcitys)
+								status := common.Int64All((*eudata)["i_vip_status"])
+								starttime := common.Int64All((*eudata)["l_vip_starttime"])
+								endtime := common.Int64All((*eudata)["l_vip_endtime"])
+								vip = &entity.Vip{
+									Status:          status,
+									StartTime:       starttime,
+									EndTime:         endtime,
+									Upgrade:         upgrade,
+									Areacount:       areacount,
+									Buyerclasscount: buyerclasscount,
+									MaxKeyLength:    300,
+									PowerType:       2,
+									HasKey:          haskey,
+									NewCitys:        newcitysArr,
+								}
+							} else if strings.Contains(product_type, entity.ProductType_member) {
+								member.PowerType = 2
+								status := common.Int64All((*eudata)["i_member_status"])
+								starttime := common.Int64All((*eudata)["l_member_starttime"])
+								endtime := common.Int64All((*eudata)["l_member_endtime"])
+								member = &entity.Member{
+									Status:     status,
+									StartTime:  starttime,
+									EndTime:    endtime,
+									MemberName: level_map[status],
+									PowerType:  2,
+									HasKey:     haskey,
+								}
+								//是否试用
+								if this.Conn.MgoJy.Count("member", map[string]interface{}{"userid": positionId}) > 0 {
+									member.Used = true
+								}
 							}
 						}
 					}
 				}
 			}
-		}
-		//大会员相关权益数量
-		memberServiceMap := map[int64]bool{}
-		serviceList := this.Conn.Mysql.Find("bigmember_service_user", bigmemberServiceUserQuery, "DISTINCT(s_serviceid),i_frequency", "", -1, -1)
-		if serviceList != nil && len(*serviceList) != 0 {
-			pCount, eCount, dailyNum, customers := 0, 0, 0, 10
-			for _, item := range *serviceList {
-				serviceid := common.Int64All(item["s_serviceid"])
-				memberServiceMap[serviceid] = true
-				member.MemberPowerList = append(member.MemberPowerList, serviceid)
-				if serviceid == 14 { //项目数量
-					pCount = common.IntAll(item["i_frequency"])
-				} else if serviceid == 4 || serviceid == 12 || serviceid == 13 { //企业情报监控 企业中标动态
-					tEcount := common.IntAll(item["i_frequency"])
-					if tEcount > eCount {
-						eCount = tEcount
+			//大会员相关权益数量
+			memberServiceMap := map[int64]bool{}
+			serviceList := this.Conn.Mysql.Find("bigmember_service_user", bigmemberServiceUserQuery, "DISTINCT(s_serviceid),i_frequency", "", -1, -1)
+			if serviceList != nil && len(*serviceList) != 0 {
+				pCount, eCount, dailyNum, customers := 0, 0, 0, 10
+				for _, item := range *serviceList {
+					serviceid := common.Int64All(item["s_serviceid"])
+					memberServiceMap[serviceid] = true
+					member.MemberPowerList = append(member.MemberPowerList, serviceid)
+					if serviceid == 14 { //项目数量
+						pCount = common.IntAll(item["i_frequency"])
+					} else if serviceid == 4 || serviceid == 12 || serviceid == 13 { //企业情报监控 企业中标动态
+						tEcount := common.IntAll(item["i_frequency"])
+						if tEcount > eCount {
+							eCount = tEcount
+						}
+					} else if serviceid == 17 || serviceid == 18 { //每日数据包
+						dailyNum = common.IntAll(item["i_frequency"])
+					} else if serviceid == 7 { //潜在客户 关注客户
+						customers = common.IntAll(item["i_frequency"])
 					}
-				} else if serviceid == 17 || serviceid == 18 { //每日数据包
-					dailyNum = common.IntAll(item["i_frequency"])
-				} else if serviceid == 7 { //潜在客户 关注客户
-					customers = common.IntAll(item["i_frequency"])
 				}
+				member.EntNum = int64(eCount)
+				member.ProNum = int64(pCount)
+				member.DailyNum = int64(dailyNum)
+				member.Customers = int64(customers)
 			}
-			member.EntNum = int64(eCount)
-			member.ProNum = int64(pCount)
-			member.DailyNum = int64(dailyNum)
-			member.Customers = int64(customers)
-		}
-		//
-		if phone != "" {
-			//查询是否是商机管理付费用户
-			res := this.Conn.Mysql.SelectBySql(`SELECT i. STATUS AS status,i.model , i.isNew, i.power_source, r.role_id, u.power, i.name,i.id,i.startdate,i.enddate,i.auth_status,i.auth_reason,i.dept_subscribe FROM (entniche_user u LEFT JOIN entniche_user_role r ON r.user_id = u.id ) LEFT JOIN entniche_info i ON u.ent_id = i.id WHERE u.phone = ? and i.id = ? ORDER BY i. STATUS DESC, i.auth_status DESC`, phone, entId)
-			if res != nil && len(*res) > 0 {
-				for _, v := range *res {
-					if common.IntAll(v["id"]) == 0 {
-						continue
-					}
-					entnicheStatus := common.Int64All(v["status"])
-					entnichePower := int64(0)
-					if entnicheStatus == 1 {
-						entnichePower = common.Int64All(v["power"])
-					}
-					entniche.IsNew = common.Int64All(v["isNew"])
-					entniche.Status = entnicheStatus    //企业是否有商机管理权限
-					entniche.IsEntPower = entnichePower //个人是否有商机管理权限
-					entniche.PowerSource = common.Int64All(v["power_source"])
-					entniche.StartTime = common.Int64All(v["startdate"])
-					entniche.EndTime = common.Int64All(v["enddate"])
-					entniche.Model = common.Int64All(v["model"])
-					//
-					ent.Name = common.ObjToString(v["name"])
-					ent.EntRoleId = common.Int64All(v["role_id"])
-					ent.EntAuthStatus = common.Int64All(v["auth_status"])
-					ent.EntAuthReason = common.ObjToString(v["auth_reason"])
-					ent.DeptSubscribe = common.Int64All(v["dept_subscribe"])
-					//判断企业是否购买企业版相关
-					eweData := this.Conn.Mysql.SelectBySql(`select * from entniche_wait_empower where ent_id=? and end_time>?`, entId, time.Now().Format(date.Date_Full_Layout))
-					if eweData != nil && len(*eweData) > 0 {
-						for _, v := range *eweData {
-							product_type := common.ObjToString(v["product_type"])
-							if strings.Contains(product_type, entity.ProductType_member) {
-								ent.BuyMember = 1
-							} else if strings.Contains(product_type, entity.ProductType_vip) {
-								ent.BuyVip = 1
+			//
+			if phone != "" {
+				//查询是否是商机管理付费用户
+				res := this.Conn.Mysql.SelectBySql(`SELECT i. STATUS AS status,i.model , i.isNew, i.power_source, r.role_id, u.power, i.name,i.id,i.startdate,i.enddate,i.auth_status,i.auth_reason,i.dept_subscribe FROM (entniche_user u LEFT JOIN entniche_user_role r ON r.user_id = u.id ) LEFT JOIN entniche_info i ON u.ent_id = i.id WHERE u.phone = ? and i.id = ? ORDER BY i. STATUS DESC, i.auth_status DESC`, phone, entId)
+				if res != nil && len(*res) > 0 {
+					for _, v := range *res {
+						if common.IntAll(v["id"]) == 0 {
+							continue
+						}
+						entnicheStatus := common.Int64All(v["status"])
+						entnichePower := int64(0)
+						if entnicheStatus == 1 {
+							entnichePower = common.Int64All(v["power"])
+						}
+						entniche.IsNew = common.Int64All(v["isNew"])
+						entniche.Status = entnicheStatus    //企业是否有商机管理权限
+						entniche.IsEntPower = entnichePower //个人是否有商机管理权限
+						entniche.PowerSource = common.Int64All(v["power_source"])
+						entniche.StartTime = common.Int64All(v["startdate"])
+						entniche.EndTime = common.Int64All(v["enddate"])
+						entniche.Model = common.Int64All(v["model"])
+						//
+						ent.Name = common.ObjToString(v["name"])
+						ent.EntRoleId = common.Int64All(v["role_id"])
+						ent.EntAuthStatus = common.Int64All(v["auth_status"])
+						ent.EntAuthReason = common.ObjToString(v["auth_reason"])
+						ent.DeptSubscribe = common.Int64All(v["dept_subscribe"])
+						//判断企业是否购买企业版相关
+						eweData := this.Conn.Mysql.SelectBySql(`select * from entniche_wait_empower where ent_id=? and end_time>?`, entId, time.Now().Format(date.Date_Full_Layout))
+						if eweData != nil && len(*eweData) > 0 {
+							for _, v := range *eweData {
+								product_type := common.ObjToString(v["product_type"])
+								if strings.Contains(product_type, entity.ProductType_member) {
+									ent.BuyMember = 1
+								} else if strings.Contains(product_type, entity.ProductType_vip) {
+									ent.BuyVip = 1
+								}
 							}
 						}
-					}
-					//企业管理员
-					if ent.EntRoleId == 1 && ((ent.BuyMember == 1 || ent.BuyVip == 1) || (entniche.Status == 1 && entniche.PowerSource == 0)) {
-						ent.EntSubscribeManager = 1
-					}
-					//部门管理员
-					if ent.EntRoleId == 2 && entniche.Status == 1 && entniche.PowerSource == 0 && ent.DeptSubscribe == 1 {
-						ent.EntSubscribeManager = 1
-					}
-					//
-					if ent.EntRoleId == 1 || ent.EntRoleId == 2 {
-						if (ent.BuyMember == 1 || ent.BuyVip == 1) || (entniche.Status == 1 && entniche.PowerSource == 0) {
-							ent.EntSubscribe = 1
+						//企业管理员
+						if ent.EntRoleId == 1 && ((ent.BuyMember == 1 || ent.BuyVip == 1) || (entniche.Status == 1 && entniche.PowerSource == 0)) {
+							ent.EntSubscribeManager = 1
+						}
+						//部门管理员
+						if ent.EntRoleId == 2 && entniche.Status == 1 && entniche.PowerSource == 0 && ent.DeptSubscribe == 1 {
+							ent.EntSubscribeManager = 1
+						}
+						//
+						if ent.EntRoleId == 1 || ent.EntRoleId == 2 {
+							if (ent.BuyMember == 1 || ent.BuyVip == 1) || (entniche.Status == 1 && entniche.PowerSource == 0) {
+								ent.EntSubscribe = 1
+							}
 						}
 					}
-				}
 
-				//广东移动DICT 用户
-				ent.PrivateGD = this.Conn.Mysql.CountBySql(`select count(1) from privatedata where phone = ?`, phone) > 0
+					//广东移动DICT 用户
+					ent.PrivateGD = this.Conn.Mysql.CountBySql(`select count(1) from privatedata where phone = ?`, phone) > 0
+				}
+			}
+			if vip.Status <= 0 && member.Status <= 0 && entniche.Status <= 0 {
+				free.IsFree = true
+			} else {
+				free.IsFree = false
 			}
 		}
-		if vip.Status <= 0 && member.Status <= 0 && entniche.Status <= 0 {
-			free.IsFree = true
-		} else {
-			free.IsFree = false
+		userPower = entity.Power{
+			Vip:      vip,
+			Member:   member,
+			Free:     free,
+			Ent:      ent,
+			Entniche: entniche,
 		}
 	}
-	log.Println(vip.Status, vip.StartTime, vip.EndTime)
-	userPower = entity.Power{
-		Vip:      vip,
-		Member:   member,
-		Free:     free,
-		Ent:      ent,
-		Entniche: entniche,
-	}
 	//存储缓存
 	go func() {
 		if bytes, err := json.Marshal(userPower); err == nil && bytes != nil {
@@ -376,15 +376,43 @@ func (this *PowerService) Power(userid string, baseUserId, accountId, entId, pos
 			_ = redis.PutBytes("newother", GetRedisName(positionId), &bytes, oneDayMore)
 		}
 	}()
+	//实时更新的数据 没法存缓存
+	//免费用户在企业画像/采购单位画像/附件下载留资
+	userPower.Free.FreeEntPort, userPower.Free.FreeBuyerPort, userPower.Free.FreeFile = FreeExperience(userid, positionId, positionType)
+	//
+	notBigFileBool := userPower.Member.Status <= 0 || !strings.Contains(intStringsJoin(userPower.Member.MemberPowerList), "3")
+	uk := common.If(notBigFileBool && userPower.Vip.Status > 0 && userPower.Vip.Upgrade > 0, "v", "f").(string)
+	userPower.Vip.FileNum = FileNum(uk, userid, positionId, positionType)
+
 	return &userPower
 }
 
 //免费用户体验会员功能权限
 //免费用户在企业画像/采购单位画像/附件下载留资 留资成功后用户才有功能使用次数
-func FreeExperience(userId string) (int, int, int) {
-	return redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheEntPortKey, userId)),
-		redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheBuyerPortKey, userId)),
-		redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheFileKey, userId))
+func FreeExperience(userId string, positionId, positionType int64) (int64, int64, int64) {
+	if positionType == 1 {
+		userId = strconv.Itoa(int(positionId))
+	}
+	entPortKey := common.Int64All(redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheEntPortKey, userId)))
+	buyerPortKey := common.Int64All(redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheBuyerPortKey, userId)))
+	fileKey := common.Int64All(redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.PowerCacheFileKey, userId)))
+	return entPortKey, buyerPortKey, fileKey
+}
+
+//获取附件下载次数
+func FileNum(uk, userid string, positionId, positionType int64) int64 {
+	if positionType == 1 {
+		userid = strconv.Itoa(int(positionId))
+	}
+	fileUploadNum := map[string]int{
+		"f": 0,
+		"v": 10,
+	}
+	//附件下载包的剩余次数
+	filePackKey := fmt.Sprintf(entity.FilePackNumKey, userid, fmt.Sprint(time.Now().Month()))
+	filePackNum := redis.GetInt(entity.PowerCacheDb, filePackKey)
+	num := fileUploadNum[uk] - redis.GetInt(entity.PowerCacheDb, fmt.Sprintf(entity.VipFileUploadNumKey, userid, fmt.Sprint(time.Now().Month()))) + filePackNum
+	return int64(num)
 }
 
 /*
@@ -430,3 +458,11 @@ func (this *PowerService) DelRedisPower(positionId int64) bool {
 	}
 	return redis.Del("newother", GetRedisName(positionId))
 }
+
+func intStringsJoin(is []int64) string {
+	var str = []string{}
+	for _, v := range is {
+		str = append(str, strconv.Itoa(int(v)))
+	}
+	return strings.Join(str, ",")
+}

+ 7 - 2
service/power_test.go

@@ -108,8 +108,13 @@ func TestPowerService_Power(t *testing.T) {
 			this := &PowerService{
 				Conn: NewPowerService.Conn,
 			}
-			if got := this.Power("637dd8315c0c8e9c47c9bf0a", 335487, 60052, 15470, 1, 60040); !reflect.DeepEqual(got, tt.want) {
-				log.Println(got.Vip.Status)
+			//(userid string, baseUserId, accountId, entId, positionType, positionId int64)
+			if got := this.Power("63e346b5d093bbbf6d1f7019", 425409, 60229, 15456, 1, 60170); !reflect.DeepEqual(got, tt.want) {
+				log.Println(got.Vip)
+				log.Println(got.Member)
+				log.Println(got.Ent)
+				log.Println(got.Entniche)
+				log.Println(got.Free)
 				t.Errorf("Power() = %v, want %v", got, tt.want)
 			}
 		})