package com.dxhy.erp.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dxhy.common.util.pojo.GlobalInfo;
import com.dxhy.erp.service.SignedService;
import com.dxhy.erp.utils.OpenApiUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jiaohongyang 艺龙 接口入口
 *
 */
@Controller
@RequestMapping("/YLinvoice")
@Slf4j
public class YlInterfaceController {

    @Resource
    private SignedService signedService;

    @ResponseBody
    @RequestMapping(value = "/dii", method = RequestMethod.POST)
    public String test(HttpServletResponse response, HttpServletRequest request) {
        GlobalInfo globalInfo = null;
        String returnResult;
        ObjectMapper mapper = new ObjectMapper();
        try {
            // 获取json
            Map<String, String> requestParam = OpenApiUtils.getRequestParam(request, mapper);
            // 签权校验接口安全
            int validParams = signedService.validParams(requestParam);
            // 解析头信息
            globalInfo = signedService.getGlobalInfo(requestParam);
            if (validParams == 1) {
                returnResult = signedService.getSignFailure(response, globalInfo);
                log.info("==========返回报文:" + returnResult);
                return returnResult;
            }
            // 判断接口调用
            switch (globalInfo.getInterfaceCode()) {
                // 获得发票信息
                case "INVOICE.GET":
                    returnResult = signedService.get(globalInfo, requestParam, mapper, request, response);
                    break;
                case "INVOICE.GETSOLA":
                    returnResult = signedService.getSola(globalInfo, requestParam, mapper, request, response);
                    break;
                case "INVOICE.GETAMOUNT":
                    returnResult = signedService.getAmount(globalInfo, requestParam, mapper, request, response);
                    break;
                // 接口编码不存在
                default:
                    returnResult = signedService.getInterfaceCodeError(response, globalInfo);
                    break;
            }
        } catch (Exception e) {
            returnResult = signedService.getDealWithFailure(response, globalInfo);
            log.error("艺龙接口调用错误:", e);
        }
        log.info("==========返回报文:" + returnResult);
        return returnResult;
    }

}
