package org.fouram.core.base.service;

import java.util.List;

import javax.annotation.Resource;

import org.fouram.core.base.dao.DAO;
import org.fouram.core.util.Base64Util;
import org.fouram.core.util.ConfConfig;
import org.fouram.entity.SapUser;
import org.springframework.http.HttpHeaders;

/**
 * 业务逻辑层基类
 */
public abstract class BaseService {

	@Resource(name = "daoSupport")
	private DAO dao;
	
	public Integer save(String sqlStr, Object paramObj) throws Exception {
		return dao.save(sqlStr, paramObj);
	}

	public Integer update(String sqlStr, Object paramObj) throws Exception {
		return dao.update(sqlStr, paramObj);
	}

	public Integer delete(String sqlStr, Object paramObj) throws Exception {
		return dao.delete(sqlStr, paramObj);
	}

	public Object findObject(String sqlStr, Object paramObj) throws Exception {
		return dao.findForObject(sqlStr, paramObj);
	}

	public Object findList(String sqlStr, Object paramObj) throws Exception {
		return dao.findForList(sqlStr, paramObj);
	}
	
	public HttpHeaders getHttpHeaders() {
		HttpHeaders headers = new HttpHeaders();
		StringBuilder stringBuilder = new StringBuilder();
		String sapApiUser = ConfConfig.getConfigString("sapApi.user");
		String authorizationStr = stringBuilder.append("Basic ").append(Base64Util.encodeString(sapApiUser)).toString();
		headers.add("Authorization", authorizationStr);
		return headers;
	}
	
	public static void main(String[] args) {
		StringBuilder stringBuilder = new StringBuilder();
		String sapApiUser = ConfConfig.getConfigString("sapApi.user");
		String authorizationStr = stringBuilder.append("Basic ").append(Base64Util.encodeString(sapApiUser)).toString();
		System.out.println(authorizationStr);
	}
	
	public String getPersonIds(List<SapUser> sapUsers) {
		String personIds = "";
		for (SapUser sapUser : sapUsers) {
			personIds += "'" + sapUser.getPersonId() + "',";
		}
		return personIds;
	}

	public String getUserIds(List<SapUser> sapUsers) {
		String userIds = "";
		for (SapUser sapUser : sapUsers) {
			userIds += "'" + sapUser.getUserId() + "',";
		}
		return userIds;
	}
}