package org.dromara.payment.merchant.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.merchant.domain.vo.PubMerchantVo;
import org.dromara.payment.merchant.domain.bo.PubMerchantBo;
import org.dromara.payment.merchant.service.IPubMerchantService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 商户申请表
 * 前端访问路由地址为:/payment/merchant
 *
 * @author LionLi
 * @date 2024-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/merchant1")
public class PubMerchantController extends BaseController {

    private final IPubMerchantService pubMerchantService;

    /**
     * 查询商户申请表列表
     */
    @SaCheckPermission("payment:merchant:list")
    @GetMapping("/list")
    public TableDataInfo<PubMerchantVo> list(PubMerchantBo bo, PageQuery pageQuery) {
        return pubMerchantService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商户申请表列表
     */
    @SaCheckPermission("payment:merchant:export")
    @Log(title = "商户申请表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PubMerchantBo bo, HttpServletResponse response) {
        List<PubMerchantVo> list = pubMerchantService.queryList(bo);
        ExcelUtil.exportExcel(list, "商户申请表", PubMerchantVo.class, response);
    }

    /**
     * 获取商户申请表详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("payment:merchant:query")
    @GetMapping("/{id}")
    public R<PubMerchantVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(pubMerchantService.queryById(id));
    }

    /**
     * 新增商户申请表
     */
    @SaCheckPermission("payment:merchant:add")
    @Log(title = "商户申请表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PubMerchantBo bo) {
        return toAjax(pubMerchantService.insertByBo(bo));
    }

    /**
     * 修改商户申请表
     */
    @SaCheckPermission("payment:merchant:edit")
    @Log(title = "商户申请表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PubMerchantBo bo) {
        return toAjax(pubMerchantService.updateByBo(bo));
    }

    /**
     * 删除商户申请表
     *
     * @param ids 主键串
     */
    @SaCheckPermission("payment:merchant:remove")
    @Log(title = "商户申请表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(pubMerchantService.deleteWithValidByIds(List.of(ids), true));
    }
}
