package org.dromara.payment.sys.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.sys.domain.bo.SysSydCashoutBo;
import org.dromara.payment.sys.domain.vo.SysSydAccountVo;
import org.dromara.payment.sys.domain.vo.SysSydCashoutVo;
import org.dromara.payment.sys.service.ISysSydAccountService;
import org.dromara.payment.sys.service.ISysSydCashoutService;
import org.dromara.payment.sys.service.ISysSydService;
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.common.mybatis.core.page.TableDataInfo;

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

    private final ISysSydCashoutService sysSydCashoutService;

    private final ISysSydAccountService sysSydAccountService;

    private final ISysSydService sysSydService;

    /**
     * 查询服务商提现记录列表
     */
    @SaCheckPermission("payment:sydCashout:list")
    @GetMapping("/list")
    public TableDataInfo<SysSydCashoutVo> list(SysSydCashoutBo bo, PageQuery pageQuery) {
        return sysSydCashoutService.queryPageList(bo, pageQuery);
    }

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

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

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

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

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

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