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

import com.ssj.dao.weixin.grword.dao.ReadQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

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

@Repository
public class ReadQueryDaoImpl implements ReadQueryDao {
	
		@Autowired
		private PagingHibernateJdbcDao dao;

		@Override
		public Map<String, Object> findSubjectByIsbn13(String isbn13) {
			StringBuilder selSql = new StringBuilder();
			List<Object> queryParams = new ArrayList<Object>();
			selSql.append(" SELECT * from tb_read_subject t where t.isbn13=? ORDER BY t.sort desc LIMIT 1 ");
			queryParams.add(isbn13);
			List<Map<String, Object> > list=dao.findMap(selSql.toString(), queryParams.toArray());
			if(list!=null && list.size()>0){
				return list.get(0);
			}
			return null;
		}

		@Override
		public Map<String, Object> findAnswerByUserId(String userId,String isbn13,String scoreId) {
			StringBuilder selSql = new StringBuilder();
			List<Object> queryParams = new ArrayList<Object>();
			selSql.append(" SELECT * from tb_read_answer t where t.isbn13=? AND t.user_id=?  AND t.score_id=? ORDER BY t.sort desc LIMIT 1");
			queryParams.add(isbn13);
			queryParams.add(userId);
			queryParams.add(scoreId);
			List<Map<String, Object> > list=dao.findMap(selSql.toString(), queryParams.toArray());
			if(list!=null && list.size()>0){
				return list.get(0);
			}
			return null;
		}

		@Override
		public Map<String, Object> findSubjectByIsbn13AndSort(String isbn13,Integer sort) {
			StringBuilder selSql = new StringBuilder();
			List<Object> queryParams = new ArrayList<Object>();
			selSql.append(" SELECT * from tb_read_subject t where t.isbn13=? AND t.sort>? ORDER BY t.sort asc LIMIT 1 ");
			queryParams.add(isbn13);
			queryParams.add(sort);
			List<Map<String, Object> > list=dao.findMap(selSql.toString(), queryParams.toArray());
			if(list!=null && list.size()>0){
				return list.get(0);
			}
			return null;
		}

		@Override
		public List<Map<String, Object> > findBookByUserIdAndLevel(String userId,Integer level,Integer userGr,Integer size) {
			StringBuilder selSql = new StringBuilder();
			List<Object> queryParams = new ArrayList<Object>();
			//分馆的写法
//			selSql.append(" SELECT i.*,tmp1.state from (  ");
//			selSql.append(" 	SELECT * from tb_read_subject su WHERE EXISTS(  ");
//			selSql.append(" 	 SELECT DISTINCT(lib_id)  from tb_lib_vip_service t LEFT JOIN tb_lib_vip v ON t.vip_id=v.id   ");
//			selSql.append("   WHERE su.lib_id=t.lib_id  AND t.stat=1 and  TO_DAYS(end_time)  >=  TO_DAYS(now()) AND v.user_id=?  ");
//			selSql.append(" ) AND su.`level`=?  GROUP BY isbn13  )as tmp  ");
//			selSql.append(" LEFT JOIN tb_lib_book_info i ON tmp.isbn13=i.isbn13 AND tmp.lib_id=i.lib_id ");
//			selSql.append(" LEFT JOIN (  SELECT * from (SELECT * from tb_read_score t WHERE t.user_id=? ORDER BY t.state DESC )   ");
//			selSql.append(" as tt GROUP BY tt.isbn13  ");
//			selSql.append(" ) as tmp1 ON tmp.isbn13= tmp1.isbn13  ");
//			selSql.append(" LIMIT ? ");
//			queryParams.add(userId);
//			queryParams.add(level);
//			queryParams.add(userId);
//			queryParams.add(size);
			
			
			//不分馆的写法
			selSql.append("  SELECT i.*,IF(tmp1.state=0,'no',tmp1.state) as state,tmp1.adopt from (   ");
			selSql.append(" 	SELECT * from tb_read_subject su WHERE  su.`level`=?  GROUP BY isbn13   )as tmp    ");
			selSql.append(" 	LEFT JOIN tb_lib_book_info i ON tmp.isbn13=i.isbn13 AND tmp.lib_id=i.lib_id  ");
			selSql.append("   LEFT JOIN (    ");
			selSql.append(" SELECT * from (SELECT * from tb_read_score t WHERE t.user_id=? ORDER BY t.state DESC )   as tt GROUP BY tt.isbn13    ");
			selSql.append("  ) as tmp1 ON tmp.isbn13= tmp1.isbn13   ");
			selSql.append("  ORDER BY  ABS(?-i.book_gr) ASC ,i.create_time desc ");
			selSql.append(" LIMIT ? ");
			queryParams.add(level);
			queryParams.add(userId);
			queryParams.add(userGr);
			queryParams.add(size);
			return dao.findMap(selSql.toString(), queryParams.toArray());
		}

		@Override
		public List<Map<String, Object>> findBookByUserIdAndLevelSuccess(String userId) {
			StringBuilder selSql = new StringBuilder();
			List<Object> queryParams = new ArrayList<Object>();
			selSql.append(" SELECT tmp.*,(SELECT DISTINCT(r.`level`) from tb_read_subject r WHERE r.isbn13= tmp.isbn13  LIMIT 1 ) as `level` from  ( ");
			selSql.append(" SELECT DISTINCT(isbn13) as isbn13 from tb_read_score t WHERE t.user_id=? AND t.state=1 ");
			selSql.append(" ) as tmp  ");
			queryParams.add(userId);
			return dao.findMap(selSql.toString(), queryParams.toArray());
			
		}

		@Override
		public List<Map<String, Object>> findSubjectByIsbn13Page(String isbn13) {
			StringBuilder selSql = new StringBuilder();
			List<Object> queryParams = new ArrayList<Object>();
			selSql.append(" SELECT * from tb_read_subject t where t.isbn13=? ORDER BY t.sort asc ");
			queryParams.add(isbn13);
			return dao.findMap(selSql.toString(), queryParams.toArray());
		}

		@Override
		public List<Map<String, Object>> findAnswerByUserIdPage(String userId,String isbn13, String scoreId) {
			StringBuilder selSql = new StringBuilder();
			List<Object> queryParams = new ArrayList<Object>();
			selSql.append(" SELECT * from tb_read_answer t where t.isbn13=? AND t.user_id=?  AND t.score_id=? ORDER BY t.sort desc ");
			queryParams.add(isbn13);
			queryParams.add(userId);
			queryParams.add(scoreId);
			return dao.findMap(selSql.toString(), queryParams.toArray());
		}

		
	
}
