package com.ruoyi.ibs.grid.domain.dto;


import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.ibs.grid.domain.vo.AddressResultVO;
import lombok.Data;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;

import java.util.List;
import java.util.Objects;

@Data
public class GeoInfoDTO {

    private Integer id;

    /**
     * 省
     */
    private String province;

    /**
     * 市
     */
    private String city;

    /**
     * 区县
     */
    private String county;

    /**
     * 街道
     */
    private String town;

    /**
     * 村
     */
    private String village;

    /**
     * 行政编码
     */
    private String code;

    /**
     * 父级行政编码
     */
    private String parentCode;

    /**
     * 行政区划等级
     */
    private Integer level;

    /**
     * 地理围栏
     */

    private Geometry grid;

    private List<GeoInfoDTO> children;


    public AddressResultVO childrenMatching(Point point) {
        if (Objects.isNull(this.children)) {
            AddressResultVO addressResultVO = new AddressResultVO();
            BeanUtils.copyProperties(this, addressResultVO);
            addressResultVO.setStatus(0);
            addressResultVO.setLng(point.getX());
            addressResultVO.setLat(point.getY());
            return addressResultVO;
        }
        for (GeoInfoDTO geoInfoDTO : this.children) {
            if (Objects.isNull(geoInfoDTO.getGrid())) {
                continue;
            }
            if (geoInfoDTO.getGrid().contains(point)) {
                return geoInfoDTO.childrenMatching(point);
            }
        }
        AddressResultVO addressResultVO = new AddressResultVO();
        BeanUtils.copyProperties(this, addressResultVO);
        addressResultVO.setStatus(0);
        addressResultVO.setLng(point.getX());
        addressResultVO.setLat(point.getY());
        return addressResultVO;
    }

}
