package com.ssj.service.course.service.impl;

import com.ssj.bean.course.domain.CourseSeriesDetail;
import com.ssj.dao.course.dao.CourseQueryDao;
import com.ssj.dao.course.dao.ICSDetailDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.persistence.DynamicSpecifications;
import com.ssj.framework.core.persistence.SearchFilter;
import com.ssj.service.course.service.ICSDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class CSDetailServiceImpl extends BaseServiceImpl<CourseSeriesDetail, String> implements ICSDetailService {

	@Autowired
	private ICSDetailDao dao;
	
	@Autowired
	private  CourseQueryDao  courseQueryDao;
	
	@Override
	public PagingAndSortingRepository<CourseSeriesDetail, String> getDao() {
		return dao;
	}

	@Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Page<CourseSeriesDetail> findByPage(Map<String, Object> params, Pageable initPage) {
		Map<String, SearchFilter> filters = SearchFilter.parse(params);
		Specification<CourseSeriesDetail> specification = DynamicSpecifications.bySearchFilter(filters.values(), CourseSeriesDetail.class);    
		return  dao.findAll(specification, initPage);
	}

	@Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<CourseSeriesDetail> findBySeriesId(String seriesId) {
		return dao.findBySeriesId(seriesId);
	}

	@Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Integer findBySeriesIdCount(String seriesId) {
		return dao.findBySeriesIdCount(seriesId);
	}

	@Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Map<String, Object>> findSeriesDetailsByUserId(String seriesId,
			String userId,String vipId, String libId) {
		return courseQueryDao.findSeriesDetailsByUserId(seriesId, userId,vipId, libId);
	}

	@Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public CourseSeriesDetail getCourseSeriesDetailByCourseIndex(
			String seriesId, Integer courseIndex) {
		return dao.getCourseSeriesDetailByCourseIndex(seriesId, courseIndex);
	}

	@Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<CourseSeriesDetail> findBySeriesIdAndAfterIndex(String seriesId, Integer courseIndex) {
	
		return dao.findBySeriesIdAndAfterIndex(seriesId,courseIndex);
	}

}
