package com.jeequan.jeepay.thirdparty.channel.ryxpay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.jeequan.jeepay.core.entity.MchApplyment;
import com.jeequan.jeepay.core.interfaces.paychannel.IIsvmchAlipayConfigService;
import com.jeequan.jeepay.core.model.applyment.ApplymentSignInfo;
import com.jeequan.jeepay.core.model.params.ruipay.RyxpayIsvParams;
import com.jeequan.jeepay.core.utils.JeepayKit;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.db.entity.MchSubInfoEntity;
import com.jeequan.jeepay.service.impl.MchSubInfoService;
import com.jeequan.jeepay.thirdparty.channel.IsvFactory;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class RyxpayIsvmchAlipayConfigService implements IIsvmchAlipayConfigService {

    @Autowired
    private MchSubInfoService mchSubInfoService;

    @Override
    public ApplymentSignInfo alipayOpenSignInfo(MchApplyment mchApplyment) {

        ApplymentSignInfo result = new ApplymentSignInfo();

        JSONObject suJson = JSON.parseObject(mchApplyment.getSuccResParameter());
        // 获取到 服务商配置信息
        ConfigContextQueryService configContextQueryService = SpringBeansUtil.getBean(ConfigContextQueryService.class);
        RyxpayIsvParams isvParams = (RyxpayIsvParams) configContextQueryService.queryIsvParams(mchApplyment);
        // 支付宝渠道拓展地址
        result.setSignUrl(isvParams.getAliUrl());
        // 图片类型
        result.setImgType(ApplymentSignInfo.IMG_TYPE_QRCONTENT);

        if (StringUtils.isEmpty(mchApplyment.getChannelMchNo())) {
            result.setErrInfo("随行付商户号为空");
            return result;
        }
        // 银盛商户号
        String mercId = suJson.getString("mercId");

        LambdaQueryChainWrapper<MchSubInfoEntity> zfbSubInfoQuery = mchSubInfoService.lambdaQuery()
                .eq(MchSubInfoEntity::getMchApplyId, mchApplyment.getApplyId())
                .eq(MchSubInfoEntity::getChannelMchNo, mchApplyment.getChannelMchNo())
                .eq(MchSubInfoEntity::getMainUse, 1)
                .eq(MchSubInfoEntity::getSubMchType, "ZFB");
        MchSubInfoEntity zfb = zfbSubInfoQuery.one();

        if (zfb == null) {
            IsvFactory.getIsvMchApplymentService(JeepayKit.getIfCodeOrigin(mchApplyment.getIfCode()))
                    .subMchColl(mchApplyment);

            zfb = zfbSubInfoQuery.one();
        }

        if (zfb == null) {
            result.setState("未报备");
            result.setErrInfo("[未获取到子商户信息]");
            return result;
        } else if ("0".equals(zfb.getStatus())) {
            result.setChannelSubMchId(zfb.getSubMchId());
            result.setState("当前通道暂不支持查询");
        } else {
            result.setState("未报备");
            result.setErrInfo("[" + zfb.getRemark() + "]");
            return result;
        }



        return result;
    }

}
