package com.fs.watch.controller;

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.SleepSection;
import com.fs.watch.domain.WatchSleepData;
import com.fs.watch.domain.vo.WatchSleepDataVo;
import com.fs.watch.service.WatchSleepDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/watch/sleep/data")
public class WatchSleepDataController {
    @Autowired
    private WatchSleepDataService watchSleepDataService;


    @PostMapping("/insert")
    public AjaxResult insertSleepData(@RequestBody WatchSleepData data){
        return AjaxResult.success(watchSleepDataService.insert(data));
    }

    /**
     * 查询某天的数据,及分析结论
     * @param  date 日期
     * @param  deviceId 主键
     * @return 查询某天的数据,及分析结论
     */
    @GetMapping("/queryByDateAndDeviceId")
    public AjaxResult queryByDataAndDeviceId(@RequestParam(value = "date",required = false) String date,
                                             @RequestParam(value = "deviceId") String deviceId) {
        return AjaxResult.success(this.watchSleepDataService.queryByDateAndDeviceIdAndState(date,deviceId));
    }

    /**
     * 导出某天数据
     *
     * @param  date 日期
     * @param  deviceId 主键
     * @return 单条数据
     */
    @GetMapping("/exportByDate")
    public AjaxResult exportByDate(@RequestParam(value = "date",required = false) String date,
                                   @RequestParam(value = "deviceId") String deviceId) {
        WatchSleepDataVo vo = this.watchSleepDataService.queryByDateAndDeviceIdAndState(date, deviceId);
        List<SleepSection> list = vo.getSleepSection();
        ExcelUtil<SleepSection> util = new ExcelUtil<>(SleepSection.class);
        return util.exportExcel(list, "睡眠数据");
    }

    /**
     * 查询某个时间段睡眠数据 分页
     *
     * @param  date 日期
     * @param  deviceId 主键
     * @return 查询某个时间段数据
     */
    @GetMapping("/page")
    public TableDataInfo page(@RequestParam(value = "date",required = false) String date,
                              @RequestParam(value = "pageSize") Integer pageSize,
                              @RequestParam(value = "pageNum") Integer pageNum,
                              @RequestParam(value = "deviceId") String deviceId) {
        WatchSleepDataVo vo = this.watchSleepDataService.queryByDateAndDeviceIdAndState(date, deviceId);
        if (vo!=null){
            List<SleepSection> list = vo.getSleepSection();
            if(list != null && list.size() > 0){
                int fromIndex = (pageNum-1) * pageSize;
                int toIndex = pageNum * pageSize;
                List<SleepSection> sleepSections = list.subList(fromIndex, Math.min(toIndex, list.size()));
                return new TableDataInfo(sleepSections,list.size());
            }
        }
        return new TableDataInfo(null,0);
    }
}
