package com.bizzan.bitrade.service;

import com.bizzan.bitrade.dao.AppHeaderDao;
import com.bizzan.bitrade.entity.AppHeader;
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 Header 业务层
 */
@Service
public class AppHeaderService extends BaseService<AppHeader> {

    @Autowired
    private AppHeaderDao appHeaderDao;

    public AppHeader save(AppHeader header) {
        return appHeaderDao.save(header);
    }

    public AppHeader findById(Long id) {
        return appHeaderDao.findOne(id);
    }

    @Override
    public List<AppHeader> findAll() {
        return appHeaderDao.findAll();
    }

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

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

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

    /**
     * 查询当前生效的Header列表（App端调用）
     */
    public List<AppHeader> findActiveHeaders() {
        return appHeaderDao.findActiveHeaders();
    }
}
