package com.mingsoft.job.action.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.mingsoft.job.action.JobBaseAction;
import com.mingsoft.job.biz.IFrontResumeBiz;
import com.mingsoft.job.biz.IJobBiz;
import com.mingsoft.job.biz.IPeopleJobBiz;
import com.mingsoft.job.entity.JobEntity;
import com.mingsoft.job.entity.PeopleJobEntity;
import com.mingsoft.job.entity.ResumeEntity;
import com.mingsoft.people.biz.IPeopleBiz;
import com.mingsoft.people.constant.e.CookieConstEnum;
import com.mingsoft.people.constant.e.SessionConstEnum;
import com.mingsoft.people.entity.PeopleEntity;

import net.mingsoft.basic.util.BasicUtil;


@Controller("webJobMain")
@RequestMapping("/webJob")
public class FrontJobAction extends JobBaseAction{
	/**
	 * 注入职位业务层
	 * 
	 * */
	@Autowired 
	private IJobBiz jobBiz;
	
	@Autowired
	private IPeopleBiz peopleBiz;
	
	@Autowired
	private IPeopleJobBiz peopleJobBiz;
	
	@Autowired
	private IFrontResumeBiz frontResumeBiz;
	
	
	/**
	 * 个人中心主页，跳转前进行登录验证
	 * 
	 * 
	 */
	@RequestMapping("/toPeopleCenter")
	@ResponseBody
	public void toPeopleCenter(HttpServletRequest request, HttpServletResponse response){
		String peopleCookieStr =  this.getCookie(request, CookieConstEnum.PEOPLE_COOKIE);
		PeopleEntity peopleSession =  this.getPeopleBySession(request);
		//判断是否已登录，未登录则跳转登录页
		if(null == peopleCookieStr || "".equals(peopleCookieStr) || "null".equals(peopleCookieStr)){
			this.outJson(response, false);
			return;
		}
		if(null == peopleSession ){
			this.outJson(response, false);
			return;
		}
		JSONObject peopleCookieJson =  (JSONObject) JSONObject.parse(peopleCookieStr);
		int cookiePeopleId = peopleCookieJson.getIntValue("peopleId");
		int sessionPeopleId = peopleSession.getPeopleId();
		if(cookiePeopleId == 0){
			this.outJson(response, false);
			return;
		}
		if(cookiePeopleId != sessionPeopleId){
			this.outJson(response, false);
			return;
		}
		
		this.outJson(response, true);
		return ; 
		
		
	}
	
	/**
	 * 个人中心主页
	 * 
	 * 
	 */
	@RequestMapping("/peopleCenter")
	public String peopleCenter(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("people") PeopleEntity people){
		PeopleEntity peopleSession =  this.getPeopleBySession(request);
		int appId = BasicUtil.getAppId();
		int peopleId = peopleSession.getPeopleId();
		people = this.peopleBiz.getByPeople(peopleSession, appId);
		request.setAttribute("people", people);
		//根据peopleId查询简历基本信息，如果无记录，返回提示新增
		Map<String,Object> whereMap = BasicUtil.assemblyRequestMap();
		whereMap.put("peopleId", people.getPeopleId());
		List<ResumeEntity> resumeList = this.frontResumeBiz.query(whereMap,null,null);
		request.setAttribute("resume", resumeList.get(0));
		BasicUtil.startPage();
		List<JobEntity> jobList =  this.jobBiz.query(null,"createdatetime", "desc");
		BasicUtil.endPage(jobList);
		request.setAttribute("jobList", jobList);
		return "/WEB-INF/front/join/join-grzx"; //front\join
	}	
	
	@RequestMapping("/jobList")
	/**
	 * @param String[] type： url参数   1社会招聘 2校园招聘 3实习生招聘 
	 * 
	 * */
	public String jobList(@RequestParam(value="type", required=true) Integer type ,HttpServletRequest request, HttpServletResponse response){
		int appId = this.getAppId(request);
		BasicUtil.startPage();
		Map where = BasicUtil.assemblyRequestMap();
		if(!"".equals(type) && null != type){
			//由于dao拼装in语句type字段使用数组，所以这里要将type封装成数组再传给dao
			String[] typeArr = {type.toString()};
			where.put("type", typeArr);
		}
		
		List<JobEntity> jobList= this.jobBiz.query(where, "createdatetime", "desc");
		BasicUtil.endPage(jobList);
		request.setAttribute("jobList", jobList);
		if(1 == type){
			return "/WEB-INF/front/join/join-shzp";
		}
		if(2 == type){
			return "/WEB-INF/front/join/join-xyzp";
		}
		if(3 == type){
			return "/WEB-INF/front/join/join-sxszp";
		}
		return "";
	}
	
