package com.pqs9900.serviceImpl.DataInfo;

import com.pqs9900.mapper.DataInfo.MiddRunStateMapper;
import com.pqs9900.pojo.DataInfo.MiddRunState;
import com.pqs9900.service.DataInfo.MiddRunStateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author denghuajun
 * @Description //TODO 中间件状态表impl
 * @Date 2019/3/15 13:44
 **/
@Service
public class MiddRunStateServiceImpl implements MiddRunStateService {
    @Autowired
    private MiddRunStateMapper middRunStateMapper;

    @Override
    public List<MiddRunState> getMiddRunState(String middIndex, Date startTime, Date endTime) {
        List<MiddRunState> middRunStates = new ArrayList<>();
        List<MiddRunState> middRunState = new ArrayList<>();
        middRunStates = middRunStateMapper.getMiddRunState(middIndex, startTime, endTime);
        if (!middRunStates.isEmpty()) {
            for (int i = 0; i < middRunStates.size(); i++) {
                if (i < (middRunStates.size() - 1)) {
                    MiddRunState middRunState1 = new MiddRunState();
                    middRunState1.setMiddIndex(middRunStates.get(i).getMiddIndex());
                    middRunState1.setTimeId(middRunStates.get(i).getTimeId());
                    middRunState1.setState(middRunStates.get(i).getState());
                    middRunState.add(middRunState1);
                    if (middRunStates.get(i).getState() != middRunStates.get(i + 1).getState()) {
                        MiddRunState middRunState2 = new MiddRunState();
                        middRunState2.setMiddIndex(middRunStates.get(i).getMiddIndex());
                        middRunState2.setTimeId(middRunStates.get(i+1).getTimeId());
                        middRunState2.setState(middRunStates.get(i).getState());
                        middRunState.add(middRunState2);
                    }
                }else {
                    MiddRunState middRunState3 = new MiddRunState();
                    middRunState3.setMiddIndex(middRunStates.get(i).getMiddIndex());
                    middRunState3.setTimeId(middRunStates.get(i).getTimeId());
                    middRunState3.setState(middRunStates.get(i).getState());
                    middRunState.add(middRunState3);
                }
            }
        } else {
            return middRunState;
        }
        return middRunState;
    }
}
