package com.younike.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.younike.common.network.HttpClientUtils;
import com.younike.system.domain.AddressParam;
import com.younike.system.domain.MemberAddress;
import com.younike.system.service.AddressService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 地址连接外部接口
 */
@Service
@Log4j2
public class AddressServiceImpl implements AddressService {

    /* 查询会员地址 路径后缀 */
    private static final String trnementSitePostfix = "/yaj-api/openApi/getHouseInfo";

    private static final String sign="&sign=s%5E22y19SO10";


    /**
     * 根据手机号调用外部接口查询地址
     * @param phone
     * @return
     */
    @Override
    public List<MemberAddress> address(String phone) {
        AddressParam addressParam = new AddressParam();
        addressParam.setPhone(phone);
        addressParam.setSign("s%5E22y19SO10");
        String url = "https://wuye.uxianxin.com" + trnementSitePostfix+"?phone="+phone+sign;

        JSONObject jsonObject = this.requestAward(url);
        List<MemberAddress> memberAddresses = new ArrayList<>();
        // 解析返回结果
        String jsonCode = jsonObject.getString("code");
        String jsonMsg = jsonObject.getString("msg");
        if ("200".equals(jsonCode)) {
            String jsonResult = jsonObject.getString("result");

            List<AddressParam> addressParams = JSONArray.parseArray(jsonResult, AddressParam.class);
            addressParams.forEach(aParam -> {
                MemberAddress address = new MemberAddress();
                //详细地址
                address.setAddress(aParam.getProvince()+aParam.getCity()+aParam.getAddress()+aParam.getHouseInfo());
                //门牌号
                address.setDetailed(aParam.getDoorplate());
                //创建时间
                address.setCreateTime(LocalDateTime.now());
                address.setPhone(phone);
                //位置坐标
                address.setLatitude(aParam.getLat());
                address.setLongitude(aParam.getLng());
                memberAddresses.add(address);

            });

        } else {
            log.error("调用 award接口 获取 用户住房信息，失败信息：" + jsonMsg);

        }
        return memberAddresses;
    }

    /**
     * 根据 路径、参数 请求 award接口
     * @param url 路径
     * @param addressParam 请求参数
     * @return
     */
    private JSONObject requestAward(String url, AddressParam addressParam) {
        // 获取 签名
        // 获取 签名
        Map<String, String> params = new HashMap<>();
        params.put("phone", addressParam.getPhone());
        params.put("sign",addressParam.getSign());
        String result = HttpClientUtils.executeGet(url,params);

        // 解析返回结果
        JSONObject jsonObject = JSONObject.parseObject(result);

        return jsonObject;

    }
    /**
     * 根据 路径、参数 请求 award接口
     * @param url 路径
     * @return
     */
    private JSONObject requestAward(String url) {

        String result = HttpClientUtils.executeGet(url);

        // 解析返回结果
        JSONObject jsonObject = JSONObject.parseObject(result);

        return jsonObject;

    }

}
