package com.hnshituo.core.auth.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.mapper.DepartmentMapper;
import com.hnshituo.core.auth.service.DepartmentService;
import com.hnshituo.core.auth.service.HomeService;
import com.hnshituo.core.auth.vo.Department;

@RemoteService(path="core/auth/dept")
public class DepartmentServiceImpl extends BaseServiceImpl<Department, String> implements DepartmentService {
	/**
	 * 有权限的所有树节点
	 * 及其权限数据
	 * */
	@RemoteService(path="tree")
	public List<Department> findTree() {
		Department t = new Department();
		t.setDel(false);
		List<Department> list = this.getMapper(DepartmentMapper.class).findWithCondition(t, null, "SEQ", 0, 0);

		String[] dataPrgs = (String[]) this.getRequest().getAttribute(HomeService.REQUEST_DATA_PRG);
		if (dataPrgs == null) {
			return findByPid(list, null);
		}

		List<Department> result = new ArrayList<Department>();
		for (String prg : dataPrgs) {
			Department dept = getByIdFromDepts(prg, list);
			if (dept != null) {
				dept.setChildren(this.findByPid(list, dept.getId()));
				result.add(dept);
			}
		}
		return result;
	}

	private Department getByIdFromDepts(String id, List<Department> depts) {
		for (Department department : depts) {
			if (id != null && id.equals(department.getId())) {
				return department;
			}
		}
		return null;
	}

	/**
	 * 递归组织数据，输出树形数据对象
	 * @param all
	 * @param pid
	 * @return
	 */
	private List<Department> findByPid(List<Department> all, String pid) {
		List<Department> list = null;
		for (Department dept : all) {
			if (StringUtils.isEmpty(pid) && StringUtils.isEmpty(dept.getPid())) {
				if (list == null) list = new ArrayList<Department>();
				list.add(dept);
			} else if (StringUtils.isNotEmpty(pid) && pid.equals(dept.getPid())) {
				if (list == null) list = new ArrayList<Department>();
				list.add(dept);
			}
		}
		if (list != null) {
			for (Department dept : list) {
				dept.setChildren(findByPid(all, dept.getId()));
			}
		}
		return list;
	}

	/**
	 * 随机数生成
	 * */
	private String RandomCreate(int i){
		String str="";
		for(int k=0;k<i;k++){
			str+=(int)(Math.random()*10);//[1-10)随机数 即0-9
		}
		return str;
	}
	
	/**
	 * 获得树结构数据，不含权限，不含子集节点
	 * */
	@Override
	public List<Department> doQueryByPidTree(Department t) {
		t.setDel(false);
		List<Department> list = this.getMapper(DepartmentMapper.class).doQueryByPid(t);

		List<Department> result = new ArrayList<Department>();
		for(Department d:list){
			d.setChildren(this.findByPid(list, d.getId()));
			result.add(d);
		}

		return result;
	}
	
	/**
	 * 获得树结构数据，不含权限，含子集节点
	 * */
	@Override
	public List<Department> doQueryByIdTree(Department t) {
		t.setDel(false);
		List<Department> list = this.getMapper(DepartmentMapper.class).doQueryById(t);

		List<Department> result = new ArrayList<Department>();
		for(Department d:list){
			d.setChildren(this.findByPid(list, d.getId()));
			result.add(d);
		}

		return result;
	}
	
	public List<Department> findAll(){
		Department dept=new Department();
		dept.setDel(false);
		List<Department> departmentList = this.getMapper(DepartmentMapper.class).find(dept, 0, 0);
		return departmentList;
	}
	
	/**
	 * 一般查询
	 * 可查询某节点详情
	 * 可查询某节点下的子节点
	 * */
	@Override
	public List<Department> doQueryWeb(Department dept) {
		dept.setDel(false);
		return this.getMapper(DepartmentMapper.class).findWithCondition(dept, null, " name ", 0, 0);
	}

	/**
	 * 新增
	 * 考虑到子部门下可能存在相同的名称
	 * */
	public Department doAddWeb(Department t) throws Exception {
		if (t==null) return null;
		String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + RandomCreate(3);
		t.setId(id);
		t.setCreate_time(new Date());
		t.setCreate_man((String)getSession().getAttribute(HomeService.SESSION_USER_NAME));
		this.getMapper(DepartmentMapper.class).insert(t);
		return t;
	}
	
	/**
	 * 修改
	 * 修改不能将其状态进行修改
	 * */
	@Override
	public Department doUpdateWeb(Department t) throws Exception {
		if (t==null) return null;
		t.setDel(false);
		t.setUpdate_time(new Date());
		t.setUpdate_man((String)getSession().getAttribute(HomeService.SESSION_USER_NAME));
		this.getMapper(DepartmentMapper.class).update(t);
		return t;
	}
	
	/**
	 * 删除
	 * 逻辑删除
	 * */
	@Override
	public Department doDeleteWeb(Department t) throws Exception {
		t.setUpdate_man((String)getSession().getAttribute(HomeService.SESSION_USER_NAME));
		this.getMapper(DepartmentMapper.class).doDelete(t);
		return t;
	}

	/**
	 * 验证是否重名
	 * 预留，实际上由于厂里许多部门都是重名的
	 * */
	@Override
	public boolean validateName(String name) {
		return !this.getMapper(DepartmentMapper.class).isNameExist(name);
	}
	 
}
