package com.sysu.admin.controller.city;

import com.alibaba.fastjson.annotation.JSONField;
import com.sysu.admin.utils.shape.GeoCastUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import lombok.experimental.FieldNameConstants;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.MultiPolygon;

import javax.persistence.*;

@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@FieldNameConstants(innerTypeName = "FIELDS")
@Table(name = "p_town")
@Entity
@DynamicInsert
@DynamicUpdate
public class Town {
    public Town(){}
    public Town(String name, Integer id, Double area){
        this.name = name;
        this.id = id;
        this.area = area;
    }

    public Town(String name, Integer id, Double area, Geometry geom) {
        this.name = name;
        this.id = id;
        this.area = area;
        this.geom = (MultiPolygon)geom;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @JSONField(serialize = false)
    @Column(columnDefinition = "geom")
    private MultiPolygon geom;


    @Column()
    private Double area;

    @Transient
    private String wkt;
    @Transient
    private String geojson;

    @Column()
    private String name;

    @Column()
    private String districtCode;

    public void castGeojson(){
        if(geom != null)
            this.geojson = GeoCastUtil.gjson.toString(geom);
    }
}
