package com.dxhy.core.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

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 com.dxhy.core.job.service.ScheduleJobService;
import com.dxhy.core.model.openservice.OpenServiceOcr;
import com.dxhy.core.service.openservice.IOpenServicePlatformService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author user
 */
@Slf4j
@RestController
@RequestMapping("/openService")
public class OpenServicePlatformController {

    @Resource
    private IOpenServicePlatformService openServicePlatformService;
    @Resource
    private ScheduleJobService scheduleJobService;

    @PostMapping("/getInvoiceOcrResult")
    public List<OpenServiceOcr> getInvoiceOcrResult(@RequestBody Map<String, String> params) {
        try {
            String picture = params.get("picture");
            String loginName = params.get("loginName");
            Map<String, String> userApplyKey = scheduleJobService.getUserApplyKey(loginName);
            String aceId = userApplyKey.get("aceId");
            String aceKey = userApplyKey.get("aceKey");
            String taxno = userApplyKey.get("taxno");
            return openServicePlatformService.ocrInvoice(picture, aceId, aceKey, taxno);
        } catch (Exception e) {
            log.error("调用开放平台OCR接口失败，", e);
            return null;
        }
    }


    @PostMapping("/getInvoiceSnOcrResult")
    public List<OpenServiceOcr> getInvoiceSnOcrResult(@RequestBody Map<String, String> params) {
        try {
            String picture = params.get("picture");
            return openServicePlatformService.ocrInvoice(picture);
        } catch (Exception e) {
            log.error("调用开放平台OCR接口失败，", e);
            return null;
        }
    }
}
