package com.ssj.service.sys.parent.service.impl;

import com.ssj.bean.sys.parent.domain.SSJPeiyouBook;
import com.ssj.dao.sys.parent.dao.SSJPeiyouBookDao;
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.conch.parents.dto.GetPeiyouBookUnitDto;
import com.ssj.service.conch.parents.dto.GetSecondUnitInfoDto;
import com.ssj.service.mapper.SSJPeiyouBookMapper;
import com.ssj.service.sys.parent.service.SSJPeiyouBookService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Transactional;

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

@Service
@Transactional
public class SSJPeiyouBookServiceImpl extends BaseServiceImpl<SSJPeiyouBook, String> implements SSJPeiyouBookService {

	private Logger  logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SSJPeiyouBookDao dao;

    @Autowired
    private SSJPeiyouBookMapper peiyouBookMapper;

	@Override
	public PagingAndSortingRepository<SSJPeiyouBook, String> getDao() {
		return dao;
	}

    @Override
    public List<String> findGroupByBookName(String courseName, String grade) {
        return dao.findGroupByBookName(courseName,grade);
    }

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

    @Override
    public List<GetPeiyouBookUnitDto> findUnitBySelective(String grade, String courseName, String bookName) {
        return peiyouBookMapper.findUnitBySelective(grade, courseName, bookName);
    }

    @Override
    public List<GetSecondUnitInfoDto> findSecondUnitBySelective(String grade, String courseName, String bookName, String unit) {
        return peiyouBookMapper.findSecondUnitBySelective(grade, courseName, bookName, unit);
    }

    @Override
    public List<SSJPeiyouBook> findBookPathBySelective(String grade, String courseName, String bookName, String unit, String secondUnit) {
        if(StringUtils.isNotBlank(bookName)) {
            return dao.findByGradeAndCourseNameAndBookNameAndUnitAndSecondUnit(grade, courseName, bookName, unit, secondUnit);
        }else {
            return dao.findByGradeAndCourseNameAndUnitAndSecondUnit(grade, courseName, unit, secondUnit);
        }
    }
}
