package com.ruoyi.demo.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.demo.entity.Statistics;
import com.ruoyi.demo.entity.bo.PlanBo;
import com.ruoyi.demo.entity.bo.StatisticsAddBo;
import com.ruoyi.demo.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/statistics")
public class StatisticsController {
    @Autowired
    StatisticsService statisticsService;

    // 扫码时间分布
    @GetMapping("/timeDis")
    public R timeDis(String option){
        return R.ok(statisticsService.getTimeDisToVo(option));
    }

    // 获取扫描日志
    @PostMapping("/scanList")
    public R scanList(@RequestBody PlanBo bo){
        if (bo.getStartTime() != null && bo.getEndTime() != null)
            if (bo.getStartTime().compareTo(bo.getEndTime()) > 0)
                return R.fail("起始时间不能大于结束时间");
        return R.ok(statisticsService.getScanList(bo));
    }

    // 获取总扫描次数
    @GetMapping("/total")
    public R scanTotal(){
        return R.ok(statisticsService.getScanTotal());
    }

    // 获取sign——用于测试
    @PostMapping("/getSign")
    public R getSign(@RequestBody StatisticsAddBo bo){
        String sign = statisticsService.getSign(bo);
        if (sign.equals("error"))
            return R.fail("无效的appkey");
        return R.ok(sign);
    }

    // 添加扫码行为记录
    @PostMapping()
    public R add(@RequestBody @Validated(value = {AddGroup.class}) StatisticsAddBo bo){
        int i = statisticsService.add(bo);
        if (i == 1)
            return R.ok("添加成功");
        if (i == -1)
            return R.fail("无效的appkey");
        if (i == -3)
            return R.fail("校验sign出错");
        return R.fail("添加失败");
    }
}
