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

import com.ssj.dao.weixin.library.dao.ISubscribeQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
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.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class ISubscribeQueryDaoImpl implements ISubscribeQueryDao{

	
	@Autowired
	private PagingHibernateJdbcDao dao;
	
	@Override
	public Page<Map<String, Object>> findByPage(Map<String, Object> searchParams,
			Pageable pageable) {
		
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT DISTINCT(t.id),v.child_name,v.school,v.grade,v.img_url,t.order_date,t.order_time from tb_lib_subscribe t   ");
        selSQL.append(" LEFT JOIN tb_lib_vip v ON  t.vip_id = v.id   ");
        selSQL.append("  LEFT JOIN tb_lib_vip_service s ON t.vip_id=s.vip_id  ");
        selSQL.append(" LEFT JOIN tb_lib_join  i ON  t.lib_id = i.id    WHERE");
        if("0".equals(searchParams.get("type"))){//预约列表查询
	        selSQL.append("  t.confirm_time is NULL   ");
	        selSQL.append(" AND t.order_time is NOT NULL ");
	        selSQL.append(" AND TO_DAYS(t.order_date)=TO_DAYS(NOW()) ");
        }else if("-1".equals(searchParams.get("type"))){//签离列表查询
	        selSQL.append("  t.leave_time is NULL   ");
	        selSQL.append(" AND t.ryke_time is NOT NULL ");
	        selSQL.append(" AND t.order_time is  NULL ");
	        selSQL.append(" AND TO_DAYS(t.ryke_time)=TO_DAYS(NOW())  AND t.flow_stat = '3' ");
        }
        selSQL.append("  AND i.id=?  AND NOW() BETWEEN s.start_time AND s.end_time  ");
            queryParams.add(searchParams.get("libId"));
        if (searchParams.get("child_name") != null && !searchParams.get("child_name").equals("")) {
        	selSQL.append(" and  child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("child_name"));
        }
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public Page<Map<String, Object>> findLibByVipPage(
			Map<String, Object> searchParams, Pageable pageable) {
		
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT DISTINCT(s.vip_id),v.child_name,v.school,v.grade,v.img_url,v.user_id from  tb_lib_vip_service s   ");
        selSQL.append(" LEFT JOIN tb_lib_vip v ON s.vip_id=v.id   ");
        selSQL.append("  LEFT JOIN tb_lib_join i  ON  s.lib_id = i.id    ");
        selSQL.append(" LEFT JOIN tb_lib_subscribe c ON v.id = c.vip_id ");
        selSQL.append(" AND TO_DAYS(NOW()) = TO_DAYS(c.order_date) ");
        selSQL.append(" AND flow_stat = '3' AND c.user_id IS NOT NULL ");
        selSQL.append(" WHERE  NOW() BETWEEN s.start_time AND s.end_time");
        selSQL.append("  AND i.id=?  AND s.stat=1 AND v.stat=1 and v.user_id is not null and c.id is null ");
        queryParams.add(searchParams.get("libId"));
        String isWeek = searchParams.get("isWeek").toString();
        String isJia = searchParams.get("isJia").toString();
        if("1".equals(isJia)){ //假期内
        	selSQL.append("and (case when s.species='S001' and s.type in ('C002','C003') then 1 ");
        	selSQL.append("when s.species='S001' and s.type='C001' and s.start_time<'2017-10-01' then 1 ");
        	selSQL.append("when s.is_jia>0 then 1  ");
        	selSQL.append("when s.species='S003' then 1 ");
        	selSQL.append("else 0 end ) = 1 ");
        }else{
        	if("1".equals(isWeek)){ //周末
        		selSQL.append(" and (s.is_jia is null or s.is_jia=0) ");
        	}else{
        		selSQL.append("and (s.is_jia is null or s.is_jia=0) and s.type not in ('C007') ");
        	}
        }
        if (searchParams.get("child_name") != null && !searchParams.get("child_name").equals("")) {
        	selSQL.append(" and  child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("child_name"));
        }
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}
	
	
	@Override
	public Page<Map<String, Object>> findLibByRykeAndLeavePage(Map<String, Object> searchParams, Pageable pageable) {
		
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("  SELECT DISTINCT(t.id),v.child_name,v.img_url,t.ryke_time, t.leave_time from tb_lib_subscribe t    ");
        selSQL.append(" LEFT JOIN tb_lib_vip v  ON  t.vip_id = v.id      ");
        selSQL.append("  LEFT JOIN  tb_lib_join  i  ON  t.lib_id = i.id      ");
        selSQL.append(" WHERE ");
        selSQL.append("  t.ryke_time is NOT  NULL AND TO_DAYS(t.ryke_time)=TO_DAYS(?) AND v.stat=1 AND i.id=?  ");
        	queryParams.add(searchParams.get("date"));
            queryParams.add(searchParams.get("libId"));

        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public Page<Map<String, Object>> findByConfirmPage(Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT DISTINCT(t.id),v.child_name,v.school,v.grade,v.img_url,t.order_date,t.order_time,t.vip_id from tb_lib_subscribe t   ");
        selSQL.append(" LEFT JOIN tb_lib_vip v ON  t.vip_id = v.id   ");
        selSQL.append("  LEFT JOIN tb_lib_vip_service s ON t.vip_id=s.vip_id  ");
        selSQL.append(" LEFT JOIN tb_lib_join  i ON  t.lib_id = i.id    WHERE");
 	    selSQL.append("  t.order_time is NOT NULL   ");
	    selSQL.append(" AND t.confirm_time is NOT NULL  ");
	    selSQL.append(" AND t.ryke_time is  NULL  ");
        selSQL.append("  AND i.id=? AND TO_DAYS(t.order_date)=TO_DAYS(?)  AND ? BETWEEN s.start_time AND s.end_time  ");
            queryParams.add(searchParams.get("libId"));
            queryParams.add(searchParams.get("date"));
            queryParams.add(searchParams.get("date"));
     
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}
	
	
	
	@Override
	public List<Map<String, Object>> findByRykeList(String libId,String date) {
		StringBuilder selSQL = new StringBuilder();
        selSQL.append(" SELECT DISTINCT(t.id),v.child_name,v.school,v.grade,v.img_url,t.ryke_time,t.vip_id from tb_lib_subscribe t   ");
        selSQL.append(" LEFT JOIN tb_lib_vip v ON  t.vip_id = v.id   ");
        selSQL.append("  LEFT JOIN tb_lib_vip_service s ON t.vip_id=s.vip_id  ");
        selSQL.append(" LEFT JOIN tb_lib_join  i ON  t.lib_id = i.id    WHERE");
 	    selSQL.append("  t.order_time is  NULL    ");
	    selSQL.append(" AND t.ryke_time is NOT  NULL  ");
        selSQL.append("  AND i.id=? AND TO_DAYS(t.order_date)=TO_DAYS(?)  AND ? BETWEEN s.start_time AND s.end_time   ");
		return dao.findMap(selSQL.toString(), new String[]{libId,date,date});
	}
	
	@Override
	public List<Map<String, Object>> querySalesUserCounter(String libId){
		StringBuilder selSQL = new StringBuilder();
		 selSQL.append(" SELECT u.wx_name,u.photo,sales_user_id,count from ( ");
		 selSQL.append(" SELECT t.sales_user_id,COUNT(1) as count from tb_lib_order_details t ");
		 selSQL.append(" LEFT JOIN tb_lib_join j ON t.lib_id=j.id  ");
		 selSQL.append(" WHERE j.id=? ");
		 selSQL.append(" GROUP BY t.sales_user_id ORDER BY count DESC LIMIT 3 ");
		 selSQL.append(" )as a LEFT JOIN tb_user u ON a.sales_user_id=u.id  ORDER BY count DESC ");
		 return dao.findMap(selSQL.toString(), new String[]{libId});
	}
	
	@Override
	public Integer queryTyperCounter(String libId,String type){
		StringBuilder selSQL = new StringBuilder();
		 selSQL.append(" SELECT  COUNT(1) as count from tb_lib_order_details t ");
		 selSQL.append(" LEFT JOIN tb_lib_join j ON t.lib_id=j.id  ");
		 selSQL.append(" WHERE j.id=? AND t.type=? ");
		 return Integer.parseInt(dao.findMap(selSQL.toString(), new String[]{libId,type}).get(0).get("count").toString());
	}
	
	
	@Override
	public Integer queryPriceTypeCounter(String libId,String priceType){
		StringBuilder selSQL = new StringBuilder();
		 selSQL.append(" SELECT  COUNT(1) as count from tb_lib_order_details t ");
		 selSQL.append(" LEFT JOIN tb_lib_join j ON t.lib_id=j.id  ");
		 selSQL.append(" WHERE j.id=? AND t.price_type=? ");
		 return Integer.parseInt(dao.findMap(selSQL.toString(), new String[]{libId,priceType}).get(0).get("count").toString());
	}

	@Override
	public Map<String, Object> countWeeklyRead(String vipId, String startTime,
			String endTime) {
		 StringBuilder selSQL = new StringBuilder();
		 selSQL.append(" SELECT t.vip_id,SUM(IF(t.leave_time IS NULL,'120',round((UNIX_TIMESTAMP(t.leave_time)-UNIX_TIMESTAMP(t.ryke_time))/60))) as time_count   ");
		 selSQL.append(" from tb_lib_subscribe t ");
		 selSQL.append(" where t.vip_id=? ");
		 selSQL.append(" AND t.ryke_time BETWEEN ? AND ?  ");
		 selSQL.append(" AND t.order_time IS NULL ");
		 selSQL.append(" AND t.ryke_time is NOT NULL ");
		 List<Map<String, Object> > lists = dao.findMap(selSQL.toString(), new String[]{vipId,startTime,endTime});
		 if(lists!=null && lists.size()>0){
			 return lists.get(0);
		 }
		 return null;
	}

	@Override
	public Map<String, Object> queryRankingByVip(String vipId,
			String startTime, String endTime) {
		 StringBuilder selSQL = new StringBuilder();
		 selSQL.append(" SELECT * from (  ");
		 selSQL.append(" SELECT *,@rownum \\:= @rownum + 1 AS rownum from (SELECT t.vip_id,COUNT(1) as count from tb_order_details t LEFT JOIN tb_lib_vip_service s ON t.vip_id=s.vip_id  where  ");
		 selSQL.append(" t.create_time BETWEEN ? AND ? ");
		 selSQL.append(" AND t.vip_id is NOT NULL AND s.lib_id in (SELECT DISTINCT(lib_id) from tb_lib_vip_service ss WHERE ss.vip_id=? and ss.stat=1)  ");
		 selSQL.append(" GROUP BY t.vip_id ORDER BY count DESC) as o ,(SELECT @rownum \\:= 0) r) as tmp ");
		 selSQL.append("  where tmp.vip_id=? ");
		 List<Map<String, Object> > lists = dao.findMap(selSQL.toString(), new String[]{startTime,endTime,vipId,vipId});
		 if(lists!=null && lists.size()>0){
			 return lists.get(0);
		 }
		return null;
	}

	@Override
	public List<Map<String, Object>> queryCompanionByVip(String vipId,
			String startTime, String endTime) {
		 StringBuilder selSQL = new StringBuilder();
		 selSQL.append(" SELECT DISTINCT(s.vip_id),v.child_name from tb_order_details s LEFT JOIN ( ");
		 selSQL.append(" 	SELECT DISTINCT(t.book_detail_id) as book_detail_id from tb_order_details t WHERE t.vip_id=?   ");
		 selSQL.append(" 	AND  t.create_time BETWEEN ? AND ? ORDER BY t.create_time DESC  LIMIT 10 ");
		 selSQL.append(" ) as tmp ON tmp.book_detail_id=s.book_detail_id ");
		 selSQL.append(" LEFT JOIN tb_lib_vip v ON s.vip_id=v.id  ");
		 selSQL.append("  WHERE s.vip_id !=? LIMIT 10");
		 List<Map<String, Object> > lists = dao.findMap(selSQL.toString(), new String[]{vipId,startTime,endTime,vipId});

		return lists;
	}

	@Override
	public Map<String, Object> queryBookTagByVip(String vipId,
			String startTime, String endTime) {
		 StringBuilder selSQL = new StringBuilder();
		 selSQL.append(" SELECT s.`name` as name,s.`code`,COUNT(1) as count from tb_order_details t  ");
		 selSQL.append(" 	LEFT JOIN tb_lib_book_detail d ON t.book_detail_id=d.id   ");
		 selSQL.append(" 	LEFT JOIN tb_lib_book_info i ON d.info_id=i.id  ");
		 selSQL.append(" LEFT JOIN sys_sort s ON i.book_tag=s.`code` ");
		 selSQL.append(" WHERE t.create_time BETWEEN  ? AND ? ");
		 selSQL.append(" AND s.`code` is NOT NULL AND s.`code` LIKE 'booktag%' ");
		 selSQL.append(" AND t.vip_id=? ");
		 selSQL.append(" GROUP BY s.`code` ORDER BY count DESC ");
		 List<Map<String, Object> > lists = dao.findMap(selSQL.toString(), new String[]{startTime,endTime,vipId});
		 Map<String, Object> data=new HashMap<String, Object>();
		 data.put("tagnum", 0);
		 data.put("count", 1);
		 data.put("tagname", "");
		 if(lists!=null && lists.size()>0){
			 Map<String, Object> mapdata=lists.get(0);
			 data.put("tagnum", mapdata.get("count"));
			 data.put("tagname", mapdata.get("name"));
			 selSQL = new StringBuilder();
			 selSQL.append(" SELECT COUNT(1) as count from  tb_order_details t  where t.vip_id=? and  t.create_time BETWEEN ? AND ?  ");
			 lists = dao.findMap(selSQL.toString(), new String[]{vipId,startTime,endTime});
			 data.put("count", lists.get(0).get("count"));
		 }
		return data;
	}

	@Override
	public List<Map<String, Object>> queryBorrowByVip(String startTime,
			String endTime) {
		 StringBuilder selSQL = new StringBuilder();
		 selSQL.append(" SELECT DISTINCT(t.vip_id),t.user_id,u.wx_account,v.child_name from tb_order_details t  ");
		 selSQL.append(" 	LEFT JOIN tb_lib_vip v ON t.vip_id=v.id   ");
		 selSQL.append(" 	LEFT JOIN tb_user u ON u.id=v.user_id ");
		 selSQL.append("   where t.create_time BETWEEN ? AND ? ");
		 List<Map<String, Object> > lists = dao.findMap(selSQL.toString(), new String[]{startTime,endTime});
		return lists;
	}

	@Override
	public Map<String, Object> queryEffectiveByVip(String vipId) {
		 StringBuilder selSQL = new StringBuilder();
		 selSQL.append(" SELECT * from tb_lib_vip_service t where t.vip_id=? AND stat='1' AND NOW() BETWEEN t.start_time AND t.end_time ");
		 List<Map<String, Object> > lists = dao.findMap(selSQL.toString(), new String[]{vipId});
		 if(lists!=null && lists.size()>0){
			 return lists.get(0);
		 }
		return null;
	}

	@Override
	public Long countBorrowByVip(String vipId, String startTime,
			String endTime) {
		 StringBuilder selSQL = new StringBuilder();
		 selSQL.append(" SELECT COUNT(1) as count from tb_order_details t WHERE t.create_time BETWEEN ? AND ? AND t.vip_id=? ");

		 return  dao.count(selSQL.toString(), new String[]{startTime,endTime,vipId});
	}

	@Override
	public List<Map<String, Object>> countLivelyByVip(String vipId,
			String startTime, String endTime) {
		 StringBuilder selSQL = new StringBuilder();
		 selSQL.append(" SELECT * from (  ");
		 selSQL.append("    (SELECT HOUR(t.ryke_time) as time,COUNT(1) as count from tb_lib_subscribe t    ");
		 selSQL.append(" 	WHERE t.ryke_time IS NOT NULL AND t.ryke_time BETWEEN ? AND ? AND t.vip_id=? ");
		 selSQL.append("    GROUP BY HOUR(t.ryke_time) ORDER BY count DESC LIMIT 1) ");
		 selSQL.append("  UNION ");
		 selSQL.append("   (SELECT  HOUR(t.leave_time) as time,COUNT(1) as count from tb_lib_subscribe t  ");
		 selSQL.append("   WHERE t.leave_time IS NOT NULL AND t.leave_time BETWEEN ? AND ?  AND t.vip_id=? ");
		 selSQL.append("   GROUP BY HOUR(t.leave_time) ORDER BY count DESC LIMIT 1) ");
		 selSQL.append("  ) as tmp ORDER BY time ASC ");
		 List<Map<String, Object> > lists = dao.findMap(selSQL.toString(), new String[]{startTime,endTime,vipId,startTime,endTime,vipId});
		return lists;
	}

	@Override
	public Long countSubscribeByVip(String vipId, String startTime,
			String endTime) {
		 StringBuilder selSQL = new StringBuilder();
		 selSQL.append(" SELECT COUNT(1) as count from tb_lib_subscribe t where t.ryke_time is NOT NULL  AND t.ryke_time BETWEEN ? AND ?  AND t.vip_id=? ");
		 return dao.count(selSQL.toString(), new String[]{startTime,endTime,vipId});
	}

	@Override
	public Map<String, Object> findComeLibInfo(String vipId) {
		StringBuilder selSQL = new StringBuilder();
		 selSQL.append(" SELECT IFNULL(a.totolcount,0) as totolcount,FORMAT(100*IFNULL(c.dgcount,0)/30,0) as dgcount,b.ryke_time from (  ");
		 selSQL.append(" SELECT count(1) as totolcount,t.vip_id from tb_lib_subscribe t  ");
		 selSQL.append(" where t.ryke_time is not null  and t.vip_id=? ");
		 selSQL.append(" )a left join (  ");
		 selSQL.append(" SELECT t.ryke_time,t.vip_id from tb_lib_subscribe t ");
		 selSQL.append("  where t.ryke_time is not null and t.vip_id=? order by t.ryke_time desc limit 1 ");
		 selSQL.append("  )b on a.vip_id = b.vip_id left join ( ");
		 selSQL.append("  SELECT count(1) as dgcount,t.vip_id from tb_lib_subscribe t ");
		 selSQL.append("  where t.ryke_time is not null  and t.vip_id=? ");
		 selSQL.append("  and t.ryke_time BETWEEN  concat(date_format(LAST_DAY(now()),'%Y-%m-'),'01')  and LAST_DAY(now())   ");
		 selSQL.append("  )c on a.vip_id = b.vip_id ");
		 List<Map<String, Object> > lists = dao.findMap(selSQL.toString(), new String[]{vipId,vipId,vipId});
		 if(lists!=null && lists.size()>0){
			 return lists.get(0);
		 }
		 return null;
	}

	@Override
	public Map<String, Object> findWeekReadInfo(String vipId) {
		StringBuilder selSQL = new StringBuilder();
		selSQL.append("SELECT IFNULL(a.jycount,0) as jycount,c.givebook,c.name,b.user_gr from (\n");
		selSQL.append("		SELECT count(1) as jycount,t.vip_id from tb_lib_user_history t \n");
		selSQL.append("		where   t.order_type=1 and t.vip_id=? \n");
		selSQL.append("				and t.start_time BETWEEN date_sub(curdate(),INTERVAL WEEKDAY(curdate())+1 DAY)  and date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 6 DAY)\n");
		selSQL.append(") a left join (\n");
		selSQL.append("		select t.pone2 as user_gr,t11.id from ydz_result t \n");
		selSQL.append("		left join tb_lib_vip t11 on t.user_id = t11.user_id\n");
		selSQL.append("    where t11.id = ? \n");
		selSQL.append(")b on 1 = 1\n");
		selSQL.append("left join (\n");
		selSQL.append("select c.book_tag,CONCAT('《',replace(GROUP_CONCAT(c.title),',','》、《'),'》') as givebook ,c.vip_id,d.name from(\n");
		selSQL.append("select ta.book_tag,tb.title,ta.vip_id from (\n");
		selSQL.append("	SELECT t.vip_id,count(*) as tag_count,t12.book_tag,t12.lib_id from tb_lib_user_history t \n");
		selSQL.append("	left join tb_lib_book_detail t11 on t.book_detail_id = t11.id\n");
		selSQL.append("  left join tb_lib_book_info t12 on t11.info_id = t12.id\n");
		selSQL.append("  where   t.order_type=1 and t.vip_id=? \n");
		selSQL.append("  and t.start_time BETWEEN date_sub(curdate(),INTERVAL WEEKDAY(curdate())+1 DAY)  and date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 6 DAY)\n");
		selSQL.append("		and t12.book_tag is not null\n");
		selSQL.append("    GROUP BY t12.book_tag\n");
		selSQL.append("    order by tag_count DESC\n");
		selSQL.append("		limit 1)ta left join tb_lib_book_info tb on ta.book_tag = tb.book_tag and ta.lib_id = tb.lib_id\n");
		selSQL.append("    limit 3) c  left join sys_sort d on c.book_tag = d.`code` and d.pid='d1ec09d3-f924-40be-9e23-daa372d983da'\n");
		selSQL.append(")c on a.vip_id = c.vip_id");
		 List<Map<String, Object> > lists = dao.findMap(selSQL.toString(), new String[]{vipId,vipId,vipId});
		 if(lists!=null && lists.size()>0){
			 return lists.get(0);
		 }
		return null;
	}

	@Override
	public Map<String, Object> findworkInfo(String vipId,String libId) {
		StringBuilder selSQL = new StringBuilder();
		selSQL.append("SELECT count(*) push_count,count(IF(t.view_time is not null,true,null)) as view_count,t.id from zyb_student_push t \n");
		selSQL.append("where t.push_time BETWEEN date_sub(curdate(),INTERVAL WEEKDAY(curdate())+ 1 DAY)  \n");
		selSQL.append("and date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 6 DAY) and t.vip_id = ? \n");
		selSQL.append("and t.lib_id=?");
		 List<Map<String, Object> > lists = dao.findMap(selSQL.toString(), new String[]{vipId,libId});
		 if(lists!=null && lists.size()>0){
			 return lists.get(0);
		 }
		return null;
	}

	@Override
	public Map<String, Object> findWorkSug(String pushId) {
		
		StringBuilder selSQL = new StringBuilder();
		selSQL.append("SELECT t.suggest,t.evaluate,t.`level`,t.push_time,t14.img,t12.how_id,t13.id,t11.time_cost,t13.how_details_id,count(if(t13.how_details_id is not null,true,null)) as how_count,GROUP_CONCAT(t11.time_cost) from zyb_student_push t\n");
		selSQL.append("left join zyb_student_work t11 on t.id = t11.student_push_id\n");
		selSQL.append("left join zyb_student_work_how t12 on t11.id = t12.student_work_id\n");
		selSQL.append("left join zyb_student_work_how_details t13 on t12.id = t13.work_how_id\n");
		selSQL.append("left join zyb_student_work_img t14 on t13.id = t14.how_details_id\n");
		selSQL.append("where  t14.id is null");
		selSQL.append(" and t.push_time BETWEEN date_sub(curdate(),INTERVAL WEEKDAY(curdate())+ 1 DAY)  \n");
		selSQL.append("and date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 6 DAY) and t.vip_id = ? \n");
		 List<Map<String, Object> > lists = dao.findMap(selSQL.toString(), new String[]{pushId});
		 if(lists!=null && lists.size()>0){
			 return lists.get(0);
		 }
		return null;
	}
	
	public Map<String, Object> findTheMonthCountAndTime (String vipId, String beginTime, String endTime) {
		StringBuilder sb = new StringBuilder();
		sb.append("select count(1) count, max(ryke_time) time from tb_lib_subscribe where flow_stat >= 3 and vip_id = '")
			.append(vipId).append("' and ryke_time between '").append(beginTime).append("' and '").append(endTime).append("'");
		List<Map<String, Object> > lists = dao.findMap(sb.toString(), null);
		if (lists != null && lists.size() > 0){
			return lists.get(0);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> pushBorrowTask(String libId,String lastMonth) {
		StringBuilder selSQL = new StringBuilder();
		 selSQL.append(" select * from ( ");
		 selSQL.append(" SELECT t11.child_name,t11.flow_id,count(1) as borrow_num from tb_lib_vip_service t   ");
		 selSQL.append(" INNER JOIN tb_lib_vip t11 on t.vip_id=t11.id  ");
		 selSQL.append(" INNER JOIN tb_order_details t13 on t11.id = t13.vip_id ");
		 selSQL.append(" where   t13.create_time  like   concat(?,'%') ");
		 selSQL.append(" and t.lib_id=? ");
		 selSQL.append("  GROUP BY t11.id  ");
		 selSQL.append("  )b where b.borrow_num >9 ");
		 
		 return dao.findMap(selSQL.toString(), new String[]{lastMonth,libId});
	}

    @Override
    public Page<String> findVipIdPageSignedTodayByLibId(HashMap<String, Object> params, Pageable pageable) {
		StringBuilder sql = new StringBuilder();
		ArrayList<Object> paramsList = new ArrayList<>();

		sql.append("select vip_id from tb_lib_subscribe, tb_lib_vip ");
		sql.append("where tb_lib_subscribe.vip_id = tb_lib_vip.id and ");
		sql.append("lib_id = ? and ");paramsList.add(params.get("libId"));
		sql.append("tb_lib_vip.stat = '1' and ");
		sql.append("tb_lib_subscribe.stat = '1' and ");
		sql.append("flow_stat = '3' and ");
		sql.append("date(ryke_time) = date(?) and ");paramsList.add(params.get("currentDateTime"));
		sql.append("tb_lib_vip.child_name like concat('%', ?, '%') ");paramsList.add(params.get("studentName"));

		return dao.findPage(sql.toString(), paramsList.toArray(), pageable, String.class);
    }


}
