/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.pushinfo.service;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.JPushClientUtil;
import com.jeeplus.modules.pushinfo.dao.PushinfoDao;
import com.jeeplus.modules.pushinfo.entity.Pushinfo;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 测试Service
 * @author yangfan
 * @version 2017-07-28
 */
@Service
@Transactional(readOnly = true)
public class PushinfoService extends CrudService<PushinfoDao, Pushinfo> {
	@Autowired
	private PushinfoDao pushinfoDao;
	public Pushinfo get(String id) {
		return super.get(id);
	}
	public  List<Pushinfo> getByPushId(Pushinfo pushinfo) {
		return dao.getByPushId(pushinfo);
	}

	public List<Pushinfo> findList(Pushinfo pushinfo) {
		return super.findList(pushinfo);
	}
	
	public Page<Pushinfo> findPage(Page<Pushinfo> page, Pushinfo pushinfo) {
		return super.findPage(page, pushinfo);
	}
	public Page<Pushinfo> findPushList(Page<Pushinfo> page, Pushinfo pushinfo) {
		List<Pushinfo> pages = null;
		try{
			pushinfo.setPage(page);
			pages = pushinfoDao.findPushList(pushinfo);
			page.setList(pages);
		}catch (Exception e){
			logger.info("Exception e:"+e);
			e.printStackTrace();
		}
		return page;
	}

	@Transactional(readOnly = false)
	public void save(Pushinfo pushinfo) {
		super.save(pushinfo);
	}
	
	@Transactional(readOnly = false)
	public void delete(Pushinfo pushinfo) {
		super.delete(pushinfo);
	}
	
	@Transactional(readOnly = false)
	public int updateDelflagByPushId(String pushId,String delflag){
		return pushinfoDao.updateDelflagByPushId(pushId,delflag);
	}

	@Transactional(readOnly = false)
	public int updateStatusByPushId(String pushId,String status){
		return pushinfoDao.updateStatusByPushId(pushId,status);
	}

	@Transactional(readOnly = false)
	public void pushInfoToApp(Pushinfo pushinfo,Map<String,Object> extras){
		this.save(pushinfo);
		Pushinfo selectPush = new Pushinfo();
		selectPush.setPushId(pushinfo.getPushId());
		selectPush.setPushUserId(UserUtils.getUser().getId());
		List<Pushinfo> pushinfos = this.findList(selectPush);
		if (pushinfos!=null && pushinfos.size()!=0){
			Pushinfo push = pushinfos.get(0);
			push.setStatus(pushinfo.getStatus());
			this.save(push);
		}
		JPushClientUtil.sendNotificationToAlias(pushinfo.getTitle(), pushinfo.getContent(), extras, pushinfo.getPushUserId());
	}

}