package com.ms.web.controller;

import com.doudian.open.core.DoudianOpSpi;
import com.doudian.open.core.DoudianOpSpiBizHandler;
import com.ms.api.dto.dsapi.response.GetPlatformAuthUrlResponseDTO;
import com.ms.biz.bo.ShopBO;
import com.ms.biz.dto.openspi.request.*;
import com.ms.biz.dto.openspi.response.*;
import com.ms.biz.service.DistributionOrderService;
import com.ms.biz.service.DsApiService;
import com.ms.biz.service.ShopService;
import com.ms.biz.service.ShopTo1688DsMemberService;
import com.ms.biz.tool.CommonTool;
import com.ms.dal.entity.ShopTo1688DsMember;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Slf4j
@RestController
public class OpenSPIController {

    @Autowired
    private DistributionOrderService distributionOrderService;

    @Autowired
    private ShopService shopService;

    @RequestMapping(path = "supplyCenter/auth/querySupplyPlatformAuth" , method = {RequestMethod.GET})
    @ResponseBody
    public String querySupplyPlatformAuth(HttpServletRequest httpServletRequest) {
        return DoudianOpSpi.config(QuerySupplyPlatformAuthRequest.class, querySupplyPlatformAuthBizHandler, httpServletRequest).responseJson();
    }

    private final DoudianOpSpiBizHandler querySupplyPlatformAuthBizHandler = context -> {
        QuerySupplyPlatformAuthParam param = context.getParam();
        System.out.println("param"  + param);
        QuerySupplyPlatformAuthData data = context.getData();
        try {
            shopService.querySupplyPlatformAuth(param, data);
            context.wrapSuccess();
        } catch (Throwable e) {
            log.error("querySupplyPlatformAuthErr", e);
            context.wrapError(100002L, e.getMessage());
        }
    };

    @RequestMapping(path = "supplyCenter/order/batchCreate" , method = {RequestMethod.POST})
    @ResponseBody
    public String batchCreate(HttpServletRequest httpServletRequest) {
        return DoudianOpSpi.config(BatchCreateRequest.class, batchCreateBizHandler, httpServletRequest).responseJson();
    }

    private final DoudianOpSpiBizHandler batchCreateBizHandler = context -> {
        BatchCreateParam param = context.getParam();
        System.out.println("param"  + param);
        BatchCreateData data = context.getData();
        try {
            distributionOrderService.batchCreate(param, data);
            context.wrapSuccess();
        } catch (Throwable e) {
            log.error("batchCreateErr", e);
            context.wrapError(100002L, e.getMessage());
        }
    };

    @RequestMapping(path = "supplyCenter/order/batchPay" , method = {RequestMethod.POST})
    @ResponseBody
    public String batchPay(HttpServletRequest httpServletRequest) {
        System.out.println(httpServletRequest);
        return DoudianOpSpi.config(BatchCreateRequest.class, batchPayBizHandler, httpServletRequest).responseJson();
    }

    private final DoudianOpSpiBizHandler batchPayBizHandler = context -> {
        BatchPayParam param = context.getParam();
        BatchPayData data = context.getData();
        try {
            distributionOrderService.batchPay(param, data);
        } catch (Throwable e) {
            context.wrapError(100002L, e.getMessage());
        }
    };
}
