package com.njcn.serviceimpl.data;

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

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.njcn.mapper.data.GridTopMapper;
import com.njcn.pojo.data.GridTop;
import com.njcn.sso.pojo.user.User;
import com.njcn.service.data.IImportGirdTopService;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.ReadExcel;
import com.njcn.shiro.token.TokenManager;
@Service
public class ImportGridTopServiceImpl implements IImportGirdTopService {
	@Resource
	UserLogDetailService userLog;
	@Resource
	GridTopMapper gtm;
	@Override
	public List<List> getGirdTop(String vollevel) {
		// TODO Auto-generated method stub
		List<List> lists=new ArrayList<List>();
		List<GridTop> gridTops = gtm.getGirdTop(vollevel);
		if(!gridTops.isEmpty()){
			for (GridTop gridTop : gridTops) {
				List<String> str=new ArrayList<String>();
				str.add(gridTop.getSequen().toString());//序号
				int var=gridTop.getVolLevel();
				if(var==0){
					str.add("220kV");//电压等级
				}else if(var==1){
					str.add("110kV");//电压等级
				}else if(var==2){
					str.add("10kV");//电压等级
				}
				str.add(gridTop.getLongItude().toString());//经度
				str.add(gridTop.getLatItude().toString());//纬度
				lists.add(str);
			}
		}
		return lists;
	}
	@Override
	public int readExcelFile(MultipartFile file,int volLevel) {
		int falt=0;
		// TODO Auto-generated method stub  
        //创建处理EXCEL的类  
        ReadExcel readExcel=new ReadExcel();  
        //解析excel，获取上传的事件单  
        List<GridTop> useList = readExcel.getExcelInfo(file,volLevel);
        GridTop gridTop=new GridTop();
        int falts=gtm.getMaxSequen(volLevel);
        //至此已经将excel中的数据转换到list里面了,接下来就可以操作list,可以进行保存到数据库,或者其他操作,
        for (int i = 0;i<useList.size();i++){
        	gridTop.setGridTopIndex(useList.get(i).getGridTopIndex());
        	gridTop.setLongItude(useList.get(i).getLongItude());
        	gridTop.setLatItude(useList.get(i).getLatItude());
        	falts+=1;
        	gridTop.setSequen(falts);
        	gridTop.setVolLevel(useList.get(i).getVolLevel());
        	gridTop.setState(1);
        	gridTop.setUpdatetime(new Date());
        	User user=TokenManager.getToken();
        	gridTop.setUserIndex(user.getUserIndex());
        	try {
        		gtm.insert(gridTop);
        		falt=1;
			} catch (Exception e) {
				falt=0;
			}
        	
        }
        //和你具体业务有关,这里不做具体的示范  
        return falt;
	}
}
