package org.dromara.zhishu.service.strategy;

import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.zhishu.domain.dto.response.LogisticsMethodResponse;
import org.dromara.zhishu.domain.dto.request.OrderDeliveryPddRequest;
import org.dromara.zhishu.domain.dto.request.OrderDeliveryRequest;
import org.dromara.zhishu.service.client.PddClient;
import org.dromara.zhishu.util.UrlUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderPddStrategy implements OrderStrategy {

    @Autowired
    private PddClient pddClient;

    @Override
    public List<LogisticsMethodResponse> deliveryMethodList(Long shopId) {
        return pddClient.deliveryMethodList(UrlUtil.getPddServiceUrl());
    }

    @Override
    public Boolean orderDelivery(OrderDeliveryRequest request) {
        OrderDeliveryPddRequest orderDeliveryPddRequest = new OrderDeliveryPddRequest();
        BeanUtils.copyProperties(request, orderDeliveryPddRequest);
        orderDeliveryPddRequest.setLogisticsId(Long.valueOf(request.getMethodId()));
        R<Boolean> r = pddClient.orderDelivery(UrlUtil.getPddServiceUrl(), orderDeliveryPddRequest);
        if (R.isError(r)) throw new ServiceException(r.getMsg());
        return r.getData();
    }

    @Override
    public Boolean syncKfzHistoryOrder(Long shopId, Integer days) {
        String result = pddClient.fullSynchronizationOrder(UrlUtil.getPddServiceUrl(), days, "manual", List.of(shopId));
        if (!result.equals("true")) throw new ServiceException("同步历史订单失败：店铺Id-" + shopId);
        return true;
    }
}
