package org.dromara.payment.operator.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.BusMerchantAccountVo;
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.operator.domain.vo.BusOperatorAccountVo;
import org.dromara.payment.operator.domain.bo.BusOperatorAccountBo;
import org.dromara.payment.operator.service.IBusOperatorAccountService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

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

    private final IBusOperatorAccountService busOperatorAccountService;

    @DubboReference
    private RemoteAccountService accountService;

    /**
     * 查询运营商资金账户列表
     */
    @SaCheckPermission("payment:operatorAccount:list")
    @GetMapping("/list")
    public TableDataInfo<BusOperatorAccountVo> list(BusOperatorAccountBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setOpId(loginUser.getBusId());
        TableDataInfo<BusOperatorAccountVo> rtn = busOperatorAccountService.queryPageList(bo, pageQuery);
        return rtn;
    }



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


    @PostMapping("/createXn")
    @Log(title = "创建运营商虚拟户", businessType = BusinessType.INSERT)
    public R<Boolean> createXnAccount(Long sydId,Long opId,String opName,String bankType){
        return R.ok(this.busOperatorAccountService.createXnAccount(sydId,opId,opName,bankType));
    }


    /**
     * 导出运营商资金账户列表
     */
    @SaCheckPermission("payment:operatorAccount:export")
    @Log(title = "运营商资金账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusOperatorAccountBo bo, HttpServletResponse response) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setOpId(loginUser.getBusId());
        List<BusOperatorAccountVo> list = busOperatorAccountService.queryList(bo);
        ExcelUtil.exportExcel(list, "运营商资金账户", BusOperatorAccountVo.class, response);
    }

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

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

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

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