package com.blueland.bpm.manage.config.web;

import com.blueland.bpm.common.base.BaseSpringController;
import com.blueland.bpm.identity.OrgService;
import com.blueland.bpm.identity.UserService;
import com.blueland.bpm.identity.model.Org;
import com.blueland.bpm.identity.model.User;
import com.blueland.bpm.manage.util.BaseUser;
import com.blueland.bpm.manage.util.MyContants;
import com.blueland.bpm.manage.util.TreeNode;
import org.apache.commons.lang3.StringUtils;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author lw
 * @version 1.0
 * @since  
 */


@Controller
@RequestMapping(value = "/pages/user")
public class UserTreeController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = "USER_SORT"; 

	@Autowired
	private UserTreeManager userTreeManager;
	@Autowired
	private OrgService orgService;
	@Autowired
	private UserService userService;
	
	
	public UserTreeController() {
	}
	

	public void setOrgService(OrgService orgService) {
		this.orgService = orgService;
	}


	/**
	 * 增加了@ModelAttribute的方法可以在本controller的方法调用前执行,可以存放一些共享变量,如枚举值
	 */
	@ModelAttribute
	public void init(ModelMap model) {
		//model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	
	/**
	 * 根据id获取已选择的主送抄送部门
	 * @param request
	 * @return
	 */
	@RequestMapping(value="queryUserInfoByUserIdsAjax")
	@ResponseBody
	public List<Org> queryUserInfoByUserIdsAjax(HttpServletRequest request){
		String orgIds = request.getParameter("userIds");
		String[] org_ids = orgIds.split(";");
		List<String> orgIdsList =  Arrays.asList(org_ids);
		if(orgIdsList.isEmpty()){
			return null;
		}
		//List<Org> orgs = orgService.findOrgByOrgIds(orgIdsList);
		return null;
	}
	
	/**
	 * 根据id获取已选择的用户
	 * @param request
	 * @return
	 */
	@RequestMapping(value="getUserByUserIdsAjax")
	@ResponseBody
	public List<Map> getUserByUserIdsAjax(HttpServletRequest request){
		String userIds = request.getParameter("userIds");
		List<Map> users = new ArrayList<Map>();
		if(null!=userIds && !"".equals(userIds)){
			String[] user_ids = userIds.split(",");
			for (String user_id : user_ids) {
				User user = userService.getUserById(user_id);
				Org org = orgService.getOrgById(user.getOrgId());
				
				Map map = new HashMap();
				map.put("userId", user.getUserId());
				map.put("userName", user.getUserName());
				map.put("deptName", org.getOrgName());
				
				users.add(map);
			}
		}
		return users;
	}

	/**
	 * 根据userIds获取相对应的user对象
	 * @param request
	 * @return
	 */
	@RequestMapping(value="queryUserByUserIdsAjax")
	@ResponseBody
	public List<User> queryUserByUserIdsAjax(HttpServletRequest request){
		List<User> users = new ArrayList<User>();
		String userIds = request.getParameter("userIds");
		String[] user_ids = userIds.split(";");
		for (String userId : user_ids) {
			User user = userService.getUserById(userId);
			Org org = orgService.getOrgById(user.getOrgId());
			user.setOrgId(org.getOrgName());
			users.add(user);
		}
		return  users;
	}

	/**
	 *获取用户组织树
	 **/
	@RequestMapping(value="getUserOrgTree")
	@ResponseBody 
	public List<TreeNode> getUserOrgTree(HttpServletRequest request,String filterNodeIdsStr,String showTreeType) {
		String orgId=request.getParameter("id");
		String assignTreeId=request.getParameter("assignTreeId");
		String treeLevel=request.getParameter("treeLevel");
		String searchUserName = request.getParameter("queryVal");
		String checkStatus = request.getParameter("ischecked");
		String isCheckValue =  request.getParameter("isCheckValue");
		//是否默认选中
		boolean  isChecked= false;
		boolean isAssignTreeFlag=false;
		if(StringUtils.isEmpty(orgId)||orgId.equals("0")){
			if(StringUtils.isEmpty(assignTreeId)){
				orgId=MyContants.TOP_ORG_ID;
			}else{
				orgId=assignTreeId;
				isAssignTreeFlag=true;
			}
		}
		if(StringUtils.isEmpty(checkStatus)){
			isChecked = true;
		}
		List<TreeNode> treeNodes=userTreeManager.getUserOrgTree(orgId,isAssignTreeFlag,showTreeType,treeLevel,searchUserName,isChecked,isCheckValue);
		String[] filterNodeIds=null;
		if(StringUtils.isNotEmpty(filterNodeIdsStr)){
			if(filterNodeIdsStr.indexOf(",")!=-1){
				filterNodeIds=filterNodeIdsStr.split(",");
			}else{
				filterNodeIds=new String[]{filterNodeIdsStr};
			}
			for (String filterNodeId : filterNodeIds) {
				TreeNode treeNode=new TreeNode();
				treeNode.setId(filterNodeId);
				treeNodes.remove(treeNode);
			}
		}
		return treeNodes;
	}
	@RequestMapping(value="userOrgTree")
	public ModelAndView userOrgTree(HttpServletRequest request,String showTreeType,String selectNodeType,boolean isShowCheckBox,String filterNodeIdsStr) {
		ModelAndView modelAndView=new ModelAndView("/commons/userOrgTree");
		//Hashtable params = HttpUtils.parseQueryString(items);
		modelAndView.addObject("showTreeType",showTreeType);
		modelAndView.addObject("filterNodeIdsStr",filterNodeIdsStr);
		modelAndView.addObject("isShowCheckBox",isShowCheckBox);
		modelAndView.addObject("selectNodeType",selectNodeType);
		return modelAndView;
	}
	
	/**
	 *获取角色树
	 * @throws Exception 
	 **/
	@RequestMapping(value="getRoleTree")
	@ResponseBody 
	public List<TreeNode> getRoleTree(HttpServletRequest request,String filterNodeIdsStr,String showTreeType) throws Exception {
		String orgId=request.getParameter("id");
		String assignTreeId=request.getParameter("assignTreeId");
		String treeLevel=request.getParameter("treeLevel");
		User user = BaseUser.getInstance().getSessionUser(request);
		String currentAppId = BaseUser.getInstance().getLoginUserAppId(request);
		boolean isAssignTreeFlag=false;
		if(StringUtils.isEmpty(orgId)){
			if(StringUtils.isEmpty(assignTreeId)){
				orgId=MyContants.TOP_ORG_ID;
			}else{
				orgId=assignTreeId;
				isAssignTreeFlag=true;
			}
		}
		List<TreeNode> treeNodes=userTreeManager.getRoleTree(orgId,currentAppId,isAssignTreeFlag,showTreeType,treeLevel,user.getUserId());
		return treeNodes;
	}
}

