package com.jpsoft.shinestar.modules.base.service.impl;

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

import com.jpsoft.shinestar.modules.common.utils.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.jpsoft.shinestar.modules.base.dao.WatchInfoDAO;
import com.jpsoft.shinestar.modules.base.entity.WatchInfo;
import com.jpsoft.shinestar.modules.base.service.WatchInfoService;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.github.pagehelper.PageHelper;

@Transactional
@Component(value="watchInfoService")
public class WatchInfoServiceImpl implements WatchInfoService {
	@Resource(name="watchInfoDAO")
	private WatchInfoDAO watchInfoDAO;

	@Override
	public WatchInfo get(String id) {
		// TODO Auto-generated method stub
		return watchInfoDAO.get(id);
	}

	@Override
	public int insert(WatchInfo model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());
		
		return watchInfoDAO.insert(model);
	}

	@Override
	public int update(WatchInfo model) {
		// TODO Auto-generated method stub
		return watchInfoDAO.update(model);
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return watchInfoDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = watchInfoDAO.exist(id);
		
		return count > 0 ? true : false;
	}
	
	@Override
	public List<WatchInfo> list() {
		// TODO Auto-generated method stub
		return watchInfoDAO.list();
	}
		
	@Override
	public Page<WatchInfo> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize, boolean count, List<Sort> sortList) {
        Page<WatchInfo> page = PageHelper.startPage(pageNumber,pageSize,count).doSelectPage(()->{
            watchInfoDAO.search(searchParams,sortList);
        });
        
        return page;
	}

	@Override
	public WatchInfo findOneByDeviceNo(String deviceNo) {
		return watchInfoDAO.findOneByDeviceNo(deviceNo);
	}

	@Override
	public int updateHeartBeatUpdateTime(String deviceNo, Date updateTime) {
		return watchInfoDAO.updateHeartBeatUpdateTime(deviceNo,updateTime);
	}

	@Override
	public WatchInfo findDefaultByPersonId(Long personId) {
		return watchInfoDAO.findDefaultByPersonId(personId);
	}

	@Override
	public String encodeContacts(String[] arr) {
		StringBuilder sb = new StringBuilder();

		for (int i=0;i<arr.length;i++){
			if (sb.length()>0){
				sb.append(",");
			}

			if (i%2==0){
				sb.append(arr[i]);
			}
			else{
				//联系人名称进行unicode编号
				sb.append(StringUtils.toUnicode(arr[i]));
			}
		}

		return sb.toString();
	}

	@Override
	public List<WatchInfo> onlineList() {
		return watchInfoDAO.onlineList();
	}
}