package com.jkcredit.invoice.hub.controller.rest.forward;

import com.alibaba.fastjson.JSON;
import com.jkcredit.invoice.hub.constant.CommonConstant;
import com.jkcredit.invoice.hub.controller.base.RestResource;
import com.jkcredit.invoice.hub.enums.ApiResponseCodeEnum;
import com.jkcredit.invoice.hub.service.apiCarFree.ApiCarFreeService;
import com.jkcredit.invoice.hub.service.apiCompany.ApiCompanyService;
import com.jkcredit.invoice.hub.service.apiUser.ApiUserService;
import com.jkcredit.invoice.hub.service.apiWayBillCountQuery.WayBillCountQueryService;
import com.jkcredit.invoice.hub.spi.lang.exception.ApiRequestException;
import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
import com.jkcredit.invoice.hub.spi.rest.data.ApiRequestParam;
import com.jkcredit.invoice.hub.spi.rest.data.ApiResponseData;
import com.jkcredit.invoice.hub.util.EnumStatusUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/14 18:20
 * @version: V1.0
 **/
@Slf4j
@RestController
public class ForwardResource extends RestResource {
    @Autowired
    ApiUserService apiUserService;
    @Autowired
    ApiCompanyService apiCompanyService;
    @Autowired
    ApiCarFreeService apiCarFreeService;
    @Autowired
    WayBillCountQueryService wayBillCountQueryService;
    @PostMapping("/rest")
    public ApiResponseData forwardRequest(@RequestBody ApiRequestParam param) {
        // todo 确定余额控制是在上传还是每个接口
        try {
            validate(param);
        } catch (ServiceException se) {
            log.error("接口参数ApiRequestParam校验失败,失败原因：{}", se.getMessage());
            return ApiResponseData.failure(ApiResponseCodeEnum.CODE_1010.getValue(), ApiResponseCodeEnum.CODE_1010.getDesc());
        }

        try {
            // 校验用户key secret, 权限，余额
            Integer validateUserResult = apiUserService.validateUser(param);
            if (!validateUserResult.equals(ApiResponseCodeEnum.CODE_200.getValue())) {
                return ApiResponseData.failure(validateUserResult, EnumStatusUtils.getStatusByValue(ApiResponseCodeEnum.class, validateUserResult).getDesc());
            }

            if (param.getApi().equals(CommonConstant.COMPANY_QUERY_V1)) {
                return apiCompanyService.companyQuery(JSON.parseObject(JSON.toJSONString(param.getData())));
            } else if (param.getApi().equals(CommonConstant.COMPANY_ADD_V1)) {
                return apiCompanyService.companyAdd(JSON.parseObject(JSON.toJSONString(param.getData())));
            } else if (param.getApi().equals(CommonConstant.VEHICLE_REGISTER_QUERY)) {
                return apiCarFreeService.vehicleRegisterQuery(JSON.parseObject(JSON.toJSONString(param.getData())));
            } else if (param.getApi().equals(CommonConstant.VEHICLE_REGISTER)) {
                return apiCarFreeService.vehicleRegister(JSON.parseObject(JSON.toJSONString(param.getData())));
            } else if (param.getApi().equals(CommonConstant.WAY_BILL_START)) {
                return apiCarFreeService.wayBillStart(JSON.parseObject(JSON.toJSONString(param.getData())));
            } else if (param.getApi().equals(CommonConstant.WAY_BILL_END)) {
                return apiCarFreeService.wayBillEnd(JSON.parseObject(JSON.toJSONString(param.getData())));
            } else if (param.getApi().equals(CommonConstant.WAY_BILL_HISTORY_START)) {
                return apiCarFreeService.wayBillHistoryStart(JSON.parseObject(JSON.toJSONString(param.getData())));
            } else if (param.getApi().equals(CommonConstant.WAY_BILL_HISTORY_END)) {
                return apiCarFreeService.wayBillHistoryEnd(JSON.parseObject(JSON.toJSONString(param.getData())));
            } else if (param.getApi().equals(CommonConstant.WAY_BILL_COUNT_QUERY)) {
                return wayBillCountQueryService.wayBillCountQuery(JSON.parseObject(JSON.toJSONString(param.getData())));
            } else if (param.getApi().equals(CommonConstant.BALANCE_QUERY)) {
                return apiUserService.balanceQuery(JSON.parseObject(JSON.toJSONString(param.getData())));
            } else if (param.getApi().equals("FIND_INVOICE")) {
                // 自用接口，不对客户开放
                return apiCarFreeService.wayBillNumFindInvoice(JSON.parseObject(JSON.toJSONString(param.getData())));
            } else if (param.getApi().equals(CommonConstant.WAY_BILL_NUM_FIND_INVOICE)) {
                // 客户用 运单查询发票
                return apiCarFreeService.findInvoice(JSON.parseObject(JSON.toJSONString(param.getData())));
            } else if (param.getApi().equals(CommonConstant.FIND_NO_SEARCH_NUM)) {
                return apiCarFreeService.findNoSearchNums(JSON.parseObject(JSON.toJSONString(param.getData())));
            } else {
                return ApiResponseData.failure(ApiResponseCodeEnum.CODE_1080.getValue(), ApiResponseCodeEnum.CODE_1080.getDesc());
            }
        } catch (ServiceException se) {
            log.error("接口调用失败,失败原因：{}", se.getMessage());
            return ApiResponseData.failure(ApiResponseCodeEnum.CODE_9998.getValue(), ApiResponseCodeEnum.CODE_9998.getDesc());
        } catch (ApiRequestException ae) {
            log.error("接口业务失败,失败原因：{}", ae.getMessage());
            return ApiResponseData.failure(ApiResponseCodeEnum.CODE_200.getValue(), ae.getMessage());
        }
    }
}
