package org.dromara.payment.operator.controller;

import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.enums.BusRole;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.payment.channel.domain.bo.BusChannelCashoutBo;
import org.dromara.payment.merchant.domain.vo.BusMerchantAccountVo;
import org.dromara.payment.operator.domain.vo.BusOperatorAccountVo;
import org.dromara.payment.operator.service.IBusOperatorAccountService;
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.operator.domain.vo.BusOperatorCashoutVo;
import org.dromara.payment.operator.domain.bo.BusOperatorCashoutBo;
import org.dromara.payment.operator.service.IBusOperatorCashoutService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

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

    private final IBusOperatorCashoutService busOperatorCashoutService;

    private final IBusOperatorAccountService operatorAccountService;

    private void bindDataPermission(BusOperatorCashoutBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(loginUser.getCurRoleId() == BusRole.OPERATOR.getId()){
            bo.setOpId(loginUser.getBusId());
        }
    }

    /**
     * 查询运营商提现记录列表
     */
    @SaCheckPermission("payment:operatorCashout:list")
    @GetMapping("/list")
    public TableDataInfo<BusOperatorCashoutVo> list(BusOperatorCashoutBo bo, PageQuery pageQuery) {
        bindDataPermission(bo);
        return busOperatorCashoutService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出运营商提现记录列表
     */
    @SaCheckPermission("payment:operatorCashout:export")
    @Log(title = "运营商提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusOperatorCashoutBo bo, HttpServletResponse response) {
        bindDataPermission(bo);
        List<BusOperatorCashoutVo> list = busOperatorCashoutService.queryList(bo);
        ExcelUtil.exportExcel(list, "运营商提现记录", BusOperatorCashoutVo.class, response);
    }

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

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

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

    /**
     * 审批运营商提现记录
     */
    @Log(title = "审批运营商提现记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/verifyCashout")
    public R<Void> verifyCashout(@Positive(message = "提现记录id无效") long id,@Positive(message = "审核是否通过无效") long verifyStatus,String verifyReason) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        BusOperatorCashoutBo bo = new BusOperatorCashoutBo();
        bo.setId(id);
        bo.setVerifyStatus(verifyStatus);
        bo.setVerifyReason(verifyReason);
        bo.setVerifyBy(loginUser.getNickname());
        return toAjax(busOperatorCashoutService.verifyCashout(bo));
    }

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

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