package com.yn.bftl.thirdparty.modules.unionpay.controller;

import com.alibaba.fastjson.JSONObject;
import com.yn.bftl.thirdparty.modules.log.service.LoggerService;
import com.yn.bftl.thirdparty.modules.unionpay.service.UnionpayService;
import com.yn.bftl.thirdparty.modules.unionpay.service.impl.UnionpayServiceImpl;
import com.yn.bftl.thirdparty.modules.unionpay.vo.AggregateCallBackVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author huabiao
 * @create 2022/9/6  11:27
 **/
@Validated
@Api(value = "UnionpayController", tags = "公共-银联")
@RestController
@RequestMapping("/unionpay")
public class UnionpayController {

    @Resource
    private UnionpayService unionpayService;
    @Resource
    private LoggerService loggerService;

    @ApiOperation("银联聚合支付回调")
    @PostMapping("/aggregate/call_back")
    public AggregateCallBackVO aggregateCallBack(String merOrderId, HttpServletRequest request) {
        Map<String, String[]> map = request.getParameterMap();
        Map<String, String> paramMap = new LinkedHashMap<>();
        map.forEach((String key, String[] values) -> {
            paramMap.put(key, values[0]);
        });
        loggerService.unionpay("[银联回调]银联回调信息", (JSONObject) JSONObject.toJSON(paramMap), UnionpayServiceImpl.class.getName(), "payCallBack", "unionpay");

        // TODO 不校验签名，直接根据订单号去银联查询，以查询到的支付状态为准
        String result = unionpayService.aggregateCallBack(merOrderId);
        AggregateCallBackVO aggregateCallBackVO = new AggregateCallBackVO();
        aggregateCallBackVO.setStatus(result);
        return aggregateCallBackVO;
    }
}
