package com.iamberry.app.service;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jpush.api.JPushClient;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iamberry.app.config.ImberryConfig;
import com.iamberry.app.core.entity.User;
import com.iamberry.app.mapper.LogMapper;
import com.iamberry.app.mapper.MachineMapper;
import com.iamberry.app.mapper.MessageMapper;
import com.iamberry.app.mapper.MilkMapper;
import com.iamberry.app.mapper.UserMapper;
import com.iamberry.app.ulitity.Utility;

@Service
public class BaseService {

	@Autowired
	protected UserMapper userMapper;
	@Autowired
	protected MachineMapper machineMapper;
	@Autowired
	protected MilkMapper milkMapper;
	@Autowired
	protected LogMapper logMapper;
	@Autowired
	protected MessageMapper messageMapper;

	protected static final ObjectMapper mapper = new ObjectMapper();

	public static JPushClient jpushClient = new JPushClient(ImberryConfig.MASTERSECRET, ImberryConfig.APPKEY);

	protected static final int PAGE_SIZE = 10;

	protected String getEncToken(long userId) {
		String tokenKey = Utility.getRandomString(10);
		String base64 = userId + "|" + tokenKey + "|" + System.currentTimeMillis();
		return new String(Base64.encodeBase64(base64.getBytes()));
	}

	public User validateUserToken(String token) {
		User userInfo = userMapper.selectUserByToken(token);

		if (userInfo == null) {
			return null;
		}
		return userInfo;
	}
}
