package com.cf.imes.module.executor.controller.admin.funds.invoice;


import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import com.cf.imes.module.executor.controller.admin.funds.invoice.vo.*;
import com.cf.imes.module.executor.service.funds.invoice.InvoiceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;

import static com.cf.imes.framework.common.pojo.CommonResult.success;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.IMPORT;

/**
 * @author 发票管理
 */
@Tag(name = "管理后台 - 发票管理")
@RestController
@RequestMapping("/executor/invoice")
@Validated
public class InvoiceController {


    @Resource
    private InvoiceService invoiceService;



    @GetMapping("/{organId}")
    @Operation(summary = "获取组织的开票金额信息")
    @Parameter(name = "organId", description = "组织ID", required = true, example = "1")
    public CommonResult<InvoiceAmountRespVO> getInvoiceAmount(@PathVariable("organId") Long organId) {

        return success(invoiceService.getInvoiceAmount(organId));

    }





    @GetMapping("/details")
    @Operation(summary = "获取组织可开票数据明细")
    public CommonResult<PageResult<InvoiceDetailsRespVO>> getInvoiceDetails(@Valid InvoiceDetailsPageReqVO pageReqVO) {

        return success(invoiceService.getInvoiceDetails(pageReqVO));

    }





    @GetMapping("/records")
    @Operation(summary = "获取组织的开票记录")
    public CommonResult<PageResult<InvoiceRecordsRespVO>> getInvoiceRecords(@Valid InvoiceDetailsPageReqVO pageReqVO) {

        return success(invoiceService.getInvoiceRecords(pageReqVO));

    }




    @GetMapping("/invoice")
    @Parameter(name = "invoiceId", description = "发票ID", required = true)
    @Operation(summary = "获取发票对应的购买记录和抬头信息")
    public CommonResult<InvoiceDetailsRespVO> getInvoiceIncome(@Valid @RequestParam("invoiceId") Long invoiceId) {

        return success(invoiceService.getInvoiceIncome(invoiceId));

    }





    @PostMapping("")
    @Operation(summary = "组织新增开票记录")
    public CommonResult<Boolean> insetInvoiceRecords(@Valid @RequestBody InvoiceRecordsReqVO reqVO) {

        invoiceService.insetInvoiceRecords(reqVO);

        return success(Boolean.TRUE);

    }





    @PutMapping("")
    @Operation(summary = "同意/拒绝开票")
    public CommonResult<Boolean> invoicing(@Valid @RequestBody InvoiceInvoicingReqVO reqVO) {

        invoiceService.invoicing(reqVO);

        return success(Boolean.TRUE);

    }




    @PostMapping("/upload")
    @Operation(summary = "发票上传")
    @Parameter(name = "invoiceId", description = "发票ID", required = true)
    @OperateLog(type = IMPORT)
    public CommonResult<Boolean> uploadInvoice(@RequestParam("file") MultipartFile file,@RequestParam("invoiceId") Long invoiceId) {

        invoiceService.uploadInvoice(file,invoiceId);

        return success(Boolean.TRUE);

    }





    @GetMapping("/download")
    @Operation(summary = "发票文件下载")
    @Parameter(name = "invoiceId", description = "发票ID", required = true)
    public void downloadInvoice(@RequestParam("invoiceId") Long invoiceId, HttpServletResponse response) {

        invoiceService.downloadInvoice(invoiceId,response);

    }


}
