package org.dromara.payment.channel.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.enums.BusRole;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.payment.bill.domain.bo.MerBillBo;
import org.dromara.payment.channel.domain.bo.BusChannelCashoutBo;
import org.dromara.payment.channel.domain.vo.BusChannelAccountVo;
import org.dromara.payment.channel.domain.vo.BusChannelCashoutVo;
import org.dromara.payment.channel.service.IBusChannelAccountService;
import org.dromara.payment.channel.service.IBusChannelCashoutService;
import org.dromara.payment.channel.service.IBusChannelService;
import org.dromara.system.api.model.LoginUser;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

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

    private final IBusChannelCashoutService busChannelCashoutService;

    private final IBusChannelAccountService channelAccountService;

    private void bindDataPermission(BusChannelCashoutBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(loginUser.getCurRoleId() == BusRole.OPERATOR.getId()){
            bo.getParams().putAll(Map.of("channel_no",loginUser.getBusNo()));
        }else if(loginUser.getCurRoleId() == BusRole.AGENT.getId()){
            bo.setChannelId(loginUser.getBusId());
        }
    }

    /**
     * 查询代理商提现记录列表
     */
    @SaCheckPermission("payment:channelCashout:list")
    @GetMapping("/list")
    public TableDataInfo<BusChannelCashoutVo> list(BusChannelCashoutBo bo, PageQuery pageQuery) {
        bindDataPermission(bo);
        return busChannelCashoutService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出代理商提现记录列表
     */
    @SaCheckPermission("payment:channelCashout:export")
    @Log(title = "代理商提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusChannelCashoutBo bo, HttpServletResponse response) {
        bindDataPermission(bo);
        List<BusChannelCashoutVo> list = busChannelCashoutService.queryList(bo);
        ExcelUtil.exportExcel(list, "代理商提现记录", BusChannelCashoutVo.class, response);
    }

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

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

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

    /**
     * 审批代理商提现记录
     */
    @Log(title = "审批代理商提现记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/verifyCashout")
    public R<Void> verifyCashout(@Positive(message = "提现记录id无效") long id,@Positive(message = "审核是否通过无效") long verifyStatus,@NotBlank(message = "支付凭证不能为空") String invoicePic,String verifyReason) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        BusChannelCashoutBo bo = new BusChannelCashoutBo();
        bo.setId(id);
        bo.setVerifyStatus(verifyStatus);
        bo.setVerifyReason(verifyReason);
        bo.setInvoicePic(invoicePic);
        bo.setVerifyBy(loginUser.getNickname());
        return toAjax(busChannelCashoutService.verifyCashout(bo));
    }

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

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