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

import com.ssj.dao.sys.counter.dao.CounterDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.sys.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

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


@Repository
public class CounterDaoImpl implements CounterDao{
	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public List<Map<String, Object>> selectByCounter(String libId,String type,Date startTime, Date endTime) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("  select  ");
        if (StringUtils.isNotEmpty(libId) && !libId.contains(";")) {
        	 selSQL.append(" * ");
        }else{
        	 selSQL.append(" count_time,SUM(count) as count ");
        }
        selSQL.append(" from tb_counter ");
        selSQL.append(" where  count_time BETWEEN  ? and ?");
        	queryParams.add(startTime);
        	queryParams.add(endTime);
        if (StringUtils.isNotEmpty(libId)){
        	String[] libIds=libId.split(";");
        	selSQL.append(" and lib_id in ( ");
        	for (int i = 0; i < libIds.length; i++) {
        		selSQL.append(" ? ");
        		if(libIds.length-1>i){
        			selSQL.append(" , ");
        		}
            	queryParams.add(libIds[i]);
			}
        	selSQL.append(" ) ");
        }
        if (StringUtils.isNotEmpty(type)) {
        	selSQL.append(" and type=?");
        	queryParams.add(type);
        }
        if (StringUtils.isEmpty(libId) || libId.contains(";")) {
        	selSQL.append(" GROUP BY count_time ");
        }
        selSQL.append(" order by  ");
        selSQL.append(" count_time desc ");
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public Integer frontByCounter(String libId, String type) {
		
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("  select IFNULL(SUM(count),0) as count from tb_counter t");
        if(!Constants.ORDER.equals(type)){//总的改为当月的
        	selSQL.append(" where TO_DAYS(t.count_time)=TO_DAYS(date_add(NOW(), interval -1 day)) ");
        }else{
        	selSQL.append(" where date_format(t.count_time,'%Y-%m')=date_format(now(),'%Y-%m') ");
        }
        if (StringUtils.isNotEmpty(libId)) {
        	String[] libIds=libId.split(";");
        	selSQL.append(" and lib_id in ( ");
        	for (int i = 0; i < libIds.length; i++) {
        		selSQL.append(" ? ");
        		if(libIds.length-1>i){
        			selSQL.append(" , ");
        		}
            	queryParams.add(libIds[i]);
			}
        	selSQL.append(" ) ");
        }
        if (StringUtils.isNotEmpty(type)) {
        	selSQL.append(" and type=? ");
        	if(!Constants.ORDER.equals(type)){
        		queryParams.add(type);
        	}else{
        		queryParams.add(Constants.ORDERDAY);
        	}
        }
        selSQL.append(" order by  ");
        selSQL.append(" count_time desc ");
        List<Map<String, Object>>  counts=dao.findMap(selSQL.toString(),queryParams.toArray());
        return Integer.parseInt(counts.get(0).get("count").toString());
	}

