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.BatchCreateParam;
import com.ms.biz.dto.openspi.response.BatchCreateData;
import com.ms.biz.dto.openspi.response.BatchPayData;
import com.ms.biz.service.DistributionOrderService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@ExtensionService("batchCreate")
@Slf4j
@AllArgsConstructor
public class BatchCreateService extends SPIBaseService implements ExtensionServiceHandler<BatchCreateParam, JSONObject> {
    private DistributionOrderService distributionOrderService;

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