package org.dromara.payment.channel.controller;

import java.util.List;
import java.util.Map;
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.dromara.common.core.enums.BusRole;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.payment.bill.domain.bo.MerBillBo;
import org.dromara.payment.channel.domain.BusChannel;
import org.dromara.payment.channel.domain.vo.BusChannelVo;
import org.dromara.payment.channel.service.IBusChannelService;
import org.dromara.payment.sys.domain.vo.SysSydAccountVo;
import org.dromara.payment.sys.domain.vo.SysSydVo;
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.channel.domain.vo.BusChannelAccountVo;
import org.dromara.payment.channel.domain.bo.BusChannelAccountBo;
import org.dromara.payment.channel.service.IBusChannelAccountService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 代理商账户
 * 前端访问路由地址为:/payment/channelAccount
 *
 * @author LionLi
 * @date 2024-04-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/channelAccount")
public class BusChannelAccountController extends BaseController {

    private final IBusChannelAccountService busChannelAccountService;

    private final IBusChannelService channelService;

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

    /**
     * 查询代理商账户列表
     */
//    @SaCheckPermission("payment:channelAccount:list")
    @GetMapping("/list")
    public TableDataInfo<BusChannelAccountVo> list(BusChannelAccountBo bo, PageQuery pageQuery) {
        bindDataPermission(bo);
        TableDataInfo<BusChannelAccountVo> rtn =  busChannelAccountService.queryPageList(bo, pageQuery);
//        if(rtn.getRows() != null && rtn.getRows().size() > 0){
//            BusChannelVo vo  = channelService.queryById(loginUser.getBusId());
//            rtn.setRows(rtn.getRows().stream().peek(d -> d.setAccName(vo.getKhm())).collect(Collectors.toList()));
//        }
        return rtn;
    }

    /**
     * 导出代理商账户列表
     */
    @SaCheckPermission("payment:channelAccount:export")
    @Log(title = "代理商账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusChannelAccountBo bo, HttpServletResponse response) {
        bindDataPermission(bo);
        List<BusChannelAccountVo> list = busChannelAccountService.queryList(bo);
        ExcelUtil.exportExcel(list, "代理商账户", BusChannelAccountVo.class, response);
    }

    /**
     * 获取代理商账户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("payment:channelAccount:query")
    @GetMapping("/{id}")
    public R<BusChannelAccountVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {

        BusChannelAccountVo vo = busChannelAccountService.queryById(id);
        BusChannelVo bc = this.channelService.queryById(LoginHelper.getBusId());
        vo.setBankCard(bc.getBankCard());
        return R.ok(vo);
    }

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

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

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