package com.ssj.dao.weixin.library.dao.impl;

import com.ssj.dao.weixin.library.dao.ILibJoinConsumeQueryDao;
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;

@Repository
public class ILibJoinConsumeQueryDaoImpl   implements ILibJoinConsumeQueryDao {

	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public Page<Map<String, Object>> findByPage(Map<String, Object> params, Pageable pageable) {
		StringBuilder selSql = new StringBuilder(); 
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t.*,fm.unit_name as fm_unit_name,tm.unit_name as tm_unit_name,fm.type as fm_type,tm.type as tm_type,fj.league_name as fj_league_name,tj.league_name as tj_league_name  FROM `tb_lib_join_consume` t ");
        selSql.append(" LEFT JOIN fx_merchant fm ON t.from_merchant_id=fm.id ");
		selSql.append(" LEFT JOIN fx_merchant tm ON t.to_merchant_id=tm.id ");
		selSql.append(" LEFT JOIN tb_lib_join fj ON t.from_lib_id=fj.id ");
		selSql.append(" LEFT JOIN tb_lib_join tj ON t.to_lib_id=tj.id ");
		selSql.append(" where (t.from_merchant_id is NOT NULL OR t.to_merchant_id is NOT NULL) ");
		if(params.containsKey("remarks") && StringUtils.isNotEmpty(params.getOrDefault("remarks","").toString())) {
			selSql.append(" AND t.remarks like concat('%',?,'%')   ");
			queryParams.add(params.get("remarks"));
		}
		if(params.containsKey("type") && StringUtils.isNotEmpty(params.getOrDefault("type","").toString())) {
			if(params.get("type").toString().equals("-1")) {
				selSql.append(" AND t.type in (1,2,3,4,5)  ");
			}else if(params.get("type").toString().equals("-2")) {
				selSql.append(" AND t.type in (6,7,8,9,10,11)  ");
			} else {
				selSql.append(" AND t.type =?  ");
				queryParams.add(params.get("type"));
			}
		}
		if(params.containsKey("startTime") && StringUtils.isNotEmpty(params.getOrDefault("startTime","").toString())) {
			selSql.append(" AND TO_DAYS(t.create_time)>=TO_DAYS(?)  ");
			queryParams.add(params.get("startTime"));
		}
		if(params.containsKey("endTime") && StringUtils.isNotEmpty(params.getOrDefault("endTime","").toString())) {
			selSql.append(" AND TO_DAYS(t.create_time)<=TO_DAYS(?)  ");
			queryParams.add(params.get("endTime"));
		}
		if(params.containsKey("merchantId") && StringUtils.isNotEmpty(params.getOrDefault("merchantId","").toString())) {
			selSql.append(" AND (  ");
			selSql.append("  t.to_merchant_id=? AND t.type=1 OR ");	queryParams.add(params.get("merchantId"));
			selSql.append("  t.to_merchant_id=? AND t.type=4 OR ");	queryParams.add(params.get("merchantId"));
			selSql.append("  t.from_merchant_id=? AND t.type=2 OR ");	queryParams.add(params.get("merchantId"));
			selSql.append("  t.from_merchant_id=? AND t.type=3 OR ");	queryParams.add(params.get("merchantId"));
			selSql.append("  t.from_merchant_id=? AND t.type=5 OR ");	queryParams.add(params.get("merchantId"));
			
			selSql.append("  t.to_merchant_id=? AND t.type=6 OR ");	queryParams.add(params.get("merchantId"));
			selSql.append("  t.to_merchant_id=? AND t.type=9 OR ");	queryParams.add(params.get("merchantId"));
			
			selSql.append("  t.from_merchant_id=? AND t.type=7 OR");	queryParams.add(params.get("merchantId"));
			selSql.append("  t.from_merchant_id=? AND t.type=8 OR");	queryParams.add(params.get("merchantId"));
			selSql.append("  t.from_merchant_id=? AND t.type=10 OR");	queryParams.add(params.get("merchantId"));
			selSql.append("  t.from_merchant_id=? AND t.type=11 ");	queryParams.add(params.get("merchantId"));
			
			selSql.append(" )  ");
		}
		selSql.append(" ORDER BY t.create_time DESC  ");
		return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
	}
	

}
