package org.dromara.web.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.web.domain.vo.InvoiceRecordVo;
import org.dromara.web.domain.bo.InvoiceRecordBo;
import org.dromara.web.service.IInvoiceRecordService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 开票记录
 *
 * @author Huanyi
 * @date 2025-08-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/invoice/record")
public class InvoiceRecordController extends BaseController {

    private final IInvoiceRecordService invoiceRecordService;

    /**
     * 查询开票记录列表
     */
    @SaCheckPermission("web:record:list")
    @GetMapping("/list")
    public TableDataInfo<InvoiceRecordVo> list(InvoiceRecordBo bo, PageQuery pageQuery) {
        return invoiceRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出开票记录列表
     */
    @SaCheckPermission("web:record:export")
    @Log(title = "开票记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(InvoiceRecordBo bo, HttpServletResponse response) {
        List<InvoiceRecordVo> list = invoiceRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "开票记录", InvoiceRecordVo.class, response);
    }

    /**
     * 获取开票记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("web:record:query")
    @GetMapping("/{id}")
    public R<InvoiceRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(invoiceRecordService.queryById(id));
    }

    /**
     * 新增开票记录
     */
    @SaCheckPermission("web:record:add")
    @Log(title = "开票记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody InvoiceRecordBo bo) {
        return toAjax(invoiceRecordService.insertByBo(bo));
    }

    /**
     * 修改开票记录
     */
    @SaCheckPermission("web:record:edit")
    @Log(title = "开票记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody InvoiceRecordBo bo) {
        return toAjax(invoiceRecordService.updateByBo(bo));
    }

    /**
     * 删除开票记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("web:record:remove")
    @Log(title = "开票记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(invoiceRecordService.deleteWithValidByIds(List.of(ids), true));
    }
}
