package cn.com.countrygarden.bi.main.service.personnel.impl;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.dao.performance.bdpsndocMapper;
import cn.com.countrygarden.bi.main.dao.personnel.PersonnelTagMapper;
import cn.com.countrygarden.bi.main.dao.review.ReviewQuestionnaireProblemClassificationMapper;
import cn.com.countrygarden.bi.main.dao.review.ReviewQuestionnaireProblemMapper;
import cn.com.countrygarden.bi.main.dao.sharppathevaluation.PersonnelSharppathevaluationMapper;
import cn.com.countrygarden.bi.main.dao.writtenExamination.PersonnelSelectionscoreMapper;
import cn.com.countrygarden.bi.main.dto.LabelVo;
import cn.com.countrygarden.bi.main.dto.PersonInfoVO;
import cn.com.countrygarden.bi.main.dto.PersonnelCampusevaluationDTO;
import cn.com.countrygarden.bi.main.dto.bdpsndocDTO;
import cn.com.countrygarden.bi.main.dto.personnel.PersonnelAdvisorDTO;
import cn.com.countrygarden.bi.main.dto.personnel.PersonnelAppDTO;
import cn.com.countrygarden.bi.main.dto.personnel.PersonnelAssessmentDTO;
import cn.com.countrygarden.bi.main.dto.sharppathevaluation.PersonnelSharppathevaluationDTO;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.dup.DupOrgan;
import cn.com.countrygarden.bi.main.model.personnel.PersonnelTag;
import cn.com.countrygarden.bi.main.model.review.ReviewQuestionnaireProblem;
import cn.com.countrygarden.bi.main.model.review.ReviewQuestionnaireProblemClassification;
import cn.com.countrygarden.bi.main.model.sharppathevaluation.PersonnelSharppathevaluation;
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 org.apache.commons.collections.CollectionUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;

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 cn.com.countrygarden.bi.main.model.personnel.Personnel;
import cn.com.countrygarden.bi.main.dao.personnel.PersonnelMapper;
import cn.com.countrygarden.bi.main.service.personnel.PersonnelCommenService;
import cn.com.countrygarden.bi.main.service.personnel.PersonnelService;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 人才信息-业务逻辑层实现
 *
 * @author 李森林
 * @email www.justintoforest@gamail.com
 * @url www.yuhi.com.cn
 * @date 2018-07-10
 */
@Service
public class PersonnelServiceImpl implements PersonnelService {
	@Autowired
	private PersonnelMapper personnelMapper;
	@Autowired
	private PersonnelTagMapper personnelTagMapper;
	@Autowired
	private bdpsndocMapper bdpsndocMapper;
	@Autowired
	private PersonnelSelectionscoreMapper personnelSelectionscoreMapper;
	@Autowired
	private ReviewQuestionnaireProblemClassificationMapper reviewQuestionnaireProblemClassificationMapper;
	@Autowired
	private ReviewQuestionnaireProblemMapper reviewQuestionnaireProblemMapper;
	@Autowired

	private PersonnelSharppathevaluationMapper sharppathevaluationMapper;
	@Autowired
	private PersonnelCommenService personnelCommenService;

	@Override
	public Result queryPageList(Personnel personnel, int page, int size) {
		Page<Object> pageList = PageHelper.startPage(page, size);
		Condition condition = new Condition(Personnel.class);
		Example.Criteria criteria = condition.createCriteria();
		// TODO 添加条件
		personnelMapper.selectByCondition(condition);
		PageData data = new PageData(pageList);
		data.setData(pageList.getResult());
		return Response.response(data);
	}

	@Override
	public Result queryById(Integer id) {
		return Response.response(personnelMapper.selectByPrimaryKey(id));
	}

	@Override
	public Result add(Personnel personnel, AccessToken accessToken) {
		personnelMapper.insert(personnel);
		return Response.response();
	}

	@Override
	public Result edit(Personnel personnel, AccessToken accessToken) {
		personnelMapper.updateByPrimaryKey(personnel);
		return Response.response();
	}

	@Override
	public Result del(Integer id) {
		int count = personnelMapper.deleteByPrimaryKey(id);
		return Response.response(count);
	}

