/**
 * Company Name : 中贸促信息技术有限责任公司
 * Project Name:project
 * File Name:OperateController.java
 * Package Name:org.ccpit.base.privilegeManage
 * Date:2015年9月23日下午3:33:39
 * Copyright (c) 2015, dingwei@ccpit.org All Rights Reserved.
 *
*/

package org.ccpit.base.privilegeManage;

import java.io.UnsupportedEncodingException;
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.HttpServletResponse;

import org.ccpit.base.role.Role;
import org.ccpit.base.role.RoleService;
import org.ccpit.base.role.RoleUrl;
import org.ccpit.base.user.User;
import org.ccpit.base.user.UserController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * ClassName:OperateController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2015年9月23日 下午3:33:39 <br/>
 * @author   dingwei
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Controller
@RequestMapping("/admin/privilegeManage")
public class OperateController {

	private static final Logger log = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private OperateService operateService;
	@Autowired
    private RoleService roleService;

	
	/*
	 * 进入权限管理列表界面
	 */
	@RequestMapping("/goinPriListPage")
	public Object goinPriListPage() {
		ModelAndView mv = new ModelAndView("admin/privilegeManage/privilegeListPage");
		return mv;
	}
	
	/*
	 * 进入角色权限分配管理列表界面
	 */
	@RequestMapping("/goRolePriListPage")
	public Object goRolePriListPage() {
		ModelAndView mv = new ModelAndView("admin/privilegeManage/rolePriListPage");
		return mv;
	}

	@RequestMapping({ "/getLeftTree" })
	@ResponseBody
	public Object getLeftTree(HttpServletRequest request) {
		String isroot = request.getParameter("isroot");
		User user = (User) request.getSession().getAttribute("user_in_session");
		List result = new ArrayList();
		List<Operate> orglist = null;
		// if (("true".equals(isroot)) ||
		// ("superadmin".equals(userBo.getLoginName()))){
		// listBo = this.unitService.findUnitByHierarchy("01");
		// }
		// else {
		// }
		orglist = this.operateService.getOperateList();
		List listBo1 = null;
		if (null != orglist) {
			for (Operate operate : orglist) {
				Map item = new HashMap();
				if (1 == operate.getId()) {
					item.put("id", operate.getId());
					item.put("text", operate.getOperateName());
					item.put("priUrl", operate.getPriUrl());
					item.put("children", getChildren(operate.getId(), orglist));
					result.add(item);
				}
			}
		}
		return result;
	}

	public List<Map<String, Object>> getChildren(long id, List<Operate> orgList) {
		List items = new ArrayList();
		for (Operate operate : orgList) {
			if (operate.getParentId() == id) {
				Operate gg = (Operate) operate;
				Map item = new HashMap();
				item.put("id", gg.getId());
				item.put("text", gg.getOperateName());
				item.put("priUrl", gg.getPriUrl());
				item.put("children", getChildren(gg.getId(), orgList));
				items.add(item);
			}
		}
		return items;
	}
	
	@RequestMapping("/addOrUpdateOperate")
	@ResponseBody
	public Object addOrUpdateOperate(HttpServletRequest request,
			HttpServletResponse response, Operate opeBo) {
		Map map = new HashMap();
		User user = (User) request.getSession().getAttribute("user_in_session");
		String operatePId = request.getParameter("operatePId");
		String operatePName = request.getParameter("operatePName");
		String operateType = request.getParameter("operateType");
		if (null != operatePName) {
			try {
				operatePName = java.net.URLDecoder.decode(operatePName, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		if ("add".equals(operateType)) {
			opeBo.setParentId(Long.valueOf(operatePId));
			opeBo.setParentName(operatePName);
			opeBo.setCreateDate(new Date());
			opeBo.setCreateUser(user.getUsername());
			operateService.addOperate(opeBo);
			map.put("flag", true);
			map.put("info", "权限信息保存成功！");
		}else if("edit".equals(operateType)) {
			Operate ope = operateService.getOperateByID(Long.valueOf(operatePId));
			if(null != opeBo.getOperateName()){
				ope.setOperateName(opeBo.getOperateName());
			}if(null != opeBo.getPriUrl()){
				ope.setPriUrl(opeBo.getPriUrl());
			}if(null != opeBo.getDescription()){
				ope.setDescription(opeBo.getDescription());
			}
			operateService.updateOperate(ope);
			map.put("flag", true);
			map.put("info", "权限信息保存成功！");
		}else {
			map.put("flag", false);
			map.put("info", "权限信息保存失败...");
		}
		response.setContentType("text/html");
		return map;
	}
	/**
	 * 
	 * isExistNext:(判断是否存在子权限). <br/>
	 * TODO(这里描述这个方法适用条件 – 可选).<br/>
	 * TODO(这里描述这个方法的执行流程 – 可选).<br/>
	 * TODO(这里描述这个方法的使用方法 – 可选).<br/>
	 * TODO(这里描述这个方法的注意事项 – 可选).<br/>
	 *
	 * @author dingwei
	 * @param request
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping({ "/isExistNext" })
	@ResponseBody
	public Object isExistNext(HttpServletRequest request) {
		String orgId = request.getParameter("orgId");
		Map result = new HashMap();
		List list = operateService.getChildOperateById(Long.valueOf(orgId));
		if (null != list) {
			if (list.size() > 0) {
				result.put("flag", false);
				result.put("info", "当前菜单存在子菜单，无法进行删除！");
			} else {
				result.put("flag", true);
			}
		} else {
			result.put("flag", true);
		}
		return result;
	}
	
	/**
	 * 根据id删除
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/deleteOrg")
	@ResponseBody
	public Object deleteOrg(HttpServletRequest request, HttpServletResponse response){
		Map result = new HashMap();
		String orgId = request.getParameter("ids");
		if(null != orgId){
			boolean flag = operateService.delOperateByID(Long.valueOf(orgId));
			result.put("flag", true);
		}else {
			result.put("flag", false);
		}
		return result;
	}

	@RequestMapping("/queryOrgById")
	@ResponseBody
	public Object queryOrgById(HttpServletRequest request,
			HttpServletResponse response) {
		String orgId = request.getParameter("orgId");
		Operate orgInfo = operateService.getOperateByID(Long.valueOf(orgId));
		return orgInfo;
	}
	
	/**
	 * 根据角色Id查询权限id
	 */
	@RequestMapping("/queryPriurlByRoleId")
	@ResponseBody
	public Object queryPriurlByRoleId(HttpServletRequest request){
		Map result = new HashMap();
		String roleId = request.getParameter("roleId");
		List<Operate> oList = operateService.getOperateList();
		
		Role role = roleService.get(Long.valueOf(roleId));
		List<RoleUrl> urlPerfixs = role.getUrlPerfixs();
		List<Integer> urlList = new ArrayList<Integer>();
		if(null != urlPerfixs){
			for(int i=0;i<urlPerfixs.size();i++){
				RoleUrl roleUrl = urlPerfixs.get(i);
				if(null != oList){
					for(Operate o : oList){
						if(roleUrl.getUrl().equals(o.getPriUrl())){
							urlList.add((int) o.getId());
						}
					}
				}
			}
			result.put("flag", urlList);
		}
		return result;
	}
}

