package com.java110.user.dao.impl;

import com.java110.core.base.dao.BaseServiceDao;
import com.java110.user.dao.property.IAttendanceRecordV1ServiceDao;
import com.java110.po.property.AttendanceRecordPo;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

@Service("attendanceRecordV1ServiceDaoImpl")
public class AttendanceRecordV1ServiceDaoImpl extends BaseServiceDao implements IAttendanceRecordV1ServiceDao {

    @Override
    public int saveAttendanceRecord(AttendanceRecordPo po) {
        return sqlSessionTemplate.insert("AttendanceRecordV1ServiceDaoImpl.saveAttendanceRecord", po);
    }

    @Override
    public List<Map> queryAttendanceRecords(Map params) {
        return sqlSessionTemplate.selectList("AttendanceRecordV1ServiceDaoImpl.queryAttendanceRecords", params);
    }

    @Override
    public int queryAttendanceRecordsCount(Map params) {
        List<Map> result = sqlSessionTemplate.selectList("AttendanceRecordV1ServiceDaoImpl.queryAttendanceRecordsCount", params);
        if (result == null || result.isEmpty()) return 0;
        return Integer.parseInt(result.get(0).get("count").toString());
    }

    @Override
    public Map queryTodayAttendanceByType(Map params) {
        List<Map> result = sqlSessionTemplate.selectList("AttendanceRecordV1ServiceDaoImpl.queryTodayAttendanceByType", params);
        return (result != null && !result.isEmpty()) ? result.get(0) : null;
    }

    @Override
    public int countTodayByType(Map params) {
        List<Map> result = sqlSessionTemplate.selectList("AttendanceRecordV1ServiceDaoImpl.countTodayByType", params);
        if (result == null || result.isEmpty()) return 0;
        Object count = result.get(0);
        if (count instanceof Integer) return (Integer) count;
        if (count instanceof Long) return ((Long) count).intValue();
        return Integer.parseInt(count.toString());
    }

    @Override
    public Map getLastPunch(Map params) {
        List<Map> result = sqlSessionTemplate.selectList("AttendanceRecordV1ServiceDaoImpl.getLastPunch", params);
        return (result != null && !result.isEmpty()) ? result.get(0) : null;
    }
}
