package com.hdkj.lt.si.service.psrdevice.impl;

import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.hdkj.lt.core.rest.service.IRemoteCallService;
import com.hdkj.lt.modle.dto.psrdevice.JoinEcTransformerDTO;
import com.hdkj.lt.modle.dto.psrdevice.PsrDeviceQueryDTO;
import com.hdkj.lt.modle.po.JoinEcTransformerPO;
import com.hdkj.lt.modle.vo.psrdevice.PsrDeviceResponseVO;
import com.hdkj.lt.si.service.psrdevice.IPsrDeviceService;
import com.hdkj.lt.si.service.remote.impl.PsrDeviceRemoteCallServiceImpl;
import com.hdkj.hussar.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Description 中压用户接入点和专变接口实现
 * @Author wcx
 * @Date 2025/7/29 20:43
 * @Version 1.0
 */
@Slf4j
@Service
@RefreshScope
public class PsrDeviceServiceImpl implements IPsrDeviceService {

    @Resource(type = PsrDeviceRemoteCallServiceImpl.class)
    private IRemoteCallService remoteCallService;

    @Value("${psr.method.deviceCommonUrl}")
    private String deviceCommonUrl;

    @Value("${psr.method.joinEcTransformerUrl}")
    private String joinEcTransformerUrl;

    @Override
    public ApiResponse<PsrDeviceResponseVO> getPsrDeviceByCommonInterface(PsrDeviceQueryDTO psrDeviceQuery) {
        PsrDeviceResponseVO psrDeviceResponse = remoteCallService.call(deviceCommonUrl, HttpMethod.POST, psrDeviceQuery, PsrDeviceResponseVO.class);
        return ApiResponse.success(psrDeviceResponse);
    }

    @Override
    public ApiResponse<List<JoinEcTransformerPO>> getJoinEcTransformerInfo(JoinEcTransformerDTO joinEcTransformer) {
        Map<String, Object> callback = remoteCallService.call(joinEcTransformerUrl, HttpMethod.POST, joinEcTransformer, HashMap.class);
        List<JoinEcTransformerPO> joinEcTransformerPOS = Optional.ofNullable(callback)
                .filter(en -> ObjectUtils.isNotEmpty(en.get("resData")))
                .map(en -> JSON.parseArray(JSON.toJSONString(en.get("resData")), JoinEcTransformerPO.class))
                .orElse(Lists.newArrayList());
        return ApiResponse.success(joinEcTransformerPOS);
    }
}
