package com.hdkj.lt.mq.listener;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.hdkj.lt.feign.ICityFeederTopoApiClient;
import com.hdkj.lt.feign.ILineSvgFileApiClient;
import com.hdkj.lt.modle.dto.svg.SvgGenerationDTO;
import com.hdkj.hussar.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

/**
 * @author Annie
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RocketConsumerCityFeederTopoListener implements MessageListener {

    private final ICityFeederTopoApiClient cityFeederTopoApiClient;

    private final ILineSvgFileApiClient lineSvgFileApiClient;

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        try {
            log.info("===== 收到异动，开始处理拓扑数据 =====");
            String value = new String(message.getBody(), StandardCharsets.UTF_8);
            List<String> lineIds = Collections.singletonList(value);
            // 调用业务层操作
            this.executeFunc(lineIds);
            return Action.CommitMessage;
        } catch (Exception e) {
            e.printStackTrace();
            return Action.ReconsumeLater;
        }
    }

    /**
     * 执行mq消费方法
     */
    public void executeFunc(List<String> lineIds) throws Exception {
        ApiResponse<List<String>> cityFeederSvg = cityFeederTopoApiClient.getCityFeederSvg();
        List<String> list = cityFeederSvg.getData();
        if (CollectionUtils.size(list) > 0) {
            //调成图服务解析数据
            ApiResponse<Object> objectApiResponse = cityFeederTopoApiClient.doCityFeederTopo(list);
            if (objectApiResponse.isSuccess()) {
                log.info("===== 处理拓扑数据完成 =====");
            }
            //解析完成后重新成图
//            SvgGenerationDTO svgGenerationDTO = new SvgGenerationDTO();
//            svgGenerationDTO.setSvgType("1");
//            svgGenerationDTO.setFeederList(lineIds);
//            lineSvgFileApiClient.lineDynamicSvgSave(svgGenerationDTO);
            /* // 连接响应时间无法保证,废弃此方式改成topo服务主动触发
            if (objectApiResponse.isSuccess() && rocketMQProperties.getEnabled()) {
                log.info("===== 处理拓扑数据完成 =====");
                RocketMQMessageEntity messageEntity = RocketMQMessageEntity.builder()
                        .topicId(RocketMQConstants.TOPIC_ID_DIST_LINE_MAPPING_FILE_TICKET)
                        .cacheKey(RocketMQConstants.TOPIC_ID_DIST_LINE_MAPPING_FILE_TICKET)
                        .tag("1")
                        .body(LocalDate.now().format(DateTimeFormatter.ofPattern(PURE_DATE_PATTERN))).build();
                rocketMQTemplate.sendAsyncMessage(messageEntity);
            }*/
        }

    }

}
