package com.xisc.pm.infra.client;

import com.alibaba.fastjson.JSONObject;
import com.xisc.pm.domain.vo.WmInStockVO;
import com.xisc.pm.domain.vo.WmSigleQtyVO;
import com.xisc.pm.infra.feign.WmApiFeign;
import com.xisc.pm.infra.util.Utils;
import io.choerodon.core.exception.CommonException;
import io.choerodon.core.exception.ExceptionResponse;
import lombok.extern.slf4j.Slf4j;
import org.hzero.core.util.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @date 2024/10/23 13:37
 * @description
 */
@Slf4j
@Component
public class WmApiClient {
    @Autowired
    private WmApiFeign wmApiFeign;

    public void syncSigleQty(Long tenantId, List<WmSigleQtyVO> WmSigleQtyVOs) {
        log.info("调用Wm-API【syncSigleQty】，入参：tenantId = {}, WmSigleQtyVOs = {}", tenantId, JSONObject.toJSON(WmSigleQtyVOs));
        ResponseEntity<String> res = wmApiFeign.syncSigleQty(tenantId, WmSigleQtyVOs);
        log.info("调用Wm-API【syncSigleQty】，返回：" + res.getBody());
        if (ResponseUtils.isFailed(res)) {
            ExceptionResponse response = Utils.getExceptionResponse(res);
            throw new CommonException("pm.call_wm_api_info_001", response.getMessage());
        }
    }

    public void matRemove(Long tenantId, List<WmInStockVO> wmInStockVOS) {
        log.info("调用Wm-API【matRemove】，入参：tenantId = {}, wmInStockVOS = {}", tenantId, JSONObject.toJSON(wmInStockVOS));
        ResponseEntity<String> res = wmApiFeign.matRemove(tenantId, wmInStockVOS);
        log.info("调用Wm-API【matRemove】，返回：" + res.getBody());
        if (ResponseUtils.isFailed(res)) {
            ExceptionResponse response = Utils.getExceptionResponse(res);
            throw new CommonException("pm.call_wm_api_info_001", response.getMessage());
        }
    }

    public void inStock(Long tenantId,  List<WmInStockVO> wmInStockVOS) {
        log.info("调用Wm-API【inStock】，入参：tenantId = {}, wmInStockVOS = {}", tenantId, JSONObject.toJSON(wmInStockVOS));
        ResponseEntity<String> res = wmApiFeign.inStock(tenantId, wmInStockVOS);
        log.info("调用Wm-API【inStock】，返回：" + res.getBody());
        if (ResponseUtils.isFailed(res)) {
            ExceptionResponse response = Utils.getExceptionResponse(res);
            throw new CommonException("pm.call_wm_api_info_001", response.getMessage());
        }
    }
}
