package com.ruoyi.database.task;
import java.util.List;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.business.util.StringKit;
import com.ruoyi.database.domain.BaseParkingLotInfo;
import com.ruoyi.database.domain.ParkingNearInfo;
import com.ruoyi.database.service.BaseParkingLotInfoService;
import com.ruoyi.database.service.ParkingNearInfoService;
import com.ruoyi.database.util.AmapGeocoding;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

@Component
@RequiredArgsConstructor
public class ParkingNearInfoTask {

    /**
     * 用于获取停车场经纬度
     * 以及停车场周边地址经纬度，名称。
     */

    private static final Logger logger = LoggerFactory.getLogger(ParkingNearInfoTask.class);

    private final BaseParkingLotInfoService baseParkingLotInfoService;
    private final ParkingNearInfoService parkingNearInfoService;


//    @Scheduled(initialDelay = 100, fixedRate = 3000000)
    @RequestMapping("/test")
    public void getPois() {
        try {
            List<BaseParkingLotInfo> list = baseParkingLotInfoService.list();
            list.forEach(baseParkingLotInfo -> {
                String location = AmapGeocoding.getLocation(baseParkingLotInfo.getAddress());
                JSONArray pois = AmapGeocoding.getPois(location,1000,"停车场");
                for (int i = 0; i < pois.size(); i++) {
                    JSONObject poi = pois.getJSONObject(i);
                    String name = poi.getStr("name");
                    String address = poi.getStr("address");
                    String location1 = poi.getStr("location"); // 格式: "经度,纬度"
                    String[] lngLat = location1.split(",");
                    String longitude = lngLat[0]; // 经度
                    String latitude = lngLat[1];  // 纬度
                    String distance = poi.getStr("distance", "未知"); // 距离中心点的距离
                    ParkingNearInfo parkingNearInfo = new ParkingNearInfo();
                    parkingNearInfo.setName(name);
                    parkingNearInfo.setAddress(address);
                    parkingNearInfo.setLng(Double.valueOf(longitude));
                    parkingNearInfo.setLat(Double.valueOf(latitude));
                    parkingNearInfo.setNearType(1);
                    parkingNearInfo.setDistance(distance + "米");
                    parkingNearInfo.setCode(baseParkingLotInfo.getCode());
                    LambdaUpdateWrapper<ParkingNearInfo> queryWrapper = new LambdaUpdateWrapper<>();
                    queryWrapper.eq(ParkingNearInfo::getCode, baseParkingLotInfo.getCode());
                    queryWrapper.eq(ParkingNearInfo::getName, name);
                    queryWrapper.eq(ParkingNearInfo::getAddress, address);
                    parkingNearInfoService.saveOrUpdate(parkingNearInfo,queryWrapper);
                }

                JSONArray pois1 = AmapGeocoding.getPois(location,1000,"常客隆超市");
                for (int i = 0; i < pois1.size(); i++) {
                    JSONObject poi = pois1.getJSONObject(i);
                    String name = poi.getStr("name");
                    String address = poi.getStr("address");
                    String location1 = poi.getStr("location"); // 格式: "经度,纬度"
                    String[] lngLat = location1.split(",");
                    String longitude = lngLat[0]; // 经度
                    String latitude = lngLat[1];  // 纬度
                    String distance = poi.getStr("distance", "未知"); // 距离中心点的距离
                    ParkingNearInfo parkingNearInfo = new ParkingNearInfo();
                    parkingNearInfo.setName(name);
                    parkingNearInfo.setAddress(address);
                    parkingNearInfo.setLng(Double.valueOf(longitude));
                    parkingNearInfo.setLat(Double.valueOf(latitude));
                    parkingNearInfo.setNearType(2);
                    parkingNearInfo.setDistance(distance + "米");
                    parkingNearInfo.setCode(baseParkingLotInfo.getCode());
                    LambdaUpdateWrapper<ParkingNearInfo> queryWrapper = new LambdaUpdateWrapper<>();
                    queryWrapper.eq(ParkingNearInfo::getCode, baseParkingLotInfo.getCode());
                    queryWrapper.eq(ParkingNearInfo::getName, name);
                    queryWrapper.eq(ParkingNearInfo::getAddress, address);
                    parkingNearInfoService.saveOrUpdate(parkingNearInfo,queryWrapper);
                }
            });
        } catch (Exception e) {
            logger.info(StringKit.getTrace(e));
        }
    }


}
