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.WatchSportData;
import com.fs.watch.domain.vo.WatchSportListVo;
import com.fs.watch.service.WatchSportDataService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/watch/sport/data")
public class WatchSportDataController extends BaseController {
    @Autowired
    private WatchSportDataService watchSportDataService;

    @PostMapping("/insert")
    public AjaxResult insertSportData(@RequestBody WatchSportData data){
        return AjaxResult.success(watchSportDataService.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.watchSportDataService.queryByDateAndDeviceIdAndState(date,deviceId));
    }

    /**
     * 查询某个时间段的分类数据
     * @param  startTime 开始时间
     * @param  endTime 结束时间
     * @param  deviceId 主键
     * @return 单条数据
     */
    @GetMapping("/queryPageByDataAndDeviceId")
    public TableDataInfo queryPageByDataAndDeviceId(@RequestParam(value = "startTime",required = false) Date startTime,
                                                    @RequestParam(value = "endTime",required = false) Date endTime,
                                                    @RequestParam(value = "pageSize") Integer pageSize,
                                                    @RequestParam(value = "pageNum") Integer pageNum,
                                                    @RequestParam(value = "deviceId") String deviceId) {
        List<WatchSportListVo> list = watchSportDataService.queryPageByDataAndDeviceId(startTime,endTime,deviceId,pageSize,pageNum);
        if (list != null && !list.isEmpty()) {
            int fromIndex = (pageNum-1) * pageSize;
            int toIndex = pageNum * pageSize;
            List<WatchSportListVo> watchSportListVos = list.subList(fromIndex, Math.min(toIndex, list.size()));
            return new TableDataInfo(watchSportListVos,list.size());
        }
        return new TableDataInfo(null,0);
    }

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

    /**
     * 健康周报
     */
    @GetMapping("/getUserHealthInfoByDeviceId")
    @ApiOperation("健康周报-步数,卡路里,睡眠")
    public AjaxResult getUserHealthInfoByDeviceId(@RequestParam(value = "startTime") Date startTime,
                                                  @RequestParam(value = "endTime") Date endTime,
                                                  @RequestParam(value = "deviceId") String deviceId,
                                                  @RequestParam(value = "userId",required = false) Long userId) {
        if (com.fs.common.utils.StringUtils.isBlank(deviceId)) {
            return null;
        }
        return AjaxResult.success(watchSportDataService.getUserHealthInfoByDeviceId(deviceId,startTime,endTime,userId));
    }
}