	@Override
	public Result selectByUserIdOrName(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")) {

				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=bdpsndocMapper.selectByPersonInfoByCodeOrName(search,null,resuaslt.get(i).getOrganName());
//    				dtoper.addAll(persapper);
//				}    		
//    		
//		}else {
//			dtoper =personnelMapper.selectByPersonInfoByCodeOrName(search,null,null);
//		}
    	
		List<PersonInfoVO> psndocsList = bdpsndocMapper.selectByPersonInfoByCodeOrName(search,null,userquyu);
		List<String> result = new LinkedList<>();
		for (PersonInfoVO psndoc : psndocsList) {
			result.add(psndoc.getCode() + " - " + psndoc.getWorkdept() + " - " + psndoc.getName());
		}
		return Response.response(result);
	}

	@Override
	public Result selectInfoByUid(String name, String code, int page, int size) {
		Page object = PageHelper.startPage(page, size);
		List<LabelVo> labelVos = personnelMapper.selectInfomationByUid(name, code);
		for (LabelVo labelVo : labelVos) {
			Condition condition = new Condition(PersonnelTag.class);
			condition.excludeProperties("userId", "createdDate", "createdBy");
			condition.createCriteria().andEqualTo("userId", labelVo.getUserId());
			labelVo.setTagsList(personnelTagMapper.selectByCondition(condition));
		}
		PageData pageData = new PageData(object);
		pageData.setData(object.getResult());
		return Response.response(pageData);
	}

	@Override
	public Result selectUid(String uid) {
		LabelVo labelVo = personnelMapper.selectByUid(uid);
		Condition condition = new Condition(PersonnelTag.class);
		condition.excludeProperties("userId");
		condition.createCriteria().andEqualTo("userId", labelVo.getUserId());
		labelVo.setTagsList(personnelTagMapper.selectByCondition(condition));
		return Response.response(labelVo);
	}

	public Result bdpsndoc(String bdid) {

		List<bdpsndocDTO> bdpsndoc = bdpsndocMapper.bdpsndoc(bdid);

		if (bdpsndoc == null) {
			return null;
		}

		if (bdpsndoc.size() < 1) {
			return null;
		}

		return Response.response(bdpsndoc);
	}

	public Result selectUidList(String uid) {
		String[] uids = uid.split(",");
		// 人员基本信息,校园评测信息
		List<PersonnelAppDTO> appDTOList = personnelMapper.selectByUids2(uids);
		Condition condition = null;
		for (PersonnelAppDTO appDTO : appDTOList) {
			condition = new Condition(PersonnelTag.class);
			condition.createCriteria().andEqualTo("userId", appDTO.getUserId());
			// 处理标签
			appDTO.setTagsList(personnelTagMapper.selectByCondition(condition));
			// 校园评测
			appDTO.setCampusevaluationDTOList(personnelMapper.findPersonnelCampusevaluation(appDTO.getPersonnelId()));
			// 处理导师评测
			appDTO.setAdvisorType1(personnelMapper.findPersonnelAdvisorDTO(appDTO.getPersonnelId(), "导航师"));
			appDTO.setAdvisorType2(personnelMapper.findPersonnelAdvisorDTO(appDTO.getPersonnelId(), "辅导员"));
			// TODO 270数据
			appDTO.setPersonnelAssessmentDTOList(find270Info(appDTO.getUserId()));
			// 处理锐途评测数据,app只查看最新数据
			PersonnelSharppathevaluationDTO dto = new PersonnelSharppathevaluationDTO();
			dto.setPersonnelId(appDTO.getPersonnelId());
			List<PersonnelSharppathevaluationDTO> newList = new ArrayList<>();
			Map<String,Object> map=new HashMap<String, Object>();
			List<PersonnelSharppathevaluationDTO> personnelSharppathevaluationList = sharppathevaluationMapper
					.selectSharppathevaluationNewest(dto,map);
			for (PersonnelSharppathevaluationDTO DTO : personnelSharppathevaluationList) {
				newList.addAll(sharppathevaluationMapper.findPersonnelSharppathevaluationDTO(DTO));
			}
			appDTO.setSharppathevaluationList(newList);
			// 处理笔试数据
			appDTO.setWrittenType1(personnelSelectionscoreMapper.findSelectionscoreAll(appDTO.getPersonnelId(), "1"));
			appDTO.setWrittenType2(personnelSelectionscoreMapper.findSelectionscoreAll(appDTO.getPersonnelId(), "2"));
		}
		return Response.response(appDTOList);
	}

