package com.wechat.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import com.beust.jcommander.internal.Sets;
import com.google.common.collect.Maps;
import com.wechat.common.Constants;
import com.wechat.common.utils.StringsUtils;
import com.wechat.dao.MdeEducationExperienceDao;
import com.wechat.dao.MdePositionCvRelDao;
import com.wechat.dao.MdeUserDao;
import com.wechat.global.base.dao.Pagination;
import com.wechat.global.base.dao.PagingResult;
import com.wechat.global.base.dao.RedisWithExpiryTimeDao;
import com.wechat.model.dbEntity.MdeEducationExperience;
import com.wechat.model.dbEntity.MdeJobAdvertised;
import com.wechat.model.dbEntity.MdeUser;
import com.wechat.model.dto.AdImageDto;
import com.wechat.model.dto.TalentRetrievalDto;
import com.wechat.model.dto.WorkDateDto;
import com.wechat.model.requestDto.AlentRetrievalReq;
import com.wechat.model.requestDto.KeyWordReq;
import com.wechat.model.requestDto.PageReq;
import com.wechat.model.responseDto.ContactUserInfoResp;

@Service
public class TalentRetrievalService {

	@Autowired
	private MdeUserDao mdeUserDao;

	@Resource
	private RedisWithExpiryTimeDao redisDao;
	
	@Autowired
	private MdePositionCvRelDao mdePositionCvRelDao;

	@Autowired
	private MdeEducationExperienceDao mdeEducationExperienceDao;

	@Autowired
	private CommonService commonService;

	public Map<String, Object> init(HttpServletRequest req) {
		Map<String, Object> maps = Maps.newHashMap();
		String userCode = commonService.getUserCode(req);
		//String language = commonService.getLanguage(req);
		List<MdeUser> mdeUser = commonService.getRecommentPeople(userCode);
		List<AdImageDto> adImages = commonService.getAdImages(Constants.PC_RENCAI_TOP_AD);
		maps.put("adImages", adImages);
		maps.put("talentReTrieval", mdeUser);// 优选人才 
		return maps;
	}

	/**
	 * 人才关键词检索
	 * 
	 * @param req
	 * @param resp
	 * @return
	 */
//	@Deprecated
//	public PagingResult<TalentRetrievalDto> searchKeyWord(KeyWordReq keyWordReq, HttpServletRequest req) {
//		if (keyWordReq != null && StringsUtils.isEmpty(keyWordReq.getKeyWord())) {
//			PagingResult<TalentRetrievalDto> pagingResult = new PagingResult<>();
//			pagingResult.setCurrentPage(keyWordReq.getIndex());
//			pagingResult.setTotalSize(0);
//			pagingResult.setPageSize(keyWordReq.getSize());
//			pagingResult.setResultList(new ArrayList<TalentRetrievalDto>());
//			pagingResult.setPageNoSize(10);
//			return pagingResult;
//		}
//
//		Pagination pagination = new Pagination(keyWordReq.getIndex(), keyWordReq.getSize());
//		String keyWord = keyWordReq.getKeyWord();
//		// 拆分关键字
//		// 提取字符串中的数字
//		String regEx = "[^0-9]";
//		Pattern p = Pattern.compile(regEx);
//		Matcher m = p.matcher(keyWord);
//		String number = m.replaceAll("").trim();
//		keyWord = keyWord.replaceAll("\\d+", "");
//		List<String> keyWordSplit = keyWordSplit(keyWord);
//		Map<Object, Object> param = Maps.newHashMap();
//		param.put("keyWord", keyWordSplit);
//		String eduId = mdeUserDao.getEducationId(param);
//		param.put("experience", number);
//		//根据工作年限获取工作起始时间范围
//		WorkDateDto workDate =commonService.getDateByExperience(number);
//		param.put("from", workDate.getFrom());
//		param.put("to", workDate.getTo());
//
//		param.put("eduId", eduId);
//		param.put("publisher", commonService.getUserCode(req));
//		pagination.setParams(param);
//		PagingResult<TalentRetrievalDto> page = mdeUserDao.searchKeyWord(pagination);
//		List<TalentRetrievalDto> list = commonService.addDomainUrlTalentRetrieval(page.getResultList()); // 拼接图像域名
//		page.setResultList(list);
//		return page;
//	}

