package com.ms.biz.spi.callback;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinritemai.cloud.base.api.BaseRequest;
import com.jinritemai.cloud.base.api.BaseResponse;
import com.jinritemai.cloud.base.api.ExtensionService;
import com.jinritemai.cloud.base.api.ExtensionServiceHandler;
import com.ms.biz.common.R;
import com.ms.biz.common.SPIBaseService;
import com.ms.biz.dto.openspi.request.BatchPayParam;
import com.ms.biz.dto.openspi.response.BatchPayData;
import com.ms.biz.dto.openspi.response.QuerySupplyPlatformAuthData;
import com.ms.biz.service.DistributionOrderService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@ExtensionService("batchPay")
@Slf4j
@AllArgsConstructor
public class BatchPayService extends SPIBaseService implements ExtensionServiceHandler<BatchPayParam, JSONObject> {
    private DistributionOrderService distributionOrderService;

    @Override
    public BaseResponse<JSONObject> handle(BaseRequest<BatchPayParam> req) {
        try {
            BatchPayParam param = req.getData();
            BatchPayData data = new BatchPayData();
            distributionOrderService.batchPay(param, data);
            return BaseResponse.<JSONObject>builder().success(true).message("success").code("0").data(JSON.parseObject(JSON.toJSONString(data))).build();
        } catch (RuntimeException e) {
            return BaseResponse.<JSONObject>builder().success(true).message(e.getMessage()).code("100003").build();
        } catch (Throwable e) {
            log.error("batchPay error", e);
            return BaseResponse.<JSONObject>builder().success(true).message("系统异常").code("100003").build();
        }
    }
}