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.WatchContinuousSpo2Data;
import com.fs.watch.domain.vo.WatchSpo2DataVo;
import com.fs.watch.service.WatchContinuousSpo2DataService;
import com.fs.watch.service.WatchSpo2DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/watch/continuous/spo2/data")
public class WatchContinuousSpo2DataController extends BaseController {
    @Autowired
    private WatchContinuousSpo2DataService watchContinuousSpo2DataService;

    @Autowired
    private WatchSpo2DataService watchSpo2DataService;

    @PostMapping("/insert")
    public AjaxResult insertContinuousSpo2Data(@RequestBody WatchContinuousSpo2Data data){
        return AjaxResult.success(watchContinuousSpo2DataService.insert(data));
    }

    /**
     * 查询当天最新数据
     * @param  date 日期
     * @param  deviceId 主键
     * @return 某天的数据
     */
    @GetMapping("/queryPulseRate")
    public AjaxResult queryByDataAndDeviceId(@RequestParam(value = "date",required = false) String date,
                                             @RequestParam(value = "deviceId") String deviceId) {
        return AjaxResult.success(this.watchContinuousSpo2DataService.queryOneByDateAndDeviceIdAndState(date,deviceId));
    }

    /**
     * 查询某个时间段的分类数据
     * @param  deviceId 主键
     * @return 某天的数据
     */
    @GetMapping("/queryPageByDateAndDeviceId")
    public TableDataInfo queryPageByDateAndDeviceId(@RequestParam(value = "beginTime",required = false) Date beginTime,
                                             @RequestParam(value = "endTime",required = false) Date endTime,
                                             @RequestParam(value = "pageSize") Integer pageSize,
                                             @RequestParam(value = "pageNum") Integer pageNum,
                                             @RequestParam(value = "deviceId") String deviceId) {
        // 获取总记录数
        int total = this.watchSpo2DataService.countSpPageByDate(beginTime, endTime, deviceId);
        List<WatchSpo2DataVo> list = watchSpo2DataService.querySpPageByDate(
                beginTime, endTime, deviceId, ((pageNum - 1) * pageSize), pageSize);

        return new TableDataInfo(list, total);
    }
}
