package com.fs.app.controller;


import cn.hutool.json.JSONUtil;
import com.fs.app.AppPayService;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.StringUtils;
import com.fs.course.domain.FsCoursePlaySourceConfig;
import com.fs.course.service.IFsCoursePlaySourceConfigService;
import com.fs.his.config.AppConfig;
import com.fs.his.domain.FsPayConfig;
import com.fs.his.domain.MerchantAppConfig;
import com.fs.his.service.IMerchantAppConfigService;
import com.fs.system.domain.SysConfig;
import com.fs.system.mapper.SysConfigMapper;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.google.gson.Gson;
import com.hc.openapi.tool.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@Api("app支付回调接口")
@RestController
@RequestMapping("/appPay")
public class AppPayController extends AppBaseController {

    @Autowired
    private AppPayService appPayService;

    @Autowired
    private IFsCoursePlaySourceConfigService fsCoursePlaySourceConfigService;
   @Autowired
    private IMerchantAppConfigService merchantAppConfigService;
    @Autowired
    private SysConfigMapper sysConfigMapper;


    @ApiOperation("微信支付回调")
    @PostMapping("/wxNotify")
    public String wxNotify(@RequestBody String xmlData) throws WxPayException {
        WxPayService payService = getWxPayService();
        WxPayOrderNotifyResult result = payService.parseOrderNotifyResult(xmlData);
        return appPayService.wxNotify(result);
    }

    private WxPayService getWxPayService(){
        SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey("app.config");
        AppConfig config = new Gson().fromJson(sysConfig.getConfigValue(), AppConfig.class);
        FsCoursePlaySourceConfig fsCoursePlaySourceConfig = fsCoursePlaySourceConfigService.selectCoursePlaySourceConfigByAppId(config.getAppId());
        if (fsCoursePlaySourceConfig == null) {
            throw new CustomException("未找到appId对应的小程序配置: " + config.getAppId());
        }
        MerchantAppConfig merchantAppConfig = merchantAppConfigService.selectMerchantAppConfigById(fsCoursePlaySourceConfig.getMerchantConfigId());
        FsPayConfig payConfig1 = JSON.parseObject(merchantAppConfig.getDataJson(), FsPayConfig.class);

        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(payConfig1.getAppId());
        payConfig.setMchId(payConfig1.getWxMchId());
        payConfig.setMchKey(payConfig1.getWxMchKey());
        payConfig.setSubAppId(StringUtils.trimToNull(null));
        payConfig.setSubMchId(StringUtils.trimToNull(null));
        payConfig.setKeyPath(null);
        payConfig.setNotifyUrl(payConfig1.getNotifyUrlScrm());
        WxPayServiceImpl payService = new WxPayServiceImpl();
        payService.setConfig(payConfig);
        return payService;
    }
}
