package org.dromara.payment.invoice.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.payment.invoice.domain.vo.MerInvoiceBillVo;
import org.dromara.payment.invoice.domain.bo.MerInvoiceBillBo;
import org.dromara.payment.invoice.service.IMerInvoiceBillService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 发票明细
 * 前端访问路由地址为:/payment/invoiceBill
 *
 * @author LionLi
 * @date 2024-05-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/invoiceBill")
public class MerInvoiceBillController extends BaseController {

    private final IMerInvoiceBillService merInvoiceBillService;

    /**
     * 查询发票明细列表
     */
    //@SaCheckPermission("payment:invoiceBill:list")
    @GetMapping("/list")
    public TableDataInfo<MerInvoiceBillVo> list(MerInvoiceBillBo bo, PageQuery pageQuery) {
        return merInvoiceBillService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出发票明细列表
     */
    @SaCheckPermission("payment:invoiceBill:export")
    @Log(title = "发票明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MerInvoiceBillBo bo, HttpServletResponse response) {
        List<MerInvoiceBillVo> list = merInvoiceBillService.queryList(bo);
        ExcelUtil.exportExcel(list, "发票明细", MerInvoiceBillVo.class, response);
    }

    /**
     * 获取发票明细详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("payment:invoiceBill:query")
    @GetMapping("/{id}")
    public R<MerInvoiceBillVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(merInvoiceBillService.queryById(id));
    }

    /**
     * 新增发票明细
     */
    @SaCheckPermission("payment:invoiceBill:add")
    @Log(title = "发票明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MerInvoiceBillBo bo) {
        return toAjax(merInvoiceBillService.insertByBo(bo));
    }

    /**
     * 修改发票明细
     */
    @SaCheckPermission("payment:invoiceBill:edit")
    @Log(title = "发票明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MerInvoiceBillBo bo) {
        return toAjax(merInvoiceBillService.updateByBo(bo));
    }

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