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.InvoiceSettingVo;
import org.dromara.web.domain.bo.InvoiceSettingBo;
import org.dromara.web.service.IInvoiceSettingService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 发票设置
 *
 * @author Huanyi
 * @date 2025-08-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/setting")
public class InvoiceSettingController extends BaseController {

    private final IInvoiceSettingService invoiceSettingService;

    /**
     * 查询发票设置列表
     */
    @SaCheckPermission("web:setting:list")
    @GetMapping("/list")
    public TableDataInfo<InvoiceSettingVo> list(InvoiceSettingBo bo, PageQuery pageQuery) {
        return invoiceSettingService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出发票设置列表
     */
    @SaCheckPermission("web:setting:export")
    @Log(title = "发票设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(InvoiceSettingBo bo, HttpServletResponse response) {
        List<InvoiceSettingVo> list = invoiceSettingService.queryList(bo);
        ExcelUtil.exportExcel(list, "发票设置", InvoiceSettingVo.class, response);
    }

    /**
     * 获取发票设置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("web:setting:query")
    @GetMapping("/{id}")
    public R<InvoiceSettingVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(invoiceSettingService.queryById(id));
    }

    /**
     * 新增发票设置
     */
    @SaCheckPermission("web:setting:add")
    @Log(title = "发票设置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody InvoiceSettingBo bo) {
        return toAjax(invoiceSettingService.insertByBo(bo));
    }

    /**
     * 修改发票设置
     */
    @SaCheckPermission("web:setting:edit")
    @Log(title = "发票设置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody InvoiceSettingBo bo) {
        return toAjax(invoiceSettingService.updateByBo(bo));
    }

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