package com.ylx.web.controller.massage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.annotation.Log;
import com.ylx.common.core.domain.R;
import com.ylx.common.enums.BusinessType;
import com.ylx.common.exception.ServiceException;
import com.ylx.massage.domain.TGeoFence;
import com.ylx.massage.service.TGeoFenceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 地理围栏控制器
 */
@RestController
@RequestMapping("/api/geoFence/v1")
@Slf4j
@Api(tags = {"地理围栏管理"})
public class TGeoFenceController {

    @Resource
    private TGeoFenceService geoFenceService;

    /**
     * 分页查询地理围栏。
     *
     * @param page 分页参数
     * @param geoFence 查询参数
     * @return R<Page<TGeoFence>>
     */
    @GetMapping("/select")
    @ApiOperation("分页查询地理围栏")
    public R<Page<TGeoFence>> select(Page<TGeoFence> page, TGeoFence geoFence) {
        try {
            return R.ok(geoFenceService.pageGeoFence(page, geoFence));
        } catch (ServiceException s) {
            log.error("分页查询地理围栏失败：{}", s.getMessage());
            return R.fail(s.getMessage());
        } catch (Exception e) {
            log.error("分页查询地理围栏系统异常", e);
            return R.fail("系统异常");
        }
    }

    /**
     * 查询地图展示用地理围栏。
     *
     * @param geoFence 查询参数
     * @return R<List<TGeoFence>>
     */
    @GetMapping("/map")
    @ApiOperation("查看地图地理围栏")
    public R<List<TGeoFence>> map(TGeoFence geoFence) {
        try {
            return R.ok(geoFenceService.listMapGeoFence(geoFence));
        } catch (ServiceException s) {
            log.error("查询地图地理围栏失败：{}", s.getMessage());
            return R.fail(s.getMessage());
        } catch (Exception e) {
            log.error("查询地图地理围栏系统异常", e);
            return R.fail("系统异常");
        }
    }

    /**
     * 新增地理围栏。
     *
     * @param geoFence 地理围栏
     * @return R
     */
    @Log(title = "地理围栏管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增地理围栏")
    public R add(@RequestBody TGeoFence geoFence) {
        try {
            return R.ok(geoFenceService.addGeoFence(geoFence));
        } catch (ServiceException s) {
            log.error("新增地理围栏失败：{}", s.getMessage());
            return R.fail(s.getMessage());
        } catch (Exception e) {
            log.error("新增地理围栏系统异常", e);
            return R.fail("系统异常");
        }
    }

    /**
     * 编辑地理围栏。
     *
     * @param geoFence 地理围栏
     * @return R
     */
    @Log(title = "地理围栏管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("编辑地理围栏")
    public R update(@RequestBody TGeoFence geoFence) {
        try {
            return R.ok(geoFenceService.updateGeoFence(geoFence));
        } catch (ServiceException s) {
            log.error("编辑地理围栏失败：{}", s.getMessage());
            return R.fail(s.getMessage());
        } catch (Exception e) {
            log.error("编辑地理围栏系统异常", e);
            return R.fail("系统异常");
        }
    }

    /**
     * 删除地理围栏。
     *
     * @param geoFence 地理围栏
     * @return R
     */
    @Log(title = "地理围栏管理", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    @ApiOperation("删除地理围栏")
    public R del(@RequestBody TGeoFence geoFence) {
        try {
            return R.ok(geoFenceService.deleteGeoFence(geoFence));
        } catch (ServiceException s) {
            log.error("删除地理围栏失败：{}", s.getMessage());
            return R.fail(s.getMessage());
        } catch (Exception e) {
            log.error("删除地理围栏系统异常", e);
            return R.fail("系统异常");
        }
    }
}
