package com.gci.boot.modules.gis.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.common.validated.SaveValidated;
import com.gci.boot.common.vo.Result;
import com.gci.boot.modules.gis.entity.TbGisGps;
import com.gci.boot.modules.gis.service.ITbGisGpsService;
import com.gci.boot.modules.gis.query.GpsHistoryQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 2464
 * @since 2023-09-01
 */
@RestController
@RequestMapping("/gis/gps")
public class TbGisGpsController {
    @Autowired
    private ITbGisGpsService iTbGisGpsService;

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<?> queryPageList(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        Page<TbGisGps> page = new Page<TbGisGps>(pageNo, pageSize);
        LambdaQueryWrapper<TbGisGps> lambdaQueryWrapper = new LambdaQueryWrapper<TbGisGps>();
        IPage<TbGisGps> pageList = iTbGisGpsService.page(page, lambdaQueryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     */
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody TbGisGps tbGisGps) {
        iTbGisGpsService.save(tbGisGps);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     */
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody TbGisGps tbGisGps) {
        iTbGisGpsService.updateById(tbGisGps);
        return Result.ok("编辑成功！");
    }

    /**
     * 删除
     */
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        iTbGisGpsService.removeById(id);
        return Result.ok("删除成功！");
    }

    /**
     * 批量删除
     */
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        iTbGisGpsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 获取分时段的gps数据
     */
    @PostMapping("/getGpsHistory")
    public Result<?> getGpsHistory (@RequestBody @Validated(SaveValidated.class) GpsHistoryQuery vo) {
        return Result.ok(iTbGisGpsService.getGpsHistory(vo));
    }
}
