package com.pine.admin.pay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pine.admin.modules.business.entity.UserInfo;
import com.pine.common.dto.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * com.pine.admin.modules.business.controller
 *
 * @author Pine
 * @email 771190883@qq.com
 * @date 2019/11/29
 */

@Slf4j
@ApiIgnore
public class PayResult {
    public static void main(String[] args) {
        Object all = PayResult.all("2", "47176542923691344527369369912", null);
    }

    public static Object all(String payChannel, String billNo, String payTime) {

        FinancialPaySetting paySetting = new FinancialPaySetting();
        JSONObject json = new JSONObject();
        String queryUrl = null;

        json.put("msgSrc","WWW.HEJIASHH.COM");
        json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        json.put("mid", "898340255110746");
        json.put("tid", "02160497");

        if ("1".equals(payChannel)) {
            queryUrl = FinancialPaySetting.APIurl.H5_QUERY_API_URL;
            json.put("msgType", "query");
            json.put("merOrderId", billNo);
        }

        if ("2".equals(payChannel)) {
            queryUrl = FinancialPaySetting.APIurl.PUBLIC_QUERY_API_URL;
            json.put("msgType", "query");
            json.put("merOrderId", billNo);

        }

        if ("3".equals(payChannel)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            queryUrl = FinancialPaySetting.APIurl.PC_API_URL;
            json.put("billNo", billNo);
            json.put("billDate", LocalDateTime.parse(payTime, formatter));
            json.put("msgType", "bills.query");
        }

        Map<String, String> paramsMap = PayUtils.jsonToMap(json);
        paramsMap.put("sign", PayUtils.makeSign("ZGS2rj4QmNTE5N6SEjyTYacJc3rDhzk2zxjCzpJT4fD2Tt3N", paramsMap));

        JSONObject jsonObject = PostUtil.sendRequest(JSON.toJSONString(paramsMap), queryUrl);
        log.info("searchPayOrderInfo result is : {} ,queryUrl：{}", jsonObject.toJSONString(), queryUrl);
        if (!StringUtils.isEmpty(jsonObject)) {
            if ("SUCCESS".equals(jsonObject.getString("errCode"))) {
                log.info("searchPayOrderInfo success billNo : {} ,queryUrl", billNo);

            }
        }
        return jsonObject.toJSONString();
    }

}
