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_city")
@Entity
@DynamicInsert
@DynamicUpdate
public class City{

    public City(){}
    public City(String name, String code, Double area, Geometry geom) {
        this.name = name;
        this.code = code;
        this.area = area;
        this.geom = (MultiPolygon)geom;
    }
    public City(String name, String code, Double area) {
        this.name = name;
        this.code = code;
        this.area = area;
    }

    @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;

    @Column()
    private String name;

    @Column()
    private String code;

    @Transient
    @JSONField(serialize = false)
    private List<District> districtList;

    @Transient
    @JSONField(serialize = true)
    private List<District> districts;

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