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

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.agent.ctrl.CommonCtrl;
import com.jeequan.jeepay.components.mq.vender.IMQSender;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.model.DBApplicationConfig;
import com.jeequan.jeepay.db.entity.MchAppEntity;
import com.jeequan.jeepay.db.entity.PayInterfaceConfig;
import com.jeequan.jeepay.service.impl.MchAppService;
import com.jeequan.jeepay.service.impl.MchInfoService;
import com.jeequan.jeepay.service.impl.PayInterfaceConfigService;
import com.jeequan.jeepay.service.impl.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Set;

/**
 * 商户支付接口配置类
 *
 * @author zhuxiao
 * 
 * @date 2021-04-27 15:50
 */
@RestController
@RequestMapping("/api/mch/payConfigs")
public class MchPayInterfaceConfigController extends CommonCtrl {

    @Autowired
    private PayInterfaceConfigService payInterfaceConfigService;
    @Autowired
    private MchInfoService mchInfoService;
    @Autowired
    private MchAppService mchAppService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private IMQSender mqSender;


    /**
     * 查询支付宝商户授权URL
     **/
    @GetMapping("/alipayIsvsubMchAuthUrls/{mchAppId}/{isvNo}")
    public ApiRes queryAlipayIsvsubMchAuthUrl(@PathVariable("mchAppId") String mchAppId,
                                              @PathVariable("isvNo") String isvNo) {

        MchAppEntity mchAppEntity = mchAppService.getById(mchAppId);

        DBApplicationConfig dbApplicationConfig = sysConfigService.getDBApplicationConfig();
        String authUrl = dbApplicationConfig.genAlipayIsvsubMchAuthUrl(isvNo, mchAppId);
        String authQrImgUrl = dbApplicationConfig.genScanImgUrl(authUrl);

        JSONObject result = new JSONObject();
        result.put("authUrl", authUrl);
        result.put("authQrImgUrl", authQrImgUrl);
        return ApiRes.ok(result);
    }


    /**
     * 查询当前应用支持的支付接口
     */
    @PreAuthorize("hasAuthority( 'ENT_DIVISION_RECEIVER_ADD' )")
    @RequestMapping(value = "ifCodes/{appId}", method = RequestMethod.GET)
    public ApiRes getIfCodeByAppId(@PathVariable("appId") String appId) {

        if (mchAppService.lambdaQuery().eq(MchAppEntity::getMchNo, getCurrentAgentNo()).eq(MchAppEntity::getAppId, appId).count() <= 0) {
            throw new BizException("商户应用不存在");
        }

        Set<String> result = new HashSet<>();

        payInterfaceConfigService.lambdaQuery().select(PayInterfaceConfig::getIfCode)
                .eq(PayInterfaceConfig::getState, CS.PUB_USABLE)
                .eq(PayInterfaceConfig::getInfoId, appId)
                .eq(PayInterfaceConfig::getInfoType, CS.SYS_ROLE_TYPE.MCH_APP)
                .list().forEach(r -> result.add(r.getIfCode()));

        return ApiRes.ok(result);
    }


}
