package com.kym.admin.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.kym.common.R;
import com.kym.common.annotation.SysLog;
import com.kym.entity.admin.queryParams.CommonQueryParam;
import com.kym.entity.admin.queryParams.InvoiceDetailQueryParam;
import com.kym.entity.admin.queryParams.InvoiceQueryParam;
import com.kym.service.admin.InvoiceDetailService;
import com.kym.service.miniapp.InvoiceService;
import com.kym.service.miniapp.RefundLogService;
import com.kym.service.wechat.WxPayService;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;


/**
 * @author skyline
 * @description 财务
 * @date 2023-09-14 16:21
 */
@RestController
@RequestMapping("/finance")
public class FinanceController {

    private final Logger logger = LoggerFactory.getLogger(FinanceController.class);

    private final WxPayService wxPayService;

    private final RefundLogService refundLogService;

    private final InvoiceService invoiceService;
    private final InvoiceDetailService invoiceDetailService;

    public FinanceController(WxPayService wxPayService, RefundLogService refundLogService, InvoiceService invoiceService, InvoiceDetailService invoiceDetailService) {
        this.wxPayService = wxPayService;
        this.refundLogService = refundLogService;
        this.invoiceService = invoiceService;
        this.invoiceDetailService = invoiceDetailService;
    }

    @SysLog("退款申请列表")
    @GetMapping("/listRefundLog")
    R<?> listRefundLog(@ModelAttribute CommonQueryParam params) {
        return R.success(refundLogService.listRefundLog(params));
    }

    @SysLog("处理用户微信退款")
    @GetMapping("/customWxRefund/{refundLogId}")
    R<?> customWxRefund(@PathVariable("refundLogId") long refundLogId) {
        wxPayService.wxRefund(refundLogId);
        return R.success();
    }

    @GetMapping("/getUserTitle/{applyId}")
    Object getUserTitle(@PathVariable String applyId) {
        return wxPayService.userTitle(applyId);
    }

    @SysLog("开具发票")
    @GetMapping("/handleInvoice/{invoiceId}")
    R<?> handleInvoice(@PathVariable("invoiceId") String invoiceId) {
        wxPayService.fapiaoApplication(invoiceId);
        return R.success();
    }

    @SysLog("下载发票")
    @GetMapping("/downloadInvoice/{invoiceId}")
    R<?> downloadInvoice(@PathVariable("invoiceId") String invoiceId) {
        return R.success(wxPayService.downloadInvoice(invoiceId));
    }

    /**
     * 发票列表
     *
     * @param params
     * @return 发票列表
     */
    @PostMapping("/listInvoice")
    R<?> list(@RequestBody InvoiceQueryParam params) {
        return R.success(invoiceService.listInvoice(params));
    }

    /**
     * 取消申请发票，发票抬头填写页关闭事件调用
     *
     * @return
     */
    @GetMapping("/cancelApplyInvoice/{invoiceId}")
    R<?> cancelApplyInvoice(@PathVariable String invoiceId) {
        invoiceService.cancelApplyInvoice(invoiceId);
        return R.success();
    }

    /**
     * 查询发票
     */
    @GetMapping("/getInvoice/{applyId}")
    R<?> getInvoice(@PathVariable String applyId) {
        return R.success(wxPayService.queryFapiao(applyId));
    }


    /**
     * 发票详情
     */
    @GetMapping("/listInvoiceDetail")
    R<?> listInvoiceDetail(@ModelAttribute InvoiceDetailQueryParam params) {
        return R.success(invoiceDetailService.listInvoiceDetail(params));
    }


    /**
     * 导出发票列表
     *
     * @param params
     * @return 导出发票列表
     */
    @GetMapping("/export/invoiceList")
    void exportInvoiceList(@ModelAttribute InvoiceDetailQueryParam params, HttpServletRequest request, HttpServletResponse response) {
        ExcelWriter writer = invoiceDetailService.exportInvoiceDetail(params);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("发票详情列表", StandardCharsets.UTF_8) + ".xlsx");
        ServletOutputStream excelOut = null;
        try {
            excelOut = response.getOutputStream();
            writer.flush(excelOut, true);
        } catch (Exception e) {
            logger.error("exportInvoiceList error", e);
        } finally {
            writer.close();
        }
        IoUtil.close(excelOut);
    }

}


