package mtwmapi

import (
	"fmt"
	"git.rosy.net.cn/jx-callback/globals"
	"testing"
	"time"

	"git.rosy.net.cn/baseapi/utils"
)

func TestFullDiscountBatchSave(t *testing.T) {
	result, err := api.FullDiscountBatchSave(testPoiCode, &FullDiscountActInfo{
		// ActIDs:    "12345678",
		ActName:   "测试活动0402",
		StartTime: time.Now().Unix(),
		EndTime:   time.Now().Add(24 * time.Hour).Unix(),
		ActType:   ActTypeStoreFullDiscount,
	}, []*FullDiscountActDetail{
		&FullDiscountActDetail{
			OriginalPrice: 10000,
			ActPrice:      1,
		},
	}, /*[]*ActSkuInfo{
			&ActSkuInfo{
				AppFoodCode: "30902",
				DayLimit:    1,
			},
		}*/nil)
	if err != nil {
		t.Fatal(err)
	}
	// 30902
	t.Log(result)
}

func TestFullDiscountList(t *testing.T) {
	result, err := api.FullDiscountList("9846711", ActTypeStoreFullDiscount)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestFullDiscountFoodsList(t *testing.T) {
	result, err := api.FullDiscountFoodsList("17626555", "9162539254")
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestFulllDiscountShippingFeeBatchSave(t *testing.T) {
	err := api.FulllDiscountShippingFeeBatchSave(testPoiCode, []*ShippingFeeActData{&ShippingFeeActData{}})
	if err != nil {
		t.Fatal(err)
	}
}

func TestFulllDiscountShippingFeeList(t *testing.T) {
	result, err := api.FulllDiscountShippingFeeList(testPoiCode)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestRetailDiscountBatchSave(t *testing.T) {
	result, _, err := api.RetailDiscountBatchSave2("18702580", RetailActTypeDirectDown, []*RetailDiscountActData{
		&RetailDiscountActData{
			AppFoodCode: "6045386",
			UserType:    0,
			StartTime:   1705593600,
			EndTime:     1717171199,
			OrderLimit:  1,
			DayLimit:    -1,
			SettingType: 1,
			ActPrice:    0.5,
			Sequence:    1,
		},
		&RetailDiscountActData{
			AppFoodCode: "30700",
			UserType:    0,
			StartTime:   1705593600,
			EndTime:     1717171199,
			OrderLimit:  1,
			DayLimit:    -1,
			SettingType: 1,
			ActPrice:    0.8,
			Sequence:    2,
		},
		&RetailDiscountActData{
			AppFoodCode: "30686",
			UserType:    0,
			StartTime:   1705593600,
			EndTime:     1717171199,
			OrderLimit:  1,
			DayLimit:    -1,
			SettingType: 1,
			ActPrice:    1,
			Sequence:    3,
		},
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestRetailDiscountList(t *testing.T) { //56 1001
	threefoldDown := 0 // 0-3 折活动数量
	threefoldUp := 0   // 3.01-9 折活动
	// 获取美团活动
	directList, _ := api.RetailDiscountList("32235182", RetailActTypeDirectDown) // 折扣活动
	for _, v := range directList {
		if v.ActPrice/v.OriginalPrice*10 >= 3.01 {
			threefoldUp++
		} else {
			threefoldDown++
		}
	}
	secKillList, _ := api.RetailDiscountList("32235182", RetailActTypeSecKill) // 爆品活动
	if secKillList != nil {
		threefoldDown = threefoldDown + len(secKillList)
	}

	fmt.Println(threefoldDown)
	fmt.Println(threefoldUp)
}

func TestNew(t *testing.T) {
	startTime := 1696831082 //2023-10-09 13:58:02
	endTime := 1697090282   //2023-10-12 13:58:02
	sTime := 1696089600     //2023-10-01 00:00:00
	eTime := 1698768000     //2023-11-01 00:00:00
	//fmt.Printf("1=%s", startTime < eTime && eTime < endTime)
	//fmt.Printf("2=%s", startTime < sTime)
	if sTime > endTime || eTime < startTime {
		fmt.Println("false")
	} else {
		fmt.Println("true")
	}
}

func TestParseErr4RetailDiscountDelete(t *testing.T) {
	errStr := `
	{"error_list":[{"act_id":1053302130,"error_msg":"活动ID不存在"},{"act_id":1053302127,"error_msg":"活动ID不存在"},{"act_id":1053302128,"error_msg":"活动ID不存在"},{"act_id":1053302129,"error_msg":"活动ID不存在"},{"act_id":1053302135,"error_msg":"活动ID不存在"},{"act_id":1053302132,"error_msg":"活动ID不存在"},{"act_id":1053302123,"error_msg":"活动ID不存在"},{"act_id":1053302125,"error_msg":"活动ID不存在"},{"act_id":1053302124,"error_msg":"活动ID不存在"},{"act_id":1053302133,"error_msg":"活动ID不存在"},{"act_id":1053302126,"error_msg":"活动ID不存在"},{"act_id":1053302134,"error_msg":"活动ID不存在"},{"act_id":1053302131,"error_msg":"活动ID不存在"}]}

	`
	errExt := utils.NewErrorCode(errStr, "")
	failedList := ParseErr4RetailDiscountDelete(errExt)
	t.Log(utils.Format4Output(failedList, false))
}

func TestGetByAppPoiCodeAndType(t *testing.T) {
	result, err := api.GetByAppPoiCodeAndType("9472886", 1, 1)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestSgin(t *testing.T) {
	timestamp := time.Now().Unix()
	sign := api.signParams("https://waimaiopen.meituan.com/api/v1/wm/IM/getConnectionToken", map[string]interface{}{"timestamp": timestamp, "app_id": 589})
	fmt.Println(sign)
	fmt.Println(timestamp)
}

func TestBaokuanHuodong(t *testing.T) {
	// 获取门店所有的爆款活动
	storeId := 103201
	vendorStoreId := "8694421"

	actList, err := api.RetailDiscountList(vendorStoreId, 56)
	fmt.Println(err)
	if len(actList) > 0 {
		allActivitySkuIdList := make([]string, 0, 0)               // 此门店全部的折扣(爆款)活动商品
		activationActivitySkuIdList := make([]*StoreSkuInfo, 0, 0) // 此门店正在进行的折扣(爆款)活动商品
		loseActivitySkuIdList := make([]*StoreSkuInfo, 0, 0)       // 此门店已经结束的折扣(爆款)活动商品
		for _, ac := range actList {
			allActivitySkuIdList = append(allActivitySkuIdList, utils.Int64ToStr(ac.ItemID))
			// 已经生效的爆款活动
			if ac.Status == 1 && ac.SkuId != "" {
				activity := &StoreSkuInfo{
					SkuID:       utils.Str2Int(ac.SkuId),
					IsSpecialty: 1,
				}
				activationActivitySkuIdList = append(activationActivitySkuIdList, activity)
			}
			// 已经失效的爆款活动
			if ac.Status == 0 && ac.SkuId != "" {
				lose := &StoreSkuInfo{
					SkuID:       utils.Str2Int(ac.SkuId),
					IsSpecialty: 0,
				}
				loseActivitySkuIdList = append(loseActivitySkuIdList, lose)
			}
		}

		if err := UpdateStoreSkusSpecTag(nil, "589", storeId, vendorStoreId, loseActivitySkuIdList); err != nil {
			globals.SugarLogger.Debugf("取消力荐错误:= %v", err)
		}
		// 重新推荐力荐商品
		if err := UpdateStoreSkusSpecTag(nil, "589", storeId, vendorStoreId, activationActivitySkuIdList); err != nil {
			globals.SugarLogger.Debugf("重推力荐错误:= %v", err)
		}
	}

}

func UpdateStoreSkusSpecTag(aa interface{}, vendorOrgCode string, storeID int, vendorStoreID string, storeSkuList []*StoreSkuInfo) (err error) {
	var foodDataList = []map[string]interface{}{}
	for _, v := range storeSkuList {
		var foodData = make(map[string]interface{})
		if v.IsSpecialty == -1 {
			v.IsSpecialty = 0
		}
		foodData["is_specialty"] = v.IsSpecialty
		foodData["app_food_code"] = v.SkuID
		foodDataList = append(foodDataList, foodData)
	}
	if len(foodDataList) == 1 {
		err = api.RetailInitData("1111", vendorStoreID, utils.Int2Str(storeSkuList[0].SkuID), foodDataList[0])
	} else if len(foodDataList) > 0 {
		_, err = api.RetailBatchInitData("22222", vendorStoreID, foodDataList)
	}
	return err
}

type StoreSkuInfo struct {
	SkuID        int    `json:"skuID,omitempty"`
	VendorSkuID  string `json:"vendorSkuID,omitempty"`
	NameID       int    `json:"nameID,omitempty"`
	VendorNameID string `json:"vendorNameID,omitempty"`

	Stock       int   `json:"stock,omitempty"`
	VendorPrice int64 `json:"price,omitempty"`
	Status      int   `json:"status,omitempty"`

	Seq int `json:"seq,omitempty"`

	ActPrice       int64   `json:"actPrice,omitempty"`
	VendorActID    string  `json:"vendorActID,omitempty"`
	IsSpecialty    int     `json:"isSpecialty,omitempty"`
	JxPrice        int64   `json:"jxPrice,omitempty"`
	JxUnitPrice    int64   `json:"jxUnitPrice,omitempty"`
	VendorSkuID2   string  `json:"vendorSkuID2,omitempty"`
	JdsStockSwitch int     `json:"jdsStockSwitch"`
	IsDeletedBySku bool    `json:"isDeletedBySku"`
	VendorOrgCode  string  `json:"vendorOrgCode"`
	SpecUnit       string  `json:"specUnit"`
	SpecQuality    float32 `json:"specQuality"`

	VendorMainId    string `json:"vendorMainId"`    // 主商品id
	VendorSkuAttrId string `json:"vendorSkuAttrId"` //主商品sku_id
	VendorSonSkuID  string `json:"vendorSonSkuID"`  // 子商品skuid
}

//42597433459
func TestDeleteActSku(t *testing.T) {
	failedList2, err2 := api.RetailDiscountDelete2("18698027", 1001, []string{"131226110670"})
	fmt.Println(utils.Format4Output(failedList2, false))
	fmt.Println(err2)

}

func TestCEA(t *testing.T) {

	a := "11:%s"

	fmt.Println(fmt.Sprintf(a, "1"))
}
