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

import com.ssj.dao.weixin.admin.dao.BorrowingQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
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.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class BorrowingQueryDaoImpl implements  BorrowingQueryDao{
	@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 s.end_time ,s.vip_id,v.child_name,v.img_url,v.school,s.lib_id,v.grade ");
        selSQL.append("  FROM tb_lib_vip_service s ");
        selSQL.append(" LEFT JOIN tb_lib_vip v on v.id=s.vip_id ");
        selSQL.append("  WHERE s.species='S001'AND s.stat='1' AND NOW() <= s.end_time AND s.lib_id=? 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"));
        }
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
}

	

	@Override
	public Map<String, Object> getTodayData(String libId) {
		StringBuffer selSql = new StringBuffer();
		List<Object> queryParams = new ArrayList<Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		selSql.append("SELECT a.yyrs,b.dgrs ,c.jytu ,d.ddsl from " );
		selSql.append("(SELECT  count(DISTINCT(vip_id)) as yyrs from tb_lib_subscribe  " );
		selSql.append(" WHERE order_time is NOT NULL " );
		selSql.append(" and order_date = CURDATE() " );
		selSql.append(" AND lib_id = ? " );
		selSql.append(" ) a  " );
		selSql.append("left join (" );
		selSql.append("SELECT  count(DISTINCT(vip_id)) as dgrs  from tb_lib_subscribe " );
		selSql.append("WHERE order_date = CURDATE() " );
		selSql.append("and order_time is  NULL " );
		selSql.append(" AND lib_id = ? " );
		selSql.append(" )b on 1=1 " );
		selSql.append(" LEFT JOIN (" );
		selSql.append(" SELECT COUNT(1) AS jytu FROM tb_order_details t " );
		selSql.append(" LEFT JOIN tb_lib_book_detail d ON t.book_detail_id = d.id " );
		selSql.append(" WHERE t.create_time >= CURDATE() " );
		selSql.append(" and  d.lib_id= ?" );
		selSql.append(" )c on 1=1 " );
		selSql.append(" LEFT JOIN(" );
		selSql.append(" SELECT COUNT(1) AS ddsl FROM tb_lib_order_details  " );
		selSql.append(" WHERE create_time >=CURDATE() " );
		selSql.append(" and lib_id = ? " );
		selSql.append(" ) d ON 1=1 " );
		queryParams.add(libId);
		queryParams.add(libId);
		queryParams.add(libId);
		queryParams.add(libId);
		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>>  findForEventReminderPage(
			Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("  SELECT a.id,CONCAT(b.parent_name,'家长购买',a.details,'会员服务，支付金额',a.price,'元') as showstr,'1' as type,b.img_url,a.create_time ");
        selSQL.append("  from tb_lib_order_details a  ");
        selSQL.append(" INNER JOIN tb_lib_vip b on a.vip_id = b.id ");
        selSQL.append("  where a.create_time > CURDATE()   and b.img_url is NOT NULL   and a.lib_id = ? ");
        selSQL.append("  union ALL ");
        selSQL.append("  SELECT a.id,CONCAT(b.wx_name,'销售人员代付',a.details,'支付金额',a.price,'元') as showstr,'1' as type,v.img_url,a.create_time  ");
        selSQL.append(" from tb_lib_order_details a ");
        selSQL.append("   INNER JOIN tb_lib_vip v on a.vip_id = v.id ");
        selSQL.append("   INNER JOIN tb_user b on a.user_id = b.id ");
        selSQL.append("   where a.create_time > CURDATE()  AND v.img_url IS NULL   and a.lib_id = ? ");
        selSQL.append("  union ALL ");
        selSQL.append("  SELECT t.id,CONCAT(t11.parent_name,'家长预约到馆，希望您能提供相应服务的准备工作') as showstr,'2' as type,t11.img_url ,t.create_time ");
        selSQL.append(" from tb_lib_subscribe t ");
        selSQL.append(" INNER JOIN tb_lib_vip t11 on t.vip_id = t11.id ");
        selSQL.append("  where t.create_time > CURDATE() and t.confirm_user IS  NULL  and t.order_time is NOT NULL       and t.lib_id = ? ");
        queryParams.add(searchParams.get("lib_id"));
        queryParams.add(searchParams.get("lib_id"));
        queryParams.add(searchParams.get("lib_id"));
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
}



	@Override
	public List<Map<String, Object>> findForEventReminder(String libId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("  SELECT a.id,CONCAT(b.parent_name,'家长购买',a.details,'会员服务，支付金额',a.price,'元') as showstr,'1' as type,b.img_url,a.create_time");
        selSQL.append("  from tb_lib_order_details a  ");
        selSQL.append(" INNER JOIN tb_lib_vip b on a.vip_id = b.id ");
        selSQL.append("  where a.create_time > CURDATE()   and b.img_url is NOT NULL   and a.lib_id = ? ");
        selSQL.append("  union ALL ");
        selSQL.append("  SELECT a.id,CONCAT(b.wx_name,'销售人员代付',a.details,'支付金额',a.price,'元') as showstr,'1' as type,v.img_url,a.create_time  ");
        selSQL.append(" from tb_lib_order_details a ");
        selSQL.append("   INNER JOIN tb_lib_vip v on a.vip_id = v.id ");
        selSQL.append("   INNER JOIN tb_user b on a.user_id = b.id ");
        selSQL.append("   where a.create_time > CURDATE()  AND v.img_url IS NULL   and a.lib_id = ? ");
        selSQL.append("  union ALL ");
        selSQL.append("  SELECT t.id,CONCAT(t11.parent_name,'家长预约到馆，希望您能提供相应服务的准备工作') as showstr,'2' as type,t11.img_url ,t.create_time ");
        selSQL.append(" from tb_lib_subscribe t ");
        selSQL.append(" INNER JOIN tb_lib_vip t11 on t.vip_id = t11.id ");
        selSQL.append("  where t.create_time > CURDATE() and t.confirm_user IS  NULL  and t.order_time is NOT NULL       and t.lib_id = ? ");
        queryParams.add(libId);
        queryParams.add(libId);
        queryParams.add(libId);
        return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	

}
