/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.smartapplication.smartmonitor.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springblade.smartapplication.smartmonitor.constant.ZHConstant;
import org.springblade.smartapplication.smartmonitor.entity.SmartVideoMonitor;
import org.springblade.smartapplication.smartmonitor.vo.SmartVideoMonitorVO;
import org.springblade.smartapplication.smartmonitor.mapper.SmartVideoMonitorMapper;
import org.springblade.smartapplication.smartmonitor.service.ISmartVideoMonitorService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-08-06
 */
@Service
public class SmartVideoMonitorServiceImpl extends BaseServiceImpl<SmartVideoMonitorMapper, SmartVideoMonitor> implements ISmartVideoMonitorService {

	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	private final static String ZH_DEVICE_INSTRUCT_COMMAND_SEQ = "zh:device:instruct:commonSeq";

	@Override
	public IPage<SmartVideoMonitorVO> selectSmartVideoMonitorPage(IPage<SmartVideoMonitorVO> page, SmartVideoMonitorVO smartVideoMonitor) {
		return page.setRecords(baseMapper.selectSmartVideoMonitorPage(page, smartVideoMonitor));
	}

	@Override
	public void personStat(List<String> serialNums) {
		for(String serialNum : serialNums){
			HashOperations<String, String, String> hashOperations = this.redisTemplate.opsForHash();
			String num = hashOperations.get(ZH_DEVICE_INSTRUCT_COMMAND_SEQ, serialNum);
			Integer commonSeq = 1000;
			if(StringUtils.hasText(num)){
				commonSeq = Integer.parseInt(num) + 1;
				num = commonSeq + "";
			}else{
				num = "1000"; //设备指令的commonSeq从1000开始
			}
			Map<String, Object> statMap = new HashMap<>();
			statMap.put("Ch", 1);
			statMap.put("Command", "frmPersonSmartRecords");
			statMap.put("Dev", 1);
			statMap.put("Type", 0);
			Map<String, Object> data = new HashMap<>();
			data.put("QueryType", 2);
			data.put("RegionIdx", 0);
			data.put("StartTime", 2021080600);
			data.put("StopTime", 2021080623);
			data.put("TableName", "Counter");
			data.put("TimeType", 1);
			statMap.put("Data", data);
			String commandStr = JSONUtil.toJsonStr(statMap);
			//设备指令commonSeq计数更新
			this.redisTemplate.opsForHash().put(ZH_DEVICE_INSTRUCT_COMMAND_SEQ, serialNum, num);
			//设备指令Data更新
			String listName = serialNum + "_list";
			this.redisTemplate.opsForList().leftPush(listName, commandStr);
		}
	}

	@Override
	public SmartVideoMonitor getBySerialNum(String serialNum) {
		LambdaQueryWrapper<SmartVideoMonitor> lambda = new LambdaQueryWrapper<>();
		lambda.eq(SmartVideoMonitor::getSerialNum, serialNum);
		return baseMapper.selectOne(lambda);
	}

	@Override
	public List<SmartVideoMonitor> getByAgencyId(Long agencyId) {
		LambdaQueryWrapper<SmartVideoMonitor> lambda = new LambdaQueryWrapper<>();
		lambda.eq(SmartVideoMonitor::getAgencyId, agencyId);
		return baseMapper.selectList(lambda);
	}

}
