package com.njcn.device.pq.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.utils.PubUtils;
import com.njcn.device.pq.mapper.AlarmMapper;
import com.njcn.device.pq.pojo.param.AlarmParam;
import com.njcn.device.pq.pojo.param.TerminalCheckParam;
import com.njcn.device.pq.pojo.vo.TerminalAlarmVO;
import com.njcn.device.pq.pojo.vo.TerminalCheckVO;
import com.njcn.device.pq.service.TerminalAlarmService;
import com.njcn.web.factory.PageFactory;
import com.njcn.web.utils.RequestUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;

/**
 * pqs
 *
 * @author cdf
 * @date 2022/5/23
 */
@Service
@RequiredArgsConstructor
public class TerminalAlarmServiceImpl implements TerminalAlarmService {
    private final AlarmMapper alarmMapper;

    @Override
    public Page<TerminalAlarmVO> alarmList(AlarmParam alarmParam) {

       // String sql = "SELECT last(dev_line_type) as dev_line_type,line_id ,type  FROM pqs_alarm  group by type,line_id,dev_line_type";
        Page<TerminalAlarmVO> page = new Page<>(PageFactory.getPageNum(alarmParam),PageFactory.getPageSize(alarmParam));
        return alarmMapper.getAlarmInfo(page,PubUtils.beginTimeToLocalDateTime(alarmParam.getStartTime()),PubUtils.endTimeToLocalDateTime(alarmParam.getEndTime()),alarmParam.getFlag(),alarmParam.getType(),alarmParam.getAlarmLevel(),alarmParam.getLineGrade());
    }

    @Override
    public List<TerminalAlarmVO> alarmDetail(AlarmParam alarmParam) {
        LocalDateTime localDateTime = null;
        if(StrUtil.isNotBlank(alarmParam.getProcessTime())){
            localDateTime = PubUtils.localDateTimeFormat(alarmParam.getProcessTime());
        }
        return alarmMapper.getAlarmDetail(PubUtils.beginTimeToLocalDateTime(alarmParam.getStartTime()),PubUtils.endTimeToLocalDateTime(alarmParam.getEndTime()),alarmParam.getFlag(),alarmParam.getType(),alarmParam.getDevLineId(),localDateTime);
    }

    @Override
    public boolean updateAlarmInfo(String type, String devLineId) {
       return alarmMapper.updateAlarmInfo(type,devLineId,LocalDateTime.now(),RequestUtil.getUserIndex());
    }

    @Override
    public Page<TerminalCheckVO> terminalCheckPage(TerminalCheckParam terminalCheckParam) {
        Page<TerminalCheckVO> page = new Page<>(PageFactory.getPageNum(terminalCheckParam),PageFactory.getPageSize(terminalCheckParam));
        Page<TerminalCheckVO> pageResult = alarmMapper.terminalCheckPage(page,terminalCheckParam);
        return pageResult;
    }
}
