package com.xhpc.payment.controller;

import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.domain.XhpcSettingConfig;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.payment.service.IXhpcSettingConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author yuyang
 * @date 2022/2/14 15:53
 */
@EnableScheduling
@RestController
@RequestMapping("/settingConfig")
@Api(value = "支付配置接口", tags = "支付配置接口")
public class XhpcSettingConfigController {

    @Autowired
    private IXhpcSettingConfigService xhpcSettingConfigService;

    /**
     * 支付配置详情
     * @param
     * @return
     */
    @GetMapping("/info")
    @ApiOperation(value = "充值订单详情")
    public R info(@RequestParam Integer status) {
        if(status ==null){
            return R.fail("类型为空");
        }
        return R.ok(xhpcSettingConfigService.getXhpcSettingConfigStatus(status));
    }

    /**
     * 支付配置修改
     * @param
     * @return
     */
    @PostMapping("/updateXhpcSettingConfig")
    @Log(title = "支付配置修改", businessType = BusinessType.UPDATE)
    public R updateXhpcSettingConfig(@RequestBody XhpcSettingConfig xhpcSettingConfig) {

        if(xhpcSettingConfig.getStatus() ==null){
            return R.fail("类型为空");
        }

        if(xhpcSettingConfig.getStatus()== UserTypeUtil.RECHARGE_WX){
            if(xhpcSettingConfig.getWxAppId()==null || "".equals(xhpcSettingConfig.getWxAppId())){
                return R.fail("微信小程序AppId为空");
            }
            if(xhpcSettingConfig.getWxAppSecret()==null || "".equals(xhpcSettingConfig.getWxAppSecret())){
                return R.fail("微信小程序密钥为空");
            }
            if(xhpcSettingConfig.getWxMchId()==null || "".equals(xhpcSettingConfig.getWxMchId())){
                return R.fail("微信商户id为空");
            }
            if(xhpcSettingConfig.getWxMchKey()==null || "".equals(xhpcSettingConfig.getWxMchKey())){
                return R.fail("微信商户平台Key为空");
            }
            if(xhpcSettingConfig.getWxPaymentUrl()==null || "".equals(xhpcSettingConfig.getWxPaymentUrl())){
                return R.fail("微信小程序支付地址为空");
            }
            if(xhpcSettingConfig.getWxCallbackUrl()==null || "".equals(xhpcSettingConfig.getWxCallbackUrl())){
                return R.fail("微信回调地址为空");
            }
            if(xhpcSettingConfig.getWxTransfersUrl()==null || "".equals(xhpcSettingConfig.getWxTransfersUrl())){
                return R.fail("微信用户个人付款地址为空");
            }
//            if(xhpcSettingConfig.getWxApiclientCertPem()==null || "".equals(xhpcSettingConfig.getWxApiclientCertPem())){
//                return R.fail("微信证书p12为空");
//            }
//            if(xhpcSettingConfig.getWxApiclientKeyPem()==null || "".equals(xhpcSettingConfig.getWxApiclientKeyPem())){
//                return R.fail("微信证书key.pem为空");
//            }else{
//                String[] split = xhpcSettingConfig.getWxApiclientKeyPem().split(".");
//                if(split.length>1){
//                    if(!"pem".equals(split[1])){
//                        return R.fail("请上传key.pem类型证书");
//                    }
//                }else{
//                    return R.fail("请上传key.pem类型证书");
//                }
//            }
//            if(xhpcSettingConfig.getWxAppCertPublicKey()==null || "".equals(xhpcSettingConfig.getWxAppCertPublicKey())){
//                return R.fail("微信证书cret.pem为空");
//            }else{
//                String[] split = xhpcSettingConfig.getWxApiclientKeyPem().split("\\.");
//                if(split.length>1){
//                    if(!"pem".equals(split[1])){
//                        return R.fail("请上传cret.pem类型证书");
//                    }
//                }else{
//                    return R.fail("请上传cret.pem类型证书");
//                }
//            }
        }else{
            if(xhpcSettingConfig.getZfbAppId()==null || "".equals(xhpcSettingConfig.getZfbAppId())){
                return R.fail("支付宝小程序AppId为空");
            }
            if(xhpcSettingConfig.getZfbPrivateKey()==null || "".equals(xhpcSettingConfig.getZfbPrivateKey())){
                return R.fail("支付宝应用私钥为空");
            }
            if(xhpcSettingConfig.getZfbCallbackUrl()==null || "".equals(xhpcSettingConfig.getZfbCallbackUrl())){
                return R.fail("支付宝回调地址为空");
            }
//            if(xhpcSettingConfig.getZfbAppCertPublicKey()==null || "".equals(xhpcSettingConfig.getZfbAppCertPublicKey())){
//                return R.fail("支付宝应用公钥证书路径为空");
//            }else{
//                String[] split = xhpcSettingConfig.getZfbAppCertPublicKey().split("\\.");
//                if(split.length>1){
//                    if(!"crt".equals(split[1])){
//                        return R.fail("请上传支付宝公钥.crt类型证书");
//                    }
//                }else{
//                    return R.fail("请上传支付宝应用公钥.crt类型证书");
//                }
//            }
//            if(xhpcSettingConfig.getZfbAlipayCertPublicKeyRsa()==null || "".equals(xhpcSettingConfig.getZfbAlipayCertPublicKeyRsa())){
//                return R.fail("支付宝公钥证书路径为空");
//            }else{
//                String[] split = xhpcSettingConfig.getZfbAlipayCertPublicKeyRsa().split("\\.");
//                if(split.length>1){
//                    if(!"crt".equals(split[1])){
//                        return R.fail("请上传支付宝公钥.crt类型证书");
//                    }
//                }else{
//                    return R.fail("请上传支付宝公钥.crt类型证书");
//                }
//            }
//            if(xhpcSettingConfig.getZfbAlipayRootCert()==null || "".equals(xhpcSettingConfig.getZfbAlipayRootCert())){
//                return R.fail("支付宝根证书路径为空");
//            }else{
//                String[] split = xhpcSettingConfig.getZfbAlipayRootCert().split("\\.");
//                if(split.length>1){
//                    if(!"crt".equals(split[1])){
//                        return R.fail("请上传支付宝根.crt类型证书");
//                    }
//                }else{
//                    return R.fail("请上传支付宝根.crt类型证书");
//                }
//            }
        }
        //查询
        Map<String, Object> xhpcSettingConfigStatus = xhpcSettingConfigService.getXhpcSettingConfigStatus(xhpcSettingConfig.getStatus());
        if(xhpcSettingConfigStatus !=null){
            xhpcSettingConfig.setSettingConfigId(Long.valueOf(xhpcSettingConfigStatus.get("settingConfigId").toString()));
        }
        if(xhpcSettingConfig.getSettingConfigId() !=null){
            return xhpcSettingConfigService.updateXhpcSettingConfig(xhpcSettingConfig);
        }else{
            return xhpcSettingConfigService.insertXhpcSettingConfig(xhpcSettingConfig);
        }
    }
}
