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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xueyi.common.web.annotation.TenantIgnore;
import com.xueyi.common.web.entity.service.impl.BaseServiceImpl;
import com.xueyi.system.api.pass.domain.dto.DmRecognizedRecordsDto;
import com.xueyi.system.pass.domain.query.DmRecognizedRecordsQuery;
import com.xueyi.system.pass.manager.IDmRecognizedRecordsManager;
import com.xueyi.system.pass.mapper.DmRecognizedRecordsMapper;
import com.xueyi.system.pass.service.IDmRecognizedRecordsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 DmRecognizedRecordsServiceImpl extends BaseServiceImpl<DmRecognizedRecordsQuery, DmRecognizedRecordsDto, IDmRecognizedRecordsManager> implements IDmRecognizedRecordsService {


    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Autowired
    private DmRecognizedRecordsMapper recognizedRecordsMapper;

    private static final Logger log = LoggerFactory.getLogger(DmRecognizedRecordsServiceImpl.class);
    /**
     * 查询人员识别对象列表 | 数据权限
     *
     * @param dmRecognizedRecords 人员识别对象
     * @return 人员识别对象集合
     */
    @Override
    //@DataScope(userAlias = "createBy", mapperScope = {"DmRecognizedRecordsMapper"})
    public List<DmRecognizedRecordsDto> selectListScope(DmRecognizedRecordsQuery dmRecognizedRecords) {
        return baseManager.selectList(dmRecognizedRecords);
    }


    @TenantIgnore(tenantLine = true)
    public void syncToRedis(){
        Long count = recognizedRecordsMapper.selectCount(new QueryWrapper<>());
        log.info("同步redis mysql识别人数：{}", count);
        redisTemplate.opsForValue().set("dashboard:recognizedPersonCount",count);
    }

    public void setRedisRecognizedCount(Integer num){
        if (!redisTemplate.hasKey("dashboard:recognizedPersonCount")){
            syncToRedis();
        } else {
            log.info("同步redis 新增识别人数："+num);
            if (null != redisTemplate.opsForValue().get("dashboard:recognizedPersonCount")) {
                Long count = ((Number) (redisTemplate.opsForValue().get("dashboard:recognizedPersonCount"))).longValue();
                redisTemplate.opsForValue().set("dashboard:recognizedPersonCount", count + num);
            }
        }

    }


}