package com.ruoyi.front.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.logistics.service.IMonthQRCodeTaskService;
import com.ruoyi.logistics.service.LogisticsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * WX-月结二维码生成
 *
 * @author lydgt
 * @date 2026-02-04
 */
@RestController
@RequestMapping("/front/qrcode")
public class MonthQRCodeController extends BaseController {

    @Autowired
    private LogisticsOrderService logisticsOrderService;

    @Autowired
    private IMonthQRCodeTaskService monthQRCodeService;

    @RequestMapping
    public AjaxResult qrcode(@RequestParam("source") Integer source, @RequestParam("waybillCode") String waybillCode) {
        try{
            JSONObject jsonObject = logisticsOrderService.getQRCode(source);
//            JSONObject jsonObject = logisticsOrderService.queryOrderDetailByWaybillCode(source, waybillCode);
//            JSONObject jsonObject = logisticsOrderService.queryOrderByQRCode(source, waybillCode);


//            CommonMonthSettleQRQueryResponse response = (CommonMonthSettleQRQueryResponse) jsonObject.get("msg");
            // 返回完整的二维码信息，包括图片数据
            return AjaxResult.success(jsonObject);
        } catch (ServiceException e) {
            return AjaxResult.error("获取二维码失败！ 原因:"+e.getMessage());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @InnerAuth
    @PostMapping("/consume")
    public R<String> consume() {
        try {
            monthQRCodeService.consumeQRCode();
            return R.ok();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
