package com.palmnest.service.impl;

import com.palmnest.application.Constants;
import com.palmnest.application.ElementConstant;
import com.palmnest.application.bean.Page;
import com.palmnest.application.bean.Pagenation;
import com.palmnest.application.dto.output.app.OrganizationDto;
import com.palmnest.dao.OrganizationDao;
import com.palmnest.dao.property.PropertyDao;
import com.palmnest.model.*;
import com.palmnest.model.property.BuildOrgRelation;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.model.property.TbOrganizationConfig;
import com.palmnest.service.OrganizationManager;
import com.palmnest.service.UserManager;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 组织组织管理业务层接口实现
 *
 * @author yubing
 * @since 2016-01-11
 */
@Component("organizationManager")
public class OrganizationManagerImpl extends GenericManagerImpl<Object, Integer> implements OrganizationManager {

	private OrganizationDao organizationDao;
	@Autowired
	private PropertyDao     propertyDao;
	@Autowired
	private UserManager     userManager;

	@Autowired
	public void setOrganizationDao(OrganizationDao organizationDao) {
		this.organizationDao = organizationDao;
		this.dao = organizationDao;
	}

	public Pagenation<TbCompany> queryCompanyList(int pageNo, List<Criterion> filterList) {
		Map<String, String> aliasMap  = Collections.<String, String>emptyMap();
		List<Order>         orderList = new ArrayList<Order>();
		orderList.add(Order.asc("orderNumber"));
		return organizationDao.queryPage(pageNo, Constants.LIMIT_PAGESIZE, aliasMap, filterList, orderList, TbCompany.class);
	}

	@Override
	public TbCompany getCompanyById(int id) {
		return organizationDao.getCompanyById(id);
	}

	@Override
	public TbOrganization getOrganizationById(int id) {
		return organizationDao.getOrganizationById(id);
	}

	@Override
	public List<TbCompany> getCompanyList(String isenabled) {
		return organizationDao.getCompanyList(isenabled);
	}

	@Override
	public List<User> getUserList(String projectcode, String position) {
		return organizationDao.getUserList(projectcode, position);
	}

	@Override
	public Page queryCompanyList(int pageNo, String strHql) {
		return organizationDao.queryCompanyList(pageNo, Constants.LIMIT_PAGESIZE, strHql);
	}
	
	/*@Override
	public String saveOrUpdateCompany(TbCompany company, String[] buildIds) {
		String retCode="0";
		if(company.getId()!=null){
			organizationDao.updateObj(company);
			propertyDao.deleteBuildOrg(company.getId(), ElementConstant.USER_ORG_COMPANY);
			retCode="2";
		}
		else{
			organizationDao.saveObj(company);
			retCode="1";
			
			User user=userManager.getUserByUsername2(company.getCreator());
			
			AppUserOrgRelation relation=new AppUserOrgRelation();
			relation.setOrgType(ElementConstant.USER_ORG_COMPANY);
			relation.setOrgId(company.getId());
			relation.setUserId(user.getId().intValue()); 
			relation.setCreator(company.getCreator());
			relation.setCreateTime(new Date());
			organizationDao.saveObj(relation);
			
			if(!company.getCreator().equals(Constants.ADMIN_NAME)){
				//company add user relation
				AppUserOrgRelation relation1=new AppUserOrgRelation();
				relation1.setOrgType(ElementConstant.USER_ORG_COMPANY);
				relation1.setOrgId(company.getId());
				relation1.setUserId(1); 
				relation1.setCreator(company.getCreator());
				relation1.setCreateTime(new Date());
				organizationDao.saveObj(relation1);
			}
		}
		if(buildIds!=null&&buildIds.length>0){
			for (String buildId : buildIds) {
				BuildOrgRelation relation=new BuildOrgRelation();
				relation.setType(ElementConstant.USER_ORG_COMPANY);
				relation.setOrgId(company.getId());
				relation.setBuildId(Integer.parseInt(buildId));
				relation.setCreateTime(new Date());
				relation.setCreator(company.getCreator());
				organizationDao.saveObj(relation);
			}
		}
		return retCode;
	}*/

	@Override
	public String saveOrUpdateCompany(TbCompany company) {
		String retCode = "0";
		if (company.getId() != null) {
			organizationDao.updateObj(company);
			retCode = "2";
		} else {
			organizationDao.saveObj(company);
			retCode = "1";
		}
		return retCode;
	}

	@Override
	public Page queryDeptList(int pageNo, String strHql) {
		return organizationDao.queryDeptList(pageNo, Constants.LIMIT_PAGESIZE, strHql);
	}

