package com.kym.service.impl;

import com.github.yulichang.base.MPJBaseServiceImpl;
import com.kym.entity.FaultRecord;
import com.kym.mapper.FaultRecordMapper;
import com.kym.service.FaultRecordService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class FaultRecordServiceImpl extends MPJBaseServiceImpl<FaultRecordMapper, FaultRecord> implements FaultRecordService {

    @Override
    public FaultRecord getUnrecovered(String stationId, String deviceName, String faultType) {
        return lambdaQuery()
                .eq(FaultRecord::getStationId, stationId)
                .eq(FaultRecord::getDeviceName, deviceName)
                .eq(FaultRecord::getFaultType, faultType)
                .eq(FaultRecord::getIsRecovered, FaultRecord.IS_RECOVERED_未恢复)
                .orderByDesc(FaultRecord::getFaultTime)
                .last("limit 1")
                .one();
    }

    @Override
    public void markRecovered(Long id) {
        lambdaUpdate()
                .set(FaultRecord::getIsRecovered, FaultRecord.IS_RECOVERED_已恢复)
                .set(FaultRecord::getRecoverTime, LocalDateTime.now())
                .eq(FaultRecord::getId, id)
                .update();
    }

    @Override
    public void markNotified(Long id) {
        lambdaUpdate()
                .set(FaultRecord::getIsNotified, FaultRecord.IS_NOTIFIED_已通知)
                .set(FaultRecord::getNotifyTime, LocalDateTime.now())
                .eq(FaultRecord::getId, id)
                .update();
    }
}
