package com.mingsoft.job.action;

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.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mingsoft.basic.action.BaseAction;
import com.mingsoft.job.biz.IJobBiz;
import com.mingsoft.job.biz.IOrgBiz;
import com.mingsoft.job.constant.ModelCode;
import com.mingsoft.job.entity.JobEntity;
import com.mingsoft.job.entity.OrgEntity;
import com.mingsoft.job.entity.PeopleJobEntity;
import com.mingsoft.people.constant.e.CookieConstEnum;
import com.mingsoft.people.entity.PeopleEntity;
import com.mingsoft.util.StringUtil;

import net.mingsoft.basic.util.BasicUtil;

@Controller("jobManagerMain")
@RequestMapping("/${managerPath}/job")
public class JobAction extends BaseAction {
	/**
	 * 注入职位业务层
	 * 
	 * */
	@Autowired 
	private IJobBiz jobBiz;
	@Autowired 
	private IOrgBiz orgBiz;
	
	
	/**
	 * 查询职位
	 * 
	 * 
	 */
	@RequestMapping("/list")
	public String List(HttpServletRequest request, HttpServletResponse response){
		int appId = this.getAppId(request);
		BasicUtil.startPage();
		Map where = BasicUtil.assemblyRequestMap();
		List<JobEntity> jobList= this.jobBiz.query(where, "createdatetime", "desc");
		BasicUtil.endPage(jobList);
		request.setAttribute("jobList", jobList);
		return view("/job/job_list");
	}
		
	/**
	 * 新增职位信息
	 * @return 新增职位页面
	 */
	@RequestMapping("/add")
	public String add(ModelMap model,HttpServletRequest request, HttpServletResponse response){
//		int appId = BasicUtil.getAppId();
//		model.addAttribute("appId", appId);
		model.addAttribute("job",new JobEntity());
		//加载组织架构
		List<OrgEntity> orgList = this.orgBiz.query(null, null, null);
		model.addAttribute("orgList", JSONArray.toJSONString(orgList));
		return view("/job/job_add");
	}
	
	
	@RequestMapping("/save")
	@ResponseBody
	public void save(@ModelAttribute JobEntity job, HttpServletRequest request, HttpServletResponse response){
		if(null == job){
			this.outJson(response, ModelCode.JOB_MANAGE, false);
			return;
		}
		if(!this.checkJobInfo(job,request, response)){
			return;
		}
		job.setCreatedatetime(new Date());
		job.setUpdateDate(job.getCreatedatetime());
		this.jobBiz.saveJob(job);
		this.outJson(response, ModelCode.JOB_MANAGE,true);
		
	}
	
	public boolean checkJobInfo(JobEntity job, HttpServletRequest request, HttpServletResponse response){
		
		return true;
	}
	
	/**
	 * 前往编辑职位信息页面
	 * @return 编辑职位页面
	 */
	@RequestMapping("/{jobid}/edit")
	public String edit(@PathVariable int jobid, ModelMap model, HttpServletRequest request, HttpServletResponse response){
		JobEntity job = (JobEntity) this.jobBiz.getEntity(jobid);
		int appId = BasicUtil.getAppId();
		model.addAttribute("appId",appId);
		model.addAttribute("job",job);
		//加载组织架构
		List<OrgEntity> orgList = this.orgBiz.query(null, null, null);
		model.addAttribute("orgList", JSONArray.toJSONString(orgList));		
		return view("/job/job_edit");
		
	}
	
	/**
	 * 更新职位信息
	 * @param job 职位vo
	 * @param request
	 * @param response
	 */	
	@RequestMapping("/update")
	@ResponseBody
	public void update(@ModelAttribute JobEntity job, HttpServletRequest request, HttpServletResponse response){
		if(null == job){
			//未填写,返回错误信息
			this.outJson(response, ModelCode.JOB_MANAGE,false,"请填写职位信息");
			return ;
		}
		
		//丢失主键，返回错误信息
		if(job.getJobid()<1){
			this.outJson(response, ModelCode.JOB_MANAGE,false,"请填写职位信息");
			return;
		}
		
		//数据库内中不存在此记录，则返回错误信息
		JobEntity oldJob = (JobEntity) jobBiz.getEntity(job.getJobid());
		if(null == oldJob ){
			this.outJson(response, ModelCode.JOB_MANAGE,false,"查无此记录");
			return;
		}
		
		//更新操作
 		this.jobBiz.updateEntity(job);
		//返回更新成功
		this.outJson(response, ModelCode.JOB_MANAGE,true, "更新成功");
		return;
	}
	
	
	/**
	 * 更新职位状态
	 * @param job 职位vo
	 * @param request
	 * @param response
	 */	
	@RequestMapping("/updateStatus")
	public void updateStatus(@ModelAttribute JobEntity job, HttpServletRequest request, HttpServletResponse response){
		if(job == null){
			this.outJson(response, ModelCode.JOB_MANAGE,false,"无法获取职位信息");
			return;
		}
		if("".equals(job.getStatus()) || (null == job.getStatus())){
			this.outJson(response, ModelCode.JOB_MANAGE, false, "无法获取职位状态");
			return;
		}
		if(job.getStatus().equals("1")){ //当前状态为1有效，则改为失效
			job.setStatus("2");
		}else if(job.getStatus().equals("2")){ //当前状态为2无效，则改为有效
			job.setStatus("1");
		}		
		//this.jobBiz.updateEntity(job);
		this.jobBiz.updateJobStatus(job.getJobid(), job.getStatus(), new Date());
		this.outJson(response, ModelCode.JOB_MANAGE, true,job.getStatus());
		
	}
	
	/**
	 * 获取职位详细信息
	 * @param jobid 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getEntity")
	public void getEntity(int jobid,HttpServletRequest request,HttpServletResponse response){
		if(StringUtil.isBlank(jobid) || !StringUtil.isInteger(jobid)){
			this.outJson(response, ModelCode.JOB_MANAGE,false);
			return ;
		}
		JobEntity job = (JobEntity) this.jobBiz.getEntity(jobid);
		if(job == null){
			this.outJson(response, ModelCode.JOB_MANAGE,false);
			return ;
		}
		this.outJson(response, ModelCode.JOB_MANAGE,true,job.getDuty(),job.getRequirements());
	}
	
	
	/**
	 *获取职位申请记录 
	 * 
	 */
	@RequestMapping("/searchApplyList")
	public String searchApplyList(HttpServletRequest request, HttpServletResponse response){
		String peopleId = request.getParameter("peopleId");
		BasicUtil.startPage();
		List<JobEntity> applyList = this.jobBiz.queryApply(Integer.parseInt(peopleId), "p.createdatetime", "desc");
		BasicUtil.endPage(applyList);
		request.setAttribute("applyList", applyList);
		request.setAttribute("peopleId", peopleId);
		return view("/job/apply_list");
	}
	
	/**
	 *ajax方法 
	 * 查询申请记录
	 */
	@RequestMapping("/countApply")
	@ResponseBody
	public void countApply(HttpServletRequest request, HttpServletResponse response, int peopleId){
		int count = this.jobBiz.countApply(peopleId);
		if(count>0){
			this.outJson(response, true);
		}else{
			this.outJson(response, false);
		}
	}
}
