package com.shining.cloud.serviceImpl.report;

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

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.njcn.pojo.commons.AreaGeneralData;
import com.njcn.pojo.configuration.GDInformation;
import com.njcn.pojo.configuration.Line;
import com.njcn.pojo.configuration.Province;
import com.njcn.pojo.configuration.SubStation;
import com.njcn.utils.UserUtil;
import com.shining.cloud.mapper.report.ShiningDeviceMapper;
import com.shining.cloud.service.report.ShiningDeviceService;

@Service
public class ShiningDeviceServiceImpl implements ShiningDeviceService {
	@Resource
	private UserUtil userUtil;
	
	@Resource
	private ShiningDeviceMapper shiningDeviceMapper;

	public List<Integer> listLine(String userId){
		List<Integer> listLine = new ArrayList<>();
		List<AreaGeneralData> listAreaGeneralData = userUtil.getAppAreaGeneral(userId);
        if (CollectionUtils.isEmpty(listAreaGeneralData)) {
            return listLine;
        }
        for (AreaGeneralData areaGeneralData : listAreaGeneralData) {
            listLine.addAll(areaGeneralData.getLineIndexs());
        }
        return listLine;
	}
	
	@Override
	public List<Province> getProvince(String userId){
		List<Province> pro = new ArrayList<>();
		List<Integer> lineIndexs = listLine(userId);
		
		pro = shiningDeviceMapper.getProvince(lineIndexs);
		return pro;
	}

	@Override
	public List<GDInformation> getGdInfo(String userId,Long provinceIndex){
		List<GDInformation> gd = new ArrayList<>();
		List<Integer> lineIndexs = listLine(userId);
		
		gd = shiningDeviceMapper.getGdInfo(lineIndexs,provinceIndex);
		
		return gd;
	}

	@Override
	public List<SubStation> getSubInfo(String userId,Long gdIndex){
		List<SubStation> sub = new ArrayList<>();
		List<Integer> lineIndexs = listLine(userId);
		
		sub = shiningDeviceMapper.getSubInfo(lineIndexs,gdIndex);
		
		return sub;
	}

	@Override
	public List<Line> getLineInfo(String userId,Long subIndex,int type){
		List<Line> line = new ArrayList<>();
		List<Integer> lineIndexs = listLine(userId);
		
    	Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        Date endTime = calendar.getTime();
        
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.add(Calendar.MONTH,0);
        Date startTime = calendar.getTime();
        
		switch (type) {
		case 0:
			line = shiningDeviceMapper.getLineInfo(lineIndexs,subIndex);
			break;
		case 1:
			line = shiningDeviceMapper.getLineInfoStatus(lineIndexs,subIndex);
			break;
		default:
			line = shiningDeviceMapper.getLineInfoFlowA(lineIndexs,subIndex);
			List<Line> lineb = shiningDeviceMapper.getLineInfoFlowB(lineIndexs,subIndex,startTime,endTime);
			line.addAll(lineb);
			break;
		}
		
		return line;
	}
	
	@Override
	public List<Line> queryByName(String userId,String lineName){
		List<Integer> lineIndexs = listLine(userId);
		
		 List<Line> line ;
		 lineName = "%"+lineName+"%"; 
		 line = shiningDeviceMapper.queryByName(lineName,lineIndexs);
		 
		 return line;
	}
	
}