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

import com.ssj.dao.weixin.library.dao.LibUserHistoryQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.framework.weixin.util.Collections3;
import com.ssj.service.reading.out.*;
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 LibUserHistoryQueryDaoImpl implements LibUserHistoryQueryDao{
	@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 a.child_name,a.title,a.user_id,a.update_time,a.create_time,a.state,a.local_image,a.qr_url,a.flow_id FROM(\n ");
	        selSQL.append(" SELECT t11.child_name,t.end_time,t13.user_id,t15.update_time,t15.create_time,t14.local_image,t.id,t.title,t14.lib_id,t13.qr_url,t11.flow_id, ");
	        selSQL.append(" case when t15.update_time is null and t.end_time>=SYSDATE() then 1 ");
	        selSQL.append(" when t15.update_time is  null and t.end_time<SYSDATE() then 2 ");
	        selSQL.append(" when t15.update_time is not null then 3 end as state");
	        selSQL.append(" from (select t.end_time,t.id,t.title,t.detail_id,t.book_detail_id,t.vip_id from tb_lib_user_history t WHERE t.order_type='1' ) t");
	        selSQL.append(" left join (select t15.update_time,t15.create_time,t15.id from  tb_order_details t15) t15 on t.detail_id = t15.id");
	        selSQL.append(" INNER JOIN (select t11.id,t11.child_name,t11.flow_id from tb_lib_read_vip t11) t11 on  t.vip_id = t11.id ");
	        selSQL.append(" left join (select t13.user_id,t13.qr_url,t13.id,t13.info_id from tb_lib_book_detail t13) t13 on t.book_detail_id = t13.id");
	        selSQL.append(" left join (select t14.local_image,t14.lib_id,t14.id,t14.org_code from tb_lib_book_info t14) t14 on t13.info_id = t14.id");
	        selSQL.append(" WHERE 1=1 ");
	        
	        if (searchParams.get("orgCode") != null && !searchParams.get("orgCode").equals("")) {
	        	selSQL.append(" and  t14.org_code in ( ");
	        	selSQL.append(searchParams.get("orgCode"));
	        	selSQL.append(" ) ");
	        }
	        
	        if (searchParams.get("title") != null && !searchParams.get("title").equals("")) {
	            selSQL.append(" and  t.title LIKE  CONCAT(CONCAT('%',?,'%')) ");
	            queryParams.add(searchParams.get("title"));
	        }
	        if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
	        	selSQL.append(" and  t14.lib_id = ?  ");
	        	queryParams.add(searchParams.get("libId"));
	        }
	        if (searchParams.get("child_name") != null && !searchParams.get("child_name").equals("")) {
	        	selSQL.append(" and  t11.child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
	        	queryParams.add(searchParams.get("child_name"));
	        }
	        if (searchParams.get("beginDate") != null && !searchParams.get("beginDate").equals("")) {
	        	selSQL.append(" and TO_DAYS(t15.create_time) >=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("beginDate"));
	        }
	        if (searchParams.get("endDate") != null && !searchParams.get("endDate").equals("")) {
	        	selSQL.append(" and TO_DAYS(t15.create_time) <=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("endDate"));
	        }
	        selSQL.append(") a ");
	        if (searchParams.get("state") != null && !searchParams.get("state").equals("")) {
	            selSQL.append(" where  a.state =? ");
	            queryParams.add(searchParams.get("state"));
	        }
	        selSQL.append("  ORDER BY a.create_time DESC");
	        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public Page<Map<String, Object>> findForQGPage(
			Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT t.ryke_time,t.leave_time,v.child_name, t.lib_id ,i.league_name ");
        selSQL.append("  FROM tb_lib_subscribe t  ");
        selSQL.append(" inner JOIN tb_lib_read_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  t.ryke_time is not null ");
        
        if (searchParams.get("orgCode") != null && !searchParams.get("orgCode").equals("")) {
        	selSQL.append(" and  i.org_code in ( ");
        	selSQL.append(searchParams.get("orgCode"));
        	selSQL.append(" ) ");
        }
        if (searchParams.get("child_name") != null && !searchParams.get("child_name").equals("")) {
        	selSQL.append(" and  v.child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("child_name"));
        }
        if (searchParams.get("ryke_time") != null && !searchParams.get("ryke_time").equals("")) {
        	selSQL.append(" and  t.ryke_time LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("ryke_time"));
        }
        if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
        	selSQL.append(" and  i.id= ? ");
        	queryParams.add(searchParams.get("libId"));
        }
        selSQL.append("  ORDER BY t.ryke_time DESC");
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
}

	@Override
	public Page<Map<String, Object>> findForYDPage(
			Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" select a.frequency,a.child_name,a.school,a.grade,a.img_url,a.league_name,concat(ROUND( a.frequency/ daycount * 100,2),'%') as qdl from (\n");
        selSQL.append("select COUNT(1) AS frequency, v.child_name, v.school,v.grade,v.img_url, i.league_name ,DAYOFMONTH(last_day(t.ryke_time)) as daycount \n");
        selSQL.append("  FROM tb_lib_subscribe t  ");
        selSQL.append(" LEFT JOIN tb_lib_read_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  t.ryke_time is not null ");
        
        if (searchParams.get("orgCode") != null && !searchParams.get("orgCode").equals("")) {
        	selSQL.append(" and  i.org_code in ( ");
        	selSQL.append(searchParams.get("orgCode"));
        	selSQL.append(" ) ");
        }
        	selSQL.append("  and date_format(t.ryke_time,'%Y-%m') =? ");
        	queryParams.add(searchParams.get("monthDate"));
        	 if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
             	selSQL.append(" and  i.id= ? ");
             	queryParams.add(searchParams.get("libId"));
             }
        selSQL.append(" GROUP BY t.vip_id");
        if (searchParams.get("child_name") != null && !searchParams.get("child_name").equals("")) {
        	selSQL.append(" having  child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("child_name"));
        }
        selSQL.append(") a ");
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public List<Map<String, Object>> getLibQGList(
			Map<String, Object> searchParams) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT DATE_FORMAT(t.ryke_time,'%Y-%m-%T')as sb ,DATE_FORMAT(t.leave_time,'%Y-%m-%T')as sx,v.child_name,i.league_name ");
        selSQL.append("  FROM tb_lib_subscribe t  ");
        selSQL.append("  LEFT JOIN tb_lib_read_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 t.ryke_time is not null ");
        if (searchParams.get("orgCode") != null && !searchParams.get("orgCode").equals("")) {
        	selSQL.append(" and  i.org_code in ( ");
        	selSQL.append(searchParams.get("orgCode"));
        	selSQL.append(" ) ");
        }
        if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
        	selSQL.append(" and  i.id= ? ");
        	queryParams.add(searchParams.get("libId"));
        }
        if (searchParams.get("child_name") != null && !searchParams.get("child_name").equals("")) {
        	selSQL.append(" and  v.child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("child_name"));
        }
        if (searchParams.get("ryke_time") != null && !searchParams.get("ryke_time").equals("")) {
        	selSQL.append(" and  t.ryke_time LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("ryke_time"));
        }
        selSQL.append("  ORDER BY t.ryke_time DESC");
        return dao.findMap(selSQL.toString(), queryParams.toArray());
}

	@Override
	public List<Map<String, Object>> findRechargeInfo(String userId) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT id,user_id,change_msg,change_sum,now_balance,DATE_FORMAT(create_time,'%Y-%m-%d') as chargeTime from tb_lib_user_wallet where user_id=? ORDER by create_time DESC");
		queryParams.add(userId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	

	@Override
	public List<Map<String, Object>> getLibYDList(
			Map<String, Object> searchParams) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" select a.user_id,a.frequency,a.child_name, a.league_name ,concat(ROUND( a.frequency/ daycount * 100,2),'%') as qdl from (\n");
        selSQL.append(" SELECT t.user_id, COUNT(1) AS frequency, v.child_name,DAYOFMONTH(last_day(t.ryke_time)) as daycount  , i.league_name  \n");
        selSQL.append("  FROM tb_lib_subscribe t  ");
        selSQL.append("  LEFT JOIN tb_lib_read_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 t.ryke_time is not null and ");
        	selSQL.append(" date_format(t.ryke_time,'%Y-%m') =? ");
        	queryParams.add(searchParams.get("monthDate"));
        	
            
            if (searchParams.get("orgCode") != null && !searchParams.get("orgCode").equals("")) {
	        	selSQL.append(" and  i.org_code in ( ");
	        	selSQL.append(searchParams.get("orgCode"));
	        	selSQL.append(" ) ");
	        }
            
        	 if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
        		 selSQL.append(" and  i.id= ? ");
             	queryParams.add(searchParams.get("libId"));
             }	
        selSQL.append(" GROUP BY t.user_id");
        if (searchParams.get("child_name") != null && !searchParams.get("child_name").equals("")) {
        	selSQL.append(" having  child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("child_name"));
        }
        selSQL.append(") a ");
        return dao.findMap(selSQL.toString(), queryParams.toArray());
	
	}

	@Override
	public Page<Map<String, Object>> findCheckPage(
			Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT v.child_name,v.school,v.grade,v.img_url,v.user_id,v.id ,v.parent_name ");
        selSQL.append(" FROM tb_lib_vip_service s   ");
        selSQL.append(" JOIN tb_lib_read_vip v ON s.vip_id = v.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 s.stat = 1 AND NOW() BETWEEN s.start_time  ");
        selSQL.append(" AND s.end_time AND s.lib_id = ? AND c.id IS NULL and v.user_id IS NOT NULL ");
        queryParams.add(searchParams.get("libId"));
        if (searchParams.get("child_name") != null && !searchParams.get("child_name").equals("")) {
        	selSQL.append(" and  v.child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("child_name"));
        }
        selSQL.append(" GROUP BY  s.vip_id ");
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public Page<Map<String, Object>> findCheckOutPage(
			Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT v.child_name,v.school,v.grade,v.img_url,v.user_id,t.id,v.parent_name from  tb_lib_subscribe t   ");
        selSQL.append(" LEFT JOIN tb_lib_read_vip v ON t.vip_id = v.id   ");
        selSQL.append(" WHERE t.lib_id =? AND TO_DAYS(NOW()) = TO_DAYS(t.order_date)  AND t.flow_stat = 3 ");
        queryParams.add(searchParams.get("libId"));
        if (searchParams.get("child_name") != null && !searchParams.get("child_name").equals("")) {
        	selSQL.append(" and  v.child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("child_name"));
        }
        selSQL.append(" GROUP BY  v.id ");
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public Page<VipSignOutDto> signList(Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		boolean isCheckout = searchParams.get("type").toString().equals("2")?true:false;
		if(isCheckout){
			selSQL.append(" SELECT t.id,v.child_name,v.school,v.grade,v.img_url from  tb_lib_subscribe t   ");
			selSQL.append(" LEFT JOIN tb_lib_read_vip v ON t.vip_id = v.id   ");
			selSQL.append(" JOIN tb_lib_vip_service t11 ON t11.vip_id = v.id  and  t11.species='S001' ");
			selSQL.append(" WHERE t.lib_id =?  AND TO_DAYS(NOW()) = TO_DAYS(t.order_date)  AND t.flow_stat = 3 and v.user_id is not null ");
			queryParams.add(searchParams.get("libId"));
			if (searchParams.get("childName") != null && !searchParams.get("childName").equals("")) {
				selSQL.append(" and  v.child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
				queryParams.add(searchParams.get("childName"));
			}
			selSQL.append(" GROUP BY  v.id ");
		}else {
			selSQL.append(" SELECT v.id,v.child_name,v.school,v.grade,v.img_url from  tb_lib_vip_service t   ");
			selSQL.append(" LEFT JOIN tb_lib_read_vip v ON t.vip_id = v.id   ");
			selSQL.append(" left JOIN tb_lib_subscribe t11 ON t11.vip_id = v.id  AND TO_DAYS(NOW()) = TO_DAYS(t11.order_date)  AND t11.flow_stat = 3  ");
			selSQL.append(" WHERE t.lib_id =? and t.stat=1 and t.species='S001' AND NOW() BETWEEN t.start_time and t.end_time and v.user_id is not null and t11.id is null ");
			queryParams.add(searchParams.get("libId"));
			if (searchParams.get("childName") != null && !searchParams.get("childName").equals("")) {
				selSQL.append(" and  v.child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
				queryParams.add(searchParams.get("childName"));
			}
			selSQL.append(" GROUP BY  v.id ");
		}

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

	@Override
	public List<VipSignOutDto> signList2(Map<String, Object> searchParams) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		boolean isCheckout = searchParams.get("type").toString().equals("2")?true:false;
		if(isCheckout){
			selSQL.append(" SELECT t.id,v.child_name,v.school,v.grade,v.img_url,v.user_id,v.parent_name from  tb_lib_subscribe t   ");
			selSQL.append(" LEFT JOIN tb_lib_read_vip v ON t.vip_id = v.id   ");
			selSQL.append(" JOIN tb_lib_vip_service t11 ON t11.vip_id = v.id  and  t11.species='S001' ");
			selSQL.append(" JOIN tb_user u on v.user_id = u.id ");
			selSQL.append(" WHERE t.lib_id =? AND TO_DAYS(NOW()) = TO_DAYS(t.order_date)  AND t.flow_stat = 3 ");
			queryParams.add(searchParams.get("libId"));
			if(!"".equals(searchParams.get("childName").toString())){
				selSQL.append(" and  v.child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
				queryParams.add(searchParams.get("childName"));
			}
			selSQL.append(" GROUP BY  v.id ");
		}else {
			selSQL.append(" SELECT v.id,v.child_name,v.school,v.grade,v.img_url,v.user_id,v.parent_name from  tb_lib_vip_service t   ");
			selSQL.append(" LEFT JOIN tb_lib_read_vip v ON t.vip_id = v.id   ");
			selSQL.append(" LEFT JOIN tb_lib_subscribe t11 ON t11.vip_id = v.id  AND TO_DAYS(NOW()) = TO_DAYS(t11.order_date)  AND t11.flow_stat = 3  ");
			selSQL.append(" WHERE t.lib_id =? and t.stat=1 and t.species='S001' AND NOW() BETWEEN t.start_time and t.end_time and v.user_id is not null and t11.id is null ");
			queryParams.add(searchParams.get("libId"));
			if(!"".equals(searchParams.get("childName").toString())){
				selSQL.append(" and  v.child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
				queryParams.add(searchParams.get("childName"));
			}
			selSQL.append(" GROUP BY  v.id ");
		}

		return dao.findList(selSQL.toString(), queryParams.toArray(), VipSignOutDto.class);
	}

	@Override
	public Page<LibSignMonthOutDto> signMonthList(Map<String, Object> params, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append(" SELECT v.child_name,v.school,v.grade,v.img_url from  tb_lib_subscribe t   ");
		selSQL.append(" LEFT JOIN tb_lib_read_vip v ON t.vip_id = v.id   ");
		selSQL.append(" JOIN tb_lib_vip_service t11 ON t11.vip_id = v.id  and  t11.species='S001'    ");
		selSQL.append(" WHERE t.lib_id =? AND DATE_FORMAT(t.ryke_time,'%Y-%m') =? ");
		selSQL.append(" GROUP BY  v.id ");
		queryParams.add(params.get("libId"));
		queryParams.add(params.get("month"));
		return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable,LibSignMonthOutDto.class);
	}

	@Override
	public Page<LibSignDayOutDto> dayInfoList(Map<String, Object> params, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append(" SELECT t11.vip_id,v.child_name,DATE_FORMAT(t.ryke_time,'%Y-%m-%d %H:%i') as sign_time, case when t.leave_time is not null then DATE_FORMAT(t.leave_time,'%Y-%m-%d %H:%i') else '' end as leave_time,v.img_url from  tb_lib_subscribe t   ");
		selSQL.append(" LEFT JOIN tb_lib_read_vip v ON t.vip_id = v.id   ");
		selSQL.append(" JOIN tb_lib_vip_service t11 ON t11.vip_id = v.id  and  t11.species='S001'    ");
		selSQL.append(" WHERE t.lib_id =? AND DATE_FORMAT(t.ryke_time,'%Y-%m-%d') =? ");
		queryParams.add(params.get("libId"));
		queryParams.add(params.get("date"));
		if("1".equals(params.get("group").toString())){
			selSQL.append(" group by  v.id ");
		}else{
			selSQL.append(" and  v.id =? ");
			queryParams.add(params.get("vipId"));
		}
		selSQL.append(" order BY t.ryke_time desc, v.id ");

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

	@Override
	public Page<LibAgencyOutDto> agencyVipList(Map<String, Object> params, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append(" SELECT t11.child_name,t11.img_url,t11.school,t11.grade,t11.id from tb_lib_vip_service t   ");
		selSQL.append(" join tb_lib_read_vip t11 on t.vip_id = t11.id  ");
		selSQL.append(" where t.lib_id=? and t.stat=1 and NOW() BETWEEN t.start_time and t.end_time and  t.species='S001' ");
		queryParams.add(params.get("libId"));
		if(!params.get("childName").toString().equals("")){
			selSQL.append(" and t11.child_name like CONCAT('%',?,'%') ");
			queryParams.add(params.get("childName"));
		}
		selSQL.append(" GROUP BY  t11.id ");
		return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable,LibAgencyOutDto.class);
	}

	@Override
	public List<LibBookLableOutDto> findThemeInfo(Map<String, Object> params) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append(" select t11.name ,count(if(t.id is not null,true,null)) as total,t11.code     ");
		selSQL.append(" from sys_sort t11  ");
		selSQL.append("  left join (");
		selSQL.append("  select case when t.child_theme is null or t.child_theme='' then 'other' else t.child_theme end child_theme,t.id ");

		selSQL.append("  from  tb_lib_book_info t  ");
		selSQL.append("  join tb_lib_book_detail t12 on t.id = t12.info_id  ");
		selSQL.append("   where  t12.lib_id = ?  ) t on t11.code = t.child_theme ");
		selSQL.append(" where   t11.pid='a316f70c-1184-44b9-a636-dbe10262f176' ");
		queryParams.add(params.get("libId"));
		selSQL.append(" GROUP BY  t11.id ");
		return dao.findList(selSQL.toString(), queryParams.toArray(),LibBookLableOutDto.class);
	}

	@Override
	public List<LibBookGradeOutDto> findGradeInfo(Map<String, Object> params) {

		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append(" select t11.grade ,count(if(t.id is not null,true,null)) as total,t11.grade_code     ");
		selSQL.append(" from (");
		selSQL.append("select '学前段' as grade,'学前段' as grade_code \n");
		selSQL.append("union all \n");
		selSQL.append("select '低年段' as grade,'低年段' as grade_code \n");
		selSQL.append("union all \n");
		selSQL.append("select '中年段' as grade,'中年段' as grade_code \n");
		selSQL.append("union all \n");
		selSQL.append("select '高年段' as grade,'高年段' as grade_code \n");
		selSQL.append("union all \n");
		selSQL.append("select '其他' as grade,'其他' as grade_code ");
		selSQL.append(") t11 ");
		selSQL.append("  left join (");
		selSQL.append("  select case when t.section is null or t.section='' then '其他' else t.section end grade,t.id ");

		selSQL.append("  from  tb_lib_book_info t  ");
		selSQL.append("  join tb_lib_book_detail t12 on t.id = t12.info_id  ");
		selSQL.append("   where  t12.lib_id = ?  ) t on t11.grade = t.grade ");
		queryParams.add(params.get("libId"));
		selSQL.append(" GROUP BY  t11.grade ");

		return dao.findList(selSQL.toString(), queryParams.toArray(),LibBookGradeOutDto.class);
	}

	@Override
	public Page<BookBorrowOutDto> borrowInfoList(Map<String, Object> params, Pageable pageable) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		String  type = params.get("type").toString();
		selSql.append("select * from ( \n");
		selSql.append("SELECT  a.title,a.local_image,a.summary,a.info_id,case when a.is_close='1' then update_time else create_time end as borrow_date  from ( \n");
		selSql.append("SELECT t11.id,t.title,ifnull(t14.summary,'') as summary,t.is_close,t13.info_id,t14.local_image,DATE_FORMAT(t.create_time ,'%Y.%m.%d') as create_time,t.vip_id,DATE_FORMAT(t.update_time,'%Y.%m.%d')  as update_time,t11.child_name  \n");
		selSql.append("from tb_order_details t  \n");
		selSql.append("left join tb_lib_read_vip t11 on t.vip_id = t11.id \n");
		selSql.append("left join tb_lib_book_detail t13 on t.book_detail_id = t13.id \n");
		selSql.append("left join tb_lib_book_info t14 on t13.info_id = t14.id \n");
		selSql.append("where t.lib_id = ?  and t.is_close= ?  and t11.user_id is not null  \n");
		queryParams.add(params.get("libId"));
		queryParams.add(params.get("isClose"));
		//1-未过期 2-已过期3-近7天4-近30天5-近180天
		if(type.equals("1")){
			selSql.append(" and to_days(t13.back_book_time)>=to_days(now())  \n");
		}else if("2".equals(type)){
			selSql.append(" and to_days(t13.back_book_time)<to_days(now())  \n");
		}else if("3".equals(type)){
			selSql.append(" and date_sub(now(), interval 7 day)< t.update_time\n");
		}else if("4".equals(type)){
			selSql.append(" and date_sub(now(), interval 30 day)< t.update_time \n");
		}else if("5".equals(type)){
			selSql.append(" and date_sub(now(), interval 150 day)< t.update_time \n");
		}
		selSql.append(") a ) b \n");
		String isGroup = params.get("group").toString();
		if("1".equals(isGroup)){
			//根据date 分组
			if("2".equals(params.get("infoId").toString())){
				selSql.append(" GROUP BY b.borrow_date \n");
			}else{
				selSql.append(" where b.borrow_date=? \n");
				queryParams.add(params.get("borrowDate"));
				selSql.append(" GROUP BY b.info_id \n");
			}
        }
        selSql.append("  order by b.borrow_date desc\n");
		return dao.findPage(selSql.toString(), queryParams.toArray(),pageable,BookBorrowOutDto.class);
	}

	@Override
	public List<VipBorrowOutDto> findBorrowDetailList(String infoId, String dateStr,String isClose,String libId) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select a.child_name,count(1) as borrow_num,sum(case when borrow_day=0 then 1 else borrow_day end) borrow_day from ( \n");
		selSql.append("SELECT t11.child_name,t13.info_id,t11.id,case when t.is_close='1' then to_days(t.update_time)-to_days(t.create_time) else to_days(now())-to_days(t.create_time) end as borrow_day  \n");
		selSql.append("from tb_order_details t  \n");
		selSql.append("left join tb_lib_read_vip t11 on t.vip_id = t11.id \n");
		selSql.append("left join tb_lib_book_detail t13 on t.book_detail_id = t13.id \n");
		selSql.append("where t.lib_id = ?  and t.is_close= ? and t13.info_id=?   \n");
		queryParams.add(libId);
		queryParams.add(isClose);
		queryParams.add(infoId);
		selSql.append("and   DATE_FORMAT(case when t.is_close='1' then t.update_time else  t.create_time end ,'%Y.%m.%d')=?  \n");
		queryParams.add(dateStr);
		selSql.append("  ) a   group by a.info_id,a.id  \n");


		return dao.findList(selSql.toString(), queryParams.toArray(),VipBorrowOutDto.class);
	}

	@Override
	public Page<LibAgencyOutDto> libVipList(Map<String, Object> params, Pageable pageable) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT t.child_name,t.id,t.school,t.grade,t.img_url from tb_lib_read_vip t  \n");
		selSql.append("join tb_lib_vip_service t11 on t.id = t11.vip_id \n");
		selSql.append("where t11.lib_id = ? and t11.stat=1 and t11.species='S001' \n");
		queryParams.add(params.get("libId"));
		if(params.get("name")!=null && !params.get("name").equals("")){
			selSql.append(" and t.child_name like concat('%',?,'%')\n");
			queryParams.add(params.get("name"));
		}
		return dao.findPage(selSql.toString(), queryParams.toArray(),pageable,LibAgencyOutDto.class);
	}

	@Override
	public List<VipServiceOutDto> findVipLibServiceList(Map<String, Object> params) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT t11.id,concat('阅读',t12.name) as detail,t.flow_id as vip_no,DATE_FORMAT(t11.start_time,'%Y.%m.%d') as start_time,DATE_FORMAT(t11.end_time,'%Y.%m.%d') as end_time,t11.stat as state  \n");
		selSql.append("from tb_lib_read_vip t\n");
		selSql.append("join tb_lib_vip_service t11 on t.id = t11.vip_id \n");
		selSql.append("join sys_sort t12 on t11.type = t12.code and t12.pid = 'ec14cc13-7af4-4498-96b7-468691058c04' \n");
		selSql.append("where t11.lib_id = ?  and t.id=? and t11.species='S001' \n");
		selSql.append("order by t11.end_time desc \n");
		queryParams.add(params.get("libId"));
		queryParams.add(params.get("vipId"));
		return dao.findList(selSql.toString(), queryParams.toArray(),VipServiceOutDto.class);
	}

	@Override
	public List<VipSignDayDetailOutDto> vipSignInfoList(Map<String, Object> params) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT order_date as sign_date,DATE_FORMAT(ryke_time,'%Y.%m.%d  %H:%i') as sign_time, DATE_FORMAT(leave_time,'%Y.%m.%d  %H:%i') as leave_time from tb_lib_subscribe  \n");
		selSql.append("where flow_stat in ('3','4') and vip_id=? and lib_id=? and order_date regexp ?  order by order_date asc \n");
		queryParams.add(params.get("vipId"));
		queryParams.add(params.get("libId"));
		queryParams.add(params.get("signMonth"));
		return dao.findList(selSql.toString(), queryParams.toArray(),VipSignDayDetailOutDto.class);
	}

	@Override
	public Page<ManagerOutDto> findManagerList(Map<String, Object> params, Pageable pageable) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT a.id,group_CONCAT(a.jobs separator '、') as jobs,a.name,a.img_url from (   \n");
		selSql.append("SELECT a.id,ifnull(a.name,b.wx_name) as name,case when a.type =1 then '馆长助理' else '馆长' end as jobs, \n");
		selSql.append("b.photo as img_url,a.user_id  \n");
		selSql.append("from tb_lib_manager  a \n");
		selSql.append("left join tb_user b on a.user_id = b.id  \n");
		selSql.append("where type in(0,1)  and a.state=1  and a.lib_id=? \n");
		selSql.append(") a     \n");
		queryParams.add(params.get("libId"));
		if(params.get("name") !=null && !params.get("name").equals("")){
			selSql.append(" where  a.name like concat('%',?,'%')\n");
			queryParams.add(params.get("name"));
		}
		selSql.append(" GROUP BY a.user_id   \n");
		return dao.findPage(selSql.toString(), queryParams.toArray(),pageable,ManagerOutDto.class);
	}

	@Override
	public List<ManagerOutDto> findUserList(Map<String, Object> params) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT  id ,wx_name as name,'' as jobs,photo as img_url from tb_user u   \n");
		selSql.append("where u.wx_account is not null and  not exists (select 1 from tb_lib_manager t where t.user_id = u.id and t.lib_id is not null and t.state=1 and t.type in (0,1)) \n");
		if(!params.get("name").equals("")){
			selSql.append(" and   u.wx_name like concat('%',?,'%')\n");
			queryParams.add(params.get("name"));
		}
		selSql.append("limit 5 \n");
		return dao.findList(selSql.toString(), queryParams.toArray(),ManagerOutDto.class);
	}

	@Override
	public Map<String, Object> findBorrowInfo(Map<String, Object> params) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append(" SELECT count(1) as num_total,count(DISTINCT create_time) as times_total from tb_order_details   \n");
		selSql.append(" where lib_id=?  \n");
		queryParams.add(params.get("libId"));
		if(!params.get("searchDate").equals("")){
			selSql.append(" and   create_time REGEXP ? \n");
			queryParams.add(params.get("searchDate"));
		}
		List<Map<String, Object> > list = dao.findMap(selSql.toString(),queryParams.toArray());
		if(Collections3.isNotEmpty(list)){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> callBookBack(String libId) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.wx_account,GROUP_CONCAT(concat('《',t.title,'》') separator '、') as title,t.back_book_time,t.days1,t.days2,t.parent_name from (  \n");
		selSql.append("select  t11.wx_account,t.back_book_time,t.borrowed_time,t12.title,datediff(now(),t.back_book_time) as days1,\n");
		selSql.append("DATE_FORMAT(t.back_book_time,'%Y-%m-%d')  as days2 ,t13.parent_name\n");
		selSql.append("from tb_lib_book_detail t  \n");
		selSql.append(" join tb_user t11 on t.user_id = t11.id  \n");
		selSql.append("left join tb_lib_book_info t12 on t.info_id = t12.id  \n");
		selSql.append("left join tb_lib_read_vip t13 on t.user_id = t13.user_id  and  t.vip_id = t13.id\n");
		selSql.append("where    \n");
		selSql.append("   t.lib_id=? and t.back_book_time<now() and t11.wx_account is not null \n");
		selSql.append(") t group by t.wx_account,t.days2  \n");
		queryParams.add(libId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getBackDetailList(List<String> idArr) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select t.wx_account,GROUP_CONCAT(concat('《',t.title,'》') separator '、') as title,t.name,t.user_id,t.vip_id,t.back_book_time from (  \n");
		selSql.append("select  t12.title,t11.wx_account,ifnull(t13.child_name,t11.wx_name) as name,t.user_id,t.vip_id,case when t.back_book_time is null then '' else DATE_FORMAT(t.back_book_time,'%Y年%m月%d日 %H时%i分') end back_book_time \n");
		selSql.append("from tb_lib_book_detail t  \n");
		selSql.append(" join tb_user t11 on t.user_id = t11.id  \n");
		selSql.append("left join tb_lib_book_info t12 on t.info_id = t12.id  \n");
		selSql.append("left join tb_lib_read_vip t13 on t.user_id = t13.user_id  and  t.vip_id = t13.id\n");
		selSql.append("where    \n");
		selSql.append("   t.id in ( ");
		int i =1;
		int size = idArr.size();
		for (String str:idArr){
			selSql.append("'");
			selSql.append(str);
			if(i<size){
				selSql.append("',");
			}
			i++;
		}
		selSql.append("  ') ");
		selSql.append(" and t11.wx_account is not null  \n");
		selSql.append(") t group by t.user_id \n");
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<BookBackOutDto> findBackdList(List<String> bookDetailsId) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT t14.title,t14.local_image,t14.summary,case when to_days(t.update_time)-to_days(t.create_time)=0 then 1 else to_days(t.update_time)-to_days(t.create_time) end  as borrowed_days,DATE_FORMAT(t.create_time,'%Y-%m-%d %H:%i') as borrowed_time,DATE_FORMAT(t.update_time,'%Y-%m-%d %H:%i') as back_time  \n");
		selSql.append("from tb_order_details t  \n");
		selSql.append("left join tb_lib_book_detail t13 on t.book_detail_id = t13.id \n");
		selSql.append("left join tb_lib_book_info t14 on t13.info_id = t14.id \n");
		selSql.append("where  \n");
		selSql.append("   t.book_detail_id in (");
		int i =1;
		int size = bookDetailsId.size();
		for (String str:bookDetailsId){
			selSql.append("'");
			selSql.append(str);
			if(i<size){
				selSql.append("',");
			}
			i++;
		}
		selSql.append("  ')  and to_days(t.update_time)= to_days(now()) and t.is_close='1' and not exists(select 1 from tb_order_details where t.book_detail_id= book_detail_id and t.update_time< update_time) ");
		return dao.findList(selSql.toString(), queryParams.toArray(),BookBackOutDto.class);
	}

}
