package cn.com.countrygarden.bi.main.service.personnel.impl;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.dup.DupOrgan;
import cn.com.countrygarden.bi.main.model.test.Test;
import cn.com.countrygarden.bi.main.service.project.ProjectJointPlanService;
import cn.com.countrygarden.bi.main.service.test.TestService;
import sun.security.rsa.RSAPadding;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import static org.hamcrest.CoreMatchers.nullValue;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.sys.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import cn.com.countrygarden.bi.main.model.personnel.Personnel;
import cn.com.countrygarden.bi.main.model.personnel.PersonnelAdvisor;
import cn.com.countrygarden.bi.main.model.personnel.PersonnelTag;
import cn.com.countrygarden.bi.main.dao.performance.bdpsndocMapper;
import cn.com.countrygarden.bi.main.dao.personnel.PersonnelAdvisorMapper;
import cn.com.countrygarden.bi.main.dao.personnel.PersonnelCampusevaluationMapper;
import cn.com.countrygarden.bi.main.dao.personnel.PersonnelMapper;
import cn.com.countrygarden.bi.main.dao.personnel.PersonnelTagMapper;
import cn.com.countrygarden.bi.main.dto.PersonInfoVO;
import cn.com.countrygarden.bi.main.dto.PersonnelAdvisorDTO;
import cn.com.countrygarden.bi.main.dto.PersonnelAdvisorlistDTO;
import cn.com.countrygarden.bi.main.dto.PersonnelAdvisoroneidDTO;
import cn.com.countrygarden.bi.main.dto.PersonnelTgDTO;
import cn.com.countrygarden.bi.main.dto.personnelTagDTO;
import cn.com.countrygarden.bi.main.dto.personnel.PersonExaminationDTO;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.service.personnel.PersonnelAdvisorService;
import cn.com.countrygarden.bi.main.service.personnel.PersonnelCommenService;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;
import cn.com.countrygarden.bi.main.core.Response;

/**
 * 导师信息-业务逻辑层实现
 *
 * @author 吕伟峰
 * @email www.justintoforest@gamail.com
 * @url 
 * @date 2018-09-27
 */
@Service
@Transactional
public class PersonnelAdvisorServiceImpl implements PersonnelAdvisorService{
	@Autowired
	private bdpsndocMapper bdpsndocMapper;
	
    @Autowired
    private PersonnelAdvisorMapper personnelAdvisorMapper;
    
    @Autowired
    private PersonnelMapper personnelMapper;
    
    @Autowired
    private PersonnelCampusevaluationMapper personnelCampusevaluationMapper;
    
    @Autowired
    private PersonnelTagMapper personnelTagMapper;
    
    @Autowired
    private PersonnelCommenService personnelCommenService;