	public PagingResult<TalentRetrievalDto> searchKeyWord(KeyWordReq keyWordReq, HttpServletRequest req) {
		if (keyWordReq != null && StringsUtils.isEmpty(keyWordReq.getKeyWord())) {
			PagingResult<TalentRetrievalDto> pagingResult = new PagingResult<>();
			pagingResult.setCurrentPage(keyWordReq.getIndex());
			pagingResult.setTotalSize(0);
			pagingResult.setPageSize(keyWordReq.getSize());
			pagingResult.setResultList(new ArrayList<TalentRetrievalDto>());
			pagingResult.setPageNoSize(10);
			return pagingResult;
		}

		Pagination pagination = new Pagination(keyWordReq.getIndex(), keyWordReq.getSize());

		Map<Object, Object> param = Maps.newHashMap();
		param.put("keyWord", keyWordReq.getKeyWord());
		param.put("publisher", commonService.getUserCode(req));
		pagination.setParams(param);
		PagingResult<TalentRetrievalDto> page = mdeUserDao.searchKeyWord(pagination);
		// 拼接图像域名
		List<TalentRetrievalDto> list = commonService.addDomainUrlTalentRetrieval(page.getResultList());
		page.setResultList(list);
		return page;
	}

	/**
	 * 人才筛选
	 * 
	 * @param AlentRetrievalReq
	 * @return
	 */
	public PagingResult<TalentRetrievalDto> filterPersonAbility(AlentRetrievalReq alentRetrievalReq,HttpServletRequest req) {
		Pagination pagination = new Pagination(alentRetrievalReq.getIndex(), alentRetrievalReq.getSize());
		Map<Object, Object> params = Maps.newHashMap();
		List<String> positionIdList = null;
		if (StringsUtils.isNotEmpty(alentRetrievalReq.getPositionIdList())) {
			positionIdList = Arrays.asList(alentRetrievalReq.getPositionIdList().split(","));
		}
		params.put("positionIdList", positionIdList);
		params.put("deptId", alentRetrievalReq.getDeptId());
		params.put("provinceId", alentRetrievalReq.getProvinceId());
		params.put("cityId", alentRetrievalReq.getCityId());
		params.put("publisher", commonService.getUserCode(req));
		List<String> brandIdList = null;
		if (StringsUtils.isNotEmpty(alentRetrievalReq.getBrandIdList())) {
			brandIdList = Arrays.asList(alentRetrievalReq.getBrandIdList().split(","));
		}
		params.put("brandIdList", brandIdList);
		
		int workExperience = alentRetrievalReq.getWorkExperience();
		params.put("workExperience", alentRetrievalReq.getWorkExperience());
		params.put("eduExperience", alentRetrievalReq.getEduExperience());
		//根据工作年限获取工作起始时间范围
		WorkDateDto workDate =commonService.getDateByExperience(String.valueOf(workExperience));
		params.put("from", workDate.getFrom());
		params.put("to", workDate.getTo());
		
		pagination.setParams(params);
		PagingResult<TalentRetrievalDto> page = mdeUserDao.filterPersonAbility(pagination);
		List<TalentRetrievalDto> list = commonService.addDomainUrlTalentRetrieval(page.getResultList()); // 拼接图像域名
		page.setResultList(list);
		return page;
	}

	/**
	 * 获取联系方式
	 * 
	 * @param userCode
	 * @return
	 */
	public ContactUserInfoResp getContactNumber(String userCode,String lang) {
		// 根据用户code 查询用户信息和工作履历
		ContactUserInfoResp contactUserInfoResp = mdeUserDao.getContactNumber(userCode);
		if (contactUserInfoResp != null) {
			List<MdeEducationExperience> educationList = 
					mdeEducationExperienceDao.getEducationExperienceByUserCode(userCode,lang);
			contactUserInfoResp.setEducationExperience(educationList);
			contactUserInfoResp = commonService.addDomainUrlLookUserCv(contactUserInfoResp); // 拼接图像域名
		}
		
		return contactUserInfoResp;
	}

	/**
	 * 关键词拆分
	 * 
	 * @param keyWord
	 * @return
	 */
	private List<String> keyWordSplit(String keyWord) {
		Set<String> set = Sets.newHashSet();
		List<String> split = Arrays.asList(keyWord.trim().split(" "));
		if (CollectionUtils.isEmpty(split)) {
			set.add(keyWord);
		} else {
			for (String string : split) {
				set.add(string);
			}
		}
		List<String> list = new ArrayList<>(set);
		return list;
	}
	
	/**
	 * 优选人才 -分页查询
	 * 
	 * @return
	 */
	public PagingResult<TalentRetrievalDto> getPagingRecmtPep(PageReq searchReq,String userCode) {
		Pagination page = new Pagination(searchReq.getIndex(), searchReq.getSize());
		Map<Object, Object> pageParam = new HashMap<Object, Object>();
		pageParam.put("userCode", userCode);
		page.setParams(pageParam);
		return mdeUserDao.getPagingRecommentTalent(page);
	}

}
