package com.hzy.project.business.invoice.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hzy.framework.aspectj.lang.annotation.Log;
import com.hzy.framework.aspectj.lang.enums.BusinessType;
import com.hzy.project.business.invoice.domain.Invoice;
import com.hzy.project.business.invoice.service.IInvoiceService;
import com.hzy.framework.web.controller.BaseController;
import com.hzy.framework.web.domain.AjaxResult;
import com.hzy.common.utils.poi.ExcelUtil;
import com.hzy.framework.web.page.TableDataInfo;

/**
 * 发票Controller
 * 
 * @author ruoyi
 * @date 2026-01-02
 */
@Controller
@RequestMapping("/business/invoice")
public class InvoiceController extends BaseController
{
    private String prefix = "business/invoice";

    @Autowired
    private IInvoiceService invoiceService;

    @RequiresPermissions("business:invoice:view")
    @GetMapping()
    public String invoice()
    {
        return prefix + "/invoice";
    }

    /**
     * 查询发票列表
     */
    @RequiresPermissions("business:invoice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Invoice invoice)
    {
        startPage();
        List<Invoice> list = invoiceService.selectInvoiceList(invoice);
        return getDataTable(list);
    }

    /**
     * 导出发票列表
     */
    @RequiresPermissions("business:invoice:export")
    @Log(title = "发票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Invoice invoice)
    {
        List<Invoice> list = invoiceService.selectInvoiceList(invoice);
        ExcelUtil<Invoice> util = new ExcelUtil<Invoice>(Invoice.class);
        return util.exportExcel(list, "发票数据");
    }

    /**
     * 新增发票
     */
    @RequiresPermissions("business:invoice:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存发票
     */
    @RequiresPermissions("business:invoice:add")
    @Log(title = "发票", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Invoice invoice)
    {
        return toAjax(invoiceService.insertInvoice(invoice));
    }

    /**
     * 修改发票
     */
    @RequiresPermissions("business:invoice:edit")
    @GetMapping("/edit/{invoiceId}")
    public String edit(@PathVariable("invoiceId") Long invoiceId, ModelMap mmap)
    {
        Invoice invoice = invoiceService.selectInvoiceByInvoiceId(invoiceId);
        mmap.put("invoice", invoice);
        return prefix + "/edit";
    }

    /**
     * 修改保存发票
     */
    @RequiresPermissions("business:invoice:edit")
    @Log(title = "发票", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Invoice invoice)
    {
        return toAjax(invoiceService.updateInvoice(invoice));
    }

    /**
     * 删除发票
     */
    @RequiresPermissions("business:invoice:remove")
    @Log(title = "发票", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(invoiceService.deleteInvoiceByInvoiceIds(ids));
    }
}
