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

import com.ssj.dao.weixin.library.dao.IOneBookQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.service.reading.out.BookListOutDto;
import com.ssj.service.weixin.library.dto.SchoolManagerDTO;
import com.ssj.service.weixin.library.dto.TeacherInfoDTO;
import com.ssj.service.weixin.library.dto.out.BookApplyShowOutDto;
import com.ssj.service.weixin.library.dto.out.BookDetaiOutDto;
import com.ssj.service.weixin.library.dto.out.BookHisOutDto;
import org.apache.commons.collections.CollectionUtils;
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.*;


@Repository
public class OneQueryDaoImpl implements IOneBookQueryDao {
	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public Map<String, Object> getTjInfo(String userId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		selSql.append("select a.yjbook,b.signnum,c.readnum,concat(ifnull(d.h,0),'') as h,concat(ifnull(d.m,0),'') as m from (\n" );
		selSql.append("SELECT count(1) as yjbook from tb_lib_user_history t\n" );
		selSql.append("where t.user_id = ? and t.order_type='1'\n" );
		selSql.append(") a left join (\n" );
		selSql.append("SELECT count(1) as signnum from tb_jxy_read_sign t\n" );
		selSql.append("where t.user_id = ?\n" );
		selSql.append(") b on 1=1\n" );
		selSql.append("left join (\n" );
		selSql.append("select count(1) as readnum from tb_lib_book_pl t \n" );
		selSql.append("where t.user_id = ?\n" );
		selSql.append(") c on 1=1\n" );
		selSql.append("left join (\n" );
		selSql.append("select FLOOR(bb.need_time/60) as h,bb.need_time%60 as m from (\n" );
		selSql.append("select sum(IFNULL(t13.need_time,0)) as need_time from tb_jxy_read_sign a\n" );
		selSql.append("left join tb_jxy_read_leady t13 on a.isbn13 = t13.isbn13\n" );
		selSql.append("where a.user_id = ?\n" );
		selSql.append(")bb)d on 1=1 ");
		queryParams.add(userId);
		queryParams.add(userId);
		queryParams.add(userId);
		queryParams.add(userId);
		List<Map<String, Object>> list = dao.findMap(selSql.toString(), queryParams.toArray());
		if(list.size()>0){
			map = list.get(0);
		}
		return map;
	}

