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

import com.ssj.dao.weixin.story.dao.StoryQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
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.ArrayList;
import java.util.List;
import java.util.Map;


/** 
 * @author  ZhangCaibao 
 * @version 2017年8月22日 上午10:11:00 
 * 类说明 
 */
@Repository
public class StoryQueryDaoImpl  implements  StoryQueryDao {

	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public List<Map<String, Object>> findByStoryTaskId(String storyTaskId) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append(" SELECT  id ,user_name ,coor_name, date_format(create_time, '%Y-%m-%d %H:%i:%s')as create_time  , concat('https://t.sharingschool.com/upload',voice) as voice   from    tb_story_card");
		selSql.append(" where  story_task_id = ? ");
		queryParams.add(storyTaskId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findByStoryIdAndNum(
			String storyId, String num,String userId) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append(" select  k.* ,MAX(d.user_id) as max_type,    ");
		selSql.append(" CASE k.days_num     ");
		selSql.append(" WHEN 1 THEN '第一课'    ");
		selSql.append(" WHEN 2 THEN '第二课'    ");
		selSql.append(" WHEN 3 THEN '第三课'    ");
		selSql.append(" WHEN 4 THEN '第四课'    ");
		selSql.append(" WHEN 5 THEN '第五课'    ");
		selSql.append(" WHEN 6 THEN '第六课'    ");
		selSql.append(" WHEN 7 THEN '第七课'    ");
		selSql.append(" WHEN 8 THEN '第一课'    ");
		selSql.append(" WHEN 9 THEN '第二课'    ");
		selSql.append(" WHEN 10 THEN '第三课'    ");
		selSql.append(" WHEN 11 THEN '第四课'    ");
		selSql.append(" WHEN 12 THEN '第五课'    ");
		selSql.append(" WHEN 13 THEN '第六课'    ");
		selSql.append(" WHEN 14 THEN '第七课'    ");
		selSql.append(" WHEN 15 THEN '第一课'    ");
		selSql.append(" WHEN 16 THEN '第二课'    ");
		selSql.append(" WHEN 17 THEN '第三课'    ");
		selSql.append(" WHEN 18 THEN '第四课'    ");
		selSql.append(" WHEN 19 THEN '第五课'    ");
		selSql.append(" WHEN 20 THEN '第六课'    ");
		selSql.append(" WHEN 21 THEN '第七课'    ");
		selSql.append(" ELSE '第七课' END  as tian     ");
		selSql.append("   from  tb_story_task  k  left  JOIN  tb_story_card d  ");
		selSql.append(" on k.id =  d.story_task_id and  d.user_id = ? ");
		selSql.append(" where  k.story_id = ? and k.num = ? ");
		selSql.append(" GROUP BY k.id   ");
		selSql.append(" ORDER BY  k.days_num asc  ");
		queryParams.add(userId);
		queryParams.add(storyId);
		queryParams.add(num);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findByUserIdList(String userId,String storyTaskId) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append(" SELECT  *  from   tb_story_card t  where  t.user_id = ? ");
		queryParams.add(userId);
		if(StringUtils.isNotEmpty(storyTaskId)){
			selSql.append(" and t.story_task_id = ? ");
			queryParams.add(storyTaskId);
		}else{
			selSql.append(" and TO_DAYS(t.create_time)=TO_DAYS(NOW()) ");
		}
		selSql.append(" ORDER BY   create_time DESC ");
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public Map<String, Object> findGrImg(String isbn13) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append(" SELECT convert(sum(replace(file_gr,',',''))/count(num),decimal(10,0)) as sumnum from tb_story_img where stat=1 and object_id = ? and file_gr is not null");
		queryParams.add(isbn13);
		List<Map<String, Object>> mapList = dao.findMap(selSql.toString(), queryParams.toArray());
		return mapList != null && mapList.size()>0 ?mapList.get(0):null;
	}

	@Override
	public List<Map<String, Object>> randBookByGrLists(String filegr) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append(" SELECT * from ( SELECT DISTINCT(t.isbn13),t.title,t.start_age,t.end_age,t.local_image,t.book_gr  from tb_lib_book_info t  WHERE t.isbn13 is not NULL and t.isbn13 <> '' AND t.summary is not NULL AND t.summary <> '' ");
		if(StringUtils.isNotEmpty(filegr) && !"0".equals(filegr)){
			selSql.append("  ORDER BY ABS(?-t.book_gr) ASC LIMIT 12 ");
			queryParams.add(filegr);
		}
		selSql.append(" ) as tmp ORDER BY RAND() LIMIT 3  ");
		
		return  dao.findMap(selSql.toString(), queryParams.toArray());
	}
	
	@Override
	public List<Map<String, Object>> randBookByKeywordLists(String keywordFeel) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("SELECT * from (SELECT DISTINCT(t.isbn13),t.title,t.start_age,t.end_age,t.local_image   from tb_lib_book_info t  WHERE t.isbn13 is not NULL and t.isbn13 <> '' AND t.summary is not NULL AND t.summary <> ''  ORDER BY ABS(?-t.keyword_feel) asc LIMIT 12 ) as tmp ORDER BY RAND() LIMIT 3 ");
		queryParams.add(keywordFeel);
		return  dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public Page<Map<String, Object>> integralRank(
			Map<String, Object> searchParams, Pageable pageable) {
		 StringBuilder selSQL = new StringBuilder();
	        List<Object> queryParams = new ArrayList<Object>();
	        selSQL.append(" SELECT  t.id,t.user_id,t.num ,t.type,t.total, ");
	        selSQL.append("  t.num*25 as score,  ");
	        selSQL.append(" u.wx_name ,u.photo  from  tb_story_task_integral  t LEFT JOIN tb_user u  ");
	        selSQL.append(" on t.user_id = u.id   ");
	        selSQL.append(" where t.type = ? ");
	        queryParams.add(searchParams.get("type"));
	        selSQL.append(" ORDER BY t.num desc  ");
	        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}
}
