package org.dromara.payment.operator.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.operator.domain.vo.ReProductInvoiceVo;
import org.dromara.payment.operator.domain.bo.ReProductInvoiceBo;
import org.dromara.payment.operator.service.IReProductInvoiceService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 产品开票类目
 * 前端访问路由地址为:/payment/productInvoice
 *
 * @author LionLi
 * @date 2024-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/productInvoice")
public class ReProductInvoiceController extends BaseController {

    private final IReProductInvoiceService reProductInvoiceService;

    /**
     * 查询产品开票类目列表
     */
    @SaCheckPermission("payment:productInvoice:list")
    @GetMapping("/list")
    public TableDataInfo<ReProductInvoiceVo> list(ReProductInvoiceBo bo, PageQuery pageQuery) {
        return reProductInvoiceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出产品开票类目列表
     */
    @SaCheckPermission("payment:productInvoice:export")
    @Log(title = "产品开票类目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ReProductInvoiceBo bo, HttpServletResponse response) {
        List<ReProductInvoiceVo> list = reProductInvoiceService.queryList(bo);
        ExcelUtil.exportExcel(list, "产品开票类目", ReProductInvoiceVo.class, response);
    }

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

    /**
     * 新增产品开票类目
     */
    @SaCheckPermission("payment:productInvoice:add")
    @Log(title = "产品开票类目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ReProductInvoiceBo bo) {
        return toAjax(reProductInvoiceService.insertByBo(bo));
    }

    /**
     * 修改产品开票类目
     */
    @SaCheckPermission("payment:productInvoice:edit")
    @Log(title = "产品开票类目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ReProductInvoiceBo bo) {
        return toAjax(reProductInvoiceService.updateByBo(bo));
    }

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