package com.ssj.dao.sys.wallet.dao.impl;

import com.ssj.dao.sys.wallet.dao.WalletQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

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

/**
 * @author Mount Liu
 * @date 2018/11/19
 */
@Repository
public class WalletQueryDaoImpl implements WalletQueryDao {
    @Autowired
    private PagingHibernateJdbcDao dao;

    @Override
    public Page<Map<String, Object>> findWalletBankcardByPage(Map<String, Object> searchParams, Pageable pageable) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<>();
        selSql.append("select t1.*, " +
                " t2.bank_name, " +
                " t2.bank_icon," +
                " t2.rate " +
                " from ssj_wallet_bankcard t1 " +
                " left join ssj_bank t2 on t1.bank_id = t2.id ");
        String userId= (String) searchParams.get("userId");
        selSql.append(" and t1.user_id = ?");
        queryParams.add(userId);
        String maxCreateTime = (String) searchParams.get("maxCreateTime");
        if (StringUtils.isNotBlank(maxCreateTime)) {
            selSql.append(" and t1.create_time <= ?");
            queryParams.add(maxCreateTime);
        }
        selSql.append(" order by t1.create_time desc ");
        return dao.findPage(selSql.toString(), queryParams.toArray(), pageable);
    }
}
