package com.bizzan.bitrade.service;

import com.bizzan.bitrade.dao.AppBannerDao;
import com.bizzan.bitrade.entity.AppBanner;
import com.bizzan.bitrade.service.Base.BaseService;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * APP Banner 业务层
 */
@Service
public class AppBannerService extends BaseService<AppBanner> {

    @Autowired
    private AppBannerDao appBannerDao;

    public AppBanner save(AppBanner banner) {
        return appBannerDao.save(banner);
    }

    public AppBanner findById(Long id) {
        return appBannerDao.findOne(id);
    }

    @Override
    public List<AppBanner> findAll() {
        return appBannerDao.findAll();
    }

    public Page<AppBanner> findAll(Predicate predicate, Pageable pageable) {
        return appBannerDao.findAll(predicate, pageable);
    }

    public void deleteById(Long id) {
        appBannerDao.delete(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            AppBanner banner = findById(id);
            Assert.notNull(banner, "validate id!");
            deleteById(id);
        }
    }

    /**
     * 查询当前生效的Banner列表（App端调用）
     */
    public List<AppBanner> findActiveBanners() {
        return appBannerDao.findActiveBanners();
    }
}
