package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.FncInvoiceVo;
import com.ruoyi.system.service.IFncInvoiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 发票Controller
 *
 * @author jlf
 * @date 2022-11-20
 */
@Api(tags = "发票Controller")
@RestController
@RequestMapping("/finance/invoice")

public class FncInvoiceController extends BaseController {

    @Autowired
    private  IFncInvoiceService fncInvoiceService;

    @ApiOperation("查询发票列表")
    @PreAuthorize("@ss.hasPermi('finance:invoice:list')")
    @GetMapping("/list")
    public TableDataInfo<FncInvoiceVo> list(FncInvoiceVo entity) {
        return fncInvoiceService.queryList(entity);
    }

    @ApiOperation("查询发票所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(FncInvoiceVo entity) {
        return AjaxResult.success("查询成功", fncInvoiceService.queryAll(entity));
    }

    @ApiOperation("导出发票列表")
    @PreAuthorize("@ss.hasPermi('finance:invoice:export')")
    @Log(title = "发票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FncInvoiceVo entity) {
        List<FncInvoiceVo> list = fncInvoiceService.queryAll(entity);
        ExcelUtil<FncInvoiceVo> util = new ExcelUtil<>(FncInvoiceVo.class);
        util.exportExcel(response, list, "发票数据");
    }

    @ApiOperation("获取发票详细信息")
    @PreAuthorize("@ss.hasPermi('finance:invoice:query')")
    @GetMapping(value = "/getInfo/{invoiceId}")
    public AjaxResult getInfo(@PathVariable("invoiceId") Long invoiceId) {
        return AjaxResult.success("查询成功", fncInvoiceService.queryById(invoiceId));
    }

    @ApiOperation("新增发票")
    @PreAuthorize("@ss.hasPermi('finance:invoice:add')")
    @Log(title = "发票", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody FncInvoiceVo entity) {
        return toAjax(fncInvoiceService.save(entity));
    }

    @ApiOperation("修改发票")
    @PreAuthorize("@ss.hasPermi('finance:invoice:edit')")
    @Log(title = "发票", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody FncInvoiceVo entity) {
        return toAjax(fncInvoiceService.updateById(entity));
    }

    @ApiOperation("删除发票")
    @PreAuthorize("@ss.hasPermi('finance:invoice:remove')")
    @Log(title = "发票", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{invoiceIds}")
    public AjaxResult remove(@PathVariable Long[] invoiceIds) {
        return toAjax(fncInvoiceService.removeByIds(Arrays.asList(invoiceIds)) ? 1 : 0);
    }
}
