package com.zswl.cloud.springBtach.server.core.api.xiaoju;

import com.xiaoju.open.oil.commons.config.OilConfig;
import com.xiaoju.open.oil.core.QueryClient;
import com.xiaoju.open.oil.core.QueryClientFactory;
import com.xiaoju.open.oil.interfaces.domain.AllowanceInfo;
import com.xiaoju.open.oil.interfaces.request.QueryApiStorePriceRequest;
import com.xiaoju.open.oil.interfaces.request.QueryCalPriceRequest;
import com.xiaoju.open.oil.interfaces.request.QueryStoreDetailRequest;
import com.xiaoju.open.oil.interfaces.request.QueryTokenRequest;
import com.xiaoju.open.oil.interfaces.response.QueryApiStorePriceResponse;
import com.xiaoju.open.oil.interfaces.response.QueryCalPriceResponse;
import com.xiaoju.open.oil.interfaces.response.QueryStoreDetailResponse;
import com.xiaoju.open.oil.interfaces.response.QueryTokenResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class XiaoJuApi {

    @Autowired
    OilConfig oilConfig;

    public String queryToken() {

        QueryTokenRequest queryTokenRequest = new QueryTokenRequest();
        queryTokenRequest.setAppSecret(oilConfig.getAppSecret());
        QueryClient queryClient = QueryClientFactory.create(oilConfig);
        QueryTokenResponse queryTokenResponse = queryClient.api().queryToken(queryTokenRequest);
        return queryTokenResponse.getAccessToken();
    }


    public QueryStoreDetailResponse queryStoreDetail(String userId, String storeId,Float latitude,Float longitude) {

        QueryStoreDetailRequest queryStoreDetailRequest = new QueryStoreDetailRequest();

        queryStoreDetailRequest.setLat(latitude);
        queryStoreDetailRequest.setLon(longitude);
        queryStoreDetailRequest.setOutUserId(userId);
        queryStoreDetailRequest.setStoreId(storeId);
        queryStoreDetailRequest.setOpenChannel(1);

        QueryClient queryClient = QueryClientFactory.create(oilConfig);

        return queryClient.api().queryStoreDetail(queryStoreDetailRequest);
    }

    // 查询门店价格
    public List<QueryApiStorePriceResponse> queryStorePrice(String userId, List<String> storeIdList) {

        QueryApiStorePriceRequest queryApiStorePriceRequest = new QueryApiStorePriceRequest();
        queryApiStorePriceRequest.setOpenChannel(1);
        queryApiStorePriceRequest.setOutUserId(userId);
        queryApiStorePriceRequest.setStoreIds(storeIdList);
        QueryClient queryClient = QueryClientFactory.create(oilConfig);

        return queryClient.api().queryStorePrice(queryApiStorePriceRequest);

    }

    // 价格计算
    public QueryCalPriceResponse queryCalPrice(String userId, String storeId,String itemName,Integer amount, AllowanceInfo allowanceInfo) {
        QueryCalPriceRequest queryCalPriceRequest = new QueryCalPriceRequest();
        queryCalPriceRequest.setStoreId(storeId);
        queryCalPriceRequest.setOpenChannel(1);
        queryCalPriceRequest.setOutUserId(userId);
        queryCalPriceRequest.setItemName(itemName);
        queryCalPriceRequest.setAmount(amount);

        // 优惠券
//        List<AllowanceInfo> allowanceInfos = new ArrayList<>();
//        AllowanceInfo allowanceInfo = new AllowanceInfo();
//        allowanceInfo.setAllowanceId("E1-4ji-1kiJSL");
//        allowanceInfo.setAllowanceType(2);
//        allowanceInfos.add(allowanceInfo);
        queryCalPriceRequest.setPromotionInfo(List.of(allowanceInfo));

        QueryClient queryClient = QueryClientFactory.create(oilConfig);

        return queryClient.api().queryCalPrice(queryCalPriceRequest);


    }


}
