package cmd

import (
	"context"
	"database/sql"
	"fmt"
	"hk/model/config_api"
	"hk/model/goods_info"
	"hk/model/goods_status"
	_73 "hk/pkg/util/172"
	"hk/pkg/util/strUtil"
	"strconv"
	"time"
)

func configApiList() {
	configApiInfo, err := svcCtx.ConfigApiModel.FindList(context.Background())
	if err != nil {
		fmt.Println(err)
	}
	if configApiInfo == nil {
		return
	}
	for _, item := range configApiInfo {
		switch item.Type {
		case "0":
			getGoodsStatus(item)
			updateGoodsStatus(item)
		}
	}
}

func configApiListv1() {
	configApiInfo, err := svcCtx.ConfigApiModel.FindList(context.Background())
	if err != nil {
		fmt.Println(err)
	}
	if configApiInfo == nil {
		return
	}
	for _, item := range configApiInfo {
		switch item.Type {
		case "0":
			addGoodsInfo(item)
		}
	}
}

func addGoodsInfo(configApi config_api.ConfigApi) {
	ids, err := svcCtx.GoodsStatusModel.FindNotIdOne(context.Background(), configApi.Id)
	if err != nil || ids <= 0 {
		return
	}

	req := _73.GoodsReq{
		ProductID: strconv.FormatInt(ids, 10),
		BaseParams: _73.BaseParams{
			UserID:   strUtil.ValidString(configApi.Key),
			UserSign: strUtil.ValidString(configApi.Secret),
		},
	}

	result, err := _73.GetProductV2(req)
	if err != nil {
		return
	}

	if result.Code != 0 || len(result.Data) <= 0 {
		return
	}
	data := result.Data[0]
	status := "0"
	if data.Flag {
		status = "1"
	}
	goodsStatus := goods_info.GoodsInfo{
		Name:            data.ProductName,
		Type:            "0",
		MainPic:         data.MainPic,
		LittlePicture:   data.LittlePicture,
		NetAddr:         data.NetAddr,
		Area:            data.Area,
		DisableArea:     data.DisableArea,
		DisableAge:      "",
		DisableContract: "",
		Notes:           sql.NullString{},
		ApiId:           strUtil.Int64ToNullInt64(configApi.Id),
		ApiProductId:    strUtil.Int64ToNullInt64(data.ProductID),
		NumberSel:       strconv.Itoa(data.NumberSel),
		Status:          status,
		CreateTime:      time.Now(),
		UpdateTime:      time.Now(),
		Remarks:         sql.NullString{},
	}
	svcCtx.GoodsInfoModel.Insert(context.Background(), &goodsStatus)
}

func updateGoodsStatus(item config_api.ConfigApi) {
	ids, err := svcCtx.GoodsStatusModel.FindUpList(context.Background(), item.Id)
	if err != nil {
		return
	}
	if len(ids) <= 0 {
		return
	}
	svcCtx.GoodsStatusModel.UpdateDownByIds(context.Background(), ids)
}

func getGoodsStatus(configApi config_api.ConfigApi) {
	req := _73.GoodsReq{
		ProductID: "",
		BaseParams: _73.BaseParams{
			UserID:   strUtil.ValidString(configApi.Key),
			UserSign: strUtil.ValidString(configApi.Secret),
		},
	}

	result, err := _73.GetProduct(req)
	if err != nil {
		return
	}

	if result.Code == 0 {
		var goodsStatusList []goods_status.GoodsStatus
		if len(result.Data) > 0 {
			err = svcCtx.GoodsStatusModel.DeleteByApiId(context.Background(), configApi.Id)
			if err != nil {
				return
			}
		}
		for _, item := range result.Data {
			var status int64 = 0
			if item.Flag == "上架中" {
				status = 1
			}
			goodsStatus := goods_status.GoodsStatus{
				ApiId:        strUtil.Int64ToNullInt64(configApi.Id),
				ApiProductId: strUtil.Int64ToNullInt64(item.ProductID),
				Status:       strUtil.Int64ToNullInt64(status),
			}
			svcCtx.GoodsStatusModel.Insert(context.Background(), &goodsStatus)
			goodsStatusList = append(goodsStatusList, goodsStatus)
		}
	}
}