	@Override
	public List<Map<String, Object>> groomBookList() {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT t11.isbn13,t11.title,ifnull(t12.isount,0) as isount,t11.local_image,t11.author,t11.summary  FROM tb_jxy_read_leady t  \n" );
		selSql.append("left join tb_lib_book_base t11 on t.isbn13 = t11.isbn13 \n");
		selSql.append("left join( select count(isbn13) as isount,isbn13 from   \n" );
		selSql.append("tb_jxy_read_in GROUP BY isbn13 )t12 on t.isbn13 = t12.isbn13 \n" );
		selSql.append("ORDER BY rand() LIMIT 3 " );

		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> readBookList(String userId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("select a.title,a.author,a.summary,a.id,a.isbn13,b.iscount,a.local_image from ( \n" );
		selSql.append("SELECT t11.title,t11.author,t11.summary,t.id,t11.isbn13,t11.local_image from tb_lib_book_detail t  \n" );
		selSql.append("left join tb_lib_book_info t11 on t.info_id = t11.id \n" ); 
		selSql.append("where t.user_id = ?  " );
		selSql.append(")a left join ( \n" );
		selSql.append("select t12.isbn13,count(t12.isbn13) as iscount from tb_lib_user_history t  \n" ); 
		selSql.append(" join ( SELECT t.info_id,t.id from tb_lib_book_detail t  where t.user_id = ? ) 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("  group by t12.isbn13 \n" ); 
		selSql.append(") b on a.isbn13 = b.isbn13 \n" ); 
		queryParams.add(userId);
		queryParams.add(userId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public Map<String, Object> findSignInfo(String isbn13, String userId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		selSql.append("select t11.*,t12.signNum,IFNULL(t13.readnum,0) as readnum ,t14.signCount from (\n" );
		selSql.append("SELECT *\n" );
		selSql.append("FROM (SELECT *\n" );
		selSql.append("   FROM (\n" );
		selSql.append("       SELECT\n" );
		selSql.append("        uid,\n" );
		selSql.append("        max(days)   lianxu_days,\n" );
		selSql.append("        min(login_day) start_date,\n" );
		selSql.append("        max(login_day) end_date,\n" );
		selSql.append(" DATE(SYSDATE()) nowdate\n" );
		selSql.append("       FROM (SELECT\n" );
		selSql.append("           uid,\n" );
		selSql.append("           @cont_day \\:=\n" );
		selSql.append("           (CASE\n" );
		selSql.append("           WHEN (@last_uid = uid AND DATEDIFF(created_ts, @last_dt) = 1)\n" );
		selSql.append("            THEN\n" );
		selSql.append("             (@cont_day + 1)\n" );
		selSql.append("           WHEN (@last_uid = uid AND DATEDIFF(created_ts, @last_dt) < 1)\n" );
		selSql.append("            THEN\n" );
		selSql.append("             (@cont_day + 0)\n" );
		selSql.append("           ELSE\n" );
		selSql.append("            1\n" );
		selSql.append("           END)                       AS days,\n" );
		selSql.append("           (@cont_ix \\:= (@cont_ix + IF(@cont_day = 1, 1, 0))) AS cont_ix,\n" );
		selSql.append("           @last_uid \\:= uid,\n" );
		selSql.append("           @last_dt \\:= created_ts                login_day\n" );
		selSql.append("          FROM (SELECT\n" );
		selSql.append("              user_id as uid,\n" );
		selSql.append("              DATE(sign_time) created_ts\n" );
		selSql.append("             FROM tb_jxy_read_sign\n" );
		selSql.append("              WHERE user_id = ?\n" );
		selSql.append("             ORDER BY user_id, sign_time) AS t,\n" );
		selSql.append("           (SELECT\n" );
		selSql.append("            @last_uid \\:= '',\n" );
		selSql.append("            @last_dt \\:= '',\n" );
		selSql.append("            @cont_ix \\:= 0,\n" );
		selSql.append("            @cont_day \\:= 0) AS t1\n" );
		selSql.append("         ) AS t2\n" );
		selSql.append("       GROUP BY uid, cont_ix\n" );
		selSql.append("      -- HAVING lianxu_days > 10\n" );
		selSql.append("      ) tmp\n" );
		selSql.append("   ORDER BY lianxu_days DESC) ntmp\n" );
		selSql.append("GROUP BY uid) t11 left join (\n" );
		selSql.append("SELECT count(user_id) as signNum,user_id\n" );
		selSql.append("  from tb_jxy_read_sign\n" );
		selSql.append("  where user_id=?\n" );
		selSql.append(") t12 on t11.uid = t12.user_id \n" );
		selSql.append("left join (\n" );
		selSql.append("select count(1) as readnum,t.user_id from tb_lib_book_pl t\n" );
		selSql.append("where t.user_id=?\n" );
		selSql.append(") t13 on t11.uid = t13.user_id \n");
		selSql.append("left join (\n" );
		selSql.append("select count(1) as signCount,t.isbn13 from tb_jxy_read_sign t\n" );
		selSql.append("where t.isbn13=?\n" );
		selSql.append(") t14 on 1=1");
		queryParams.add(userId);
		queryParams.add(userId);
		queryParams.add(userId);
		queryParams.add(isbn13);
		List<Map<String, Object>> list = dao.findMap(selSql.toString(), queryParams.toArray());
		if(list.size()>0){
			map = list.get(0);
		}
		return map;
	}

	@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.id,t.user_id,t.isbn13,t.pl_text,t.pl_lat,t.pl_lng,t.pl_address,t.create_time,t.prise_num as praise,\n" );
		selSQL.append("t.photo,t.nick_name,t11.mp3url as mp3_url,GROUP_CONCAT(t12.img) as img_url,IFNULL(t13.id,'') as ispraise \n" );
		selSQL.append("from tb_lib_book_pl t  \n" );
		selSQL.append("left join tb_download_file t11 on t.mp3_url = t11.id  and t11.type='2' \n" );
		selSQL.append("left join tb_story_img t12 on t.id = t12.object_id\n" );
		selSQL.append("left join tb_jxy_sharing_parise t13 on t.id = t13.pl_id   and t13.user_id=?\n" );

        queryParams.add(params.get("userId"));
        if (params.get("isbn13") != null && !params.get("isbn13").equals("")) {
        	selSQL.append(" where t.isbn13 = ? ");
        	queryParams.add(params.get("isbn13"));
        }
		selSQL.append("group by t.id");
        selSQL.append("  order by t.prise_num DESC,t.create_time DESC  ");
		return dao.findPage(selSQL.toString(),queryParams.toArray(), pageable);
	}

	@Override
	public List<Map<String, Object>> findBorrowInfo(String vipId, String userId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("SELECT t11.title,t11.local_image,DATE_FORMAT(t.back_book_time,'%Y-%m-%d %H:%i:%s') as back_book_time,\n" );
        selSQL.append("DATE_FORMAT(t12.create_time,'%Y-%m-%d %H:%i:%s') as create_time from tb_lib_book_detail t\n" );
        selSQL.append("left JOIN tb_lib_book_info t11 on t.info_id = t11.id\n" );
        selSQL.append("left join tb_order_details t12 on t.id = t12.book_detail_id\n" );
        if(vipId==null || "".equals(vipId)){ //非购买会员
        	selSQL.append("where t12.is_close='2' and t.user_id=? ");
        	queryParams.add(userId);
        }else{//购买服务
        	selSQL.append("where t12.is_close='2' and t.user_id=? and t.vip_id=?");
        	queryParams.add(userId);
        	queryParams.add(vipId);
        }
		return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public Page<Map<String, Object>> myBorrowHis(Map<String, Object> params, Pageable pageable) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("select t12.local_image,t12.title,t.create_time,t.update_time,(TIMESTAMPDIFF(DAY,t.create_time,t.update_time)+1)  as borrowed_day \n");
		selSql.append(",(ifnull(t.money,0)+ifnull(t13.xjMoney,0)+ifnull(t14.yqMoney,0)) as totol,ifnull(t13.xjTimes,0) as xjTimes,ifnull(t14.yqDay,0) as yqday,t.user_id,t.vip_id \n");
		selSql.append("from tb_order_details 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("left join ( \n");
		selSql.append("select count(1) as xjTimes,sum(a.money) as xjMoney,a.detail_id from tb_lib_user_history a  \n");
		selSql.append("left join tb_order_details b on a.detail_id = b.id \n");
		if(params.get("vipId") != null && !"".equals(params.get("vipId"))){
			selSql.append("where a.vip_id = ? and a.order_type='2'\n");
			queryParams.add(params.get("vipId"));
		}else{
			selSql.append("where a.user_id = ? and a.order_type='2'\n");
			queryParams.add(params.get("userId"));
		}
		
		selSql.append("group by a.detail_id \n");
		selSql.append(") t13 on t13.detail_id = t.id \n");
		selSql.append("left join ( \n");
		selSql.append("select sum(a.money) as yqMoney,a.detail_id,sum(TIMESTAMPDIFF(DAY,a.start_time,a.end_time)) as yqDay  from tb_lib_user_history a  \n");
		selSql.append("left join tb_order_details b on a.detail_id = b.id \n");
		if(params.get("vipId") != null && !"".equals(params.get("vipId"))){
			selSql.append("where a.vip_id = ? and a.order_type='2'\n");
			queryParams.add(params.get("vipId"));
		}else{
			selSql.append("where a.user_id = ? and a.order_type='2'\n");
			queryParams.add(params.get("userId"));
		}
		selSql.append("group by a.detail_id\n");
		selSql.append(") t14 on t14.detail_id = t.id \n");
		if(params.get("vipId") != null && !"".equals(params.get("vipId"))){
			selSql.append("where t.vip_id = ? and t.is_close = '1' \n");
			queryParams.add(params.get("vipId"));
		}else{
			selSql.append("where t.user_id = ? and t.is_close = '1' \n");
			queryParams.add(params.get("userId"));
		}
		selSql.append(" order by t.update_time desc\n");
		return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
	}

	@Override
	public Page<Map<String, Object>> myReadHis(Map<String, Object> params, Pageable pageable) {
        List<Object> queryParams = new ArrayList<Object>();
        StringBuilder selSQL = new StringBuilder();
        selSQL.append("select a.type,a.pl_text,a.create_time,a.mp3_url,a.img_url,a.title,a.local_image,a.signType from (");
        selSQL.append("select '1' as type,t.pl_text,t.create_time,t11.mp3url as mp3_url,GROUP_CONCAT(t12.img) as img_url,b.title,b.local_image,0 as signType\n");
        selSQL.append("from tb_lib_book_pl t  \n");
        selSQL.append("left join tb_download_file t11 on t.mp3_url = t11.id  and t11.type='2' \n");
        selSQL.append("left join tb_story_img t12 on t.id = t12.object_id\n");
        selSQL.append("left join (\n");
        selSQL.append("SELECT t14.title,t14.local_image,t13.isbn13 from tb_lib_book_pl t13 \n");
        selSQL.append("   left join tb_lib_book_info t14 on t13.isbn13 = t14.isbn13\n");
        selSQL.append(" where t13.user_id = ? \n");
        selSQL.append("group by t13.isbn13\n");
        selSQL.append(")b on t.isbn13 = b.isbn13\n");
        selSQL.append(" where t.user_id = ? \n");
        selSQL.append("GROUP BY t.id \n");
        selSQL.append("union all\n");
        selSQL.append("select '2' as type,'',t.sign_time,'' as  mp3_url,'' as img_url,b.title,b.local_image,count(t.isbn13) as signType\n");
        selSQL.append("from tb_jxy_read_sign t  \n");
        selSQL.append("left join (\n");
        selSQL.append("SELECT t14.title,t14.local_image,t13.isbn13 from tb_jxy_read_sign t13 \n");
        selSQL.append("   left join tb_lib_book_info t14 on t13.isbn13 = t14.isbn13\n");
        selSQL.append(" where t13.user_id= ?\n");
        selSQL.append("group by t13.isbn13\n");
        selSQL.append(")b on t.isbn13 = b.isbn13\n");
        selSQL.append(" where t.user_id= ?\n");
        selSQL.append("GROUP BY t.isbn13");
        selSQL.append(") a order by a.create_time desc ");
        queryParams.add(params.get("userId"));
        queryParams.add(params.get("userId"));
        queryParams.add(params.get("userId"));
        queryParams.add(params.get("userId"));
		return dao.findPage(selSQL.toString(),queryParams.toArray(), pageable);
	}

	@Override
	public List<Map<String,Object>>  findMasterIdByLibId(List<String[]> masterIdByLibId) {
		 List<Object> queryParams = new ArrayList<Object>();
	     StringBuilder selSQL = new StringBuilder();
	     selSQL.append(" SELECT * from tb_lib_manager t ");
	     if(masterIdByLibId!=null && masterIdByLibId.size()>0){
	    	 selSQL.append(" where ");
	    	 for (String[] pages : masterIdByLibId) {
	    		 selSQL.append(" (t.type=6 AND t.lib_id=? AND t.user_id=?) ");
	    		 queryParams.add(pages[0]);
	    		 queryParams.add(pages[1]);
	    		 if(masterIdByLibId.indexOf(pages)<masterIdByLibId.size()-1){
	    			 selSQL.append(" OR ");
	    		 }
			 }
	     }
	     return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String,Object>>  findTeacherIdByMasterId(List<String[]> teacherIdByMasterId) {
		 List<Object> queryParams = new ArrayList<Object>();
	     StringBuilder selSQL = new StringBuilder();
	     selSQL.append(" SELECT * from tb_lib_manager t ");
	     if(teacherIdByMasterId!=null && teacherIdByMasterId.size()>0){
	    	 selSQL.append(" where ");
	    	 for (String[] pages : teacherIdByMasterId) {
	    		 selSQL.append(" (t.master_id=? AND t.user_id=?) ");
	    		 queryParams.add(pages[0]);
	    		 queryParams.add(pages[1]);
	    		 if(teacherIdByMasterId.indexOf(pages)<teacherIdByMasterId.size()-1){
	    			 selSQL.append(" OR ");
	    		 }
			 }
	     }
	     return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>>  findLibManagerByLibIdAndType (String libId, int type) {
		StringBuilder sb = new StringBuilder();
		sb.append(" select u.wx_account, m.name, u.wx_name from tb_lib_manager m left join tb_user u on m.user_id=u.id where m.lib_id=? and m.type=? and m.state=1");
		List<Object> params = new ArrayList<>();
		params.add(libId);
		params.add(type);
		return dao.findMap(sb.toString(), params.toArray());
	}

	@Override
	public Page<Map<String, Object>> findBylist(Map<String, Object> params, Pageable pageable) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT d.league_name ,c.title ,t13.`name` ,t14.`name` ,c.price  \n");
		selSql.append(",(ifnull(t.money,0)+ifnull(t13.xjMoney,0)+ifnull(t14.yqMoney,0)) as totol,ifnull(t13.xjTimes,0) as xjTimes,ifnull(t14.yqDay,0) as yqday,t.user_id,t.vip_id \n");
		selSql.append("left join tb_lib_book_detail b on a.book_detail_id = b.id \n");
		selSql.append("left join tb_lib_book_detail b on a.book_detail_id = b.id \n");
		selSql.append("left join tb_lib_join d on a.in_lib_id=d.id \n");
		selSql.append("left join sys_sort t13 on c.parent_theme=t13.code and t13.pid='44903f84-a4d8-47e7-ab82-11ffd8a3425f' \n");
		selSql.append("left join sys_sort t14 on c.child_theme=t14.code and t14.pid='a316f70c-1184-44b9-a636-dbe10262f176' \n");

		selSql.append("group by a.detail_id\n");
		selSql.append(") t14 on t14.detail_id = t.id \n");
		if(params.get("vipId") != null && !"".equals(params.get("vipId"))){
			selSql.append("where t.vip_id = ? and t.is_close = '1' \n");
			queryParams.add(params.get("vipId"));
		}else{
			selSql.append("where t.user_id = ? and t.is_close = '1' \n");
			queryParams.add(params.get("userId"));
		}
		selSql.append(" order by t.update_time desc\n");
		return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
	}

	@Override
	public Page<Map<String, Object>> findLockList(Map<String, Object> params, Pageable initPage) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT t11.id,t11.league_name,t12.title,t13.`name` as fname ,t14.`name` as sname,t12.price,GROUP_CONCAT(t15.bookcase)  as bookcase,t.create_time,t.book_size \n");
		selSql.append("from tb_lib_book_locking t \n");
		selSql.append(" join tb_lib_join t11 on t.in_lib_id=t11.id\n");
		selSql.append(" join tb_lib_book_info t12 on t.book_info_id=t12.id\n");
		selSql.append(" join tb_lib_book_detail t15 on t12.id = t15.info_id \n");
		selSql.append("left join sys_sort t13 on t12.parent_theme=t13.code and t13.pid='44903f84-a4d8-47e7-ab82-11ffd8a3425f'\n");
		selSql.append("left join sys_sort t14 on t12.child_theme=t14.code and t14.pid='a316f70c-1184-44b9-a636-dbe10262f176'\n");
		selSql.append("where   t.type=2 \n");
        if(params.get("leagueName") != null && !"".equals(params.get("leagueName"))){
            selSql.append(" and t11.league_name like concat('%',?,'%')  \n");
            queryParams.add(params.get("leagueName"));
        }
		if(params.get("stat") != null && !"".equals(params.get("stat"))){
			selSql.append(" and t.stat= ? \n");
			queryParams.add(params.get("stat"));
		}
		selSql.append("GROUP BY t11.id,t12.id \n");
		selSql.append("order by t11.id\n");
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}

	@Override
	public Page<SchoolManagerDTO> findPageByType(String subject, List<Integer> typeList, Pageable pageable) {
		String sql = "select user_id manager_user_id, name manager_name from tb_lib_manager where course_name = ? and state = 1 and type in (?) ";
		String typeStr = StringUtils.join(typeList, ",");

		List<Object> paramList = new LinkedList<>();
		paramList.add(subject);
		paramList.add(typeStr);

		return dao.findPage(sql, paramList.toArray(), pageable, SchoolManagerDTO.class);
	}

    @Override
    public TeacherInfoDTO findMyInfo(String teacherId) {
		String sql =
				"select tb_lib_manager.t_photo,\n" +
                "       tb_lib_manager.name        teacher_name,\n" +
                "       tb_lib_manager.school,\n" +
                "       schoolManager.user_id      manager_user_id,\n" +
                "       schoolManager.name         manager_user_name,\n" +
                "       tb_lib_manager.course_name subject,\n" +
                "       tb_lib_manager.grade       grade_str,\n" +
                "       tb_lib_manager.period      period_str,\n" +
                "       tb_lib_manager.telephone   phone,\n" +
                "       tb_lib_manager.id_num,\n" +
                "       tb_lib_manager.id_face,\n" +
                "       tb_card_info.card_name     cardholder,\n" +
                "       tb_card_info.card_num      card_number,\n" +
                "       tb_card_info.subbranch     bank_branch,\n" +
                "       tb_card_info.bank_icon,\n" +
                "       ssj_bank.id                bank_id,\n" +
                "       tb_card_info.card_bank     bank_name\n" +
                "from tb_lib_manager\n" +
                "         left join tb_lib_manager schoolManager\n" +
                "                   on tb_lib_manager.parent_id = schoolManager.user_id and schoolManager.type in (11, 12)\n" +
                "         left join tb_card_info on tb_lib_manager.user_id = tb_card_info.user_id and tb_card_info.state = 1\n" +
                "         left join ssj_bank on tb_card_info.card_bank = ssj_bank.bank_name\n" +
                "where tb_lib_manager.user_id = ?\n" +
                "  and tb_lib_manager.type in (8,11,15)\n" +
                "  and tb_lib_manager.state = 1\n";

		List<TeacherInfoDTO> list = dao.findList(sql, Collections.singletonList(teacherId).toArray(), TeacherInfoDTO.class);
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
    }

	@Override
	public TeacherInfoDTO findMyApplicationInfo(String teacherId) {
		String sql =
				"select tb_lib_manager_audit.img_url   t_photo,\n" +
                "       tb_lib_manager_audit.name      teacher_name,\n" +
                "       tb_lib_manager_audit.school,\n" +
                "       schoolManager.user_id          manager_user_id,\n" +
                "       schoolManager.name             manager_user_name,\n" +
                "       tb_lib_manager_audit.subject   subject,\n" +
                "       tb_lib_manager_audit.grade     grade_str,\n" +
                "       tb_lib_manager_audit.period    period_str,\n" +
                "       tb_lib_manager_audit.telephone phone,\n" +
                "       tb_lib_manager_audit.id_num,\n" +
                "       tb_lib_manager_audit.id_face,\n" +
                "       tb_lib_manager_audit.cardholder,\n" +
                "       tb_lib_manager_audit.card_number,\n" +
                "       tb_lib_manager_audit.bank_branch,\n" +
                "       tb_lib_manager_audit.bank_icon,\n" +
                "       tb_lib_manager_audit.bank_name,\n" +
                "       ssj_bank.id bank_id\n" +
                "from tb_lib_manager_audit\n" +
                "         left join tb_lib_manager schoolManager\n" +
                "                   on tb_lib_manager_audit.manager_id = schoolManager.user_id and schoolManager.type in (11, 12)\n" +
                "         left join ssj_bank on tb_lib_manager_audit.bank_name = ssj_bank.bank_name\n" +
                "where tb_lib_manager_audit.user_id = ?\n" +
                "  and tb_lib_manager_audit.type in (8,11)\n" +
                "order by tb_lib_manager_audit.create_time desc\n" +
                "limit 1\n";

		List<TeacherInfoDTO> list = dao.findList(sql, Collections.singletonList(teacherId).toArray(), TeacherInfoDTO.class);
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}

		return list.get(0);
	}

	@Override
	public Page<Map<String, Object>> findInfoList(Map<String, Object> params, Pageable initPage) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT case when t.type=1 then '带书离馆' else '送书到馆' end as type, \n");
		selSql.append("t11.league_name ,t12.title ,t13.`name` as fname,t14.`name` as sname,t12.price,t15.bookcase  \n");
		selSql.append(",t.create_time,t12.isbn13 \n");
		selSql.append("from tb_lib_book_switch_info t  \n");
		selSql.append("join tb_lib_join t11 on t.lib_id=t11.id \n");
		selSql.append("join tb_lib_book_detail t15 on t.book_detail_id = t15.id \n");
		selSql.append("join tb_lib_book_info t12 on t15.info_id=t12.id \n");
		selSql.append("left join sys_sort t13 on t12.parent_theme=t13.code and t13.pid='44903f84-a4d8-47e7-ab82-11ffd8a3425f' \n");
		selSql.append("left join sys_sort t14 on t12.child_theme=t14.code and t14.pid='a316f70c-1184-44b9-a636-dbe10262f176' \n");
		selSql.append("where 1=1  and t.state = 2 \n");
		if(!"".equals(params.get("leagueName").toString())){
			selSql.append("and   t11.league_name like concat('%',?,'%')  \n");
			queryParams.add(params.get("leagueName"));
		}
		if(!"".equals(params.get("statTime").toString())){
			selSql.append("and to_days(t.create_time) >= to_days(?) \n");
			queryParams.add(params.get("statTime"));
		}
		if(!"".equals(params.get("endTime").toString())){
			selSql.append("and to_days(t.create_time) <=to_days(?)\n");
			queryParams.add(params.get("endTime"));
		}
		if(!"".equals(params.get("type").toString())){
			selSql.append("and t.type = ? \n");
			queryParams.add(params.get("type"));
		}

		selSql.append("order by t.create_time asc\n");
		return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}

	@Override
	public List<Map<String, Object>> findRadomList(String toString, Integer bookSize, String libId,String idStr) {
		StringBuilder sb = new StringBuilder();
		sb.append("select t11.id,t.id as info_id,t.isbn13 from tb_lib_book_info t left  ");
		sb.append("join tb_lib_book_detail t11 on t.id = t11.info_id ");
		sb.append("left join( ");
		sb.append(" select a.isbn13,a.id from  tb_lib_book_info a join tb_lib_book_detail b on a.id = b.info_id ");
		sb.append(" where  b.lib_id=?  ");
		sb.append(") t12 on t.isbn13 = t12.isbn13 ");
		sb.append("where t11.bookcase is not null and t12.id is null and t.book_stock_remain >0 and  t11.lib_id='fa14e041-3823-4dac-b061-1e250e5a7d49'");
		sb.append(toString);
		sb.append(idStr);
		sb.append("  group by t.id  limit ?");

		List<Object> params = new ArrayList<>();
		params.add(libId);
		params.add(bookSize);
		return dao.findMap(sb.toString(), params.toArray());
	}

	@Override
	public Page<Map<String, Object>> findLibInfoList(Map<String, Object> params, Pageable initPage) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT t11.league_name ,t12.title ,t13.`name` as fname,t14.`name` as sname,t12.price,t15.bookcase  \n");
		selSql.append(",t12.isbn13 \n");
		selSql.append("from tb_lib_join t11 \n");
		selSql.append("join tb_lib_book_info t12 on t12.lib_id = t11.id \n");
		selSql.append("join tb_lib_book_detail t15 on t12.id = t15.info_id \n");
		selSql.append("left join sys_sort t13 on t12.parent_theme=t13.code and t13.pid='44903f84-a4d8-47e7-ab82-11ffd8a3425f' \n");
		selSql.append("left join sys_sort t14 on t12.child_theme=t14.code and t14.pid='a316f70c-1184-44b9-a636-dbe10262f176' \n");
		selSql.append("where  t11.libtype>1 \n");
		if(!"".equals(params.get("leagueName").toString())){
			selSql.append("and   t11.league_name like concat('%',?,'%')  \n");
			queryParams.add(params.get("leagueName"));
		}else{
			selSql.append(" and t11.league_name='' \n");
		}
		return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}

	@Override
	public Page<Map<String, Object>> findApplyInfoList(Map<String, Object> params, Pageable initPage) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT t12.id,t11.league_name ,t12.title ,t12.publish,t12.pai_date,t12.book_src,t12.create_time,t12.state,t12.audit_msg,t12.batch_id,radom_json  \n");
		selSql.append("from tb_lib_join t11 \n");
		selSql.append("join tb_lib_book_apply t12 on t12.lib_id = t11.id \n");
		selSql.append("where t12.type=? \n");
		queryParams.add(params.get("type"));
		if(!"".equals(params.get("leagueName").toString())){
			selSql.append("and   t11.league_name like concat('%',?,'%')  \n");
			queryParams.add(params.get("leagueName"));
		}
		return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}

	@Override
	public Page<BookHisOutDto> ApplyInfoList(Map<String, Object> params, Pageable pageable) {

		StringBuffer sql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		sql.append("SELECT t11.title,DATE_FORMAT(t.update_time,'%Y-%m-%d %H:%i:%S') as show_time,sum(t.book_size) as count1,t.id,CONCAT('调书申请包括《',t11.title,'》等',sum(t.book_size),'本书籍。') as show_words,t.stat  from tb_lib_book_locking t \n");
		sql.append("left join tb_lib_book_info t11 on t.book_info_id = t11.id\n");
		sql.append("where t.in_lib_id = ? and t.batch_id is null \n");
		sql.append("GROUP BY t.update_time\n");
		sql.append(" union all ");
		sql.append("SELECT '',DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%S') as show_time,0,a.batch_id as id,CONCAT('申请随机调书',JSON_EXTRACT(a.radom_json, '$.bookSize'),'本。') as show_words,case when a.state is null then 2 else 3 end as stat  from tb_lib_book_apply a \n");
		sql.append("where a.lib_id= ? and a.type=2\n");
		sql.append("order by  show_time desc \n");
		queryParams.add(params.get("libId"));
		queryParams.add(params.get("libId"));

		return dao.findPage(sql.toString(),queryParams.toArray(),pageable,BookHisOutDto.class);
	}

	@Override
	public Page<BookDetaiOutDto> ApplyInfoDetailList(Map<String, Object> params, Pageable pageable) {
		StringBuffer sql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		sql.append("SELECT t12.title,t12.local_image,b.name as theme_name,ifnull(c.heat,0) as heat,t11.book_info_id  \n");
		sql.append("from  tb_lib_book_locking t11  \n");
		sql.append("left join tb_lib_book_info t12 on t11.book_info_id = t12.id\n");
		sql.append("left join tb_lib_book_isbn_heat c on t11.isbn13 = c.isbn13\n");
		sql.append("left join sys_sort b on t12.child_theme=b.code and b.pid='a316f70c-1184-44b9-a636-dbe10262f176'\n");
		sql.append("where t11.batch_id = ? and t11.stat = 3 ");
		queryParams.add(params.get("batchId"));

		return dao.findPage(sql.toString(),queryParams.toArray(),pageable,BookDetaiOutDto.class);
	}

	@Override
	public Page<BookListOutDto> NewBookApplyDetailList(Map<String, Object> params, Pageable pageable) {
		StringBuffer sql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		sql.append("SELECT a.batch_id,a.book_img,a.title,a.publish,a.create_time from tb_lib_book_apply a\n");
		sql.append("where a.batch_id= ?\n");
		queryParams.add(params.get("id"));
		return dao.findPage(sql.toString(),queryParams.toArray(),pageable,BookListOutDto.class);
	}

	@Override
	public Page<BookHisOutDto> NewBookApplyInfoList(Map<String, Object> params, Pageable pageable) {
		StringBuffer sql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		sql.append("SELECT DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%S') as show_time,a.batch_id as id,CONCAT('申请书单包括《',a.title,'》等',count(1),'本书籍。') as show_words,case when a.state is null then 2 else 3 end as stat  from tb_lib_book_apply a \n");
		sql.append("where a.lib_id= ? and a.type=1 \n");
		sql.append("group by a.batch_id \n");
		sql.append("order by  a.create_time desc \n");
		queryParams.add(params.get("libId"));

		return dao.findPage(sql.toString(),queryParams.toArray(),pageable,BookHisOutDto.class);
	}

	@Override
	public Page<BookHisOutDto> randomInfoList(Map<String, Object> params, Pageable pageable) {
		StringBuffer sql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		sql.append("SELECT DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i:%S') as show_time,a.batch_id as id,CONCAT('申请随机调书',JSON_EXTRACT(a.radom_json, '$.bookSize'),'本。') as show_words,case when a.state is null then 2 else 3 end as stat  from tb_lib_book_apply a \n");
		sql.append("where a.lib_id= ? and a.type=2\n");
		sql.append("order by  a.create_time desc \n");
		queryParams.add(params.get("libId"));

		return dao.findPage(sql.toString(),queryParams.toArray(),pageable,BookHisOutDto.class);
	}

	@Override
	public Page<BookDetaiOutDto> randomDetailList(Map<String, Object> params, Pageable pageable) {
		StringBuffer sql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		sql.append("SELECT t11.title,t11.local_image,b.name as theme_name,ifnull(c.heat,0) as heat,t.book_info_id from tb_lib_book_locking t \n");
		sql.append("left join tb_lib_book_info t11 on t.book_info_id = t11.id\n");
		sql.append("left join tb_lib_book_isbn_heat c on t11.isbn13 = c.isbn13\n");
		sql.append("left join sys_sort b on t11.child_theme=b.code and b.pid='a316f70c-1184-44b9-a636-dbe10262f176'\n");
		sql.append("where t.batch_id = ? ");
		queryParams.add(params.get("id"));

		return dao.findPage(sql.toString(),queryParams.toArray(),pageable,BookDetaiOutDto.class);
	}

	@Override
	public Page<BookApplyShowOutDto> userApplyList(Map<String, Object> params, Pageable pageable) {
		StringBuffer sql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		sql.append("SELECT t.title,t.book_img,t.author,t.apply_reason,t.lib_id,t12.book_info_id,t12.isbn13,case when t12.id is null then 2 else 1 end state \n");
		sql.append("from tb_lib_user_apply t\n");
		sql.append("left join (\n");
		sql.append("	select t.id,t11.isbn13,t11.id as book_info_id from tb_lib_user_apply t \n");
		sql.append("	left join tb_lib_book_info t11 on t.title = t11.title and t11.lib_id='fa14e041-3823-4dac-b061-1e250e5a7d49'\n");
		sql.append("	where t.pid=? and  t11.book_stock_remain>0\n");
		sql.append("	GROUP BY t.id\n");
		sql.append(")t12 on t.id = t12.id\n");
		sql.append("where t.pid=?");
		queryParams.add(params.get("id"));
		queryParams.add(params.get("id"));
		return dao.findPage(sql.toString(),queryParams.toArray(),pageable,BookApplyShowOutDto.class);
	}
}
