package com.ssj.service.sys.give;

import com.ssj.bean.sys.give.GiveRecord;
import com.ssj.dao.sys.give.GiveRecordDao;
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.framework.core.util.StringUtil;
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;

/**
 * @author Mount Liu
 * @date 2019/5/14
 */
@Service
@Transactional
public class GiveRecordServiceImpl extends BaseServiceImpl<GiveRecord, String> implements GiveRecordService {

    @Autowired
    private GiveRecordDao giveRecordDao;

    @Override
    public PagingAndSortingRepository<GiveRecord, String> getDao() {
        return giveRecordDao;
    }

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

    @Override
    public GiveRecord findByVipId(String vipId) {
        return giveRecordDao.findTopByVipIdAndGiveWay(vipId, 1);
    }

    @Override
    public List<GiveRecord> findValidGiveRecord(String vacationLimitId, String vipId, int giveWay, String vipServiceId) {
        if (StringUtil.isNotBlank(vipId)) {
            return giveRecordDao.findByVacationLimitIdAndVipIdAndGiveWayAndInvalidAndVipServiceId(vacationLimitId, vipId, giveWay, 0, vipServiceId);
        } else {
            return giveRecordDao.findByVacationLimitIdAndGiveWayAndInvalidAndVipServiceId(vacationLimitId, giveWay, 0, vipServiceId);
        }
    }

}