	@RequestMapping("/jobInfo")
	/**
	 * @param String[] type： url参数   1社会招聘 2校园招聘 3实习生招聘 
	 * 
	 * */
	public String jobInfo(@RequestParam(value="jobid", required=true) String jobid , @RequestParam(value="type", required=true) Integer type, HttpServletRequest request, HttpServletResponse response){
		int appId = this.getAppId(request);
		JobEntity job = (JobEntity) this.jobBiz.getEntity(Integer.parseInt(jobid));
		request.setAttribute("job", job);
		if(type == 1 ){
			return "/WEB-INF/front/join/join-social-each";
		}
		if(type == 2 ){
			return "/WEB-INF/front/join/join-school-each";
		}
		if(type == 3 ){
			return "/WEB-INF/front/join/join-intern-each";
		}
		return "";
	}
	
	@RequestMapping("/jobApply")
	@ResponseBody
	public void jobApply(HttpServletRequest request, HttpServletResponse response){
		String peopleCookieStr =  this.getCookie(request, CookieConstEnum.PEOPLE_COOKIE);
		PeopleEntity peopleSession =  this.getPeopleBySession(request);
		//判断是否已登录，未登录则跳转登录页
		if("".equals(peopleCookieStr) || null==peopleCookieStr || null==peopleSession){
			this.outJson(response, false,"1");
			return;
		}
		JSONObject peopleCookieJson =  (JSONObject) JSONObject.parse(peopleCookieStr);
		int cookiePeopleId = peopleCookieJson.getIntValue("peopleId");
		int sessionPeopleId = peopleSession.getPeopleId();
		if(cookiePeopleId == 0 || cookiePeopleId != sessionPeopleId){
			this.outJson(response, false,"1");
			return;
		}
		//校验当前用户是否已申请该职位，已申请的提示不能重复申请
		Map<String,String> wheres = new HashMap<String,String>();
		wheres.put("peopleId", sessionPeopleId+"");
		String jobid = (String) request.getAttribute("jobid");
		wheres.put("jobid", jobid);
		int count = this.peopleJobBiz.countBySQL("people_job", wheres);
		if(count>0){
			this.outJson(response, false, "2");
			return;
		}
		
		//保存申请
		PeopleJobEntity peopleJob = new PeopleJobEntity();
		peopleJob.setPeopleId(sessionPeopleId);
		peopleJob.setJobid(Integer.parseInt(jobid));
		peopleJob.setCreatedatetime(new Date());
		this.peopleJobBiz.saveApply(peopleJob);
		this.outJson(response, true);
		return ; 
	}
	
	/**
	 *申请职位管理 
	 * 
	 * */
	@RequestMapping("/applyManage")
	public String applyManage(@RequestParam("peopleId") Integer peopleId, HttpServletRequest request, HttpServletResponse response){
		BasicUtil.startPage();
		List<JobEntity> applyJobList = this.jobBiz.queryApply(peopleId, "p.createdatetime", "desc");
		BasicUtil.endPage(applyJobList);
		request.setAttribute("applyJobList", applyJobList);
		request.setAttribute("peopleId", peopleId);
		return "/WEB-INF/front/join/join-sqgl";
	
	}
	
	@RequestMapping("/cancelApply")
	@ResponseBody
	public void cancelApply(HttpServletRequest request, HttpServletResponse response ){
		String peopleId = (String) request.getAttribute("peopleId");
		String jobid = (String) request.getAttribute("jobid");
		this.peopleJobBiz.cancelApply(Integer.parseInt(peopleId), Integer.parseInt(jobid));
		this.outJson(response, true);
	}
	
	@RequestMapping("/logout")
	@ResponseBody
	public void logout(HttpServletRequest request, HttpServletResponse response){
		this.removeSession(request, SessionConstEnum.PEOPLE_SESSION);
		this.setCookie(request, response, CookieConstEnum.PEOPLE_COOKIE, null);
		this.outJson(response, true);
	}
	
}
