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.*;
import java.util.List;

@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@FieldNameConstants(innerTypeName = "FIELDS")
@Table(name = "p_district")
@Entity
@DynamicInsert
@DynamicUpdate
public class District{
    public District(String name, String code, Double area, Geometry geom) {
        this.name = name;
        this.code = code;
        this.area = area;
        this.geom = (MultiPolygon)geom;
    }
    public District(String name, String code,Double area) {
        this.name = name;
        this.code = code;
        this.area = area;
    }
    public District(String name, String code) {
        this.name = name;
        this.code = code;
    }
    public District(){}

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

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

    @Transient
    private String wkt;

    @Transient
    private String geojson;

    @Column()
    private Double area;

    @Transient
    private String tableName;

    @Column()
    private String name;

    @Column()
    private String code;

    @Transient
    private List<Town> towns;

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

}
