package org.dromara.settlement.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseController;
import org.dromara.settlement.service.IPublicService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户信息
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/public")
public class ZhiFuBaoController extends BaseController {

    private final IPublicService publicService;

    /**
     * 支付宝充值通知
     *
     * @param msg
     * @return
     * @author 张涛
     * @date 2022年8月25日
     */
    public String zfbNotify(HttpServletRequest request) {
        return publicService.zfbNotify(getParamMap(request));
    }

    /**
     * 支付宝签约通知
     *
     * @param request
     * @return
     * @author 张涛
     * @date 2022年8月29日
     */
    public String signNotify(HttpServletRequest request) {
        return publicService.signNotify(getParamMap(request));
    }

    private Map<String, String> getParamMap(HttpServletRequest request) {
        Map<String, String> params = new HashMap<String, String>();
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = paramNames.nextElement();
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length > 0) {
                params.put(paramName, paramValues[0]);
            } else {
                params.put(paramName, "");
            }
        }
        return params;
    }
}
