/**
 * 版权所有 ©2020 威海莱欧科技有限公司
 * 工程名：base02
 * 文件名：OrganizationTreeService.java
 * 创建日期：2020年12月23日 下午3:57:39
 * 作者：xmf
 */
package com.lion.service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lion.dao.LogDao;
import com.lion.dao.OrganizationTreeDao;
import com.lion.entity.LoginUserInfo;
import com.lion.util.TokenUtil;
import com.lion.util.Util;

import jodd.util.StringUtil;
import net.sf.json.JSONObject;

/**
 * 类描述
 * @author xmf
 * @since 2020年12月23日
 */
@Service
public class OrganizationTreeService {

	@Autowired
	private OrganizationTreeDao dao;
	@Autowired
	private LogDao logDao;
	/**
	 * 
	 * 方法描述  保存节点信息
	 * @author mt.W
	 * @since 2019年3月4日
	 * @param id
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public String updateNode(LoginUserInfo s,String name,String id,String level,String pid,String oid,String otherId) throws UnsupportedEncodingException{
		int rs;
		String type = "";
		String context = "";
		List<Map<String,Object>> checkName = dao.getNodeName(name,pid, oid);
		List<Map<String,Object>> checkID = dao.getID(id);
		if(checkName.size()>0){
			return Util.responseString("false","节点名称重复");
		}
		if(StringUtil.isEmpty(pid)){//修改节点名称
			if(oid.equals(id)){
				context=s.getUserAccount()+"对原节点名称进行修改,修改后节点名称为："+name;
				type ="组织架构节点名称修改";
			}else{
				List<Map<String,Object>> listUser=dao.getOrgUser(oid);
				List<Map<String,Object>> listChildOrg=dao.getChildNode(oid);
				if(listChildOrg.size()>0){
					return Util.responseString("false","当前组织下已存在节点");
				}
				if(listUser.size()>0){
					return Util.responseString("false","当前组织下已存在人员");
				}
				if(checkID.size()>0){
					return Util.responseString("false","id重复");
				}
				context=s.getUserAccount()+"对原节点名称进行修改,修改后节点id为："+id;
				type ="组织架构节点id修改";
			}
			rs = dao.editNode(name, id,oid,otherId);
		}else{//节点新增
			if(checkID.size()>0){
				return Util.responseString("false","id重复");
			}
			context=s.getUserAccount()+"进行节点新增，名称为："+name;
			type ="组织架构节点新增";
			rs = dao.addNode( pid, name, level,id,otherId);
		}
		if(rs>0){
			logDao.saveLog(context,type,s.getUserId());
			return Util.responseString("true","操作成功");
		}
		return Util.responseString("false","操作失败");
	}
	/**
	 * 
	 * 方法描述 删除节点
	 * @author mt.W
	 * @since 2019年3月4日
	 * @param req
	 * @param session
	 * @param id
	 * @return
	 */
	public String deleteNode(LoginUserInfo s,String id){
		String userName=s.getUserAccount();
		List<Map<String,Object>> listUser=dao.getOrgUser(id);
		List<Map<String,Object>> listChildOrg=dao.getChildNode(id);
		if(listChildOrg.size()>0){
			return Util.responseString("false","当前组织下已存在节点");
		}
		if(listUser.size()>0){
			return Util.responseString("false","当前组织下已存在人员");
		}
		int rs = dao.deleteNode(id);
		if(rs>0){
			logDao.saveLog(userName+"对"+id+"该节点进行删除","组织机构节点删除", s.getUserId());
			return Util.responseString("true","操作成功");
		}
		return Util.responseString("false","操作失败");
	}
	/**
	 * 
	 * 方法描述  查询组织架构树(当前查到3级节点)
	 * @author mt.W
	 * @since 2020年9月8日
	 * @param req
	 * @param session
	 * @param flag
	 * @return
	 */
	public Map<String,Object> getOrgdeptAll(String flag,HttpServletRequest req){
		//String userId=Token.getUserId(req);
		//SessionEntity s=userDao.getUserById(userId);
		/*String roleid = s.getRoleid();
		String orgid = s.getOrgid();*/
		LoginUserInfo user=TokenUtil.getUserInfo(req);
		String orgid=user.getOrgid();
		List<Map<String,Object>> temp =dao.getOrgdeptNodeByOrgid(orgid);//dao.getAllOrgdeptNode();	
		Map<String,List<Map<String,Object>>> orgdeptMap = new HashMap<String, List<Map<String,Object>>>();
		Map<String,List<Map<String,Object>>> orgdeptLevelMap = new HashMap<String, List<Map<String,Object>>>();
		int level = Integer.valueOf(temp.get(temp.size()-1).get("level").toString());
		int height_leval = Integer.valueOf(temp.get(0).get("level").toString());
		for(Map<String,Object> map:temp){
			if(orgdeptLevelMap.get(map.get("level").toString())!=null){
				orgdeptLevelMap.get(map.get("level").toString()).add(map);
			}else{
				List<Map<String,Object>> children = new ArrayList<Map<String,Object>>();
				children.add(map);
				orgdeptLevelMap.put(map.get("level").toString(), children);
			}
			if(orgdeptMap.get(map.get("pid").toString())!=null){
				orgdeptMap.get(map.get("pid").toString()).add(map);
			}else{
				List<Map<String,Object>> children = new ArrayList<Map<String,Object>>();
				children.add(map);
				orgdeptMap.put(map.get("pid").toString(), children);
			}
		}
		while(level>height_leval){
			level--;
			if(orgdeptLevelMap.get(level+"")!=null){
				for(Map<String,Object> map:orgdeptLevelMap.get(level+"")){
					List<Map<String,Object>> children = orgdeptMap.get(map.get("id").toString());
					map.put("children", children);
					if(children!=null){
						map.put("childid",children.get(children.size()-1).get("id").toString());
						map.put("child",children.size());
					}else{
						map.put("childid",map.get("id").toString()+"0");
						map.put("child",0);
					}
				}
			}
		}
		List<Map<String,Object>>list = orgdeptLevelMap.get(level+"");
		if(list!=null&&list.size()>0){
			JSONObject json  = new JSONObject();
			json.put("opened", true);
			list.get(0).put("state",json);
		}
		return Util.responseMap("ture", list);
	}
	/*public Map<String,Object> getOrgByUserOrg(HttpServletRequest req){
		LoginUserInfo user=TokenUtil.getUserInfo(req);
		String orgid=user.getOrgid();
		List<Map<String,Object>> list=dao.getOrgdeptNodeByOrgid(orgid);
		List<Map<String,Object>> listNew=new ArrayList<Map<String,Object>>();
		for(Map<String,Object> map:list){
			
		}
	}*/
	
	public Map<String,Object> getOrgdeptListInfo(String orgid){
		List<Map<String,Object>> list = dao.getOrgdeptNodeByOrgid(orgid);
		return Util.responseMap("true",list);
	}
}
