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.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.payment.merchant.domain.vo.BusMerchantRechargeVo;
import org.dromara.payment.operator.domain.bo.BusOperatorAccountBo;
import org.dromara.settlement.api.RemoteAccountService;
import org.dromara.settlement.api.domain.Result;
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.BusMerchantAccountVo;
import org.dromara.payment.merchant.domain.bo.BusMerchantAccountBo;
import org.dromara.payment.merchant.service.IBusMerchantAccountService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 商户资金账户
 * 前端访问路由地址为:/payment/merchantAccount
 *
 * @author LionLi
 * @date 2024-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/merchantAccount")
public class BusMerchantAccountController extends BaseController {

    private final IBusMerchantAccountService busMerchantAccountService;

    @DubboReference
    private RemoteAccountService accountService;

    /**
     * 查询商户资金账户列表
     */
    //@SaCheckPermission("payment:merchantAccount:list")
    @GetMapping("/list")
    public TableDataInfo<BusMerchantAccountVo> list(BusMerchantAccountBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setMerId(loginUser.getBusId());
        TableDataInfo<BusMerchantAccountVo> rtn = busMerchantAccountService.queryPageList(bo, pageQuery);
        if(rtn.getRows() != null && rtn.getRows().size() > 0){
            for (BusMerchantAccountVo vo:rtn.getRows()
                 ) {
                Result<String> rtnName = accountService.getAccountName(vo.getSydId(),vo.getBankType(),vo.getBankVirtualCard());
                vo.setAccName(rtnName.getRtnData());
            }
        }
        return rtn;
    }

    @GetMapping("/listFp")
    @Log(title = "结算账户分配查询", businessType = BusinessType.EXPORT)
    public R<List>   getQueryList(BusMerchantAccountBo bo, PageQuery pageQuery){
        return R.ok(this.busMerchantAccountService.getMerAccountList(bo,pageQuery));
    }

    @PostMapping("/createXn")
    @SaCheckPermission("payment:merchantAccount:createXn")
    @Log(title = "创建运营商虚拟户", businessType = BusinessType.INSERT)
    public R<Boolean> createXnAccount(Long sydId,Long merId,String merName,String bankType){
        return R.ok(this.busMerchantAccountService.createXnAccount(sydId,merId,merName,bankType));
    }




    /**
     * 查询商户资金账户的充值信息
     * @param id 资金账户id
     * @return
     */
    @GetMapping("/getRechargeAccount")
    public R<BusMerchantRechargeVo> getRechargeAccount(int id) {
        return R.ok(busMerchantAccountService.getRechargeAccount(id));
    }

    /**
     * 查询商户资金账户列表
     * @param sydId 服务商id
     * @return
     */
    @GetMapping("/getAccounts")
    public R<List<BusMerchantAccountVo>> getAccounts(Long sydId) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        return R.ok(busMerchantAccountService.getAccByMerIdAndSydId(loginUser.getBusId(),sydId));
    }

    /**
     * 查询商户默认资金账户
     * @return
     */
    @GetMapping("/getDefaultAccount")
    public R<BusMerchantAccountVo> getDefaultAccount() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        return R.ok(busMerchantAccountService.getDefaultAccByMerIdAndSydId(loginUser.getBusId(),loginUser.getSydId()));
    }

    /**
     * 导出商户资金账户列表
     */
    @SaCheckPermission("payment:merchantAccount:export")
    @Log(title = "商户资金账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusMerchantAccountBo bo, HttpServletResponse response) {
        List<BusMerchantAccountVo> list = busMerchantAccountService.queryList(bo);
        ExcelUtil.exportExcel(list, "商户资金账户", BusMerchantAccountVo.class, response);
    }

    /**
     * 获取商户资金账户详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("payment:merchantAccount:query")
    @GetMapping("/{id}")
    public R<BusMerchantAccountVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(busMerchantAccountService.queryById(id));
    }

    /**
     * 新增商户资金账户
     */
    @SaCheckPermission("payment:merchantAccount:add")
    @Log(title = "商户资金账户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BusMerchantAccountBo bo) {
        return toAjax(busMerchantAccountService.insertByBo(bo));
    }

    /**
     * 修改商户资金账户
     */
    @SaCheckPermission("payment:merchantAccount:edit")
    @Log(title = "商户资金账户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BusMerchantAccountBo bo) {
        return toAjax(busMerchantAccountService.updateByBo(bo));
    }

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