package com.xhpc.pp.tx;

import com.xhpc.pp.logic.ServiceMainLogic;
import com.xhpc.pp.server.ChargingPileBinaryHandler;
import com.xhpc.pp.utils.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Map;


@Controller
@RequestMapping
public class ServiceController {

    private static final String SESSIONID = "pileNo";
    private static final String SERVICE_NAME = "serviceName";

    @Autowired
    private ServiceMainLogic servicemainLogic;

    private static final Logger log = LoggerFactory.getLogger(ServiceController.class);

    @RequestMapping(value = "/api")
    @ResponseBody
    public String index(@RequestBody Map<String, Object> req) throws IOException {

        String pileNo = (String) req.get(SESSIONID);
        String serviceName = (String) req.get(SERVICE_NAME);

        ServiceParameter serviceParameter = new ServiceParameter(serviceName, pileNo, req);
        ServiceResult result = servicemainLogic.process(serviceParameter);

        if (result.getJson() != null) {
            String body = JSONUtil.toJSONString(result.getJson());
            return body;
        } else {
            return "";
        }

    }

}
