package com.hai.service.impl;

import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.dao.BsOrganizationMapper;
import com.hai.entity.BsOrganization;
import com.hai.entity.BsOrganizationExample;
import com.hai.entity.SecRegion;
import com.hai.model.CommonTreeModel;
import com.hai.service.BsOrganizationService;
import com.hai.service.CommonService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("bsOrganizationService")
public class BsOrganizationServiceImpl implements BsOrganizationService {

	@Resource
	private BsOrganizationMapper bsOrganizationMapper;

	@Resource
	private CommonService commonService;

	@Override
	public BsOrganization findById(Long organizationId) {
		BsOrganization organization = bsOrganizationMapper.selectByPrimaryKey(organizationId);
		if (organization.getRegionId() != null) {
			SecRegion secRegion = commonService.getRegionsById(Long.valueOf(organization.getRegionId()));
			if (secRegion != null){
				organization.setRegionName(secRegion.getRegionName());
			}
		}
		return organization;
	}

	@Override
	public BsOrganization findTopByCompany(Long companyId) {
		BsOrganizationExample example = new BsOrganizationExample();
		example.createCriteria().andCompanyIdEqualTo(companyId).andParentIdIsNull();
		List<BsOrganization> organizationList = bsOrganizationMapper.selectByExample(example);
		if (organizationList != null && organizationList.size() != 0) {
			return organizationList.get(0);
		}
		return null;
	}

	@Override
	public CommonTreeModel findTree(Long companyId) {
		// 顶级部门
		BsOrganizationExample example = new BsOrganizationExample();
		example.createCriteria().andCompanyIdEqualTo(companyId).andStatusEqualTo(1).andParentIdIsNull();
		List<BsOrganization> secOrganizations = bsOrganizationMapper.selectByExample(example);
		if (secOrganizations.size()!=0){
			BsOrganization secOrganization = secOrganizations.get(0);
			CommonTreeModel commonTreeModel = new CommonTreeModel();
			commonTreeModel.setId(secOrganization.getId());
			commonTreeModel.setText(secOrganization.getName());
			// 子级部门集合
			BsOrganizationExample example1 = new BsOrganizationExample();
			example1.createCriteria().andCompanyIdEqualTo(companyId).andStatusEqualTo(1).andParentIdIsNotNull();
			List<BsOrganization> secOrganizationList = bsOrganizationMapper.selectByExample(example1);
			buildTree(commonTreeModel,secOrganizationList);
			return commonTreeModel;
		}else{
			throw ErrorHelp.genException(SysCode.System, ErrorCode.UN_FIND_ORGANIZATION_ERROR, "");
		}
	}

	@Override
	public List<BsOrganization> findOrganizationList(Long companyId) {
		BsOrganizationExample example = new BsOrganizationExample();
		example.createCriteria().andCompanyIdEqualTo(companyId).andStatusEqualTo(1);
		return bsOrganizationMapper.selectByExample(example);
	}

	@Override
	public List<BsOrganization> findAllSon(Long companyId) {
		BsOrganizationExample organExample = new BsOrganizationExample();
		organExample.createCriteria().andCompanyIdEqualTo(companyId).andParentIdIsNotNull().andStatusEqualTo(1);
		return bsOrganizationMapper.selectByExample(organExample);
	}

	@Override
	public BsOrganization findByName(Long companyId, String name) {
		BsOrganizationExample example = new BsOrganizationExample();
		BsOrganizationExample.Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(name);
		criteria.andCompanyIdEqualTo(companyId);
		criteria.andStatusEqualTo(1);
		List<BsOrganization> list = bsOrganizationMapper.selectByExample(example);
		if (list != null && list.size() != 0) {
			return list.get(0);
		}else {
			return null;
		}
	}

	@Override
	public void addOrganization(BsOrganization secOrganization) {
		bsOrganizationMapper.insert(secOrganization);
	}

	@Override
	public void updateOrganization(BsOrganization secOrganization) {
		bsOrganizationMapper.updateByPrimaryKey(secOrganization);
	}

	@Override
	public List<BsOrganization> findSon(Long organizationId) {
		BsOrganizationExample example = new BsOrganizationExample();
		example.createCriteria().andParentIdEqualTo(organizationId).andStatusEqualTo(1);
		return bsOrganizationMapper.selectByExample(example);
	}

	@Override
	public BsOrganization getOrgByRegionAbbreviate(String regionAbbreviate) {
		BsOrganizationExample example = new BsOrganizationExample();
		example.createCriteria().andRegionAbbreviateEqualTo(regionAbbreviate).andStatusEqualTo(1);
		List<BsOrganization> list = bsOrganizationMapper.selectByExample(example);
		if(list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<BsOrganization> getOrganizationUnit(String regionId) {
		BsOrganizationExample example = new BsOrganizationExample();
		example.createCriteria().andRegionIdEqualTo(regionId).andStatusEqualTo(1).andCompanyIdEqualTo(-1L);
		example.setOrderByClause("id");
		return bsOrganizationMapper.selectByExample(example);
	}

	public void buildTree(CommonTreeModel parent, List<BsOrganization> baseOrganizations) {
		baseOrganizations.forEach(baseOrganization -> {
			if (baseOrganization.getParentId().equals(parent.getId())) {
				CommonTreeModel child = new CommonTreeModel();
				child.setId(baseOrganization.getId());
				child.setText(baseOrganization.getName());
				child.setpId(parent.getId());
				parent.add(child);
				buildTree(child,baseOrganizations);
			}
		});
	}
}
