Sfoglia il codice sorgente

增加功能类型

wangchuanjin 2 anni fa
parent
commit
5dd816d339

+ 10 - 9
public/entity/base_function.go

@@ -9,15 +9,16 @@ var Base_function = base_function{}
 
 //功能原始表
 type base_function struct {
-	Id         int64
-	Appid      string
-	Name       string //功能名称
-	Code       string //功能代码
-	Status     int64  //0:不可用 1:可用
-	Haspower   int64  //能否生成用户权益;0:否 1:是
-	Version    string //版本号
-	Power_rule int64  //校验权限规则;1:仅周期 2:周期+数量 3:周期+频率+数量
-	Power_type int64  //权限类型;1:互斥 2:兼容
+	Id            int64
+	Appid         string
+	Name          string //功能名称
+	Code          string //功能代码
+	Status        int64  //0:不可用 1:可用
+	Haspower      int64  //能否生成用户权益;0:否 1:是
+	Version       string //版本号
+	Function_type int64  //功能类型;1:消耗型(比如:信息发布,需要扣减) 2:额度型(比如:关注条数,不需要扣减) 3:配额型(比如:搜索结果条数)
+	Power_rule    int64  //校验权限规则;1:仅周期 2:周期+数量 3:周期+频率+数量
+	Power_type    int64  //权限类型;1:互斥 2:兼容
 }
 
 //根据代码查找功能详情

+ 27 - 10
public/entity/base_power.go

@@ -36,12 +36,21 @@ func (b *base_power) FindMyPowers(appid string, user_id, ent_id int64) *[]*base_
 }
 
 //查找我的权益
-func (b *base_power) FindMyPower(appid, function_code string, user_id, ent_id int64) *[]*base_power {
+func (b *base_power) FindMyPowersByFc(appid, function_code string, user_id, ent_id int64) *[]*base_power {
 	now := NowFormat(Date_Full_Layout)
 	list := Mysql_BaseService.SelectBySql(`select * from base_power where appid=? and function_code=? and ((power_type=1 and power_ofid=?) or (power_type=2 and power_ofid=?)) and end_time>? and status=1 order by power_type`, appid, function_code, user_id, ent_id, now)
 	return JsonUnmarshal(list, &[]*base_power{}).(*[]*base_power)
 }
 
+//查找我最新一条的权益,没有过期限制
+func (b *base_power) FindMyLastPowerByFc(appid, function_code string, user_id, ent_id int64) *base_power {
+	list := Mysql_BaseService.SelectBySql(`select * from base_power where appid=? and function_code=? and ((power_type=1 and power_ofid=?) or (power_type=2 and power_ofid=?)) order by update_time desc limit 1`, appid, function_code, user_id, ent_id)
+	if len(*list) == 0 {
+		return nil
+	}
+	return JsonUnmarshal((*list)[0], &base_power{}).(*base_power)
+}
+
 //查找我的权益,加锁
 func (b *base_power) FindMyPowerForUpdate(id int64) *base_power {
 	list := Mysql_BaseService.SelectBySql(`select * from base_power where id=? for update wait 5`, id)
@@ -60,16 +69,17 @@ func (b *base_power) OpenPower(goods_spec_id int64, appid, goods_code string, us
 			ok1 = Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power a inner join base_goods_spec_power b 
 				on (a.appid=? and a.power_type=2 and a.power_ofid=? and a.status=1 and b.appid=? and b.goods_code=? and b.spec_id=? and a.function_code=b.function_code) 
 				inner join base_function c on (c.appid=? and c.status=1 and c.power_type=1 and c.code=a.function_code) 
-				set a.status=0`, appid, ent_id, appid, goods_code, goods_spec_id, appid) > -1
+				set a.status=0,update_time=?`, appid, ent_id, appid, goods_code, goods_spec_id, appid, NowFormat(Date_Full_Layout)) > -1
 		}
 		//用户互斥的功能失效掉
 		if user_id > 0 {
 			ok2 = Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power a inner join base_goods_spec_power b 
 				on (a.appid=? and a.power_type=1 and a.power_ofid=? and a.status=1 and b.appid=? and b.goods_code=? and b.spec_id=? and a.function_code=b.function_code) 
 				inner join base_function c on (c.appid=? and c.status=1 and c.power_type=1 and c.code=a.function_code) 
-				set a.status=0`, appid, user_id, appid, goods_code, goods_spec_id, appid) > -1
+				set a.status=0,update_time=?`, appid, user_id, appid, goods_code, goods_spec_id, appid, NowFormat(Date_Full_Layout)) > -1
 		}
 		power_values, empower_values, wait_empower_values := []interface{}{}, []interface{}{}, []interface{}{}
