package com.xydl.cac.adapter;

import com.xydl.cac.adapter.busiclient.XydlService;
import com.xydl.cac.adapter.busiclient.XydlServiceImplServiceLocator;
import com.xydl.cac.entity.I2syncConfig;
import com.xydl.cac.entity.I2syncField;
import com.xydl.cac.entity.I2syncRecord;
import com.xydl.cac.entity.NSensor;
import com.xydl.cac.model.i2sync.Request;
import com.xydl.cac.transform.I2DataTransformer;
import com.xydl.cac.util.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

@Component
@Slf4j
public class BusiAdapter {

    @Value("${cac.i2.enable:false}")
    public boolean enable;
    @Value("${cac.i2.url}")
    public String url;
    @Resource
    I2DataTransformer dataTransformer;

    public void uploadData(NSensor sensor, I2syncRecord record, I2syncConfig config,
                           List<I2syncField> fieldList, List<Map<String, Object>> dataList) throws Exception {
        // 格式转换
        Request request = dataTransformer.transform(sensor, record, config, fieldList, dataList);

        // 转换结构
        String xml = JSONUtil.object2Xml(request);

        // 同步和导出
        XydlServiceImplServiceLocator locator = new XydlServiceImplServiceLocator();
        locator.setXydlServiceImplPortEndpointAddress(url);
        XydlService service = locator.getXydlServiceImplPort();
        String resp = service.uploadCACData(xml);
        log.debug("uploadData 完成. " + resp);
    }
}
