package com.leanwo.activiti.db;

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

import javax.inject.Inject;
import javax.inject.Named;

import com.leanwo.activiti.dto.base.AccountManagementDto;
import com.leanwo.activiti.dto.base.KeyValue;

@Named
public class AccountServiceImpl implements AccountService {

	@Inject
	private AccountManagementService accountManagementService;

	public synchronized List<KeyValue> getAccounts() {
		List<KeyValue> accountCache = new ArrayList<KeyValue>();
		List<AccountManagementDto> accountManagementDtos = accountManagementService.loadAccountManagement(false);
		AccountManagementDto accountManagementDto0 = getDefaultAccountManagementDto();
		KeyValue keyValue = new KeyValue();
		keyValue.setKey(accountManagementDto0.getAccountId());
		keyValue.setValue(accountManagementDto0.getAccountName());
		accountCache.add(keyValue);

		if (accountManagementDtos != null) {
			for (AccountManagementDto accountManagementDto : accountManagementDtos) {
				keyValue = new KeyValue();
				keyValue.setKey(accountManagementDto.getAccountId());
				keyValue.setValue(accountManagementDto.getAccountName());
				accountCache.add(keyValue);
			}
		}
		return accountCache;
	}

	public String uniqueAccoutName(Long accountId) {
		List<AccountManagementDto> accountManagementDtos = accountManagementService.loadAccountManagement(false);
		if (accountManagementDtos != null) {
			for (AccountManagementDto accountManagementDto : accountManagementDtos) {
				if (accountManagementDto.getAccountId().equals(accountId)) {
					return accountManagementDto.getAccountName();
				}
			}
		}
		return null;
	}

	
	/**
	 * 获取系统默认的账套管理器
	 * 
	 * @return
	 */
	private AccountManagementDto getDefaultAccountManagementDto() {
		AccountManagementDto defaultAccountManagementDto = new AccountManagementDto();
		defaultAccountManagementDto.setId(0L);
		defaultAccountManagementDto.setAccountId(0L);
		defaultAccountManagementDto.setAccountName("应用账套");
		return defaultAccountManagementDto;
	}
}