	public List<PersonnelAssessmentDTO> find270Info(String uid) {
		Condition condition = null;
		condition = new Condition(ReviewQuestionnaireProblemClassification.class);
		condition.selectProperties("id", "name");
		condition.createCriteria().andEqualTo("investigationmanagementId", 3);
		List<ReviewQuestionnaireProblemClassification> list = reviewQuestionnaireProblemClassificationMapper
				.selectByCondition(condition);
		List<PersonnelAssessmentDTO> dtoList = new ArrayList<PersonnelAssessmentDTO>();
		PersonnelAssessmentDTO dto = null;
		for (ReviewQuestionnaireProblemClassification reviewQuestionnaireProblemClassification : list) {
			dto = new PersonnelAssessmentDTO();

			dto.setProblemIds(personnelMapper.findProblemIds(reviewQuestionnaireProblemClassification.getId()));
			if (dto.getProblemIds().size() > 0) {
				dto.setProblemClassName(reviewQuestionnaireProblemClassification.getName());
				dto.setAvgScore(personnelMapper.find270Info(uid, dto.getProblemIds()));
				dtoList.add(dto);
			}

		}

		return dtoList;
	}

	@Override
	public Result personInfoCodeOrId(String search) {
		List<PersonInfoVO> personInfoCodeOrId = personnelMapper.personInfoCodeOrId(search);
		List<String> result = new LinkedList<>();
		for (PersonInfoVO psndoc : personInfoCodeOrId) {
			result.add(psndoc.getId() + " - " + psndoc.getCode() + " - " + psndoc.getName());
		}

		return Response.response(result);
	}

	public Result queryadByUid(String search, AccessToken accessToken) {

//		List<String> userIds = bdpsndocMapper.findErpUserCodes(sysUserCode,userquyu);
//		
//		List<String> userIds = personnelCommenService.findRole(accessToken);
//		if (userIds != null) {
//			if (userIds.size() == 0 || userIds.get(0).equals("-1")) {
//				// 没有匹配的角色或没有查找到数据
//				return Response.response();
//			}
//		}
//		List<String> liuserquyu=personnelCommenService.findErpRegionByUserCode(accessToken.getUserCode());
//		   String userquyu=liuserquyu.get(0);
//		 String findboolean=personnelCommenService.findboolean(accessToken,search,null,userquyu);
//		 if (findboolean.equals("role")) {                	
//     		return null;
//		
//		}else if (findboolean.equals("gj_admin")) {
//			userquyu=null;
//		}
		Map<String, Object> rolesMap = personnelCommenService.findRoleInfoMap(accessToken.getUserCode());
        String userquyu=null;
        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")) {

				resuaslt = personnelCommenService.findDupCommByUserCode(accessToken.getUserCode(),null,true);
			}
    		
    	}
    	
    	List<PersonInfoVO> dtoper=new ArrayList<PersonInfoVO>();
    	
    	if (resuaslt!=null&&resuaslt.size()!=0) {
    		for (int i = 0; i < resuaslt.size(); i++) {
    			List<PersonInfoVO> persapper=personnelMapper.selectByPersonInfoByCodeOrName(search,null,resuaslt.get(i).getOrganName());
    				dtoper.addAll(persapper);
				}    		
    		
		}else {
			dtoper =personnelMapper.selectByPersonInfoByCodeOrName(search,null,null);
		}
		 
		List<PersonInfoVO> personInfoCodeOrId = new ArrayList<PersonInfoVO>(dtoper);
		List<String> result = new LinkedList<>();
		for (PersonInfoVO psndoc : personInfoCodeOrId) {
			result.add(psndoc.getCode() + " - " + psndoc.getWorkdept() + " - " + psndoc.getName());
		}

		return Response.response(result);
	}

}