package com.xisc.pm.infra.client;

import com.alibaba.fastjson.JSONObject;
import com.xisc.pm.api.dto.InterfaceReceiveDTO;
import com.xisc.pm.domain.entity.PmSteelCastHeat;
import com.xisc.pm.infra.constant.Constants;
import com.xisc.pm.infra.feign.HeApiFeign;
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 org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @date 2024/11/4 15:07
 * @description
 */
@Slf4j
@Component
public class HeApiClient {

    @Autowired
    private HeApiFeign heApiFeign;

    public void secondPlanAssign(@PathVariable Long tenantId, @RequestBody InterfaceReceiveDTO dto) {
        log.info("调用HE-API【secondDataSend】，入参：tenantId = {}, context = {}", tenantId, JSONObject.toJSON(dto));
        ResponseEntity<String> result = heApiFeign.secondPlanAssign(tenantId, dto);
        log.info("调用HE-EVENT-API【secondDataSend】，返回：" + result.getBody());
        if (ResponseUtils.isFailed(result)) {
            // 获取异常信息
            ExceptionResponse response = Utils.getExceptionResponse(result);
            throw new CommonException("pm.call_inface_info","二级",response.getMessage());
        }
    }

    /**
     * 调用线板材MES接口：计划下达（HEXB02）
     * @param pmSteelCastHeatList
     * @param tenantId
     */
    public void callSecondPlanAssign(List<PmSteelCastHeat> pmSteelCastHeatList, Long tenantId, String interfaceCode, boolean statusFlag) {
        InterfaceReceiveDTO interfaceDto = new InterfaceReceiveDTO();
        List<Map<String, String>> context = new ArrayList<>();
        pmSteelCastHeatList.forEach(item -> {
            Map<String, String> lineMap = new HashMap<>(8);
            transToInterfaceMap(lineMap, item, statusFlag);
            context.add(lineMap);
        });
        interfaceDto.setInterfaceCode(interfaceCode);
        interfaceDto.setInterfaceData(context);
        secondPlanAssign(tenantId, interfaceDto);
    }

    private void transToInterfaceMap(Map<String, String> lineMap, PmSteelCastHeat item, boolean statusFlag) {
        lineMap.put("PONO", item.getPono());
        lineMap.put("MAIN_BACKLOG_CODE", item.getMainBacklogCode());
        lineMap.put("PONO_STATUS", statusFlag ? Constants.PonoStatus.STATUS_91 : Constants.PonoStatus.STATUS_18);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        lineMap.put("HEAT_CONFM_TIME", item.getHeatConfmTime() != null ? formatter.format(item.getHeatConfmTime()) :formatter.format(new Date()));
        lineMap.put("DECI_ST_NO", item.getDectStNo());
        lineMap.put("HEAT_CONFM_FLAG", item.getHeatComfmFlag());
        lineMap.put("HEAT_GRADE", item.getHeatGrade());
        lineMap.put("FLAG", item.getFlag());
    }
}
