package com.usky.ids.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.ids.domain.AccessEntryExitRecord;
import com.usky.ids.mapper.AccessEntryExitRecordMapper;
import com.usky.ids.service.AccessEntryExitRecordService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 车辆进出记录 服务实现类
 * </p>
 *
 * @author han
 * @since 2025-03-26
 */
@Service
public class AccessEntryExitRecordServiceImpl extends AbstractCrudService<AccessEntryExitRecordMapper, AccessEntryExitRecord> implements AccessEntryExitRecordService {
    @Override
    public List<Object> entryExitCount(){
        List<Object> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        LocalDateTime now = LocalDateTime.now();
        LocalDate today = LocalDate.now();
        LocalDateTime startOfDay = LocalDateTime.of(today, LocalTime.MIN);
        // 定义日期时间字符串
        String dateTimeString = "2025-04-25 02:31:00";
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将字符串转换为 LocalDateTime 对象
        LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, formatter);
        QueryWrapper<AccessEntryExitRecord> queryWrapper = Wrappers.query();
        queryWrapper.select("COUNT(action_direction=1 or null) as entryCount","COUNT(action_direction=2 or" +
                " null) as exitCount")
                .between("record_time",localDateTime,now);
        List<Map<String,Object>> entryExitList = this.listMaps(queryWrapper);
        map.put("entryCount", Integer.valueOf(entryExitList.get(0).get("entryCount").toString()));
        map.put("exitCount", Integer.valueOf(entryExitList.get(0).get("exitCount").toString()));
        map.put("iotCount", 102);
        list.add(map);
        return list;
    }
}
