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

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

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 com.ssj.dao.sys.role.dao.RoleQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.bean.sys.role.domain.AdminRole;
import com.ssj.bean.sys.role.domain.RoleMenu;

@Repository
public class RoleQueryDaoImpl implements RoleQueryDao{
	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public Page<Map<String, Object>> findForPage(
			Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("  select * from sys_role ");
        if (searchParams.get("name") != null && !searchParams.get("name").equals("")) {
        	selSQL.append(" where   name LIKE  CONCAT(CONCAT('%',?,'%'))  ");
        	queryParams.add(searchParams.get("name"));
        }
        selSQL.append("   order by  ");
        selSQL.append(" create_time desc ");
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public void batchInsertRoleMenu(List<RoleMenu> list) {
		dao.batchInsert(list);
	}

	@Override
	public void batchInsertAdminRole(List<AdminRole> list) {
		dao.batchInsert(list);
	}


	@Override
	public Page<Map<String, Object>> findByPageMap(Map<String, Object> params,
			Pageable sp) throws Exception {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("  select * from sys_admin      ");
        if (params.get("nickName") != null && !params.get("nickName").equals("")) {
        	selSQL.append("  where   ( login_name LIKE  CONCAT(CONCAT('%',?,'%'))  or  nick_name like  CONCAT(CONCAT('%',?,'%')) ) ");
        	queryParams.add(params.get("nickName"));
        	queryParams.add(params.get("nickName"));
        }
        selSQL.append("   order by  ");
        selSQL.append(" create_time desc ");
        return dao.findPage(selSQL.toString(), queryParams.toArray(), sp);
	}

	@Override
	public Page<Map<String, Object>> findForPushPage(Map<String, Object> params, Pageable initPage) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("  SELECT 	t.id,t.details,t.price,s.end_time,t.lib_id ");
		selSql.append(" , u.wx_account , v.parent_name ,v.child_name ,v.flow_id, us.wx_account as xs_openid ,  j.league_name");
		selSql.append("  FROM tb_lib_vip_service  s  join  tb_lib_order_details t on s.order_details_id = t.id     ");
		selSql.append("  LEFT  JOIN tb_user us ON t.sales_user_id = us.id     ");
		selSql.append("  LEFT  join  tb_lib_vip  v  on s.vip_id = v.id      ");
		selSql.append("   LEFT  JOIN tb_user u ON v.user_id = u.id  ");
		selSql.append(" LEFT   join  tb_lib_join  j on j.id = s.lib_id      ");
		selSql.append("   where  s.species !='S004' AND  s.stat =1 and v.user_id is not null and (TO_DAYS(s.end_time)) - TO_DAYS(now()) between  0  and  3  ");
		if (params.get("name") != null && !params.get("name").equals("")) {
			selSql.append(" and   v.child_name LIKE  CONCAT(CONCAT('%',?,'%'))  ");
        	queryParams.add(params.get("name"));
        }
		return dao.findPage(selSql.toString(), queryParams.toArray(), initPage);
	}
}
