package com.sysu.admin.site.lz;

import com.sysu.admin.controller.lz.area.LzArea;
import com.sysu.admin.controller.lz.area.LzAreaService;
import com.sysu.admin.utils.shape.GeoCastUtil;
import com.xiesx.fastboot.base.result.BaseResult;
import com.xiesx.fastboot.base.result.R;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.MultiPolygon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/site/lz_area")
@RestController
public class LzAreaController {

    @Autowired
    private LzAreaService lzAreaService;

    @RequestMapping("/list")
    public BaseResult list(){
        List<LzArea> list = lzAreaService.findAll();
        list.forEach(lzArea -> {
            lzArea.setWkt(GeoCastUtil.geomToWkt(lzArea.getGeom()));
            lzArea.setPointWkt(GeoCastUtil.geomToWkt(lzArea.getPoint()));
        });
        return R.succ(list);
    }

    @RequestMapping("/save")
    public BaseResult save(@RequestBody LzArea bean){
        bean.setGeom((MultiPolygon) GeoCastUtil.wktToGeom(bean.getWkt()));
        bean.setPoint(bean.getGeom().getCentroid());
        lzAreaService.save(bean);
        return R.succ();
    }

}