+		nowFormat := NowFormat(Date_Full_Layout)
 		for _, bgs := range *bgss {
 			if bgs.Base_goods_spec_powers == nil {
 				continue
@@ -104,18 +114,25 @@ func (b *base_power) OpenPower(goods_spec_id int64, appid, goods_code string, us
 					logx.Error(appid, goods_code, goods_spec_id, v.Function_code, "无效的cycle", v.Cycle)
 					continue
 				}
-				start_time, end_time := NowFormat(Date_Full_Layout), FormatDate(&end, Date_Full_Layout)
+				start_time, end_time := nowFormat, FormatDate(&end, Date_Full_Layout)
 				power_values = append(power_values, start_time, end_time)
 				use_count := buy_num
 				if bgs.Calculation_type == 2 {
 					use_count = buy_num * v.Use_count
 				}
-				power_values = append(power_values, use_count, use_count, v.Limit_strategy, v.Strategy_count, 1, NowFormat(Date_Full_Layout))
+				surplus_count := use_count
+				if v.Base_function.Function_type == 2 {
+					lastPower := b.FindMyLastPowerByFc(appid, v.Function_code, user_id, ent_id)
+					if lastPower != nil {
+						surplus_count = lastPower.Surplus_count
+					}
+				}
+				power_values = append(power_values, use_count, surplus_count, v.Limit_strategy, v.Strategy_count, 1, nowFormat, nowFormat)
 				//
 				if v.Power_type == 2 && v.Power_count == -1 && Base_ent_empower.Count(appid, v.Function_code, ent_id) == 0 {
-					empower_values = append(empower_values, appid, ent_id, 0, v.Function_code, NowFormat(Date_Full_Layout))
+					empower_values = append(empower_values, appid, ent_id, 0, v.Function_code, nowFormat)
 				} else if v.Power_type == 2 && v.Power_count > 0 {
-					wait_empower_values = append(wait_empower_values, appid, v.Id, ent_id, start_time, end_time, use_count, v.Power_count, v.Limit_strategy, NowFormat(Date_Full_Layout))
+					wait_empower_values = append(wait_empower_values, appid, v.Id, ent_id, start_time, end_time, use_count, v.Power_count, v.Limit_strategy, nowFormat)
 				}
 			}
 		}
@@ -123,7 +140,7 @@ func (b *base_power) OpenPower(goods_spec_id int64, appid, goods_code string, us
 			logx.Error(appid, goods_code, goods_spec_id, user_id, ent_id, "没有找到需要开通的权益")
 			return false
 		}
-		v1, v2 := Mysql_BaseService.InsertBatchByTx(tx, "base_power", []string{"appid", "goods_spec_power_id", "function_code", "power_type", "power_ofid", "start_time", "end_time", "use_count", "surplus_count", "limit_strategy", "strategy_count", "status", "create_time"}, power_values)
+		v1, v2 := Mysql_BaseService.InsertBatchByTx(tx, "base_power", []string{"appid", "goods_spec_power_id", "function_code", "power_type", "power_ofid", "start_time", "end_time", "use_count", "surplus_count", "limit_strategy", "strategy_count", "status", "create_time", "update_time"}, power_values)
 		ok3 := true
 		if len(empower_values) > 0 {
 			v3, v4 := Mysql_BaseService.InsertBatchByTx(tx, "base_ent_empower", []string{"appid", "ent_id", "ent_user_id", "function_code", "create_time"}, empower_values)
@@ -145,12 +162,12 @@ func (b *base_power) CancelPower(appid, goods_code string, spec_id, user_id, ent
 		if user_id > 0 {
 			ok1 = Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power a inner join base_goods_spec_power b 
 				on (a.appid=? and a.power_type=1 and a.power_ofid=? and a.status=1 and b.appid=? and b.goods_code=? and b.spec_id=? and b.power_type=1 and a.function_code=b.function_code) 
-				set a.status=-1`, appid, user_id, appid, goods_code, spec_id) > 0
+				set a.status=-1,update_time=?`, appid, user_id, appid, goods_code, spec_id, NowFormat(Date_Full_Layout)) > 0
 		}
 		if ent_id > 0 {
 			ok2 = Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power a inner join base_goods_spec_power b 
 				on (a.appid=? and a.power_type=2 and a.power_ofid=? and a.status=1 and b.appid=? and b.goods_code=? and b.spec_id=? and b.power_type=2 and a.function_code=b.function_code) 
-				set a.status=-1`, appid, ent_id, appid, goods_code, spec_id) > 0
+				set a.status=-1,update_time=?`, appid, ent_id, appid, goods_code, spec_id, NowFormat(Date_Full_Layout)) > 0
 		}
 		return ok1 || ok2
 	})

