package com.winsun.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 小白卡调取接口工具类
 * @Author: chancy
 * @Date: 2020/3/5 16:26
 */
@Slf4j
public class XbkUtil {


    public static  JSONObject xbkCreate(Map<String, Object> map, String type){
        Map<String, Object> mapParm = new HashMap<>();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String tablename = dateFormat.format(now);
        mapParm.put("sysNum", 10026);
        mapParm.put("intfCode", type);
        mapParm.put("eventTime", tablename);
        String s = MD5Utils.md5(tablename + "gzxy951753@.123");
        mapParm.put("sign", s.toUpperCase());
        mapParm.put("param", map);
        JSONObject json = new JSONObject(mapParm);
        JSONObject jsonObject = null;
        try {
            jsonObject = HttpHelper.doPost("http://enter.gd189.cn/o2oweb/outMain/service.do", json.toString());
        }catch (Exception e){
            log.error(type+"调取接口异常",e.getMessage());
        }
        return jsonObject;
    }


    /**
     * 小白下单接口 JT0002
     *
     *
     * @param customerName  用户姓名
     * @param contactNumber 联系电话
     * @param idCard        用户身份证
     * @param businessNumber 办理号码
     * @return
     */
    public static JSONObject xbkOrderJT0002(String businessNumber, String idCard,
                                          String customerName,String contactNumber,String xbId ,String netId) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("prodId", xbId);
        map.put("coUserId", netId);
        map.put("phoneNumber", businessNumber);
        map.put("isDelivery", "1");
        map.put("custName", customerName);
        map.put("idCardNo", idCard);
        map.put("contactNumber", contactNumber);
        map.put("sendSms", "0");
        map.put("openChannel", "WECHAT");
        String type = "JT0002";
        JSONObject jsonObject = XbkUtil.xbkCreate(map, type);
        return jsonObject;
    }

    /**
     * 身份证校验 JT0005
     *
     * @param customerName  用户姓名
     * @param idCard        用户身份证
     * @return
     */
    public static JSONObject idCardCheckJT0005(String idCard,String customerName) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("custName", customerName);
        map.put("idCard", idCard);
        String type = "JT0005";
        JSONObject jsonObject = XbkUtil.xbkCreate(map, type);
        return jsonObject;
    }

    /**
     * 订单详情 JT0006
     *
     * @param orderCode 订单编号
     * @return
     */
    public static JSONObject orderInfoJT0006(String orderCode) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("orderCode", orderCode);
        String type = "JT0006";
        JSONObject jsonObject = XbkUtil.xbkCreate(map, type);
        return jsonObject;
    }

    /**
     * 手机号码获取 JT0001
     *
     * @return
     */
    public static JSONObject getNumberJT0001(String contNumber,String xbId,String netId) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("prodId", xbId);
        map.put("coUserId", netId);
        map.put("pageIndex", "0");
        map.put("pageSize", "6");
        if (StringUtils.isNotBlank(contNumber)) {
            map.put("contNumber", contNumber);
        }
        String type = "JT0001";
        JSONObject jsonObject = XbkUtil.xbkCreate(map, type);
        return jsonObject;
    }
}
