package org.fouram.core.base.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.core.util.Tools;
import org.fouram.entity.SapUser;
import org.springframework.http.HttpHeaders;

import com.google.common.collect.Lists;

/**
 * 业务逻辑层基类
 */
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 = "APIADMIN@cmccoltdD:1234567";
		String authorizationStr = stringBuilder.append("Basic ").append(Base64Util.encodeString(sapApiUser)).toString();
		System.out.println("测试:"+authorizationStr);
		
		stringBuilder = new StringBuilder();
		sapApiUser = "API01@cmccoltd:cmccoltd123";
		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;
	}
	
	public static String formatPosition(String str) {
		if(Tools.isEmpty(str)) {
			return "";
		}
		str = str.replace("\n", "");
		return str;
	}

	public static String formatBusinessPhone(String str) {
		if(Tools.isEmpty(str)) {
			return "";
		}
		// 021 80171999 -> 021-80171999
		if(str.length() == 12) {
			str = str.replace(" ", "-");
		}
		if(str.indexOf("x")>-1){
			str = str.replace("x", "-");
		}
		return str;
	}

	public Map<String, List<String>> groupList(List<String> list) {
		int listSize = list.size();
		int toIndex = 100;
		Map<String, List<String>> map = new HashMap<>(); // 用map存起来新的分组后数据
		int keyToken = 0;
		for (int i = 0; i < list.size(); i += 100) {
			if (i + 100 > listSize) { // 作用为toIndex最后没有100条数据则剩余几条newList中就装几条
				toIndex = listSize - i;
			}
			List<String> newList = list.subList(i, i + toIndex);
			List<String> userIds = Lists.newArrayList();
			for (String userId : newList) {
				userIds.add(userId);
			}
			map.put("keyName" + keyToken, userIds);
			keyToken++;
		}

		return map;
	}
}
