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.dromara.common.satoken.utils.LoginHelper;
import org.dromara.payment.merchant.domain.vo.BusMerchantAccountVo;
import org.dromara.payment.merchant.service.IBusMerchantAccountService;
import org.dromara.payment.sys.domain.vo.SysSydAccountVo;
import org.dromara.system.api.model.LoginUser;
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.BusMerchantCashoutVo;
import org.dromara.payment.merchant.domain.bo.BusMerchantCashoutBo;
import org.dromara.payment.merchant.service.IBusMerchantCashoutService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 商户提现记录
 * 前端访问路由地址为:/payment/merchantCashout
 *
 * @author LionLi
 * @date 2024-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/merchantCashout")
public class BusMerchantCashoutController extends BaseController {

    private final IBusMerchantCashoutService busMerchantCashoutService;

    private final IBusMerchantAccountService merchantAccountService;

    /**
     * 查询商户提现记录列表
     */
    @SaCheckPermission("payment:merchantCashout:list")
    @GetMapping("/list")
    public TableDataInfo<BusMerchantCashoutVo> list(BusMerchantCashoutBo bo, PageQuery pageQuery) {
        return busMerchantCashoutService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商户提现记录列表
     */
    @SaCheckPermission("payment:merchantCashout:export")
    @Log(title = "商户提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusMerchantCashoutBo bo, HttpServletResponse response) {
        List<BusMerchantCashoutVo> list = busMerchantCashoutService.queryList(bo);
        ExcelUtil.exportExcel(list, "商户提现记录", BusMerchantCashoutVo.class, response);
    }

    /**
     * 获取商户提现记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("payment:merchantCashout:query")
    @GetMapping("/{id}")
    public R<BusMerchantCashoutVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(busMerchantCashoutService.queryById(id));
    }

    /**
     * 获取商户提现账户信息
     *
     * @param id 资金账户id
     */
    @GetMapping("/account/{id}")
    public R<BusMerchantAccountVo> getCashoutAccount(@NotNull(message = "资金账户id不能为空")
                                                @PathVariable Long id) {
        return R.ok(merchantAccountService.getCashoutAccount(id));
    }

    /**
     * 新增商户提现记录
     */
    //@SaCheckPermission("payment:merchantCashout:add")
    @Log(title = "商户提现记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BusMerchantCashoutBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setAddBy(loginUser.getNickname());
        return toAjax(busMerchantCashoutService.insertByBo(bo));
    }

    /**
     * 修改商户提现记录
     */
    @SaCheckPermission("payment:merchantCashout:edit")
    @Log(title = "商户提现记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BusMerchantCashoutBo bo) {
        return toAjax(busMerchantCashoutService.updateByBo(bo));
    }

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