package com.pj.api.open.web;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.pj.api.open.ResultJson;
import com.pj.api.open.bo.CheckCarNumberBO;
import com.pj.api.open.bo.InRecordBO;
import com.pj.api.open.service.OpenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RequestMapping(value = "open")
@RestController
@Slf4j
public class OpenController {
    @Resource
    OpenService openService;

    @PostMapping(value = "req")
    public ResultJson req(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        String dataType = request.getHeader("DataType");
        if ("CheckCarNumber".equals(dataType)) {
            CheckCarNumberBO checkCarNumberBO = BeanUtil.toBean(params, CheckCarNumberBO.class);
            return checkCarNumber(checkCarNumberBO);
        }else  if ("Inrecord".equals(dataType)) {
            InRecordBO inRecordBO = BeanUtil.toBean(params, InRecordBO.class);
            return handlerInRecord(inRecordBO);
        }
        return ResultJson.error("未知异常");
    }

    private ResultJson handlerInRecord(InRecordBO inRecordBO) {
        log.info("handler in record:{}", JSONUtil.toJsonStr(inRecordBO));
        openService.handlerInRecord(inRecordBO);
        return ResultJson.success();
    }

    private ResultJson checkCarNumber(CheckCarNumberBO checkCarNumberBO) {
        log.info("check car number:{}", JSONUtil.toJsonStr(checkCarNumberBO));
        return openService.checkCarNumber(checkCarNumberBO);
    }
}