	@Override
	public String saveOrUpdateDept(TbDept dept, String[] buildIds) {
		String retCode = "0";
		if (dept.getId() != null) {
			organizationDao.updateObj(dept);
			propertyDao.deleteBuildOrg(dept.getId(), ElementConstant.USER_ORG_DEPT);
			retCode = "2";
		} else {
			dept.setCode(getMaxDeptId() + "");
			dept.setParentCode("0");
			organizationDao.saveObj(dept);
			retCode = "1";
		}
		if (buildIds != null && buildIds.length > 0) {
			for (String buildId : buildIds) {
				BuildOrgRelation relation = new BuildOrgRelation();
				relation.setType(ElementConstant.USER_ORG_DEPT);
				relation.setOrgId(dept.getId());
				relation.setBuildId(Integer.parseInt(buildId));
				relation.setCreateTime(new Date());
				relation.setCreator(dept.getCreator());
				organizationDao.saveObj(relation);
			}
		}
		return retCode;
	}

	@Override
	public List<TbDept> getAllDept() {
		return organizationDao.getAllDept();
	}

	@Override
	public int getMaxDeptId() {
		return organizationDao.getMaxDeptid();
	}

	@Override
	public TbDept getDept(String treeCode, String parentCode) {
		return organizationDao.getDept(treeCode, parentCode);
	}

	@Override
	public void deleteDept(String treeCode, String parentCode) {

	}

	@Override
	public TbDept getDept(int id) {
		return organizationDao.getDept(id);
	}

	@Override
	public FloorInfo getFloor(int id) {
		return organizationDao.getFloor(id);
	}

	@Override
	public List<TbCompany> getAppUserRelationCompany(int userid) {
		List<TbCompany>              companys  = new ArrayList<TbCompany>();
		List<AppUserCompanyRelation> relations = userManager.getAppUserCompanyByUserId(userid);
		if (null != relations && relations.size() > 0) {
			for (AppUserCompanyRelation relation : relations) {
				companys.add(getCompanyById(relation.getCompanyId()));
			}
		}

		/*List<Integer> tempCompanyIds=new ArrayList<Integer>();
		
		List<AppUserOrgRelation> relaitons=userManager.getAppUserOrgByUserId(userid, ElementConstant.USER_ORG_COMPANY);
		for (AppUserOrgRelation relation : relaitons) {
			companys.add(getCompanyById(relation.getOrgId()));
			tempCompanyIds.add(relation.getOrgId().intValue());
		}
		List<Map<String,Object>> companyIds=organizationDao.getAppUserCompIds(userid);
		for (Map<String, Object> obj : companyIds) {
			Integer companyId=(Integer) obj.get("compId");
			if(!tempCompanyIds.contains(companyId.intValue())&&companyId!=null){
				companys.add(getCompanyById(companyId));
				tempCompanyIds.add(companyId);
			}
		}*/

		return companys;
	}

	@Override
	public List<TbOrganization> getUserRelationOrganizationList(Long userid) {

		User user = userManager.get(userid);
		if (user != null && user.isAdmin()) {
			return organizationDao.getAllOrganization();
		}
		List<TbOrganization>             organizations = new ArrayList<TbOrganization>();
		List<AppUserOrgnizationRelation> relaitons     = userManager.getUserOrgByUserId(userid);
		List<Integer>                    orgList       = new ArrayList<Integer>();
		for (AppUserOrgnizationRelation relation : relaitons) {
			if (!orgList.contains(relation.getOrgId())) {
				orgList.add(relation.getOrgId());
				TbOrganization org = getOrganizationById(relation.getOrgId());
				if ("1".equals(org.getIsEnabled()) && "1".equals(org.getStatus())) {
					organizations.add(org);
				}
			}
		}
		return organizations;
	}

//	@Override
//	public List<Map<String, Object>> getAppUserCompIds(int userId) {
//		return organizationDao.getAppUserCompIds(userId);
//	}

	@Override
	public List<OrganizationDto> findAll() {
		List<TbOrganization>  orgs = organizationDao.getAllOrganization();
		List<OrganizationDto> dtos = new ArrayList<OrganizationDto>();
		for (TbOrganization org : orgs) {
			dtos.add(new OrganizationDto(org.getId(), org.getName()));
		}
		return dtos;
	}

	@Override
	public TbOrganizationConfig getObj(String id) {
		return organizationDao.getObj(id);
	}

	@Override
	public TbOrganizationConfig getDiscoverConfigByOrgId(String orgId) {
		return organizationDao.getDiscoverConfigByOrgId(orgId);
	}

	@Override
	public String saveOrUpdateTbOrgConfig(TbOrganizationConfig config) {
		String retCode = "0";
		if (config.getId() != null) {
			organizationDao.updateObj(config);
			retCode = "2";
		} else {
			organizationDao.saveObj(config);
			retCode = "1";
		}
		return retCode;
	}

	@Override
	public TbOrganization getByMettingClient(String mettingClient) {
		return organizationDao.getByMettingClient(mettingClient);
	}

	@Override
	public TbOrganization getOrganizationInfoByName(String name) throws Exception {
		return organizationDao.getOrganizationInfoByName(name);
	}

	@Override
	public List<TbOrganization> enableOrgList(Integer enable) {
		return organizationDao.enableOrgList(enable);
	}
	
	@Override
	public List<TbOrganization> passTypeOrgList(Integer passType) {
		return organizationDao.passTypeOrgList(passType);
	}
}
