package com.blueland.sys.user.web;


import java.security.Principal;
import java.util.*;

import com.blueland.model.TPfApplication;
import com.blueland.model.TPubUser;
import com.blueland.result.R;
import com.blueland.service.IApplicationService;
import com.blueland.service.IMenuService;
import com.blueland.service.IPubOrgService;
import com.blueland.service.IPubUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.blueland.bo.MenuEntity;
import com.blueland.bo.PubOrgEntity;
import com.blueland.bo.PubUserEntity;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import static com.blueland.constant.ResponseConstant.*;


@RestController
@RequestMapping(value = "/user", produces = { "application/json"})
public class PubUserRest {
	@Autowired
	private IPubUserService pubUserService;
	@Autowired
	private IPubOrgService pubOrgService;
	@Autowired
	private IApplicationService appService;
	@Autowired
	private IMenuService iMenuService;
	
	@GetMapping("get")
	public TPubUser getUser(Principal user) {
		PubUserEntity entity = new PubUserEntity();
		entity.setUserName(user.getName());
		return pubUserService.get(entity);
	}
	
	/**
	 * 功能描述:查询不分页
	 * @param
	 * @return
	 */
	@RequestMapping(value = "findList",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	public List<PubUserEntity> findList(PubUserEntity pubUserEntity) {
		List<PubUserEntity> list = pubUserService.findList(pubUserEntity);
		return list;
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = "/{userId}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	public R<TPubUser> view(@PathVariable String userId)  {
		TPubUser pubUserEntity  = pubUserService.getById(userId);
		return R.ok(pubUserEntity);
	}
	
	/** 
	 * 获取厂商
	 **/
	@RequestMapping(value = "/getDevDept",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	public List<Map<String,Object>> getDevDept(@RequestParam Map<String,Object> map)  {
		List<Map<String,Object>> orgList = new ArrayList<Map<String,Object>>(); 
		Map<String,Object> topOrg = new HashMap<String, Object>();
		//获取顶级组
		List<String> orgIds = new ArrayList<String>();
		if(map.containsKey("orgId")&& !"".equals(map.get("orgId"))){
//			orgIds.add(map.get("orgId").toString());
			String[] tempOrgArray = map.get("orgId").toString().split(",");
			for (String tempOrgId: tempOrgArray) {
				orgIds.add(tempOrgId);
			}
			for (String orgId : orgIds) {
				topOrg = new HashMap<String, Object>();
				PubOrgEntity org = pubOrgService.findOrgById(orgId);
				topOrg.put("id", org.getOrgId());
				topOrg.put("name", org.getOrgName());
				topOrg.put("orgId", org.getOrgParentId()); 
				topOrg.put("companyName", org.getCompanyName());
//				topOrg.put("open", true);
				//查询二级节点
				List<Map<String,Object>> children = new ArrayList<Map<String,Object>>();
				List<Map<String,Object>> childrentemp = new ArrayList<Map<String,Object>>();
				Map<String,Object> orgMaptemp = Maps.newHashMap();
				orgMaptemp.put("id", "1");
				childrentemp.add(orgMaptemp);
				
				PubOrgEntity orgEntity = new PubOrgEntity();
				orgEntity.setOrgParentId(orgId);
				orgEntity.setOrgStatus("QY");
				orgEntity.setSortColumns("ORG_SORT");;
				List<PubOrgEntity> secondOrgList = pubOrgService.findList(orgEntity);
				
			
				for (PubOrgEntity orgEntry : secondOrgList) {
					List<Map<String,Object>> temp = Lists.newArrayList();
					PubOrgEntity entity = new PubOrgEntity();
					entity.setOrgParentId(orgEntry.getOrgId());
					entity.setOrgStatus("QY");
					entity.setSortColumns("ORG_SORT");
					List<PubOrgEntity> orgcs = pubOrgService.findList(entity);
					for (PubOrgEntity orgcscs : orgcs) {
						Map<String,Object> orgMaps = Maps.newHashMap();
						orgMaps.put("id", orgcscs.getOrgId());
						orgMaps.put("name", orgcscs.getOrgName());
						orgMaps.put("orgId", orgcscs.getOrgParentId());
						orgMaps.put("companyName", orgcscs.getCompanyName());
						orgMaps.put("children", Lists.newArrayList());
						temp.add(orgMaps);
					}
					Map<String,Object> orgMap = Maps.newHashMap();
					orgMap.put("id", orgEntry.getOrgId());
					orgMap.put("name", orgEntry.getOrgName());
					orgMap.put("orgId", orgEntry.getOrgParentId()); 
					orgMap.put("companyName", orgEntry.getCompanyName());
					orgMap.put("children", temp);

					children.add(orgMap);
				}
				topOrg.put("children", children);
				orgList.add(topOrg);
			}
		}else{ 
			orgIds.add("43");
			for (String orgId : orgIds) {
				PubOrgEntity org = pubOrgService.findOrgById(orgId);
				topOrg.put("id", org.getOrgId());
				topOrg.put("name", org.getOrgName());
				topOrg.put("orgId", org.getOrgParentId()); 
				topOrg.put("companyName", org.getCompanyName());
//				topOrg.put("open", true);
				//查询二级节点
				List<Map<String,Object>> children = new ArrayList<Map<String,Object>>();
				List<Map<String,Object>> childrentemp = new ArrayList<Map<String,Object>>();
				Map<String,Object> orgMaptemp = Maps.newHashMap();
				orgMaptemp.put("id", "1");
				childrentemp.add(orgMaptemp);
				PubOrgEntity orgEntity = new PubOrgEntity();
				orgEntity.setOrgParentId(orgId);
				orgEntity.setOrgStatus("QY");
				orgEntity.setSortColumns("ORG_SORT");
				List<PubOrgEntity> secondOrgList = pubOrgService.findList(orgEntity);
				for (PubOrgEntity orgEntry : secondOrgList) {
					List<Map<String,Object>> temp = Lists.newArrayList();
					PubOrgEntity entity = new PubOrgEntity();
					entity.setOrgParentId(orgEntry.getOrgId());
					entity.setOrgStatus("QY");
					entity.setSortColumns("ORG_SORT");
					List<PubOrgEntity> orgcs = pubOrgService.findList(entity);
					for (PubOrgEntity orgcscs : orgcs) {  
						Map<String,Object> orgMaps = Maps.newHashMap();
						orgMaps.put("id", orgcscs.getOrgId());
						orgMaps.put("name", orgcscs.getOrgName());
						orgMaps.put("orgId", orgcscs.getOrgParentId());
						orgMaps.put("companyName", orgcscs.getCompanyName());
						orgMaps.put("children", Lists.newArrayList());
						temp.add(orgMaps);
					}
					Map<String,Object> orgMap = Maps.newHashMap();
					orgMap.put("id", orgEntry.getOrgId());
					orgMap.put("name", orgEntry.getOrgName());
					orgMap.put("orgId", orgEntry.getOrgParentId()); 
					orgMap.put("companyName", orgEntry.getCompanyName());
					orgMap.put("children", temp);
					children.add(orgMap);
				}
				topOrg.put("children", children);
				orgList.add(topOrg);
			}
		}
		return orgList;
	}
	
	/** 
	 * 查看员工所拥有的一级菜单、操作权限
	 * 根据app_id获取资源
	 **/
	@RequestMapping(value = "/{appId}/{staffCode}/menuList",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public  Map<String, Object> menuList(@PathVariable("appId") String appId, @PathVariable("staffCode") String staffCode)  {
		Map < String , Object > result = new HashMap< String , Object>(); 
		List<MenuEntity> menuEntryList = null;
		try {
			if(StringUtils.isBlank(appId)){
				result.put("resultCode", RESULT_CODE_ERROR_CODE);
				result.put("resultMsg", "输入参数异常");
			}else{
				TPfApplication app = appService.getById(appId);
			    if(app==null){
			    	result.put("resultCode", RESULT_CODE_ERROR_CODE);
					result.put("resultMsg", "应用ID错误");
				}else{
					menuEntryList = iMenuService.getMenuListByStaffcode(appId, staffCode);
					result.put("resultCode", RESULT_CODE_SUCCESS_CODE);
					result.put("resultMsg", "处理成功");
					result.put("result", menuEntryList);
				}
			}
		} catch (Exception e) {
			result.put("resultCode", RESULT_CODE_ERROR_CODE);
			result.put("resultMsg", "系统内部异常");
			e.printStackTrace();
		}
		return result;
	}
	
	/** 
	 * 查询该员工所拥有的下级菜单、操作权限
	 **/
	@RequestMapping(value = "/{menuId}/menuId/{appId}/{staffCode}/resource",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public Map<String, Object> viewParent(@PathVariable("menuId") String menuId,@PathVariable("appId")String appId,@PathVariable("staffCode")String staffCode)  {
		Map < String , Object > result = new HashMap< String , Object>(); 
		List<MenuEntity> menuEntryList = null;
		try {
			menuEntryList = iMenuService.getParentByStaffcode(menuId,appId,staffCode);
			result.put("resultCode", RESULT_CODE_SUCCESS_CODE);
			result.put("resultMsg", "处理成功");
			result.put("result", menuEntryList);
		} catch (Exception e) {
			result.put("resultCode", RESULT_CODE_ERROR_CODE);
			result.put("resultMsg", "系统内部异常");
			e.printStackTrace();
		}
		return result;
	}
}



