package com.palmnest.service.estateserve.impl;

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

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

import com.palmnest.application.bean.Page;
import com.palmnest.application.dto.OutTreeDTO;
import com.palmnest.dao.estateserve.StaffDao;
import com.palmnest.dao.property.PropertyDao;
import com.palmnest.model.Staff;
import com.palmnest.model.StaffRelationBuild;
import com.palmnest.model.StaffRelationOrg;
import com.palmnest.model.property.BuildOrgRelation;
import com.palmnest.model.property.TbBuild;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.service.estateserve.StaffManager;
import com.palmnest.service.impl.GenericManagerImpl;
import com.palmnest.service.property.PropertyManager;

/**
 * 员工业务层接口实现类
 *
 * @author yubing
 * @since 2015-0
 */
@Component("staffManager")
public class StaffManagerImpl extends GenericManagerImpl<Staff, Integer>
		implements StaffManager {

	private StaffDao staffDao;

	@Autowired
	public void setstaffDao(StaffDao staffDao) {
		this.staffDao = staffDao;
		this.dao = staffDao;
	}

	@Autowired
	private PropertyDao propertyDao;


	@Override
	public Page queryStaffList(int pageNo, String strSQL) {
		return staffDao.queryStaffList(pageNo, 10, strSQL);
	}

	@Override
	public Page queryStaffListByOrganization(int pageNo, String whereSQL,
											 String organizationId) {
		return staffDao.queryStaffListByOrganization(pageNo, 10, whereSQL, organizationId);
	}

	@Override
	public Staff getStaffByStaffNoOrStaffId(String staffNo, String staffId) {
		return staffDao.getStaffByStaffNoOrStaffId(staffNo, staffId);
	}

	@Override
	public List<OutTreeDTO> getStaffRelationBuildTree(Integer staffId, Integer deptId) {
		List<OutTreeDTO> trees = new ArrayList<OutTreeDTO>();
		//根节点
		OutTreeDTO obj = new OutTreeDTO();
		obj.setId("0");
		obj.setName("所有项目");
		obj.setpId("-1");
		trees.add(obj);

		List<Map<String, Object>> dataList = null;
		List<StaffRelationBuild> relations = new ArrayList<StaffRelationBuild>();
		List<Integer> orgIds = new ArrayList<Integer>();
		//物业组织
		dataList = propertyDao.getBuildListByDept(deptId);
		//员工编辑，ID存在
		if (staffId != null) {
			relations = staffDao.getStaffRelationBuildList(staffId.intValue());
		}
		for (Map<String, Object> data : dataList) {
			Integer organizationId = (Integer) data.get("organizationId");
			Integer buildId = (Integer) data.get("buildId");
			String buildName = (String) data.get("buildName");
			if (!orgIds.contains(organizationId)) {
				String organizationName = (String) data.get("organizationName");
				OutTreeDTO obj1 = new OutTreeDTO();
				obj1.setId("o_" + organizationId.intValue());
				obj1.setName(organizationName);
				obj1.setpId("0");
				obj1.setLevel("1");
				trees.add(obj1);

				orgIds.add(organizationId);
			}
			OutTreeDTO obj2 = new OutTreeDTO();
			obj2.setId(buildId.intValue() + "");
			obj2.setName(buildName);
			obj2.setLevel("2");
			obj2.setpId("o_" + organizationId.intValue());
			for (StaffRelationBuild relation : relations) {
				if (buildId.intValue() == relation.getBuildId().intValue()) {
					obj2.setChecked(true);
				}
			}
			trees.add(obj2);
		}
		return trees;
	}

	@Override
	public void assignStaffRelationBuild(String buildIds, int staffId) {
		//删除员工与项目组织、与楼栋的关联
		staffDao.deleteStaffRelation(staffId);

		List<Integer> organizationList = new ArrayList<Integer>();
		String[] buildArray = buildIds.split(";");
		for (String buildId : buildArray) {
			//保存员工-楼栋的关联
			StaffRelationBuild relationBuild = new StaffRelationBuild();
			relationBuild.setBuildId(Integer.parseInt(buildId));
			relationBuild.setStaffId(staffId);
			propertyDao.saveObj(relationBuild);

			//保存员工-项目的关联
			TbBuild build = propertyDao.getTbBuildById(Integer.parseInt(buildId));
			if (!organizationList.contains(build.getOrganizationId())) {
				organizationList.add(build.getOrganizationId());

				StaffRelationOrg relationOrg = new StaffRelationOrg();
				relationOrg.setStaffId(staffId);
				relationOrg.setOrganizationId(build.getOrganizationId());
				propertyDao.saveObj(relationOrg);
			}
		}
	}

	@Override
	public void deleteStaff(Staff staff) {
		staffDao.deleteStaffRelation(staff.getId());
		staffDao.updateObj(staff);
	}
}
