package com.ruoyi.logistics.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.logistics.callback.SfRoutingPushCallback;
import com.ruoyi.logistics.domain.InvoiceRequest;
import com.ruoyi.logistics.service.InvoiceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


/**
 * 百旺开票相关
 * @author RuiJing
 * @date 2026-02-06
 */
@RestController
@RequestMapping("/invoice")
public class BizInvoiceController {
    private static final Logger logger = LoggerFactory.getLogger(BizInvoiceController.class);

     @Autowired
     InvoiceService invoiceService;

    /**
     * 发票开具
     */
    @PostMapping("/invoiceIssuance")
    public AjaxResult invoiceIssuance(@RequestBody InvoiceRequest invoiceRequest) {
        try {
            Map result = invoiceService.invoiceIssuance(invoiceRequest);
            if("0".equals(result.get("code").toString())){
             return   AjaxResult.success("开票成功！");
            }
            return AjaxResult.error("开票失败！  原因："+result.get("renson").toString());
        }
        catch(Exception e){
            return AjaxResult.error("系统发生错误！"+e.getMessage());

        }

    }


    /**
     * 发票查询
     */
    @PostMapping("/updateInvoice")
    public void updateInvoice() throws JsonProcessingException {
       invoiceService.updateInvoice();
    }


    /**
     * 针对已开票月度账单进行回传商城
     * 回传商城后更新状态
     */
    @PostMapping("/returnShop")
    public AjaxResult returnShop(@RequestBody InvoiceRequest invoiceRequest) {
        try{
          Map m=   invoiceService.returnShop(invoiceRequest);
          if("0".equals(m.get("code").toString())){
           return  AjaxResult.success("商城同步成功！");
          }
          else{
              return  AjaxResult.error("商城同步失败！");
          }
        } catch (Exception e) {
            logger.error(e.getMessage());
           return AjaxResult.error("商城推送给失败！");
        }
    }


}
