package com.jeequan.jeepay.agent.ctrl.transfer;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jeequan.jeepay.agent.ctrl.CommonCtrl;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.IsvUserConn;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.AgentInfo;
import com.jeequan.jeepay.db.entity.RateConfig;
import com.jeequan.jeepay.db.entity.TransferInterfaceDefineEntity;
import com.jeequan.jeepay.service.impl.IsvInfoService;
import com.jeequan.jeepay.service.impl.TransferInterfaceDefineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * TODO
 *
 * @author deng
 * @since 2024/4/25
 */
@RestController
@RequestMapping("api/transferConfig")
public class TransferConfigController extends CommonCtrl {

    @Autowired
    private TransferInterfaceDefineService transferInterfaceDefineService;

    @Autowired
    private IsvInfoService isvInfoService;

    /**
     * 查询可用的支付接口
     * <p>
     * 使用功能项目：  参数及费率的填写
     * 服务商查询全部支付接口
     * 服务商查询服务商开启的支付接口
     **/
    @GetMapping("/ifCodes")
    public ApiRes ifCodes() {

        // 搜索条件
        String ifName = getValString("ifName");

        String infoId = getValStringRequired("infoId"); // infoId
        String configMode = getValStringRequired("configMode"); // 设置类型

        // 支付接口列表
        LambdaQueryWrapper<TransferInterfaceDefineEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(TransferInterfaceDefineEntity::getState, CS.YES); // 查询可用的支付接口列表

        lambdaQueryWrapper.like(ObjUtil.isNotEmpty(ifName), TransferInterfaceDefineEntity::getTransIfName, ifName);

        AgentInfo agentInfo = agentInfoService.getById(getCurrentAgentNo());

        Map<String, IsvUserConn> ifCodeMap = new HashMap<>();

        // 服务商 配置 商户费率 | 下级服务商费率 | 自己配置信息 【显示  当前服务商所有接口】
        if (RateConfig.CONFIG_MODE_AGENTEMCH.equals(configMode) ||
                RateConfig.CONFIG_MODE_AGENTSUBAGENT.equals(configMode) ||
                RateConfig.CONFIG_MODE_AGENTSELF.equals(configMode)
        ) {

            ifCodeMap = isvInfoService.getTransIsvUserConn(CS.SYS_ROLE_TYPE.AGENT, agentInfo.getAgentNo(), CS.YES);
            List<String> ifCodeList = new ArrayList<>(ifCodeMap.keySet());

            if (ifCodeList.isEmpty()) { // 没有可用的接口
                return ApiRes.ok(ifCodeList);
            }
            lambdaQueryWrapper.in(TransferInterfaceDefineEntity::getTransIfCode, ifCodeList);

        } else if (RateConfig.CONFIG_MODE_AGENTAPPLYMENT.equals(configMode)) { // 服务商进件
            // 查询 当前服务商 开通的
            ifCodeMap = isvInfoService.getTransIsvUserConn(CS.SYS_ROLE_TYPE.AGENT, agentInfo.getAgentNo(), null);

            if (ifCodeMap.isEmpty()) { // 没有可用的接口
                return ApiRes.ok(Collections.emptyList());
            }

            lambdaQueryWrapper.in(TransferInterfaceDefineEntity::getTransIfCode, ifCodeMap.keySet());
        }


        final List<TransferInterfaceDefineEntity> list = new ArrayList<>();
        lambdaQueryWrapper.orderByAsc(TransferInterfaceDefineEntity::getId);
        final Map<String, IsvUserConn> finalMap = ifCodeMap;
        transferInterfaceDefineService.list(lambdaQueryWrapper).forEach(item -> {
            // 如果服务商上级配置的开启进件为关闭，那么最终展示的列表中显示的是否开启进件也为关闭
            IsvUserConn ifCodeInfo = finalMap.get(item.getTransIfCode());
            boolean isEnabled = ifCodeInfo.getStatus() == CS.YES && ifCodeInfo.getConfigStatus() == CS.YES;
            item.addExt("configState", ifCodeInfo.getStatus());

            item.setState(isEnabled ? CS.YES : CS.NO);
            list.add(item);
        });

        return ApiRes.ok(list);
    }
}
