package com.inspect.client.util;

import com.alibaba.fastjson.JSONObject;
import com.inspect.client.vo.Item;
import com.inspect.client.vo.LowCommand;
import com.inspect.client.vo.UpCommand;
import com.inspect.client.vo.UpResponse;
import com.inspect.base.core.utils.StringUtils;

public class CommandTransfer {
   public static UpCommand parseUpCommand(String upCommandJsonStr) {
      return (UpCommand)JSONObject.parseObject(upCommandJsonStr, UpCommand.class);
   }

   public static String transferUpToLow(UpCommand upCommand) {
      LowCommand lowCommand = new LowCommand();
      lowCommand.setSendCode(upCommand.getCode());
      lowCommand.setReceiveCode(upCommand.getReceiveCode());
      lowCommand.setCommand(upCommand.getCommand());
      lowCommand.setType(upCommand.getType());
      lowCommand.setTime(upCommand.getTime());
      lowCommand.setCode(upCommand.getCode());
      if(null != upCommand.getItem()) {
         lowCommand.setValue(upCommand.getItem().getValue());
      }

      return JSONObject.toJSONString(lowCommand);
   }

   public static UpResponse generateUpResponse(String sendCode, String receiveCode, String result) {
      JSONObject json = JSONObject.parseObject(result);
      UpResponse response = new UpResponse();
      String code = json.getString("code");
      if(StringUtils.equals("200", code)) {
         response.setCode(code);
      }

      response.setSendCode(sendCode);
      response.setReceiveCode(receiveCode);
      return response;
   }

   public static JSONObject parse(String msg) {
      JSONObject jsonObject = JSONObject.parseObject(msg);
      return jsonObject;
   }

   public static void main(String[] args) {
      UpCommand order = new UpCommand();
      order.setSendCode("1024");
      order.setReceiveCode("256");
      order.setType("1");
      order.setCommand("10");
      order.setCode("21021");
      order.setTime("2022-10-19 15:56:30");
      Item item = new Item();
      item.setValue("1.0");
      order.setItem(item);
      String orderJson = JSONObject.toJSONString(order);
      UpCommand upCommand = parseUpCommand(orderJson);
      String s = transferUpToLow(upCommand);
      System.out.println(s);
   }
}