    @Override
    public Result queryPageList(PersonnelAdvisorDTO personnelAdvisor, int page, int size,AccessToken accessToken) {
    	
//    	List<String> userIds= personnelCommenService.findRole(accessToken);
//		if(userIds!=null) {
//			if(userIds.size()==0||userIds.get(0).equals("-1")) {
//				//没有匹配的角色或没有查找到数据
//				return Response.response();
//			}		
//		}
    	Map<String, Object> rolesMap = personnelCommenService.findRoleInfoMap(accessToken.getUserCode());    	
    	List<String> userquyu=new ArrayList<>();
    	List<DupOrgan> result=null;
    	for (Object obj : rolesMap.keySet()) {
    		String key = (String) obj;
    		if (key.equals("gj_admin")) {
    			 userquyu=null;
    			 
    			 
    			 rolesMap.put("identitys", null);
    			 break;
    		}else if (key.equals("role")) {
    			 return null;
			}else if (key.equals("gj_Area")) {
				//List<String> liuserquyu=personnelCommenService.findErpRegionByUserCode(accessToken.getUserCode());
				//userquyu=liuserquyu.get(0);
				rolesMap.put("identitys", null);
				
				 result = personnelCommenService.findDupCommByUserCode(accessToken.getUserCode(),null,true);
				 for (int i = 0; i < result.size(); i++) {
						userquyu.add(result.get(i).getOrganName());
					}
			}
    		
    	}
    	
//    	List<PersonnelTgDTO> dtoper=new ArrayList<PersonnelTgDTO>();
//    	Page<Object> pageList = PageHelper.startPage(page, size);
//      	if (result!=null&&result.size()!=0) {
//      		for (int i = 0; i < result.size(); i++) {
//      			List<PersonnelTgDTO> persapper=personnelAdvisorMapper.findExistWrittenexamination(personnelAdvisor,rolesMap,result.get(i).getOrganName());
//      			if (persapper.size()!=0) {
//      				dtoper.addAll(persapper);
//  				}    		
//      		}
//  		}else {
//  			dtoper = personnelAdvisorMapper.findExistWrittenexamination(personnelAdvisor,rolesMap,null);
//  		}
      	if (personnelAdvisor.getRegionName()!=null&&!personnelAdvisor.getRegionName().equals("")&&personnelAdvisor.getUserId()!=null&&!personnelAdvisor.getUserId().equals("")
      			&&personnelAdvisor.getName()!=null&&!personnelAdvisor.getName().equals("")) {
      		
      		userquyu=null;
		}
    	
    	Page<Object> pageList = PageHelper.startPage(page, size);
    	List<PersonnelTgDTO> personals =  personnelAdvisorMapper.findExistWrittenexamination(personnelAdvisor,rolesMap,userquyu);
    	List<PersonExaminationDTO<PersonnelAdvisorDTO>> results=new ArrayList<>(personals.size());
    	
    	for (PersonnelTgDTO personnel : personals) {
    		PersonnelAdvisorDTO pe=new PersonnelAdvisorDTO();
    		pe.setPersonnelId(personnel.getId());
    		pe.setPersonneltagId(personnel.getPersonneltagId());
    		pe.setRegionName(personnel.getRegionName());
    		//获取职业导师
			List<PersonnelAdvisorDTO> PersonnelAdvisorType1=personnelAdvisorMapper.queryPageList(pe);
			//辅导员
			List<PersonnelAdvisorDTO> PersonnelAdvisorType2=personnelAdvisorMapper.queryPageListadnamelist(pe);
			
			//构造用户数据
			results.add(new PersonExaminationDTO<PersonnelAdvisorDTO>(personnel,PersonnelAdvisorType1,PersonnelAdvisorType2));
			
    	}
    	  		
        PageData data=new PageData(pageList);
        data.setData(results);
       
        return Response.response(data);
    }
    
    @Override
	public Result queryPageListadnamelist(PersonnelAdvisorDTO personnelAdvisor) {
    	List<PersonnelAdvisorDTO> querdnamelist=personnelAdvisorMapper.queryPageListadnamelist(personnelAdvisor);
    	return Response.response(querdnamelist);
	}

    @Override
    public Result queryById(Integer id) {
    	
    	Personnel per=personnelMapper.peroneid(id);
    	
    	List<PersonnelAdvisorDTO> persoAdvisordto=personnelAdvisorMapper.queryById(id);
    	
    	//List<PersonnelAdvisoroneidDTO> peroneiddto=new ArrayList<PersonnelAdvisoroneidDTO>();
    	PersonnelAdvisoroneidDTO peroneiddto=new PersonnelAdvisoroneidDTO();
    	peroneiddto.setPeid(per.getId());
    	peroneiddto.setUserId(per.getUserId());
    	peroneiddto.setName(per.getName());
    	
    	peroneiddto.setPersonnelAdvs(persoAdvisordto);
    	
    	
        return Response.response(peroneiddto);
    }

