package com.xhpc.evcs.api;

import com.xhpc.evcs.dto.CommonRequest;
import com.xhpc.evcs.dto.CommonResponse;
import com.xhpc.evcs.dto.QueryTerminalCodeRequest;
import com.xhpc.evcs.dto.QueryTerminalCodeResponse;
import com.xhpc.evcs.encryption.EvcsConst;
import com.xhpc.evcs.utils.JSONUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @Author HongYun on 2021/11/11
 */
@RestController
public class QueryTerminalCodeController {

    @PostMapping(value = "/v1/query_terminal_code")
    public CommonResponse queryTerminalCode(@RequestBody CommonRequest<QueryTerminalCodeRequest> commonRequest) throws IOException {

        CommonResponse resp = new CommonResponse();
        QueryTerminalCodeResponse queryTerminalCodeResponse = new QueryTerminalCodeResponse();
        QueryTerminalCodeRequest queryTerminalCodeRequest = JSONUtil.readParams(commonRequest.getData(), QueryTerminalCodeRequest.class);
        resp.setRet(EvcsConst.RET_FAIL);
        resp.setMsg("fail");
        if (null != queryTerminalCodeRequest) {
            String qRCode = queryTerminalCodeRequest.getQRCode();
            if (null != qRCode) {
                resp.setRet(EvcsConst.RET_SUCC);
                resp.setMsg("success");
                String terminalCode = qRCode.split("=")[1];
                queryTerminalCodeResponse.setTerminalCode(terminalCode);
                resp.setData(JSONUtil.toJSONString(queryTerminalCodeResponse));
            }
        }
        return resp;
    }

}
