/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.sysarea.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;

import org.springblade.sysarea.entity.SysArea;
import org.springblade.sysarea.service.ISysAreaService;
import org.springblade.sysarea.vo.SysAreaVO;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * 全国省市区街道 控制器
 *
 * @author BladeX
 * @since 2020-07-07
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-sysarea/sysarea")
@Api(value = "全国省市区街道", tags = "全国省市区街道接口")

public class SysAreaController extends BladeController {

	private ISysAreaService sysAreaService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入sysArea")
	@ApiIgnore
	public R<SysArea> detail(SysArea sysArea) {
		SysArea detail = sysAreaService.getOne(Condition.getQueryWrapper(sysArea));
		return R.data(detail);
	}

	/**
	 * 分页 全国省市区街道
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入sysArea")
	@ApiIgnore
	public R<IPage<SysArea>> list(SysArea sysArea, Query query) {
		IPage<SysArea> pages = sysAreaService.page(Condition.getPage(query), Condition.getQueryWrapper(sysArea));
		return R.data(pages);
	}

	/**
	 * 自定义分页 全国省市区街道
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入sysArea")
	@ApiIgnore
	public R<IPage<SysAreaVO>> page(SysAreaVO sysArea, Query query) {
		IPage<SysAreaVO> pages = sysAreaService.selectSysAreaPage(Condition.getPage(query), sysArea);
		return R.data(pages);
	}

	/**
	 * 新增 全国省市区街道
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入sysArea")
	@ApiIgnore
	public R save(@Valid @RequestBody SysArea sysArea) {
		return R.status(sysAreaService.save(sysArea));
	}

	/**
	 * 修改 全国省市区街道
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入sysArea")
	@ApiIgnore
	public R update(@Valid @RequestBody SysArea sysArea) {
		return R.status(sysAreaService.updateById(sysArea));
	}

	/**
	 * 新增或修改 全国省市区街道
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入sysArea")
	@ApiIgnore
	public R submit(@Valid @RequestBody SysArea sysArea) {
		sysArea.setSFirstLetter(sysArea.getSPinyin().substring(0, 1));
		return R.status(sysAreaService.saveOrUpdate(sysArea));
	}


	/**
	 * 删除 全国省市区街道
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	@ApiIgnore
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(sysAreaService.removeByIds(Func.toLongList(ids)));
	}

	@GetMapping("/getAllProvince")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "获取所有省", notes = "")
	public R<List<SysArea>> getAllProvince() {

		return R.data(sysAreaService.getAllProvince());
	}

	@GetMapping("/getCityOrAreaByParentCode")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "根据s_parent_code获取该省下所有市或者该市下所有区", notes = "传入code")
	public R<List<SysArea>> getCityOrAreaByParentCode(@RequestParam Long code) {

		return R.data(sysAreaService.getCityOrAreaByParentCode(code));
	}

	@GetMapping("/getDetailByCode")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "根据s_code获取该地址详细信息", notes = "传入code")
	public R<SysArea> getDetailByCode(Long code) {
		return R.data(sysAreaService.getDetailByCode(code));
	}

	@GetMapping("/getCityByProvinceSName")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "根据省名称获取该省所有城市", notes = "传入code")
	public R<List<SysArea>> getCityByProvinceSName(String province_name) {
		return R.data(sysAreaService.getCityByProvinceSName(province_name));
	}

	@GetMapping("/getParentNameAndCodeByLevel")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "根据地址层级获取其可能的父级编号加父级名称", notes = "i_layer_number")
	@ApiIgnore
	public R<List<SysArea>> getParentNameAndCodeByLevel(Integer i_layer_number) {
		return R.data(sysAreaService.getParentNameAndCodeByLevel(i_layer_number));
	}

	@GetMapping("/scodeIsExist")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "地址编号是否已存在", notes = "")
	@ApiIgnore
	public R scodeIsExist(Long scode){
		SysArea area=sysAreaService.scodeIsExist(scode);
		if(area!=null){
			return R.data("编号已存在，请重新输入");
		}
		return R.data("success");
	}

	@GetMapping("/getFullJson")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "获取地址json", notes = "")
	@ApiIgnore
	public R getFullJson(String url) {

		String result = "";
		BufferedReader in = null;
		try {
			String urlNameString = url;
			URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
			URLConnection connection = realUrl.openConnection();
// 建立实际的连接
			connection.connect();
// 获取所有响应头字段
			Map map = connection.getHeaderFields();
// 定义 BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(
				connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println("发送GET请求出现异常！" + e);
			e.printStackTrace();
		}
// 使用finally块来关闭输入流
		finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
//得到的json数据
		System.out.println(result);
		return R.data(result);

	}

	public static void main(String[] args) {
		JsonParser parse = new JsonParser();  //创建json解析器
		JsonObject json = null;  //创建jsonObject对象
		try {
			json = (JsonObject) parse.parse(new FileReader(System.getProperty("user.dir") + "/src/main/resources/json/100000_full.json"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println();

		JsonArray features = json.get("features").getAsJsonArray();

		System.out.println();
	}
}