    @Override
    public Result add(String personadvisor, AccessToken accessToken) {
    	
    	DateFormat dFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //HH表示24小时制；
    	Date dateAdditionTime=null;
    	Date dateStartTime=null;
    	Date dateTerminalTime=null;
    	Date datanormalTime=null;
    	Date datafixedTime=null;
    	String personadvisorpe="["+personadvisor+"]";   
    	
    	PersonnelAdvisorDTO Personnadvisdto=null;
    
    	JSONArray jsonArray=JSON.parseArray(personadvisorpe);	
    	
    	if(jsonArray.size()!=0) {
    		for (int i = 0; i < jsonArray.size(); i++) {
    			 Personnadvisdto=new PersonnelAdvisorDTO();
      		      JSONObject jsonObject=jsonArray.getJSONObject(i);
      		    Personnadvisdto.setPersonneltagId(jsonObject.getInteger("personneltagId"));
       		   Personnadvisdto.setPersonnelTypeId(jsonObject.getInteger("personnelTypeId"));
       		   Personnadvisdto.setPersonnelId(jsonObject.getInteger("personnelId"));
      		    personnelAdvisorMapper.deletepeadpead(Personnadvisdto.getPersonnelId(),Personnadvisdto.getPersonnelTypeId(),Personnadvisdto.getPersonneltagId());	   
    		}
    	}
    	
    	
    	if(jsonArray.size()!=0) {
    	   for (int i = 0; i < jsonArray.size(); i++) {
    		   Personnadvisdto=new PersonnelAdvisorDTO();
    		   JSONObject jsonObject=jsonArray.getJSONObject(i);
    		 
    		   Personnadvisdto.setUserId(jsonObject.getString("userId"));
    		   Personnadvisdto.setPersonnelTypeId(jsonObject.getInteger("personnelTypeId"));
    		   Personnadvisdto.setPersonnelId(jsonObject.getInteger("personnelId"));
    		   Personnadvisdto.setPersonneltagId(jsonObject.getInteger("personneltagId"));
    		  
    		  
    			   JSONArray jsonArraylist=JSON.parseArray(jsonObject.getString("List"));
    			   for (int j = 0; j < jsonArraylist.size(); j++) {
    				   JSONObject jsonObjectpe=jsonArraylist.getJSONObject(j);
    				  
    				   try {
    					   //dateAdditionTime=dFormat.parse(jsonObjectpe.getString("additionTime"));
    						 Personnadvisdto.setAdditionTime(dateAdditionTime);
    					   dateStartTime=dFormat.parse(jsonObjectpe.getString("startTime"));
    					   if(StringUtils.isNotBlank(jsonObjectpe.getString("terminalTime"))) {
    						   dateTerminalTime=dFormat.parse(jsonObjectpe.getString("terminalTime"));
    					   }
    					   
    					   if (StringUtils.isNotBlank(jsonObjectpe.getString("normalTime"))) {
    						   datanormalTime=dFormat.parse(jsonObjectpe.getString("normalTime"));
						   }
    					   
    					   if (StringUtils.isNotBlank(jsonObjectpe.getString("fixedTime"))) {
    						   datafixedTime=dFormat.parse(jsonObjectpe.getString("fixedTime"));
						   }
    					   
    					   Personnadvisdto.setStartTime(dateStartTime);
    					   Personnadvisdto.setTerminalTime(dateTerminalTime);
    					   Personnadvisdto.setNormalTime(datanormalTime);
    					   Personnadvisdto.setFixedTime(datafixedTime);
    					} catch (ParseException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				   Personnadvisdto.setAdvisorUserId(jsonObjectpe.getString("advisorUserId"));
    				   Personnadvisdto.setAdvisorUserName(jsonObjectpe.getString("advisorUserName"));
    				   Personnadvisdto.setMentorpost(jsonObjectpe.getString("mentorpost"));
    				   Personnadvisdto.setPositivevaluation(jsonObjectpe.getString("positivevaluation"));
    				   Personnadvisdto.setFixedpostevaluation(jsonObjectpe.getString("fixedpostevaluation"));    				   
    				    Personnadvisdto.setPositivescore(jsonObjectpe.getString("positivescore")); 					   
    					Personnadvisdto.setFixedscore(jsonObjectpe.getString("fixedscore"));					   
    				   Personnadvisdto.setFilesame(jsonObjectpe.getString("filesame"));
    				   Personnadvisdto.setFiles(jsonObjectpe.getString("files"));
    				   Personnadvisdto.setFlag(0);    				   
    				   Personnadvisdto.setCreatedName(jsonObjectpe.getString("createdName"));
    				   Personnadvisdto.setCreatedBy(accessToken.getUserCode());
    				   Personnadvisdto.setCreatedDate(new Date());
    				   personnelAdvisorMapper.insertadd(Personnadvisdto);
    				  
			   }
    		   
		   }
    	 
    	}    	
        return Response.response();
    }

    @Override
    public Result edit(String personnelAdvisor, AccessToken accessToken) {
    	
    	DateFormat dFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //HH表示24小时制；
    	Date dateAdditionTime=null;
    	Date dateStartTime=null;
    	Date dateTerminalTime=null;
    	Date datanormalTime=null;
    	Date datafixedTime=null;
    	String personadvisorpe="["+personnelAdvisor+"]";
    	PersonnelAdvisorDTO Personnadvisdto=null;
    	
    	JSONArray jsonArray=JSON.parseArray(personadvisorpe);	
    	
    	if(jsonArray.size()!=0) {
    		for (int i = 0; i < jsonArray.size(); i++) {
    			 Personnadvisdto=new PersonnelAdvisorDTO();
      		      JSONObject jsonObject=jsonArray.getJSONObject(i);
      		    Personnadvisdto.setPersonneltagId(jsonObject.getInteger("personneltagId"));
       		   Personnadvisdto.setPersonnelTypeId(jsonObject.getInteger("personnelTypeId"));
       		   Personnadvisdto.setPersonnelId(jsonObject.getInteger("personnelId"));
      		    personnelAdvisorMapper.deletepeadpead(Personnadvisdto.getPersonnelId(),Personnadvisdto.getPersonnelTypeId(),Personnadvisdto.getPersonneltagId());	   
    		}
    	}
    	
    	
    	if(jsonArray.size()!=0) {
    		for (int i = 0; i < jsonArray.size(); i++) {
    			 Personnadvisdto=new PersonnelAdvisorDTO();
      		   JSONObject jsonObject=jsonArray.getJSONObject(i);
      		   Personnadvisdto.setUserId(jsonObject.getString("userId"));
      		   Personnadvisdto.setPersonnelTypeId(jsonObject.getInteger("personnelTypeId"));
      		 Personnadvisdto.setPersonnelId(jsonObject.getInteger("personnelId"));
      		Personnadvisdto.setPersonneltagId(jsonObject.getInteger("personneltagId"));     		
      		
      		  JSONArray jsonArraylist=JSON.parseArray(jsonObject.getString("List"));
			       for (int j = 0; j < jsonArraylist.size(); j++) {
			    	   JSONObject jsonObjectpe=jsonArraylist.getJSONObject(j);
			    	   
			    	   try {
			    		  // dateAdditionTime=dFormat.parse(jsonObjectpe.getString("additionTime"));
							Personnadvisdto.setAdditionTime(dateAdditionTime);
    					   dateStartTime=dFormat.parse(jsonObjectpe.getString("startTime"));
    					   if(StringUtils.isNotBlank(jsonObjectpe.getString("terminalTime"))) {
    						   dateTerminalTime=dFormat.parse(jsonObjectpe.getString("terminalTime"));
    					   }
    					   if (StringUtils.isNotBlank(jsonObjectpe.getString("normalTime "))) {
    						   datanormalTime=dFormat.parse(jsonObjectpe.getString("normalTime"));
						   }
    					   if (StringUtils.isNotBlank(jsonObjectpe.getString("fixedTime"))) {
    						   datafixedTime=dFormat.parse(jsonObjectpe.getString("fixedTime"));
						   }
    					   Personnadvisdto.setStartTime(dateStartTime);
    					   Personnadvisdto.setTerminalTime(dateTerminalTime);
    					   Personnadvisdto.setNormalTime(datanormalTime);
    					   Personnadvisdto.setFixedTime(datafixedTime);
    					} catch (ParseException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
			    	  
    				   Personnadvisdto.setAdvisorUserId(jsonObjectpe.getString("advisorUserId"));
    				   Personnadvisdto.setAdvisorUserName(jsonObjectpe.getString("advisorUserName"));
    				   Personnadvisdto.setMentorpost(jsonObjectpe.getString("mentorpost"));
    				   Personnadvisdto.setPositivevaluation(jsonObjectpe.getString("positivevaluation"));
    				   Personnadvisdto.setFixedpostevaluation(jsonObjectpe.getString("fixedpostevaluation"));
    				   Personnadvisdto.setPositivescore(jsonObjectpe.getString("positivescore")); 					   
   					   Personnadvisdto.setFixedscore(jsonObjectpe.getString("fixedscore"));	
    				   Personnadvisdto.setFilesame(jsonObjectpe.getString("filesame"));
    				   Personnadvisdto.setFiles(jsonObjectpe.getString("files"));
    				   Personnadvisdto.setFlag(0);
    				   Personnadvisdto.setAdditionTime(dateAdditionTime);
    				   Personnadvisdto.setCreatedName(jsonObjectpe.getString("createdName"));
    				   Personnadvisdto.setCreatedBy(accessToken.getUserCode());
    				   Personnadvisdto.setCreatedDate(new Date());
    				   personnelAdvisorMapper.insertadd(Personnadvisdto);
			    	   
			       }
    			
    		}
    		
    	}

        return Response.response();
    }

    @Override
    public Result delperid(Integer personnelId,Integer personneltagId) {
        int count = personnelAdvisorMapper.deletepead(personnelId,personneltagId);
        return Response.response(count);
    }

	@Override
	public Result peradvisorlist(PersonnelAdvisor personnelAdvisor) {
		
		List<PersonnelAdvisorDTO> peadvlist=personnelAdvisorMapper.peradvisorlist(personnelAdvisor);
		
		return Response.response(peadvlist);
	}

	@Override
public Result queryadByUid(String search,AccessToken accessToken) {
		

//		List<String> userIds= personnelCommenService.findRole(accessToken);
//		if(userIds!=null) {
//			if(userIds.size()==0||userIds.get(0).equals("-1")) {
//				//没有匹配的角色或没有查找到数据
//				return Response.response();
//			}		
//		}
		Map<String, Object> rolesMap = personnelCommenService.findRoleInfoMap(accessToken.getUserCode());
        List<String> userquyu=new ArrayList<>();
        List<DupOrgan> resuaslt=null;
    	for (Object obj : rolesMap.keySet()) {
    		String key = (String) obj;
    		if (key.equals("gj_admin")) {
    			 userquyu=null;    			
    			 break;
    		}else if (key.equals("role")) {
    			return null;
			}else if (key.equals("gj_Area")) {
//				List<String> liuserquyu=personnelCommenService.findErpRegionByUserCode(accessToken.getUserCode());
//				userquyu=liuserquyu.get(0);
				resuaslt = personnelCommenService.findDupCommByUserCode(accessToken.getUserCode(),null,true);
				for (int i = 0; i < resuaslt.size(); i++) {
					userquyu.add(resuaslt.get(i).getOrganName());
				}
			}
    		
    	}
//    	List<PersonInfoVO> dtoper=new ArrayList<PersonInfoVO>();
//    	if (resuaslt!=null&&resuaslt.size()!=0) {
//    		for (int i = 0; i < resuaslt.size(); i++) {
//    			List<PersonInfoVO> persapper=personnelAdvisorMapper.selectByPersonInfoByCodeOrName(search,null,resuaslt.get(i).getOrganName());
//    				dtoper.addAll(persapper);
//				}    		
//    		}else {
//			dtoper = personnelAdvisorMapper.selectByPersonInfoByCodeOrName(search,null,null);
//		}

		List<PersonInfoVO> personInfoCodeOrId=personnelAdvisorMapper.selectByPersonInfoByCodeOrName(search,null,userquyu);

		 List<String> result=new LinkedList<>();
		 for (PersonInfoVO psndoc : personInfoCodeOrId) {
			 result.add(psndoc.getCode()+" - "+psndoc.getWorkdept()+" - "+psndoc.getName());
		}
		
		 return Response.response(result);
	}

	@Override
	public void excelToxls(Integer radio,String usname,MultipartFile multipartFile, String fileName, String userCode,AccessToken accessToken) {
		 Workbook xwb = null;
	       BufferedInputStream bf = null;
	       Sheet sheet = null;
	    	//List<String> liuserquyu=personnelCommenService.findErpRegionByUserCode(accessToken.getUserCode());

	       try {
	      	 CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile;
	           bf = new BufferedInputStream(commonsMultipartFile.getInputStream());
	           //String fileName = commonsMultipartFile.getOriginalFilename();
	           if (fileName.endsWith("xls")) { // Excel&nbsp;2003
	               xwb = new HSSFWorkbook(bf);
	           } else if (fileName.endsWith("xlsx")) { // Excel 2007/2010
	               xwb = new XSSFWorkbook(bf);
	           }
	           sheet = xwb.getSheetAt(0);//获取第几个页面0开始
	           Row row =null;
	           Row rowpe =null;
	          	           
	           XSSFCell cell;
	           PersonnelAdvisor persoadviso=null;
	           Personnel personnel=null;
	           PersonnelTag personnelTag=null;	          
	           SimpleDateFormat dFormat = new SimpleDateFormat("yyyy/MM/dd");
	       	   Date dAdditionTime=null;
	       	   Date dstartTime=null;
	       	   Date dterminalTime=null;
	           for (int a = 0; a < sheet.getPhysicalNumberOfRows(); a++) { 
	        	   rowpe = sheet.getRow(a+3);
	        	       if(rowpe==null) {
	            	        break;
	                   }
	        	       for(int b =0;b<sheet.getRow(a+1).getPhysicalNumberOfCells();b++) {
	        	    	   cell = (XSSFCell) rowpe.getCell(0);
	        	    	   if(StringUtils.isBlank(cell.toString())) {
	        	    		   break;
	        	    	   }
	        			   cell.setCellType(CellType.STRING);	        			   
	        			   String urid=cell.getStringCellValue();
	        			   
	        			   cell = (XSSFCell) rowpe.getCell(1);
	        			   cell.setCellType(CellType.STRING);	        			   
	        			   String name=cell.getStringCellValue();
	        			   
	        			   
	        			   cell = (XSSFCell) rowpe.getCell(2);
	        			   cell.setCellType(CellType.STRING);	        			   
	        			   String identity=cell.getStringCellValue();
	        			   
	        			   cell = (XSSFCell) rowpe.getCell(3);
	        			   //cell.setCellType(CellType.STRING);	        			   
//	        			   String additionTime=cell.getStringCellValue();
	        			   
	        			   cell = (XSSFCell) rowpe.getCell(4);
	        			   cell.setCellType(CellType.STRING);	        		   
	        			   String advisorType=cell.getStringCellValue();
	        			   
	        			   cell = (XSSFCell) rowpe.getCell(5);
	        			   cell.setCellType(CellType.STRING);	        		   
	        			   String advisorUserId=cell.getStringCellValue();
	        			   
	        			   cell = (XSSFCell) rowpe.getCell(6);
	        			   cell.setCellType(CellType.STRING);	        		   
	        			   String advisorUserName=cell.getStringCellValue();
	        			   
	        			   cell = (XSSFCell) rowpe.getCell(7);
	        			   //cell.setCellType(CellType.STRING);	        		   
	        			   //String startTime=cell.getStringCellValue();
	        			   
	        			   cell = (XSSFCell) rowpe.getCell(8);
	        			   //cell.setCellType(CellType.STRING);	        		   
	        			   //String terminalTime=cell.getStringCellValue();
	        			   
	        			   cell = (XSSFCell) rowpe.getCell(9);
	        			   cell.setCellType(CellType.STRING);	        		   
	        			   String positivevaluation=cell.getStringCellValue();
	        			   
	        			   cell = (XSSFCell) rowpe.getCell(10);
	        			   cell.setCellType(CellType.STRING);	        		   
	        			   String fixedpostevaluation=cell.getStringCellValue();
	        			   
//	        			   List<String> userIds= personnelCommenService.findRole(accessToken);
//	        				if(userIds!=null) {
//	        					if(userIds.size()==0||userIds.get(0).equals("-1")) {
//	        						throw new ServiceException("没有权限添加");
//	        						//没有匹配的角色或没有查找到数据
//	        						//return Response.response();
//	        						//throw new ServiceException("第"+(a+3)+"行"+"用户编号："+rowpe.getCell(0).getStringCellValue()+",没有权限添加");	       
//	        					}		
//	        				}
	        			// Map<String, Object> rolesMap = personnelCommenService.findRoleInfoMap(accessToken.getUserCode());
	        			   
//	        			   String userquyu=liuserquyu.get(0);
//	        			  String findboolean=personnelCommenService.findboolean(accessToken,urid,null,userquyu);
//	        			  if (findboolean.equals("role")) {                	
//	        				  throw new ServiceException("添加此用户编号："+urid+",权限不足，请检查确认好再导入");                
//	         			
//	  				       }
//	        			  	        			  
//	        			  if (findboolean.equals("gj_admin")) {
//	  						userquyu=null;
//	  							  						
//	  					}else if (findboolean.equals("gj_Area")) {                      	
//	  						 userquyu=liuserquyu.get(0);
//	                      	
//	  					}else if(personnelCommenService.findboolean(accessToken,null,identity,null).equals("identitys")){
//	  						userquyu=null;	  						
//	  					}
	                      Map<String, Object> rolesMap = personnelCommenService.findRoleInfoMap(accessToken.getUserCode());
	                      List<String> userquyu=new ArrayList<>();
	                     
	                      List<DupOrgan> result=null;
	                      	for (Object obj : rolesMap.keySet()) {
	                      		String key = (String) obj;
	                      		if (key.equals("gj_admin")) {
	                      			List<PersonInfoVO> personInfoCodeOrId=bdpsndocMapper.selectByPersonInfoByCodeOrName(urid,null,null);
	                      			if (personInfoCodeOrId.size()==0) {
	                       				throw new ServiceException("用户编号" + urid + "有误或权限不足，无法添加");
	    							}
	                      			
	                      			 rolesMap.put("identitys", null);
	                      			 break;
	                      		}else if (key.equals("role")) {
	                      			throw new ServiceException("当前账号权限不足，请检查确认好再导入");                
	                  			}else if (key.equals("gj_Area")) {
	                  				
	                  				result = personnelCommenService.findDupCommByUserCode(accessToken.getUserCode(),null,true);
	                  				for (int i = 0; i < result.size(); i++) {
	            						userquyu.add(result.get(i).getOrganName());
	            					}
	                  				List<PersonInfoVO> personInfoCodeOrId=bdpsndocMapper.selectByPersonInfoByCodeOrName(urid,null,userquyu);
		                       		if (personInfoCodeOrId.size()==0) {
		                       		 throw new ServiceException("用户编号" + urid + "有误或权限不足，无法添加");
									} 
		    				
	                  			}
	                      		
	                      	}
	                    	
	                   
	                      
	        			   List<PersonInfoVO> personoad=personnelAdvisorMapper.selectByPersonInfoByCodeOrName(advisorUserId,null,null);
	        			   if(personoad.size()<=0) {	        				  
	        				   throw new ServiceException("第"+(a+4)+"行"+"导师编码："+rowpe.getCell(5).getStringCellValue()+",有误，请检查确认好再导入");	        				   
	        			   }
	        			   List<PersonnelTgDTO> personneltag=personnelCampusevaluationMapper.personneltag(urid,identity);
	        			   Personnel persoel=personnelAdvisorMapper.perone(urid);
	        			   PersonnelAdvisorDTO persAdvisDTO=personnelAdvisorMapper.peaduserid(urid,advisorUserId,positivevaluation,fixedpostevaluation);
	        			   
	        			   if(personneltag.size()==0) {
	        				   throw new ServiceException("第"+(a+4)+"行"+"用户编号："+rowpe.getCell(0).getStringCellValue()+"的标签"+ rowpe.getCell(2).getStringCellValue()+",不存在,请检查确认好再导入");
	        			   }
	        			   
	        			   try {
	        					 dAdditionTime=dFormat.parse(getDate((XSSFCell) rowpe.getCell(3)));
	        					 dstartTime=dFormat.parse(getDate((XSSFCell) rowpe.getCell(7)));
	        					 if (getDate((XSSFCell) rowpe.getCell(8)).equals("")||getDate((XSSFCell) rowpe.getCell(8)).equals("至今")) {
	        						 dterminalTime=null;
								}else {
									dterminalTime=dFormat.parse(getDate((XSSFCell) rowpe.getCell(8)));
								}
	        				} catch (ParseException e) {
	        					throw new ServiceException("第" + (a+4) + "行时间格式不符合规范，导入失败,请按(yyyy/MM/dd)方式填写日期，或者把xlsx单元格设置日期类型");
	        					//e.printStackTrace();
	        				}
	        			   
	        			   if(radio==0) {
	        				   personnelTag=new PersonnelTag();
        					   personnelTag.setId(personneltag.get(0).getPersonneltagId());
//	        				   if(personneltag.size()>0) {
//	        					   personnelTag=new PersonnelTag();
//	        					   personnelTag.setId(personneltag.get(0).getPersonneltagId());
//	        				   }else {
//	        					   personnelTag=new PersonnelTag();
//            				       personnelTag.setUserId(urid);
//            				       personnelTag.setIdentity(identity);
//            				       personnelTag.setCreatedBy(userCode);            				       
//            				       personnelTagMapper.insertSelective(personnelTag);
//							    }
//	        					   if(persoel==null){
//		        					   personnel=new Personnel();
//	                				   personnel.setUserId(urid);
//	                				   personnel.setName(persono.get(0).getName());
//	                				   personnel.setCreatedBy(userCode);               				  
//	                				   personnelMapper.insertSelective(personnel);
//	                				
//		        				   }
							    									
	        			        
        				       
	        				   if (persAdvisDTO==null) {
		        				   persoadviso=new PersonnelAdvisor();
		        				   if(persoel!=null) {
		        					   persoadviso.setPersonnelId(persoel.getId());
		        				   }
		        				   
//		        				   else {
//		        					   persoadviso.setPersonnelId(personnel.getId());
//								    }
		        				   if(advisorType.equals("职业导航师")||advisorType.equals("导航师")) {
		        					   persoadviso.setPersonnelTypeId(1);
		        				   }else {
		        					   persoadviso.setPersonnelTypeId(2);
								   }
		        				   
		        				   persoadviso.setAdvisorUserId(advisorUserId);
		        				   persoadviso.setPersonneltagId(personnelTag.getId());
		        				   persoadviso.setAdvisorUserName(advisorUserName);
		        				   persoadviso.setPositivevaluation(positivevaluation);
		        				   persoadviso.setMentorpost(personoad.get(0).getWorkdept());
		        				   persoadviso.setFixedpostevaluation(fixedpostevaluation);
		        				   persoadviso.setFlag(0);
		        				   persoadviso.setAdditionTime(dAdditionTime);
		        				   persoadviso.setStartTime(dstartTime);
		        				   persoadviso.setTerminalTime(dterminalTime);
		        				   persoadviso.setCreatedBy(userCode);
		        				   persoadviso.setCreatedName(usname);
		        				   personnelAdvisorMapper.insertSelective(persoadviso);
		        				   	      				   
							     }
	        				   
	        			    }else if(radio==1){
	        			    	 personnelTag=new PersonnelTag();
	        					   personnelTag.setId(personneltag.get(0).getPersonneltagId());
//	        				   if(personneltag.size()>0) {
//		        				   personnelTag=new PersonnelTag();
//	        					   personnelTag.setId(personneltag.get(0).getPersonneltagId());
//		        			   }else {
//		        				   personnelTag=new PersonnelTag();
//            				       personnelTag.setUserId(urid);
//            				       personnelTag.setIdentity(identity);
//            				       personnelTag.setCreatedBy(userCode);            				       
//            				       personnelTagMapper.insertSelective(personnelTag);
//							    }
//		        				   if(persoel==null){
//		        					   personnel=new Personnel();
//	                				   personnel.setUserId(urid);
//	                				   personnel.setName(persono.get(0).getName());
//	                				   personnel.setCreatedBy(userCode);               				  
//	                				   personnelMapper.insertSelective(personnel);
//	                		
//		        				   }
	       			   
		        			   if (persAdvisDTO==null) {
		        				   persoadviso=new PersonnelAdvisor();
		        				   if(persoel!=null) {
		        					   persoadviso.setPersonnelId(persoel.getId());
		        				   }
		        				   
//		        				   else {
//		        					   persoadviso.setPersonnelId(personnel.getId());
//								    }
		        				   if(advisorType.equals("职业导航师")||advisorType.equals("导航师")) {
		        					   persoadviso.setPersonnelTypeId(1);
		        				   }else {
		        					   persoadviso.setPersonnelTypeId(2);
								   }
		        				   
		        				   persoadviso.setAdvisorUserId(advisorUserId);
		        				   persoadviso.setAdvisorUserName(advisorUserName);
		        				   persoadviso.setPersonneltagId(personnelTag.getId());
		        				   persoadviso.setPositivevaluation(positivevaluation);
		        				   persoadviso.setMentorpost(personoad.get(0).getWorkdept());
		        				   persoadviso.setFixedpostevaluation(fixedpostevaluation);
		        				   persoadviso.setFlag(0);
		        				   persoadviso.setAdditionTime(dAdditionTime);
		        				   persoadviso.setStartTime(dstartTime);
		        				   persoadviso.setTerminalTime(dterminalTime);
		        				   persoadviso.setCreatedBy(userCode);
		        				   persoadviso.setCreatedName(usname);
		        				   personnelAdvisorMapper.insertSelective(persoadviso);
		        				   	      				   
							     }else {
							    	 persoadviso.setAdvisorUserId(advisorUserId);
							    	 persoadviso.setAdvisorUserName(advisorUserName);
							    	 persoadviso.setPersonneltagId(personnelTag.getId());
			        				 persoadviso.setPositivevaluation(positivevaluation);
			        				 persoadviso.setMentorpost(personoad.get(0).getWorkdept());
			        				 persoadviso.setFixedpostevaluation(fixedpostevaluation);
			        				 persoadviso.setStartTime(dstartTime);
			        				 persoadviso.setTerminalTime(dterminalTime);
			        				 persoadviso.setId(persAdvisDTO.getId());
			        				 personnelAdvisorMapper.edit(persoadviso);
								 }	        				   							
						   }	        			  	        			   
	        			    b+=10; 	        	    	   
	        	       }
	           }	           	      		
	       }catch (IOException e1) {
                    e1.printStackTrace();
                     throw new ServiceException("导入失败！请稍后重试！");
           }
	}
	
	private String getDate(XSSFCell cell) {
		DecimalFormat df = new DecimalFormat("#");
		if (cell == null) {
			return "";
		}
		switch (cell.getCellType()) {
		case HSSFCell.CELL_TYPE_NUMERIC:
			if (HSSFDateUtil.isCellDateFormatted(cell)) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

				return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
			}
			return df.format(cell.getNumericCellValue());
		case HSSFCell.CELL_TYPE_STRING:
			return cell.getStringCellValue();
		case HSSFCell.CELL_TYPE_FORMULA:
			return cell.getCellFormula();
		case HSSFCell.CELL_TYPE_BLANK:
			return "";

		}
		return "";

	}
	
	@Override
	public Result peadone(Integer personnelId) {
		
		List<PersonnelAdvisorDTO> perelAdvlist=personnelAdvisorMapper.peadone(personnelId);		
		return Response.response(perelAdvlist);
	}

	@Override
	public Result queryPageListadd(PersonnelAdvisorDTO personnelAdvisor) {
		List<PersonnelAdvisorDTO> queryPageListadd=personnelAdvisorMapper.queryPageListadd(personnelAdvisor);
		return Response.response(queryPageListadd);
	}

	@Override
	public Result delpiliang(String personnelId, String personneltagId) {
		
		List<String> persId = JSONArray.parseArray(personnelId, String.class);
		List<String> pertagId = JSONArray.parseArray(personneltagId, String.class);
		
		personnelAdvisorMapper.delpiliang(persId, pertagId);
		
		
		return Response.response("删除成功");
	}

	

}