package com.supervision.web.peopleGateManage.Controller;

import com.supervision.web.peopleGateManage.Service.EventLogService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/event")
public class EventLogController {

    @Resource
    private EventLogService eventLogService;

//    @GetMapping("/list")
//    public Map<String, Object> list(@RequestParam Map<String, Object> params) {
//        Integer pageNum = Integer.parseInt(params.getOrDefault("pageNum","1").toString());
//        Integer pageSize = Integer.parseInt(params.getOrDefault("pageSize","20").toString());
//        return eventLogService.getEventLogs(params);
//    }
@GetMapping("/list")
public Map<String, Object> list(
        @RequestParam(required = false) String eventType,
        @RequestParam(required = false) String personId,
        @RequestParam(required = false) String jobNumber,
        @RequestParam(required = false) String name,
        @RequestParam(required = false) String remoteHost,
        @RequestParam(required = false) String reader,
        @RequestParam(required = false) String detectorId,
        @RequestParam(required = false) String operation,
        @RequestParam(required = false) String startTime,
        @RequestParam(required = false) String endTime,
        @RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "24") Integer pageSize
) {
    // 强制为非 null 值
    int pn = (pageNum == null || pageNum < 1) ? 1 : pageNum;
    int ps = (pageSize == null || pageSize < 1) ? 24 : pageSize;
    int offset = (pn - 1) * ps;

    // 如果你的 Service 接收 Map，你也可以组装 Map，并且确保都是正确类型
    Map<String, Object> params = new HashMap<>();
    params.put("eventType", eventType);
    params.put("personId", personId);
    params.put("jobNumber", jobNumber);
    params.put("name", name);
    params.put("remoteHost", remoteHost);
    params.put("reader", reader);
    params.put("detectorId", detectorId);
    params.put("operation", operation);
    params.put("startTime", startTime);
    params.put("endTime", endTime);
    params.put("offset", offset);     // **Integer**
    params.put("pageSize", ps);       // **Integer**

    // 调用 Service（内部调用 Mapper.selectByCondition(params...)）
    Map<String, Object> result = eventLogService.getEventLogs(params);

    return result;
}
}
