package com.xueyi.system.staff.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xueyi.common.web.annotation.TenantIgnore;
import com.xueyi.system.staff.domain.dto.DmEmpAttendanceDto;
import com.xueyi.system.staff.domain.query.DmEmpAttendanceQuery;
import com.xueyi.system.staff.mapper.DmEmpAttendanceMapper;
import com.xueyi.system.staff.service.IDmEmpAttendanceService;
import com.xueyi.system.staff.manager.IDmEmpAttendanceManager;
import com.xueyi.common.web.entity.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 员工考勤管理 服务层处理
 *
 * @author xueyi
 */
@Service
public class DmEmpAttendanceServiceImpl extends BaseServiceImpl<DmEmpAttendanceQuery, DmEmpAttendanceDto, IDmEmpAttendanceManager> implements IDmEmpAttendanceService {

    /**
     * 查询员工考勤对象列表 | 数据权限
     *
     * @param dmEmpAttendance 员工考勤对象
     * @return 员工考勤对象集合
     */
    @Override
    //@DataScope(userAlias = "createBy", mapperScope = {"DmEmpAttendanceMapper"})
    public List<DmEmpAttendanceDto> selectListScope(DmEmpAttendanceQuery dmEmpAttendance) {
        return baseManager.selectList(dmEmpAttendance);
    }


    @Autowired
    private DmEmpAttendanceMapper attendanceMapper;

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    @Override
    @TenantIgnore(tenantLine = true)
    public Integer selectCountIgnoreTenant() {
        if (redisTemplate.hasKey("broadcast:attendance")) {
            Integer attendance = (Integer) redisTemplate.opsForValue().get("broadcast:attendance");
            return attendance;
        } else {
            Long attendance =  attendanceMapper.selectCount(new QueryWrapper<>());
            redisTemplate.opsForValue().set("dashboard:attendance", attendance);
            return Math.toIntExact(attendance);
        }
    }

    public void updRedisCount(){
        if (redisTemplate.hasKey("dashboard:attendance")) {
            redisTemplate.opsForValue().increment("dashboard:attendance");
        } else {
            Long attendance =  attendanceMapper.selectCount(new QueryWrapper<>());
            redisTemplate.opsForValue().set("dashboard:attendance", attendance);
        }
    }
}