package com.fs.watch.controller;

import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.watch.domain.WatchBasicInfo;
import com.fs.watch.domain.vo.GnssVo;
import com.fs.watch.service.WatchBasicInfoService;
import org.springframework.web.bind.annotation.*;

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

/**
 * 基础数据(WatchBasicInfo)表控制层
 *
 * @author makejava
 * @since 2024-07-30 09:52:41
 */
@RestController
@RequestMapping("/watch/basic/info")
public class WatchBasicInfoController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private WatchBasicInfoService watchBasicInfoService;


    @PostMapping("/insert")
    public AjaxResult insertBasicInfo(@RequestBody WatchBasicInfo data){
        return AjaxResult.success(watchBasicInfoService.insert(data));
    }


    /**
     * 通过主键查询单条数据
     *
     * @param deviceId 主键
     * @return 单条数据
     */
    @GetMapping("{deviceId}")
    public AjaxResult queryById(@PathVariable("deviceId") String deviceId) {
        return AjaxResult.success(this.watchBasicInfoService.queryByDeviceId(deviceId));
    }

    /**
     * 查询某天地理信息
     *
     * @param date     日期
     * @param deviceId 主键
     * @return 单条数据
     */
    @GetMapping("/queryGnssByDateAndDeviceId")
    public AjaxResult queryByDateAndDeviceId(@RequestParam(value = "date", required = false) String date,
                                             @RequestParam(value = "deviceId") String deviceId) {
        return AjaxResult.success(this.watchBasicInfoService.queryGnssByDateAndDeviceId(date, deviceId));
    }

    /**
     * 查询某天地理信息 分页
     *
     * @return 分页
     */
    @GetMapping("/page")
    public TableDataInfo page(@RequestParam(value = "starTime", required = false) String starTime,
                              @RequestParam(value = "endTime", required = false) String endTime,
                              @RequestParam(value = "pageSize") Integer pageSize,
                              @RequestParam(value = "pageNum") Integer pageNum,
                              @RequestParam(value = "deviceId") String deviceId) {
        List<GnssVo> vo = this.watchBasicInfoService.queryGnssByDate(starTime,endTime, deviceId);
        if (vo != null && !vo.isEmpty()) {
            int fromIndex = (pageNum - 1) * pageSize;
            int toIndex = pageNum * pageSize;
            List<GnssVo> gnssVos = vo.subList(fromIndex, Math.min(toIndex, vo.size()));
            return new TableDataInfo(gnssVos, vo.size());
        }
        return new TableDataInfo(null, 0);
    }


    /**
     * 查询某天地理信息
     *
     * @param date     日期
     * @param deviceId 主键
     * @return 单条数据
     */
    @GetMapping("/queryOverview")
    public AjaxResult queryOverview(@RequestParam(value = "date", required = false) String date,
                                    @RequestParam(value = "deviceId") String deviceId) {
        return AjaxResult.success(this.watchBasicInfoService.queryOverviewsByDateAndDeviceId(date, deviceId));
    }

}

