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.MerInvoiceDetailVo;
import org.dromara.payment.invoice.domain.bo.MerInvoiceDetailBo;
import org.dromara.payment.invoice.service.IMerInvoiceDetailService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

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

    private final IMerInvoiceDetailService merInvoiceDetailService;

    /**
     * 查询开票明细列表
     */
    //@SaCheckPermission("payment:invoiceDetail:list")
    @GetMapping("/list")
    public TableDataInfo<MerInvoiceDetailVo> list(MerInvoiceDetailBo bo, PageQuery pageQuery) {
        return merInvoiceDetailService.queryPageList(bo, pageQuery);
    }

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

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

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

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

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