package com.zcloud.service.system.impl;

import java.util.ArrayList;
import java.util.List;

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

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.entity.system.Department;
import com.zcloud.mapper.dsno2.system.AdminDepartmentMapper;
import com.zcloud.service.system.AdminDepartmentService;
import com.zcloud.util.Tools;

/**
 * 说明： 组织机构
 * 创建人：FH Q313596790
 * 官网：
 */
@Service(value="adminDepartmentService")
@Transactional //开启事物
public class AdminDepartmentServiceImpl implements AdminDepartmentService {

	@Autowired
	private AdminDepartmentMapper departmentMapper;

	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		departmentMapper.save(pd);
	}

	/**删除
	 * @param pd
	 * @throws Exception
	 */
	public void delete(PageData pd)throws Exception{
		departmentMapper.delete(pd);
	}

	/**修改
	 * @param pd
	 * @throws Exception
	 */
	public void edit(PageData pd)throws Exception{
		departmentMapper.edit(pd);
	}

	/**修改
	 * @param pd
	 * @throws Exception
	 */
	public void editCornDept(PageData pd)throws Exception{
		departmentMapper.editCornDept(pd);
	}

	/**修改企业时，同步修改跟部门名称
	 * @param page
	 * @throws Exception
	 */
	public List<PageData> list(Page page)throws Exception{
		return (List<PageData>)departmentMapper.datalistPage(page);
	}

	/**通过id获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findById(PageData pd)throws Exception{
		return (PageData)departmentMapper.findById(pd);
	}
	/**通过名称获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findByName(PageData pd)throws Exception{
		return (PageData)departmentMapper.findByName(pd);
	}

	/**通过编码获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findByBianma(PageData pd)throws Exception{
		return (PageData)departmentMapper.findByBianma(pd);
	}

	/**
	 * 通过ID获取其子级列表
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	public List<Department> listSubDepartmentByParentId(String parentId) throws Exception {
		return (List<Department>)departmentMapper.listSubDepartmentByParentId(parentId);
	}

	/**
	 * 通过ID,TYPE获取其子级列表
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	public List<Department> listAllByType(PageData pd) throws Exception {
		return (List<Department>)departmentMapper.listAllByType(pd);
	}

	/**
	 * 获取所有数据并填充每条数据的子级列表(递归处理)
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	public List<Department> listAllDepartment(String parentId) throws Exception {
		List<Department> departmentList = this.listSubDepartmentByParentId(parentId);
		for(Department depar : departmentList){
//			depar.setTreeurl("department_list.html?DEPARTMENT_ID="+depar.getDEPARTMENT_ID());
			depar.setSubDepartment(this.listAllDepartment(depar.getDEPARTMENT_ID()));
			depar.setTarget("treeFrame");
			depar.setIcon("../../../assets/images/user.gif");
		}
		return departmentList;
	}

	/**
	 * 获取所有数据并填充每条数据的子级列表(递归处理)
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	public List<Department> listAllDepartment(String parentId,String url) throws Exception {
		List<Department> departmentList = this.listSubDepartmentByParentId(parentId);
		for(Department depar : departmentList){
			depar.setTreeurl(url+depar.getDEPARTMENT_ID());
			depar.setSubDepartment(this.listAllDepartment(depar.getDEPARTMENT_ID(),url));
			depar.setTarget("treeFrame");
			depar.setIcon("../../../assets/images/user.gif");
		}
		return departmentList;
	}

	/**
	 * 获取所有数据并填充每条数据的子级列表(递归处理)下拉ztree用
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	public List<PageData> listAllDepartmentToSelect(String parentId,List<PageData> zdepartmentPdList) throws Exception {
		List<PageData>[] arrayDep = this.listAllbyPd(parentId,zdepartmentPdList);
		List<PageData> departmentPdList = arrayDep[1];
		for(PageData pd : departmentPdList){
			this.listAllDepartmentToSelect(pd.getString("id"),arrayDep[0]);
		}
		return arrayDep[0];
	}


	/**下拉ztree用
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<PageData>[] listAllbyPd(String parentId,List<PageData> zdepartmentPdList) throws Exception {
		List<Department> departmentList = this.listSubDepartmentByParentId(parentId);
		List<PageData> departmentPdList = new ArrayList<PageData>();
		for(Department depar : departmentList){
			PageData pd = new PageData();
			pd.put("id", depar.getDEPARTMENT_ID());
			pd.put("parentId", depar.getPARENT_ID());
			pd.put("name", depar.getNAME());
			pd.put("icon", "../../../assets/images/user.gif");
			departmentPdList.add(pd);
			zdepartmentPdList.add(pd);
		}
		List<PageData>[] arrayDep = new List[2];
		arrayDep[0] = zdepartmentPdList;
		arrayDep[1] = departmentPdList;
		return arrayDep;
	}

	/**
	 * 获取所有数据并填充每条数据的子级列表(递归处理)下拉ztree用
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PageData> listAllbyTypeToSelect(PageData pd, List<PageData> zdepartmentPdList) throws Exception {
		List<PageData>[] arrayDep = this.listAllbyType(pd,zdepartmentPdList);
		List<PageData> departmentPdList = arrayDep[1];
		for(PageData dpd : departmentPdList){
			this.listAllDepartmentToSelect(dpd.getString("id"),arrayDep[0]);
		}
		return arrayDep[0];
	}

	/**下拉ztree用
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<PageData>[] listAllbyType(PageData pd,List<PageData> zdepartmentPdList) throws Exception {
		List<Department> departmentList = this.listAllByType(pd);
		List<PageData> departmentPdList = new ArrayList<PageData>();
		for(Department depar : departmentList){
			PageData dpd = new PageData();
			dpd.put("id", depar.getDEPARTMENT_ID());
			dpd.put("parentId", depar.getPARENT_ID());
			dpd.put("name", depar.getNAME());
			dpd.put("icon", "../../../assets/images/user.gif");
			departmentPdList.add(dpd);
			zdepartmentPdList.add(dpd);
		}
		List<PageData>[] arrayDep = new List[2];
		arrayDep[0] = zdepartmentPdList;
		arrayDep[1] = departmentPdList;
		return arrayDep;
	}

	/**获取某个部门所有下级部门ID(返回拼接字符串 in的形式， ('a','b','c'))
	 * @param DEPARTMENT_ID
	 * @return
	 * @throws Exception
	 */
	public String getDEPARTMENT_IDS(String DEPARTMENT_ID) throws Exception {
		DEPARTMENT_ID = Tools.notEmpty(DEPARTMENT_ID)?DEPARTMENT_ID:"0";
		List<PageData> zdepartmentPdList = new ArrayList<PageData>();
		zdepartmentPdList = this.listAllDepartmentToSelect(DEPARTMENT_ID,zdepartmentPdList);
		StringBuffer sb = new StringBuffer();
		sb.append("");
		for(PageData dpd : zdepartmentPdList){
			sb.append(dpd.getString("id"));
			sb.append(",");
		}
		sb.append("'fh'");
		return sb.toString();
	}

	/**通过条件获取全部
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> listAll(PageData pd)throws Exception{
		return (List<PageData>)departmentMapper.listAll(pd);
	}

	/**通过企业主部门
	 * @param pd
	 * @throws Exception
	 */
	public PageData findByCorpId(PageData pd)throws Exception{
		return departmentMapper.findByCorpId(pd);
	}
	/**列表 关联 部门级别名称
	 * @param page
	 * @throws Exception
	 */
	@Override
	public List<PageData> listForLevelName(Page page) throws Exception {
		// TODO Auto-generated method stub
		return departmentMapper.forLevelNamedatalistPage(page);
	}
}

