package com.ssm.controller.system;



import java.util.ArrayList;
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.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.DataBinder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ssm.model.system.SysDepartment;
import com.ssm.model.system.SysPosition;
import com.ssm.model.system.SysUser;
import com.ssm.model.validator.PositionValidator;
import com.ssm.service.system.SysDepartmentService;
import com.ssm.service.system.SysPositionService;
import com.ssm.util.PackageBeanUtils;
import com.ssm.util.Util;
import com.ssm.util.page.PagedResult;
import com.ssm.web.Const;
import com.ssm.web.RoleLimit;

/**
 * 职位管理
 * 
 * */
@RequestMapping("sysPosition")
@Controller
public class PositionController {
	
	@Autowired
	private SysDepartmentService serviceDepartmentService;
	@Autowired
	private SysPositionService sysPositionService;
	
	

	/**
	 * 职位查询
	 * */
	@RoleLimit(pid = Const.SYSTEM_ROLE_LIMIT_PID, mid = Const.SYSTEM_ROLE_LIMIT_MID)
	@RequestMapping(value="getPositionManagement")
	@ResponseBody
	public ModelAndView getPositionManagement(HttpServletRequest request, SysPosition sysPosition,Integer pageNo,Integer pageSize,HttpSession session){
		
		ModelAndView view = new ModelAndView("sysposition/positionManagement");	
		
		//部门
		List<SysDepartment> sysDepartmentlist=serviceDepartmentService.getDepartmentList();
		List<SysDepartment> parenpagelist = new ArrayList<SysDepartment>();
		if(null != sysDepartmentlist && sysDepartmentlist.size() > 0){
			
			//封装一级菜单
			for (SysDepartment sysDepartment2 : sysDepartmentlist) {
				if(sysDepartment2.getLevel()==1){
					parenpagelist.add(sysDepartment2);
				}
			}
			
			if(null != parenpagelist && parenpagelist.size() > 0){
				//一级菜单的每个对象和所以的菜单比较
				for (int i = 0; i < parenpagelist.size(); i++) {	
					PackageBeanUtils.packageParentDepartmentlist(parenpagelist.get(i) , sysDepartmentlist);
					
				}
			}
		}
		
		
		
		view.addObject("sysDepartmentlist", parenpagelist);
		
		
		SysUser operator = Util.getUserSession(request);
		sysPosition.setUserid(operator.getUserid());
		//分页查询职位
		PagedResult<SysPosition>  list1=sysPositionService.getPostionFind(sysPosition,pageNo,pageSize);
		
		System.out.println(com.alibaba.fastjson.JSON.toJSONString(list1));
		view.addObject("list", list1);
		return view;
	}
	
	/**
	 * 查询所以部门（新增页面）
	 * @author dn
	 * @return list
	 * */
	@RoleLimit(pid = Const.SYSTEM_ROLE_LIMIT_PID, mid = Const.SYSTEM_ROLE_LIMIT_MID)
	@RequestMapping(value="getQuerySysDepartment", method=RequestMethod.GET)
	@ResponseBody
	public ModelAndView getQuerySysDepartment(){
		ModelAndView mv = new ModelAndView("sysposition/positionInsert");
		List<SysDepartment> list=serviceDepartmentService.getDepartmentList();
		mv.addObject("list", list); 
		mv.addObject("sysPosition", new SysPosition());	
		return mv;
	}
	
	/**
	 * 职位管理表单提交 绑定验证
	 * @param binder
	 */
	@RoleLimit(pid = Const.SYSTEM_ROLE_LIMIT_PID, mid = Const.SYSTEM_ROLE_LIMIT_MID)
	@InitBinder("sysPosition")
	public void InitBinder(DataBinder binder){
		binder.setValidator(new PositionValidator());
	}

	/**
	 * 添加职位
	 * */
	@RoleLimit(pid = Const.SYSTEM_ROLE_LIMIT_PID, mid = Const.SYSTEM_ROLE_LIMIT_MID)
	@RequestMapping(value="getAddSysPosition", method=RequestMethod.POST)
	public String getAddSysPosition(HttpServletRequest request, Model model,@Validated @ModelAttribute("sysPosition") SysPosition sysPosition,
			BindingResult result,HttpSession session){
		 if(result.hasErrors()==false){
			 SysUser operator = Util.getUserSession(request);
			 sysPosition.setCreateduserid(operator.getUserid());//创建人id
			 sysPosition.setCreateddatetime(new Date());//创建时间
			 sysPosition.setIsdeleted(false);//1(false)可用,0不可用
			 sysPositionService.insertSelective(sysPosition);
			 return "redirect:getPositionManagement";
		 }
		List<SysDepartment> list=serviceDepartmentService.getDepartmentList();
		model.addAttribute("list", list);
		 return"sysposition/positionInsert";
	}
	
	/**
	 * 删除职位
	 * */
	@RoleLimit(pid = Const.SYSTEM_ROLE_LIMIT_PID, mid = Const.SYSTEM_ROLE_LIMIT_MID)
	@RequestMapping(value="getDeleteSysPosition")
	@ResponseBody
	public Map<String, Object> getDeleteSysPosition(Integer positionid){
		Map<String, Object> record = new HashMap<String, Object>();
		this.sysPositionService.deleteSysPostion(positionid);
		record.put("success", true);
		return record;
	}
	
	/**
	 * 编辑职位
	 * */
	@RoleLimit(pid = Const.SYSTEM_ROLE_LIMIT_PID, mid = Const.SYSTEM_ROLE_LIMIT_MID)
	@RequestMapping(value="getSelectSysPosition")
	public ModelAndView getSelectSysPosition(Integer positionid){
		ModelAndView view=new ModelAndView("sysposition/positionUpdate");
		List<SysDepartment> list=serviceDepartmentService.getDepartmentList();
		view.addObject("list", list); 
		
		SysPosition sp=sysPositionService.selectByPrimaryKey(positionid);
		view.addObject("sp", sp); 
		view.addObject("sysPosition", new SysPosition());
		return view;
	}
	@RoleLimit(pid = Const.SYSTEM_ROLE_LIMIT_PID, mid = Const.SYSTEM_ROLE_LIMIT_MID)
	@RequestMapping(value="getUpdateSysPosition")
	public String getUpdateSysPosition(HttpServletRequest request, @Validated @ModelAttribute("sysPosition")SysPosition sysPosition,BindingResult result,HttpSession session){
		
		if(result.hasErrors()==false){
			SysUser operator = Util.getUserSession(request);
			sysPosition.setLastupdatedatetime(new Date());//更新时间
			sysPosition.setLastupdateuserid(operator.getUserid());//更新人ID
			sysPositionService.updateByPrimaryKeySelective(sysPosition);
			
			return "redirect:getPositionManagement";
		}
		return"sysposition/positionUpdate";
	}

}
