package com.ssj.service.conch.conch.service.impl;

import com.ssj.bean.conch.conch.domain.SconPadInfo;
import com.ssj.dao.conch.conch.dao.SconPadInfoDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.service.conch.conch.padrequest.PadInitInfoRequest;
import com.ssj.service.conch.conch.service.SconPadInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author yangcan
 * 类描述:小塾学伴pad 服务实现
 * 创建时间:2019/11/7 10:32
 */
@Service
@Transactional
public class SconPadInfoServiceImpl extends BaseServiceImpl<SconPadInfo, String> implements SconPadInfoService {
	
	protected Logger	logger   = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    private TokenManager tokenManager;
	@Autowired
    private SconPadInfoDao sconPadInfoDao;

	@Override
	public PagingAndSortingRepository<SconPadInfo, String> getDao() {
		return sconPadInfoDao;
	}

	/**
	 * 初始化上传信鸽token信息,插入scon_pad_info表
	 * @param request
	 */
	@Override
	public void initInfo(PadInitInfoRequest request, int type) {
        //初始化上传信鸽token信息
        SconPadInfo sconPadInfo = sconPadInfoDao.findTopByPadCodeAndPlatform(request.getPadCode(),request.getPlatform());
        if(sconPadInfo == null) {
            //新增初始化上传信鸽token信息
            sconPadInfo = new SconPadInfo();
        }
        sconPadInfo.setPadCode(request.getPadCode());
        sconPadInfo.setPlatform(request.getPlatform());
        sconPadInfo.setXingeToken(request.getXingeToken());
        sconPadInfo.setType(type);
        sconPadInfo.setCreateTime(new Date());
        this.save(sconPadInfo);
	}

	@Override
	public List<SconPadInfo> findByVipId(String vipId) {
		return sconPadInfoDao.findByVipId(vipId);
	}

	@Override
	public SconPadInfo findTopByVipId(String vipId) {
		return sconPadInfoDao.findTopByVipId(vipId);
	}

    @Override
    public SconPadInfo findTopByVipIdAndType(String vipId, int type) {
        return sconPadInfoDao.findTopByVipIdAndType(vipId,type);
    }

    @Override
    public SconPadInfo findTopByUserIdAndType(String userId, int type) {
        return sconPadInfoDao.findTopByUserIdAndType(userId,type);
    }

    @Override
	public List<SconPadInfo> findByUserId(String userId) {
		return sconPadInfoDao.findByUserId(userId);
	}

	@Override
	public SconPadInfo findTopByPadCode(String padCode) {
		return sconPadInfoDao.findTopByPadCode(padCode);
	}

}
