/**
 * 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.stereotype.Service;

import javax.annotation.Resource;

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

    @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::getRtuCode, rtuStatusEntity.getRtuCode());
        RtuStatusEntity entity = this.getOne(querywrapper);
        if (Func.notNull(entity)) {
            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::getRtuCode, rtuStatusEntity.getRtuCode());
        RtuStatusEntity entity = this.getOne(querywrapper);
        if (Func.notNull(entity)) {
            entity.setRunStatusId(rtuStatusEntity.getRunStatusId());
            return this.updateById(entity);
        } else {
            return this.save(rtuStatusEntity);
        }
    }

    @Override
    public Long rtuUpDataCount(RtuStatusDTO rtuStatusDTO) {
        return this.baseMapper.rtuUpDataCount(rtuStatusDTO);
    }
}
