package com.xunmei.sync.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xunmei.common.core.http.service.params.HttpParamsGet;
import com.xunmei.sync.config.ThirdPartySystemConfig;
import com.xunmei.sync.dto.fjnxsync.FJNXXmlUserDto;
import com.xunmei.sync.dto.fjnxsync.SyncResponseDto;
import com.xunmei.sync.service.IFJNXUserSyncService;
import com.xunmei.sync.service.api.FjnxApi;
import com.xunmei.sync.service.api.FjnxXmlApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhulu
 * @date 2022/5/30 16:02
 */
@Service
@Slf4j
public class FJNXUserSyncServiceImpl implements IFJNXUserSyncService {
    @Autowired
    FjnxApi fjnxApi;
    @Autowired
    FjnxXmlApi fjnxXmlApi;

    @Autowired
    private ThirdPartySystemConfig thirdPartySystemConfig;
    @Override
    public SyncResponseDto getUserSyncFileInfo() {
        final Map<String, Object> params = new HashMap<>();
        // TODO 放在数据库中，放在配置文件中如果不是多套需要修改多个配置文件，可以放在数据库中 获取一次后放入Redis缓存
        params.put("systemCode", thirdPartySystemConfig.getFjnxSystemCode());
//        final JSONObject data = this.fjnxApi.get(HttpParamsGet.get(thirdPartySystemConfig.getFjnxSyncUserFileInfoUrl(), params));
        final JSONObject data = this.fjnxApi.get(HttpParamsGet.get(thirdPartySystemConfig.getFjnxSyncUserFileInfoUrl(), params));//"/fjnx/mock/orgfileInfo"
        if (data == null) {
            return null;
        }
        return  JSONObject.toJavaObject(data,SyncResponseDto.class);
    }

    @Override
    public FJNXXmlUserDto getUserInfoByFileInfo() {
        SyncResponseDto syncResponseDto =getUserSyncFileInfo();
        if(syncResponseDto==null) return null;

        // TODO 后期将参数和地址 放在数据库中，放在配置文件中如果不是多套需要修改多个配置文件，可以放在数据库中 获取一次后放入Redis缓存
        final Map<String, Object> params = new HashMap<>();
        params.put("systemCode", thirdPartySystemConfig.getFjnxSystemCode());
        params.put("fileName", syncResponseDto.getData().getFileName());
        params.put("fileId", syncResponseDto.getData().getFileId());
        params.put("storageType", "localdisk");
//        final String data= fjnxXmlApi.get(HttpParamsGet.get(thirdPartySystemConfig.getFjnxSyncUserFileDownloadUrl(), params));

        final String data= fjnxXmlApi.get(HttpParamsGet.get(thirdPartySystemConfig.getFjnxSyncUserFileDownloadUrl(), params));//"/fjnx/mock/userxmldata"

        try {
            return FjnxXmlApi.convertXmlToJavaBean(data, FJNXXmlUserDto.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
