package com.zcloud.controller.system;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.PageData;
import com.zcloud.entity.system.Department;
import com.zcloud.service.system.DepartmentService;
import com.zcloud.service.system.UsersService;
import net.sf.json.JSONArray;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：组织机构
 * 作者：luoxiaobao
 * 官网：www.fhadmin. org
 */
@Controller
@RequestMapping("/app/sys")
public class AppSystemController extends BaseController {

	@Autowired
	private DepartmentService departmentService;
	@Autowired
    private UsersService usersService;
	/**
	 * 显示列表ztree
	 * @return
	 */
	@RequestMapping(value="/listTree")
	@ResponseBody
	public Object listTree()throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String url = pd.get("url")==null?"department_list.html?DEPARTMENT_ID=":pd.getString("url");

		pd=this.departmentService.findByCorpId(pd);
		String ZDEPARTMENT_ID = pd.getString("DEPARTMENT_ID");
		List<Department> zdepartmentPdList = new ArrayList<Department>();

		PageData dept = new PageData();
		dept.put("DEPARTMENT_ID",ZDEPARTMENT_ID);
		dept=this.departmentService.findById(dept);

		Department depar = new Department();
		depar.setDEPARTMENT_ID(dept.getString("DEPARTMENT_ID"));
		depar.setPARENT_ID(dept.getString("PARENT_ID"));
		depar.setNAME(dept.getString("NAME"));
		depar.setTreeurl(url+depar.getDEPARTMENT_ID());
		depar.setSubDepartment(this.departmentService.listAllDepartment(depar.getDEPARTMENT_ID(),url));
		depar.setTarget("treeFrame");
		depar.setIcon("../../../assets/images/user.gif");
		zdepartmentPdList.add(depar);
//		zdepartmentPdList.addAll(departmentService.listAllDepartment(ZDEPARTMENT_ID));
		JSONArray arr = JSONArray.fromObject(zdepartmentPdList);
		String json = arr.toString();
		json = json.replaceAll("DEPARTMENT_ID", "id").replaceAll("NAME", "name").replaceAll("subDepartment", "children");
		map.put("zTreeNodes", json);
		map.put("result", errInfo);
		return map;
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/listUser")
	@ResponseBody
	public Object listAll() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		List<PageData>	userList = usersService.listAllUser(pd);	//列出Post列表
		map.put("userList", userList);
		map.put("result", errInfo);
		return map;
	}

	/**列表（本部门及所有上级部门领导负责人）
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/listUserLeader")
	@ResponseBody
	public Object listUserLeader() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		List<PageData>	userList = usersService.listUserLeader(pd);	//列出Post列表
		map.put("userList", userList);
		map.put("result", errInfo);
		return map;
	}
}