	@Override
	public List<Map<String, Object>> getNotActiveCounter(String libId,Boolean iscount) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("SELECT t.lib_id,COUNT(DISTINCT(t.vip_id)) as notactivenum ");
        selSQL.append(" from tb_lib_vip_service t ");
        selSQL.append(" LEFT JOIN tb_lib_vip v ON t.vip_id=v.id  LEFT JOIN tb_lib_order_details d ON d.id=t.order_details_id  WHERE  d.order_status!=2 AND ");
        if(!iscount){
        	selSQL.append(" MONTH(t.start_time)=MONTH(NOW()) AND ");
        }
        if (StringUtils.isNotEmpty(libId)) {
        	String[] libIds=libId.split(";");
        	selSQL.append(" t.lib_id in ( ");
        	for (int i = 0; i < libIds.length; i++) {
        		selSQL.append(" ? ");
        		if(libIds.length-1>i){
        			selSQL.append(" , ");
        		}
            	queryParams.add(libIds[i]);
			}
        	selSQL.append(" ) AND");
        }
        selSQL.append(" t.stat=1 AND v.user_id IS NULL  GROUP BY t.lib_id ");
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}
	
	@Override
	public List<Map<String, Object>> getPayUserCounter(String libId,Boolean iscount) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT   d.lib_id,COUNT(1) as payusernum,t.league_name ");
        selSQL.append(" from   tb_lib_order_details d  ");
        selSQL.append(" join  tb_lib_join  t  on d.lib_id = t.id   ");
        if(!iscount){
        	selSQL.append(" WHERE MONTH(d.create_time)=MONTH(NOW()) and d.order_status!=2 ");
        }
        if (StringUtils.isNotEmpty(libId)) {
        	String[] libIds=libId.split(";");
        	selSQL.append(" AND t.id in ( ");
        	for (int i = 0; i < libIds.length; i++) {
        		selSQL.append(" ? ");
        		if(libIds.length-1>i){
        			selSQL.append(" , ");
        		}
            	queryParams.add(libIds[i]);
			}
        	selSQL.append(" ) ");
        }
        selSQL.append(" GROUP BY d.lib_id ORDER BY payusernum DESC LIMIT 0,10 ");
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> exportActive(String libId) {
		
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT j.league_name, v.create_time,tv.parent_name,tv.child_name,tv.phone,tv.school,tv.grade,v.end_time,u.wx_name,'活跃' as e_type, (SELECT `name` from sys_sort s where s.`code`=v.species ) as speciesname,  ");
        selSQL.append("  (SELECT `name` from sys_sort s where s.`code`=v.type ) as typename from ( ");
        selSQL.append(" SELECT DISTINCT(vip_id) from tb_lib_subscribe t    ");
        selSQL.append(" where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(t.create_time)   ");
        selSQL.append(" AND t.ryke_time is NOT NULL ");
        selSQL.append(" ORDER BY t.create_time desc ");
        selSQL.append(" ) as tmp3 LEFT JOIN tb_lib_vip_service v ON tmp3.vip_id =v.vip_id ");
        selSQL.append(" LEFT JOIN tb_lib_vip tv ON tmp3.vip_id =tv.id ");
        selSQL.append(" LEFT JOIN tb_lib_join j ON v.lib_id=j.id  ");
        selSQL.append(" LEFT JOIN tb_lib_order_details o ON v.order_details_id=o.id  ");
        selSQL.append(" LEFT JOIN tb_user u ON o.sales_user_id=u.id  ");
        
        if (StringUtils.isNotEmpty(libId)) {
        	String[] libIds=libId.split(";");
        	selSQL.append(" where v.lib_id in ( ");
        	for (int i = 0; i < libIds.length; i++) {
        		selSQL.append(" ? ");
        		if(libIds.length-1>i){
        			selSQL.append(" , ");
        		}
            	queryParams.add(libIds[i]);
			}
        	selSQL.append(" ) ");
        }
        selSQL.append(" GROUP BY v.vip_id  ");
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> exportImminent(String libId) {
		
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT j.league_name, v.create_time,tv.parent_name,tv.child_name,tv.phone,tv.school,tv.grade,v.end_time,u.wx_name,'即将沉默' as e_type, (SELECT `name` from sys_sort s where s.`code`=v.species ) as speciesname,  ");
        selSQL.append("  (SELECT `name` from sys_sort s where s.`code`=v.type ) as typename from ( ");
        selSQL.append(" SELECT DISTINCT(vip_id1) as vip_id from (    ");
        selSQL.append(" 	SELECT * from (  ");
        selSQL.append(" SELECT DISTINCT(vip_id) as vip_id1  from tb_lib_subscribe t  ");
        selSQL.append("  where DATE_SUB(CURDATE(), INTERVAL 30 DAY) > date(t.create_time)  ");
        selSQL.append("  AND t.ryke_time is NOT NULL  ");
        selSQL.append(" ORDER BY t.create_time desc ");
        selSQL.append(" )as tmp1 LEFT  JOIN  ( ");
        selSQL.append(" SELECT DISTINCT(vip_id) as vip_id2 from tb_lib_subscribe t   ");
        selSQL.append(" 	where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(t.create_time)  ");
        selSQL.append(" AND t.ryke_time is NOT NULL  ");
        selSQL.append(" ORDER BY t.create_time desc  ");
        selSQL.append("	)as tmp2 ON tmp1.vip_id1=tmp2.vip_id2  ");
        selSQL.append(" ) as tmp3 where vip_id2 IS NULL  ");
        selSQL.append(" ) as tmp4 LEFT JOIN tb_lib_vip_service v ON tmp4.vip_id =v.vip_id ");
        selSQL.append(" LEFT JOIN tb_lib_vip tv ON tmp4.vip_id =tv.id ");
        selSQL.append(" LEFT JOIN tb_lib_join j ON v.lib_id=j.id  ");
        selSQL.append(" LEFT JOIN tb_lib_order_details o ON v.order_details_id=o.id  ");
        selSQL.append(" LEFT JOIN tb_user u ON o.sales_user_id=u.id  ");
        
        if (StringUtils.isNotEmpty(libId)) {
        	String[] libIds=libId.split(";");
        	selSQL.append(" where v.lib_id in ( ");
        	for (int i = 0; i < libIds.length; i++) {
        		selSQL.append(" ? ");
        		if(libIds.length-1>i){
        			selSQL.append(" , ");
        		}
            	queryParams.add(libIds[i]);
			}
        	selSQL.append(" ) ");
        }
        selSQL.append(" GROUP BY v.vip_id  ");
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> exportSilent(String libId) {
		
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT j.league_name, v.create_time,tv.parent_name,tv.child_name,tv.phone,tv.school,tv.grade,v.end_time,u.wx_name,'已沉默' as e_type, (SELECT `name` from sys_sort s where s.`code`=v.species ) as speciesname,  ");
        selSQL.append("  (SELECT `name` from sys_sort s where s.`code`=v.type ) as typename from ( ");
        selSQL.append(" SELECT  DISTINCT(vip_id1) as vip_id from ( ");
        selSQL.append(" SELECT DISTINCT(t.vip_id) as vip_id1 from tb_lib_vip_service t    ");
        selSQL.append(" 	where DATE_SUB(CURDATE(), INTERVAL 60 DAY) > date(t.create_time)  ");
        selSQL.append(" )as tmp1 LEFT JOIN ( ");
        selSQL.append(" 		SELECT DISTINCT(vip_id) as vip_id2  from tb_lib_subscribe t  ");
        selSQL.append(" where DATE_SUB(CURDATE(), INTERVAL 60 DAY) <= date(t.create_time) ");
        selSQL.append(" AND t.ryke_time is NOT NULL   ");
        selSQL.append(" 	ORDER BY t.create_time desc   ");
        selSQL.append("  ) as tmp2 ON tmp1.vip_id1=tmp2.vip_id2  ");
        selSQL.append("  where vip_id2 IS NULL  ");
        selSQL.append(" ) as tmp3 LEFT JOIN tb_lib_vip_service v ON tmp3.vip_id =v.vip_id   ");
        selSQL.append(" LEFT JOIN tb_lib_vip tv ON tmp3.vip_id =tv.id   ");
        selSQL.append(" LEFT JOIN tb_lib_join j ON v.lib_id=j.id  ");
        
        selSQL.append(" LEFT JOIN tb_lib_order_details o ON v.order_details_id=o.id  ");
        selSQL.append(" LEFT JOIN tb_user u ON o.sales_user_id=u.id  ");

        if (StringUtils.isNotEmpty(libId)) {
        	String[] libIds=libId.split(";");
        	selSQL.append(" where v.lib_id in ( ");
        	for (int i = 0; i < libIds.length; i++) {
        		selSQL.append(" ? ");
        		if(libIds.length-1>i){
        			selSQL.append(" , ");
        		}
            	queryParams.add(libIds[i]);
			}
        	selSQL.append(" ) ");
        }
        selSQL.append(" GROUP BY v.vip_id  ");
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}
	
	
	



}
