267 lines
9.8 KiB
Java
267 lines
9.8 KiB
Java
package com.sunnyfarm.controller;
|
||
|
||
import com.sunnyfarm.common.Result;
|
||
import com.sunnyfarm.entity.Address;
|
||
import com.sunnyfarm.service.AddressService;
|
||
import com.sunnyfarm.service.AmapService;
|
||
import com.sunnyfarm.util.UserContext;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.web.bind.annotation.*;
|
||
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.HashMap;
|
||
import java.util.ArrayList;
|
||
|
||
@RestController
|
||
@RequestMapping("/api/address")
|
||
@CrossOrigin
|
||
public class AddressController {
|
||
|
||
@Autowired
|
||
private AddressService addressService;
|
||
|
||
@Autowired
|
||
private AmapService amapService;
|
||
|
||
/**
|
||
* 获取用户地址列表
|
||
*/
|
||
@GetMapping("/list")
|
||
public Result<List<Address>> getAddressList() {
|
||
Long userId = UserContext.getCurrentUserId();
|
||
List<Address> addresses = addressService.getUserAddresses(userId);
|
||
return Result.success(addresses);
|
||
}
|
||
|
||
/**
|
||
* 获取用户默认地址
|
||
*/
|
||
@GetMapping("/default")
|
||
public Result<Address> getDefaultAddress() {
|
||
Long userId = UserContext.getCurrentUserId();
|
||
Address address = addressService.getUserDefaultAddress(userId);
|
||
return Result.success(address);
|
||
}
|
||
|
||
/**
|
||
* 获取地址详情
|
||
*/
|
||
@GetMapping("/{id}")
|
||
public Result<Address> getAddressDetail(@PathVariable Long id) {
|
||
Address address = addressService.getById(id);
|
||
if (address != null) {
|
||
return Result.success(address);
|
||
} else {
|
||
return Result.error("地址不存在");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 创建地址
|
||
*/
|
||
@PostMapping("/create")
|
||
public Result<Address> createAddress(@RequestBody Address address) {
|
||
try {
|
||
Long userId = UserContext.getCurrentUserId();
|
||
address.setUserId(userId);
|
||
|
||
Address createdAddress = addressService.createAddress(address);
|
||
return Result.success("地址创建成功", createdAddress);
|
||
} catch (Exception e) {
|
||
return Result.error(e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 更新地址
|
||
*/
|
||
@PutMapping("/{id}")
|
||
public Result<Address> updateAddress(@PathVariable Long id, @RequestBody Address address) {
|
||
try {
|
||
Long userId = UserContext.getCurrentUserId();
|
||
address.setUserId(userId);
|
||
|
||
Address updatedAddress = addressService.updateAddress(id, address);
|
||
return Result.success("地址更新成功", updatedAddress);
|
||
} catch (Exception e) {
|
||
return Result.error(e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除地址
|
||
*/
|
||
@DeleteMapping("/{id}")
|
||
public Result<String> deleteAddress(@PathVariable Long id) {
|
||
try {
|
||
boolean success = addressService.deleteAddress(id);
|
||
if (success) {
|
||
return Result.success("地址删除成功");
|
||
} else {
|
||
return Result.error("地址删除失败");
|
||
}
|
||
} catch (Exception e) {
|
||
return Result.error(e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 设置默认地址
|
||
*/
|
||
@PutMapping("/{id}/default")
|
||
public Result<String> setDefaultAddress(@PathVariable Long id) {
|
||
try {
|
||
Long userId = UserContext.getCurrentUserId();
|
||
boolean success = addressService.setDefaultAddress(userId, id);
|
||
if (success) {
|
||
return Result.success("默认地址设置成功");
|
||
} else {
|
||
return Result.error("默认地址设置失败");
|
||
}
|
||
} catch (Exception e) {
|
||
return Result.error(e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取用户地址数量
|
||
*/
|
||
@GetMapping("/count")
|
||
public Result<Long> getAddressCount() {
|
||
Long userId = UserContext.getCurrentUserId();
|
||
Long count = addressService.getUserAddressCount(userId);
|
||
return Result.success(count);
|
||
}
|
||
|
||
/**
|
||
* 地理编码 - 根据地址获取经纬度
|
||
*/
|
||
@PostMapping("/geocode")
|
||
public Result<Map<String, Object>> geocodeAddress(@RequestBody Map<String, String> request) {
|
||
try {
|
||
String address = request.get("address");
|
||
if (address == null || address.trim().isEmpty()) {
|
||
return Result.error("地址不能为空");
|
||
}
|
||
|
||
// 调用高德地图API进行地理编码
|
||
Map<String, Object> amapResult = amapService.geocode(address);
|
||
|
||
if (amapResult != null && "1".equals(amapResult.get("status"))) {
|
||
List<Map<String, Object>> geocodes = (List<Map<String, Object>>) amapResult.get("geocodes");
|
||
if (geocodes != null && !geocodes.isEmpty()) {
|
||
Map<String, Object> geocode = geocodes.get(0);
|
||
String location = (String) geocode.get("location");
|
||
|
||
Map<String, Object> result = new HashMap<>();
|
||
if (location != null && location.contains(",")) {
|
||
String[] coords = location.split(",");
|
||
result.put("lng", Double.parseDouble(coords[0]));
|
||
result.put("lat", Double.parseDouble(coords[1]));
|
||
}
|
||
result.put("formattedAddress", geocode.get("formatted_address"));
|
||
|
||
return Result.success(result);
|
||
}
|
||
}
|
||
|
||
return Result.error("地理编码失败");
|
||
} catch (Exception e) {
|
||
return Result.error("地理编码失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 逆地理编码 - 根据经纬度获取地址
|
||
*/
|
||
@PostMapping("/reverse-geocode")
|
||
public Result<Map<String, Object>> reverseGeocode(@RequestBody Map<String, Double> request) {
|
||
try {
|
||
Double lng = request.get("lng");
|
||
Double lat = request.get("lat");
|
||
|
||
if (lng == null || lat == null) {
|
||
return Result.error("坐标不能为空");
|
||
}
|
||
|
||
// 调用高德地图API进行逆地理编码
|
||
String location = lng + "," + lat;
|
||
Map<String, Object> amapResult = amapService.reverseGeocode(location);
|
||
|
||
if (amapResult != null && "1".equals(amapResult.get("status"))) {
|
||
Map<String, Object> regeocode = (Map<String, Object>) amapResult.get("regeocode");
|
||
if (regeocode != null) {
|
||
Map<String, Object> addressComponent = (Map<String, Object>) regeocode.get("addressComponent");
|
||
|
||
Map<String, Object> result = new HashMap<>();
|
||
result.put("formattedAddress", regeocode.get("formatted_address"));
|
||
if (addressComponent != null) {
|
||
result.put("province", addressComponent.get("province"));
|
||
result.put("city", addressComponent.get("city"));
|
||
result.put("district", addressComponent.get("district"));
|
||
}
|
||
|
||
return Result.success(result);
|
||
}
|
||
}
|
||
|
||
return Result.error("逆地理编码失败");
|
||
} catch (Exception e) {
|
||
return Result.error("逆地理编码失败: " + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* POI搜索
|
||
*/
|
||
@PostMapping("/search-poi")
|
||
public Result<List<Map<String, Object>>> searchPOI(@RequestBody Map<String, String> request) {
|
||
try {
|
||
String keyword = request.get("keyword");
|
||
String city = request.get("city");
|
||
|
||
if (keyword == null || keyword.trim().isEmpty()) {
|
||
return Result.error("搜索关键词不能为空");
|
||
}
|
||
|
||
// 调用高德地图API进行POI搜索
|
||
Map<String, Object> amapResult = amapService.searchPOI(keyword, city);
|
||
|
||
List<Map<String, Object>> results = new ArrayList<>();
|
||
|
||
if (amapResult != null && "1".equals(amapResult.get("status"))) {
|
||
List<Map<String, Object>> pois = (List<Map<String, Object>>) amapResult.get("pois");
|
||
if (pois != null) {
|
||
for (Map<String, Object> poi : pois) {
|
||
Map<String, Object> result = new HashMap<>();
|
||
result.put("id", poi.get("id"));
|
||
result.put("name", poi.get("name"));
|
||
result.put("address", poi.get("address"));
|
||
|
||
// 修复类型转换问题:先转换为String再连接
|
||
String pname = String.valueOf(poi.get("pname") != null ? poi.get("pname") : "");
|
||
String cityname = String.valueOf(poi.get("cityname") != null ? poi.get("cityname") : "");
|
||
String adname = String.valueOf(poi.get("adname") != null ? poi.get("adname") : "");
|
||
result.put("district", pname + cityname + adname);
|
||
|
||
// 解析坐标
|
||
String location = (String) poi.get("location");
|
||
if (location != null && location.contains(",")) {
|
||
String[] coords = location.split(",");
|
||
result.put("lng", Double.parseDouble(coords[0]));
|
||
result.put("lat", Double.parseDouble(coords[1]));
|
||
}
|
||
|
||
results.add(result);
|
||
}
|
||
}
|
||
}
|
||
|
||
return Result.success(results);
|
||
} catch (Exception e) {
|
||
return Result.error("POI搜索失败: " + e.getMessage());
|
||
}
|
||
}
|
||
}
|