package com.bizzan.bitrade.service;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.bizzan.bitrade.dao.ActivityDao;
import com.bizzan.bitrade.entity.Activity;
import com.bizzan.bitrade.pagination.Criteria;
import com.bizzan.bitrade.pagination.Restrictions;
import com.bizzan.bitrade.service.Base.BaseService;
import com.querydsl.core.types.Predicate;

import java.util.List;

@Service
public class ActivityService extends BaseService {
	
	@Autowired
    private ActivityDao activityDao;
	
	public Activity findOne(Long id) {
		return activityDao.findOne(id);
	}
	
    public Activity save(Activity activity) {
        return activityDao.save(activity);
    }

    public Activity saveAndFlush(Activity activity) {
        return activityDao.saveAndFlush(activity);
    }
    
    public Activity findById(Long id) {
        return activityDao.findOne(id);
    }
    
    public Page<Activity> findAll(Predicate predicate, Pageable pageable){
    	return activityDao.findAll(predicate, pageable);
    }

	public Page<Activity> queryByStep(int pageNo, int pageSize, int step) {
        Sort orders = Criteria.sortStatic("createTime.desc");
        //分页参数
        PageRequest pageRequest = new PageRequest(pageNo - 1, pageSize, orders);
        //查询条件
        Criteria<Activity> specification = new Criteria<Activity>();
        if(step != -1) {
        	specification.add(Restrictions.eq("step", step, false));
        }
        specification.add(Restrictions.eq("status", 1, false));
        return activityDao.findAll(specification, pageRequest);
	}

	public List<Activity> findByTypeAndStep(int type, int step) {
	    return activityDao.findAllByTypeAndStep(type, step);
    }

    public Page<Activity> findAll(Integer type, Pageable pageable) {
        return findAll(type, null, pageable);
    }

    public Page<Activity> findAll(Integer type, String activityLink, Pageable pageable) {
        Criteria<Activity> specification = new Criteria<Activity>();
        if (type != null) {
            specification.add(Restrictions.eq("type", type, false));
        }
        if (activityLink != null && !activityLink.trim().isEmpty()) {
            specification.add(Restrictions.eq("activityLink", activityLink.trim(), false));
        }
        return activityDao.findAll(specification, pageable);
    }

    /** App 公开接口：按标识码查询落地页活动（type=100，status=1，取最新一条） */
    public Activity findLandingByActivityLink(String activityLink) {
        Criteria<Activity> spec = new Criteria<Activity>();
        spec.add(Restrictions.eq("type", 100, false));
        spec.add(Restrictions.eq("status", 1, false));
        if (activityLink != null && !activityLink.trim().isEmpty()) {
            spec.add(Restrictions.eq("activityLink", activityLink.trim(), false));
        }
        PageRequest pr = new PageRequest(0, 1, new Sort(Sort.Direction.DESC, "id"));
        Page<Activity> page = activityDao.findAll(spec, pr);
        return page.getContent().isEmpty() ? null : page.getContent().get(0);
    }
}
