package com.mingsoft.job.action;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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.mingsoft.basic.action.BaseAction;
import com.mingsoft.job.biz.IOrgBiz;
import com.mingsoft.job.entity.OrgEntity;

@Controller("OrgManager")
@RequestMapping("/${managerPath}/org")
public class OrgAction extends BaseAction{
	
	/**
	 * 注入职位业务层
	 * 
	 * */
	@Autowired 
	private IOrgBiz orgBiz;
	
	@RequestMapping("list")
	public String List(HttpServletRequest request, HttpServletResponse response){
		int websiteId = this.getAppId(request);
		List<OrgEntity> orgList = new ArrayList<OrgEntity>();
		orgList = orgBiz.query(null, null, null);
		request.setAttribute("orgList", JSONArray.toJSONString(orgList));
		return view("/job/org_list");
	}
	
	@RequestMapping("add")
	public String add(ModelMap model, HttpServletRequest request, HttpServletResponse response){
		int appId = this.getAppId(request);
		List<OrgEntity> orgList = new ArrayList<OrgEntity>(); 
		orgList = orgBiz.query(null, null, null);
		model.addAttribute("orgList", JSONArray.toJSONString(orgList));
		model.addAttribute("org", new OrgEntity());
		return view("/job/org_add");
	}
	
	@RequestMapping("save")
	@ResponseBody
	public void save(HttpServletRequest request, HttpServletResponse response, @ModelAttribute OrgEntity org){
		if(null == org){
			this.outJson(response, false,"公司信息为空");
			return;
		}
		org.setCreatedatetime(new Date());
		org.setUpdatedatetime(new Date());
		this.orgBiz.saveOrg(org);
		this.outJson(response, true, "保存成功");
	}
	
	@RequestMapping("/{orgid}/edit")
	public String edit(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer orgid){
		request.setAttribute("orgid", orgid);
		List<OrgEntity> orgList = new ArrayList<OrgEntity>(); 
		orgList = orgBiz.query(null, null, null);
		request.setAttribute("orgList", JSONArray.toJSONString(orgList));
		OrgEntity orgEntity = (OrgEntity) this.orgBiz.getEntity(orgid);
		request.setAttribute("org", orgEntity);
		return view("/job/org_edit");
	}
	
	@RequestMapping("update")
	@ResponseBody
	public void update(HttpServletRequest request, HttpServletResponse response,@ModelAttribute OrgEntity org){
		if(null == org){
			//未填写,返回错误信息
			this.outJson(response, false, "职位信息提交失败");
			return ;
		}
		//丢失主键，返回错误信息
		if(org.getOrgid()<1){
			this.outJson(response, false, "职位信息提交失败");
			return;
		}
		
		//数据库不存在此记录，返回错误信息
		OrgEntity oldOrg = (OrgEntity) orgBiz.getEntity(org.getOrgid());
		if(null == oldOrg){
			this.outJson(response, false, "更新失败，无此记录");
			return;
		}
		//更新操作
		org.setUpdatedatetime(new Date());
		this.orgBiz.updateEntity(org);
		//返回更新成功
		this.outJson(response, true, "更新成功");
		
	}
	
	@RequestMapping("/{orgid}/delete")
	@ResponseBody
	public void delete(@PathVariable Integer orgid, HttpServletRequest request, HttpServletResponse response){
		if(orgid == null || orgid <0){
			this.outJson(response, false, "未获取到公司id");
			return;
		}
		//如果有子节点，返回错误提示，否则可以删
		if(this.orgBiz.queryChild(orgid).size()>0){
			this.outJson(response, false, "删除母公司前，请先删除其下属子公司");
		}else{
			this.orgBiz.deleteEntity(orgid);
			this.outJson(response, true, "删除成功");
		}
		
	}
	
}
