/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/19
 */
package org.springblade.modules.business.rtumanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.enums.EquipmentStatusEnum;
import org.springblade.modules.baseinfo.rtu.dto.RtuInfoDTO;
import org.springblade.modules.business.rtumanage.dto.RtuStatusDTO;
import org.springblade.modules.business.rtumanage.entity.RtuStatusEntity;
import org.springblade.modules.business.rtumanage.mapper.RtuManageMapper;
import org.springblade.modules.business.rtumanage.service.IRtuManageService;
import org.springblade.modules.business.rtumanage.vo.RtuStatusVO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/***
 * Date:2022/8/19
 * Title: 测站管理服务接口
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
@Service
public class RtuManageServiceImpl extends BaseServiceImpl<RtuManageMapper, RtuStatusEntity> implements IRtuManageService {
	@Resource
	private RedisTemplate redisTemplate;

	@Override
	public IPage<RtuStatusVO> selectPage(IPage<RtuStatusVO> page, RtuInfoDTO rtuInfoDTO) {
		return page.setRecords(baseMapper.selectPage(page, rtuInfoDTO));
	}

	@Override
	public IPage<RtuStatusVO> selectWarnPage(IPage<RtuStatusVO> page, RtuInfoDTO rtuInfoDTO) {
		return page.setRecords(baseMapper.selectWarnPage(page, rtuInfoDTO));
	}

	@Override
	public boolean updateLastTime(RtuStatusEntity rtuStatusEntity) {
		LambdaQueryWrapper<RtuStatusEntity> querywrapper = Wrappers.<RtuStatusEntity>query().lambda();
		querywrapper.eq(RtuStatusEntity::getIsDeleted, 0);
		querywrapper.eq(RtuStatusEntity::getRtuCode, rtuStatusEntity.getRtuCode());
		RtuStatusEntity entity = this.getOne(querywrapper);
		if (null != entity) {
//			if (null != entity.getLastUpTime()) {
//				if (entity.getLastUpTime().before(rtuStatusEntity.getLastUpTime())) {
//					entity.setLastUpTime(rtuStatusEntity.getLastUpTime());
//				}
//			} else {
				entity.setLastUpTime(rtuStatusEntity.getLastUpTime());
			//}
			return this.updateById(entity);
		} else {
			rtuStatusEntity.setRunStatusId(EquipmentStatusEnum.STATUS_RUN.getCode());
			return this.save(rtuStatusEntity);
		}
	}

	@Override
	public boolean updateRtuStatus(RtuStatusEntity rtuStatusEntity) {
		LambdaQueryWrapper<RtuStatusEntity> querywrapper = Wrappers.<RtuStatusEntity>query().lambda();
		querywrapper.eq(RtuStatusEntity::getIsDeleted, 0);
		querywrapper.eq(RtuStatusEntity::getRtuCode, rtuStatusEntity.getRtuCode());
		RtuStatusEntity entity = this.getOne(querywrapper);
		if (entity != null) {
			entity.setRunStatusId(rtuStatusEntity.getRunStatusId());
			return this.updateById(entity);
		} else {
			return this.save(rtuStatusEntity);
		}
	}

	@Override
	public boolean rtuUpCount(RtuStatusDTO rtuStatusDTO) {
		String key = "rtu.status.count.up.duration.";
		if (rtuStatusDTO.getIsRain() != null && rtuStatusDTO.getIsRain() == 1) {
			key += "rain";
		} else if (rtuStatusDTO.getIsRiver() != null && rtuStatusDTO.getIsRiver() == 1) {
			key += "river";
		} else if (rtuStatusDTO.getIsRes() != null && rtuStatusDTO.getIsRes() == 1) {
			key += "rsvr";
		} else if (rtuStatusDTO.getIsGround() != null && rtuStatusDTO.getIsGround() == 1) {
			key += "ground";
		} else {
			return false;
		}
		long duration = 90;
		LambdaQueryWrapper<RtuStatusEntity> querywrapper = Wrappers.<RtuStatusEntity>query().lambda();
		querywrapper.eq(RtuStatusEntity::getIsDeleted, 0);
		querywrapper.eq(RtuStatusEntity::getRtuCode, rtuStatusDTO.getRtuCode());
		RtuStatusEntity entity = this.getOne(querywrapper);
		if (null != entity) {
			Date lastUpTime = entity.getLastUpTime();
			if (null != lastUpTime) {
				Date tm = rtuStatusDTO.getLastUpTime();
				duration = (tm.getTime() - lastUpTime.getTime()) / 1000L / 60L;
				duration += 30;
			}
		}
		long hour = duration / 60L;
		ValueOperations<String, String> ops = redisTemplate.opsForValue();
		if (redisTemplate.hasKey(key)) {
			String hourSet = ops.get(key);
			long hourValue = Func.toLong(hourSet);
			hour = (hour + hourValue) / 2L;
			ops.set(key, Func.toStr(hour));
		} else {
			ops.set(key, Func.toStr(hour));
		}
		return true;
	}
}