+ 1 - 1
public/service/deduction.go

@@ -33,7 +33,7 @@ func Deduction(appid, function_code string, user_id, ent_id int64, count int64,
 	if function == nil {
 		return 0, errors.New("功能原始表中没有找到该功能")
 	}
-	myPowers := Base_power.FindMyPower(appid, function_code, user_id, ent_id)
+	myPowers := Base_power.FindMyPowersByFc(appid, function_code, user_id, ent_id)
 	if myPowers == nil || len(*myPowers) == 0 {
 		return -1, errors.New("功能权益表中没有找到权益记录")
 	}

+ 2 - 2
public/service/power_test.go

@@ -10,7 +10,7 @@ func TestOpenPower(t *testing.T) {
 	//站内社交
 	//OpenPower("10000", "xxfb_gyxx", 4, 0, 14184, 1)
 	//医疗领域化
-	err := OpenPower("10000", "lyh", 13, 69717, 0, 1)
+	err := OpenPower("10000", "lyh", 13, 68773, 0, 1)
 	assert.Nil(t, err)
 }
 
@@ -25,7 +25,7 @@ func TestHasPowers(t *testing.T) {
 	//企业
 	//powers, err := HasPowers("10000", 0, 14184, 4271)
 	//个人
-	powers, err := HasPowers("10000", 69717, 0, 0)
+	powers, err := HasPowers("10000", 69946, 0, 0)
 	assert.Nil(t, err)
 	t.Log(powers)
 }

+ 1 - 1
public/service/recharge.go

@@ -30,7 +30,7 @@ func Recharge(appid, function_code string, user_id, ent_id int64, count int64, i
 	if function == nil {
 		return 0, errors.New("功能原始表中没有找到该功能")
 	}
-	myPowers := Base_power.FindMyPower(appid, function_code, user_id, ent_id)
+	myPowers := Base_power.FindMyPowersByFc(appid, function_code, user_id, ent_id)
 	if myPowers == nil || len(*myPowers) == 0 {
 		return -1, errors.New("功能权益表中没有找到权益记录")
 	}

+ 1 - 1
public/service/surplus.go

@@ -31,7 +31,7 @@ func Surplus(appid, function_code string, user_id, ent_id, ent_user_id int64) (i
 	if function == nil {
 		return 0, 0, 0, errors.New("功能原始表中没有找到该功能")
 	}
-	myPowers := Base_power.FindMyPower(appid, function_code, user_id, ent_id)
+	myPowers := Base_power.FindMyPowersByFc(appid, function_code, user_id, ent_id)
 	if myPowers == nil || len(*myPowers) == 0 {
 		return -1, 0, 0, errors.New("不在有效期内")
 	}

+ 1 - 1
public/service/surplus_test.go

@@ -13,5 +13,5 @@ func TestSurplus(t *testing.T) {
 	// 医疗机构画像	lyh_yl_yljghx
 	// 医疗机构认领	lyh_yl_yljgrl
 	// 经销商认领		lyh_yl_jxsrl
-	t.Log(Surplus("10000", "lyh_yl_jxsss", 69717, 0, 0))
+	t.Log(Surplus("10000", "lyh_yl_yldy", 68773, 0, 0))
 }