package com.xisc.pm.infra.client;

import com.alibaba.fastjson.JSONObject;
import com.xisc.pm.api.dto.PassQualifiedProDTO;
import com.xisc.pm.api.dto.PmUpdateSlabDTO;
import com.xisc.pm.domain.vo.MatStatusModifyVO;
import com.xisc.pm.domain.vo.MmAutoHeatPlanVO;
import com.xisc.pm.domain.vo.MmCutSeqVO;
import com.xisc.pm.infra.feign.MmApiFeign;
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/21 15:10
 * @description
 */
@Slf4j
@Component
public class MmEventClient {
    private final MmApiFeign mmEventFeign;

    @Autowired
    private  QeApiClient qeApiClient;

    public MmEventClient(MmApiFeign mmEventFeign) {
        this.mmEventFeign = mmEventFeign;
    }
    
    public void updateSlabMscLineNo(Long tenantId,List<PmUpdateSlabDTO> pmUpdateSlabDTO) {
     // 调用 Feign 客户端，向指定租户发送事件
        log.info("调用MM-EVENT-API【updateSlabMscLineNo】，入参：tenantId = {}, eventCode = {}, pmUpdateSlabDTO = {}", tenantId, JSONObject.toJSON(pmUpdateSlabDTO));
        ResponseEntity<String> result = mmEventFeign.batchUpdateMscLineNo(tenantId, pmUpdateSlabDTO);
        log.info("调用MM-EVENT-API【updateSlabMscLineNo】，返回：" + result.getBody());
        if (ResponseUtils.isFailed(result)) {
            // 获取异常信息
            ExceptionResponse response = Utils.getExceptionResponse(result);
            throw new CommonException("pm.call_inface_info", "轧钢",response.getMessage());
        }
    }

    public void matStatusModify(Long tenantId, String eventCode, MatStatusModifyVO matStatusModifyVO) {
        // 调用 Feign 客户端，向指定租户发送事件
        log.info("调用MM-EVENT-API【matStatusModify】，入参：tenantId = {}, eventCode = {}, matStatusModifyVO = {}", tenantId, eventCode, JSONObject.toJSON(matStatusModifyVO));
        ResponseEntity<String> result = mmEventFeign.matStatusModify(tenantId, eventCode, "PM", matStatusModifyVO);
        log.info("调用MM-EVENT-API【matStatusModify】，返回：" + result.getBody());
        if (ResponseUtils.isFailed(result)) {
            // 获取异常信息
            ExceptionResponse response = Utils.getExceptionResponse(result);
            throw new CommonException("pm.call_inface_info", "轧钢",response.getMessage());
        }
    }

    public void batchMatStatusModify(Long tenantId, String eventCode, List<MatStatusModifyVO> matStatusModifyVOList) {
        // 调用 Feign 客户端，向指定租户发送事件
        log.info("调用MM-EVENT-API【batchMatStatusModify】，入参：tenantId = {}, eventCode = {}, matStatusModifyVOList = {}", tenantId, eventCode, JSONObject.toJSON(matStatusModifyVOList));
        ResponseEntity<String> result = mmEventFeign.batchMatStatusModify(tenantId, eventCode, "PM", matStatusModifyVOList);
        log.info("调用MM-EVENT-API【batchMatStatusModify】，返回：" + result.getBody());
        if (ResponseUtils.isFailed(result)) {
            // 获取异常信息
            ExceptionResponse response = Utils.getExceptionResponse(result);
            throw new CommonException("pm.call_inface_info", "轧钢",response.getMessage());
        }
    }

    public void batchUpdateCutSeq(Long tenantId, List<MmCutSeqVO> updateCutSeqDTOList) {
        // 调用 Feign 客户端，向指定租户发送事件
        log.info("调用MM-EVENT-API【batchUpdateCutSeq】，入参：tenantId = {},updateCutSeqDTOList = {}", tenantId, JSONObject.toJSON(updateCutSeqDTOList));
        ResponseEntity<String> result = mmEventFeign.batchUpdateCutSeq(tenantId, updateCutSeqDTOList);
        log.info("调用MM-EVENT-API【batchUpdateCutSeq】，返回：" + result.getBody());
        if (ResponseUtils.isFailed(result)) {
            // 获取异常信息
            // 获取异常信息
            ExceptionResponse response = Utils.getExceptionResponse(result);
            throw new CommonException("pm.call_inface_info", "轧钢", response);
        }
    }

    public void autoHeatPlan(Long tenantId, List<MmAutoHeatPlanVO> autoHeatPlanDTOList) {
        // 调用 Feign 客户端，向指定租户发送事件
        log.info("调用MM-EVENT-API【autoHeatPlan】，入参：tenantId = {},autoHeatPlanDTOList = {}", tenantId, JSONObject.toJSON(autoHeatPlanDTOList));
        ResponseEntity<String> result = mmEventFeign.autoHeatPlan(tenantId, autoHeatPlanDTOList);
        log.info("调用MM-EVENT-API【autoHeatPlan】，返回：" + result.getBody());
        if (ResponseUtils.isFailed(result)) {
            // 获取异常信息
            ExceptionResponse response = Utils.getExceptionResponse(result);
            throw new CommonException("pm.call_inface_info", "轧钢", response);
        }
    }

    public List<PassQualifiedProDTO> getPassQualifiedPro( Long tenantId, List<Long> plateIds) {
        log.info("调用MM-API【getPassQualifiedPro】，入参：tenantId = {},plateIds = {}", tenantId, JSONObject.toJSON(plateIds));
        ResponseEntity<String> result = mmEventFeign.getPassQualifiedPro(tenantId, plateIds);
        log.info("调用MM-API【getPassQualifiedPro】，返回：" + result.getBody());
        if (ResponseUtils.isFailed(result)) {
            // 获取异常信息
            ExceptionResponse response = Utils.getExceptionResponse(result);
            throw new CommonException("pm.call_inface_info", "轧钢", response);
        }
        return qeApiClient.dealResponse(result, PassQualifiedProDTO.class);
    }

}
