package org.dromara.payment.sys.controller;

import java.util.List;
import java.util.stream.Collectors;

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.sys.domain.vo.SysSydAccountVo;
import org.dromara.payment.sys.domain.vo.SysSydVo;
import org.dromara.payment.sys.service.ISysSydAccountService;
import org.dromara.payment.sys.service.ISysSydService;
import org.dromara.settlement.api.RemoteAccountService;
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.sys.domain.bo.SysSydAccountBo;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 服务商账户
 * 前端访问路由地址为:/payment/sydAccount
 *
 * @author LionLi
 * @date 2024-03-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sydAccount")
public class SysSydAccountController extends BaseController {

    private final ISysSydAccountService sysSydAccountService;

    private final ISysSydService sysSydService;

    @DubboReference
    private RemoteAccountService accountService;

    /**
     * 查询服务商账户列表
     */
    //@SaCheckPermission("payment:sydAccount:list")
    @GetMapping("/list")
    public TableDataInfo<SysSydAccountVo> list(SysSydAccountBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setSydId(loginUser.getBusId());
        TableDataInfo<SysSydAccountVo> rtn =  sysSydAccountService.queryPageList(bo, pageQuery);
        if(rtn.getRows() != null && rtn.getRows().size() > 0){
            SysSydVo vo  = sysSydService.queryById(loginUser.getBusId());
            rtn.setRows(rtn.getRows().stream().peek(d -> d.setAccName(vo.getGsmc())).collect(Collectors.toList()));
        }
        return rtn;
    }

    /**
     * 导出服务商账户列表
     */
    @SaCheckPermission("payment:sydAccount:export")
    @Log(title = "服务商账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysSydAccountBo bo, HttpServletResponse response) {
        List<SysSydAccountVo> list = sysSydAccountService.queryList(bo);
        ExcelUtil.exportExcel(list, "服务商账户", SysSydAccountVo.class, response);
    }

    /**
     * 获取服务商账户详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("payment:sydAccount:query")
    @GetMapping("/{id}")
    public R<SysSydAccountVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        SysSydVo vo  = sysSydService.queryById(loginUser.getBusId());
        SysSydAccountVo sydAccountVo = sysSydAccountService.queryById(id);
        sydAccountVo.setAccName(vo.getGsmc());
        return R.ok(sydAccountVo);
    }

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

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

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


}
