package com.ssj.service.sys.codeexchange;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssj.bean.sys.codeexchange.CodeExchange;
import com.ssj.dao.sys.codeexchange.CodeExchangeDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.mapper.sys.SysMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author Mount Liu
 * @date 2019/5/9
 */
@Service(version = "1.0")
@Transactional
public class CodeExchangeServiceImpl extends BaseServiceImpl<CodeExchange, String> implements CodeExchangeService {

    @Autowired
    private CodeExchangeDao codeExchangeDao;

    @Autowired
    private SysMapper sysMapper;

    @Override
    public PagingAndSortingRepository<CodeExchange, String> getDao() {
        return codeExchangeDao;
    }

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

    @Override
    public Page<CodeExchange> findByPage(int pageNo, int pageSize, Map<String, Object> params) {
        Page page = new Page(pageNo, pageSize);
        List<CodeExchange> list = sysMapper.findCodeExchangeListByPage(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public CodeExchange findByCode(String code) {
        return codeExchangeDao.findByCode(code);
    }

    @Override
    public CodeExchange findValidCode() {
        //电商优惠券兑换的有效的无受益人的优惠码
        return codeExchangeDao.findTopByModuleAndIsValidAndIsLockAndUserIdIsNull(2, 1, 0);
    }

}
