package com.lutao.cloud.consume.feign;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lutao.cloud.collect.dto.DataAddDTO;
import com.lutao.cloud.collect.vo.DataAddVO;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.consume.factory.CommandStrategy;
import com.lutao.cloud.consume.factory.CommandStrategyFactory;
import com.lutao.cloud.consume.util.EntityConvert;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 使用客户端
 *
 * @author yx
 * @date 2023/07/24
 */
@RestController
@Slf4j
@AllArgsConstructor
public class ConsumeClient implements RemoteConsumeService{

    @Resource
    private CommandStrategyFactory commandStrategyFactory;


    @Override
    public MessageInfo getListByIds(DataAddDTO dataAddDTO) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(dataAddDTO.getData());
            //需要操作的数据表
            String resource = dataAddDTO.getResource();
            //得到命令
            String command = dataAddDTO.getCommand();
            //数据库不同，策略不同
            DataAddVO dataAddVO = EntityConvert.dataAddDtoToVo(dataAddDTO);

            //TODO dataAddDTO 添加 appID
            CommandStrategy<Object> strategy = commandStrategyFactory.createStrategy(resource);
            return strategy.execute(command, jsonNode,dataAddVO);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
