package test.com.izouma.admintemplate.datasource;


import com.izouma.awesomeadmin.datasource.DataService;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.DistrictInfo;
import com.izouma.awesomeadmin.model.SqlServerTest;
import com.izouma.awesomeadmin.model.SuperUser;
import com.izouma.awesomeadmin.model.Zouma;
import com.izouma.awesomeadmin.service.DistrictInfoService;
import com.izouma.awesomeadmin.service.SuperUserService;
import com.izouma.awesomeadmin.util.NetUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.math.BigDecimal;
import java.util.List;

/**
 * 配置spring和junit整合，junit启动时加载springIOC容器
 * spring-test,junit
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/applicationContext.xml"})
public class DistrictInfoServiceTest {

    @Autowired
    DistrictInfoService districtInfoService;


    @Test
    public void test() {
        DistrictInfo param = new DistrictInfo();
        param.setCityCode("320100");

        List<DistrictInfo> districtInfoList = districtInfoService.getDistrictInfoList(param);
        for (DistrictInfo district : districtInfoList) {


            String url = "http://apis.map.qq.com/ws/district/v1/getchildren?&id=" + district.getCode() + "&key=GLFBZ-ZR2W6-76XSA-MF7CQ-GDJ6Z-6FB5K";

            String result = NetUtils.httpclientGet(url);
            System.out.println(result);

            JSONObject job = JSONObject.fromObject(result);
            JSONArray jsonArray = JSONArray.fromObject(job.get("result"));

            for (int i = 0; i < jsonArray.size(); i++) {
                JSONArray jsonList = jsonArray.getJSONArray(i);

                for (int j = 0; j < jsonList.size(); j++) {
                    JSONObject jobInfo = jsonList.getJSONObject(j);
                    DistrictInfo districtInfo = new DistrictInfo();

                    districtInfo.setFullName(jobInfo.getString("fullname"));
                    if (jobInfo.has("name")) {

                        districtInfo.setShortName(jobInfo.getString("name"));
                    }
                    districtInfo.setCode(jobInfo.getString("id"));

                    JSONObject location = jobInfo.getJSONObject("location");

                    districtInfo.setLat(BigDecimal.valueOf(location.getDouble("lat")));
                    districtInfo.setLng(BigDecimal.valueOf(location.getDouble("lng")));

                    districtInfoService.createDistrictInfo(districtInfo);


                }
            }
        }


    }


}



