package service

import (
	"go_sku_server/model"
	"go_sku_server/pkg/common"

	"github.com/gin-gonic/gin"
	"github.com/gomodule/redigo/redis"
	"github.com/iancoleman/orderedmap"
	"github.com/syyongx/php2go"
	"github.com/tidwall/gjson"
)

//自营公共函数类
/*
 计算单个sku总锁库数量
*/
func (qs *ZiyingService) skuLockNum(c *redis.Conn, goodsId string) int64 {
	reply, err := redis.Values((*c).Do("hvals", "Self_StockLock"+goodsId))
	if err != nil {
		return 0
	}

	var albums []int64
	if err := redis.ScanSlice(reply, &albums); err != nil {
		return 0
	}
	if len(albums) == 0 {
		return 0
	} else {
		var all int64 = 0
		for _, n := range albums {
			all = all + n
		}
		return all
	}
}

/*
 获取自营活动价
*/
func (qs *ZiyingService) ActivityPrice(ctx *gin.Context, SkuInfo string) *orderedmap.OrderedMap {
	data := qs.HDActivityPrice(SkuInfo)
	if data != nil {
		return data
	}
	return nil
}

//获取活动相关信息
func (qs *ZiyingService) GetActivity(skuInfo string) (priceActivity model.PriceActivity, giftActivity model.GiftActivity) {
	//去判断是否有活动(促销打折活动和满赠活动)
	checkData := model.ActivityCheckData{
		SupplierId: 10000,
		BrandId:    int(gjson.Get(skuInfo, "brand_id").Int()),
		GoodsId:    gjson.Get(skuInfo, "goods_id").String(),
		ClassId:    int(gjson.Get(skuInfo, "class_id2").Int()),
	}
	var activityService ActivityService
	priceActivity, giftActivity = activityService.GetActivityData(checkData)
	return
}

/*
 计算活动价

ac_type说明: 0 没有活动
活动名称 	ac_type 类型
自营系数 	6 	    自营
折扣(系数)活动 	10 	    自营/联营

*/
func (qs *ZiyingService) HDActivityPrice(SkuInfo string) *orderedmap.OrderedMap {
	priceActivity, giftActivity := qs.GetActivity(SkuInfo)
	ladderPriceArr := gjson.Get(SkuInfo, "ladder_price").Array()
	A := orderedmap.New()
	//先去判断价格活动
	if priceActivity.HasActivity && len(ladderPriceArr) > 0 {
		ratio := priceActivity.Ratio
		ladderType := make([]model.LadderPrice, 0)
		for _, v := range ladderPriceArr {
			priceCn := v.Get("price_cn").Float()
			priceAc := php2go.Round(priceCn*ratio/100*10000) / 10000
			ladderType = append(ladderType, model.LadderPrice{
				Purchases: v.Get("purchases").Int(),
				PriceUs:   v.Get("price_us").Float(),
				PriceCn:   priceCn,
				PriceAc:   priceAc,
			})
		}

		A.Set("activity_info", priceActivity)

		A.Set("ladder_price", ladderType)
		A.Set("ratio", ratio)
		A.Set("ac_type", 10)
	}

	//再去判断满赠活动
	if giftActivity.HasActivity {
		A.Set("has_gift_activity", 1)
		A.Set("gift_activity", giftActivity)
	}
	//其中一类活动不允许优惠券,这个商品就不允许使用优惠券
	var allowCoupon int
	if priceActivity.HasActivity && priceActivity.AllowCoupon == 2 {
		allowCoupon = 2
		A.Set("allow_coupon", allowCoupon)
	}
	if giftActivity.HasActivity && giftActivity.AllowCoupon == 2 {
		allowCoupon = 2
		A.Set("allow_coupon", allowCoupon)
	}
	return A
}

/*
 获取当前锁住的库存
*/
func (qs *ZiyingService) HDGoodsLimit(SkuID string, Stock string, c *redis.Conn) *orderedmap.OrderedMap {

	GoodsLimit, _ := redis.String((*c).Do("get", "Self_GoodsLimit_"+SkuID))
	if GoodsLimit == "" {
		return nil
	}
	start_time := gjson.Get(GoodsLimit, "start_time").Int()
	end_time := gjson.Get(GoodsLimit, "end_time").Int()
	buy_stock := gjson.Get(GoodsLimit, "buy_stock").Float()
	ac_stock := gjson.Get(GoodsLimit, "ac_stock").Float()
	schedule := gjson.Get(GoodsLimit, "schedule").Float()
	allow_coupon := gjson.Get(GoodsLimit, "allow_coupon").Int()

	if start_time < php2go.Time() && end_time > php2go.Time() {
		buy := buy_stock / ac_stock * 100
		schedule := common.MyRound(buy+schedule, 2)
		if schedule < 100 {
			A := orderedmap.New()
			//拼接梯度价格
			ladder_price_arr := gjson.Get(GoodsLimit, "ac_price").Array()
			ladderType := make([]model.LadderPrice, 0)
			for _, v := range ladder_price_arr {
				ladderType = append(ladderType, model.LadderPrice{
					Purchases: v.Get("purchases").Int(),
					PriceCn:   v.Get("price_cn").Float(),
					PriceAc:   v.Get("price_ac").Float(),
				})
			}

			now_stock := ac_stock * ((100 - schedule) / 100)

			A.Set("ac_type", 1)
			A.Set("ac_id", gjson.Get(GoodsLimit, "id").Int())
			A.Set("ladder_price", ladderType)
			A.Set("ac_stock", ac_stock)
			A.Set("stock", now_stock)
			A.Set("schedule", schedule)
			A.Set("activity_end_time", end_time)
			if allow_coupon > 0 {
				A.Set("allow_coupon", allow_coupon)
			} else {
				A.Set("allow_coupon", 1)
			}
			//活动库存大于当前可售库存,强制结束活动
			if now_stock > common.MyFloat64(Stock) || now_stock == 0 {
				return nil
			}
			return A
		}

	}
	return nil
}