package com.ssj.dao.sys.smarttime.dao.impl;

import com.ssj.dao.sys.smarttime.dao.SmarttimeQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.service.conch.student.dto.SmartTimeGroupDTO;
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;
import java.util.Objects;

/** 
 * @author  ZhangCaibao 
 * @version 2017年4月24日 上午11:59:29 
 * 类说明 
 */
@Repository
public class SmarttimeQueryDaoImpl  implements  SmarttimeQueryDao {
	@Autowired
	private PagingHibernateJdbcDao dao;
	@Override
	public Page<Map<String, Object>> findGroupByKeyword(Map<String, Object> searchParams, Pageable pageable) {
	 	StringBuilder selSQL = new StringBuilder();
	 	List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("SELECT date(create_time) create_date FROM scon_smarttime_picture s where s.is_delete = 0 and s.vip_id = ? ");
        selSQL.append(" GROUP BY create_date ORDER BY s.create_time desc");
        queryParams.add(searchParams.get("vipId"));
        return dao.findPage(selSQL.toString(), queryParams.toArray(),pageable);
	}

	@Override
	public Page<SmartTimeGroupDTO> searchAppSmartTimeGroupList(Map<String, Object> params, Pageable pageable) {
		StringBuilder sql = new StringBuilder("select ");

		Integer type = (Integer) params.get("type");
		if (Objects.equals(1, type)) {//根据种类分组
			sql.append("date(create_time) ");
		} else {//type=2，根据日期分组
			sql.append("keyword ");
		}

		sql.append("group_type, group_concat(file_key order by create_time desc ) smart_time_list_str ");
		sql.append("from scon_smarttime_picture ");
		sql.append("where vip_id = '").append(params.get("vipId")).append("'");
		sql.append("group by group_type ");
		sql.append("order by create_time desc ");

		return dao.findPage(sql.toString(), pageable, SmartTimeGroupDTO.class);
	}

}
