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.common.utils.poi.ExcelUtil;
import com.fs.watch.domain.WatchHeartRateData;
import com.fs.watch.service.WatchHeartRateDataService;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/watch/heart/rate")
public class WatchHeartRateDataController extends BaseController {
    @Resource
    private WatchHeartRateDataService watchHeartRateDataService;


    @PostMapping("/insert")
    public AjaxResult insertHeartData(@RequestBody WatchHeartRateData data){
        return AjaxResult.success(watchHeartRateDataService.insert(data));
    }

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

    /**
     * 查询当天数据
     *
     * @param  date 当天日期
     * @param  deviceId 主键
     * @return 单条数据
     */
    @GetMapping("/queryByDate")
    public AjaxResult queryByDate(@RequestParam(value = "date",required = false) String date,
                                  @RequestParam(value = "deviceId") String deviceId) {
        return AjaxResult.success(this.watchHeartRateDataService.queryByDate(date,deviceId));
    }

    /**
     * 查询某个时间段心率数据 分页
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param deviceId  设备编号
     * @return 查询某个时间段数据
     */
    @GetMapping("/page")
    public TableDataInfo queryHeartPageByDate(@RequestParam(value = "startTime") Date startTime,
                                              @RequestParam(value = "endTime") Date endTime,
                                              @RequestParam(value = "deviceId") String deviceId,
                                              @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                              @RequestParam(value = "pageSize", required = false) Integer pageSize) {

        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;  // 默认页面大小
        }

        // 获取总记录数
        int total = this.watchHeartRateDataService.countHeartRateByDate(startTime, endTime, deviceId);

        // 获取分页数据
        List<WatchHeartRateData> list = this.watchHeartRateDataService.queryHeartPageByDate
                (startTime, endTime, deviceId, ((pageNum - 1) * pageSize), pageSize
                );

        // 返回包含数据的TableDataInfo对象
        return new TableDataInfo(list, total);
    }

    /**
     * 导出某天数据
     *
     * @param  date 日期
     * @param  deviceId 主键
     * @return 单条数据
     */
    @GetMapping("/exportByDate")
    public AjaxResult exportByDate(@RequestParam(value = "date",required = false) String date,
                                  @RequestParam(value = "deviceId") String deviceId) {
        List<WatchHeartRateData> list = this.watchHeartRateDataService.queryByDate(date, deviceId).getList();
        ExcelUtil<WatchHeartRateData> util = new ExcelUtil<>(WatchHeartRateData.class);
        return util.exportExcel(list, "心率数据");
    }
}
