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

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

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 com.ssj.dao.sys.analysis.dao.AnalysisQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;

@Repository
public class AnalysisQueryDaoImpl  implements AnalysisQueryDao {

    @Autowired
    private PagingHibernateJdbcDao dao;
    

	@Override
	public Long countLibJoinConsumeSumByLibService(String date) {
		Long count=new Long(0);
		StringBuilder sb = new StringBuilder();
        sb.append(" SELECT IFNULL(SUM(t.lib_service),0) as count from tb_lib_join_consume t LEFT JOIN fx_merchant m ON t.from_merchant_id=m.id WHERE m.type=1 AND t.type in ('2') AND date_format(t.create_time, '%Y-%m') =?  ");
        List<Object> param = new ArrayList<>();
        param.add(date);
        List<Map<String, Object>> list=dao.findMap(sb.toString(), param.toArray());
        if(list!=null && list.size()>0) {
        	count=Long.parseLong(list.get(0).get("count").toString());
        }
        return count;
	}

	@Override
	public Long countFxMerchantSumByPortNum() {
		Long count=new Long(0);
		StringBuilder sb = new StringBuilder();
        sb.append(" SELECT IFNULL(SUM(t.port_num),0) as count from  fx_merchant t WHERE t.type=1 ");
        List<Object> param = new ArrayList<>();
        List<Map<String, Object>> list=dao.findMap(sb.toString(), param.toArray());
        if(list!=null && list.size()>0) {
        	count=Long.parseLong(list.get(0).get("count").toString());
        }
        return count;
	}

	@Override
	public List<Map<String,Object>> countFxMerchantSumByTop(String date) {
		StringBuilder sb = new StringBuilder();
        sb.append(" SELECT *,IFNULL(count/lib_service,0) as lcount from ( ");
        sb.append(" SELECT ");
        sb.append(" (SELECT IFNULL(SUM(c.lib_service),0) from tb_lib_join_consume c WHERE c.type in ('2') AND date_format(c.create_time, '%Y-%m') =? AND c.from_merchant_id=t.id ) as count,	");
        sb.append(" t.lib_service,");
        sb.append(" t.unit_name");
        sb.append(" from fx_merchant t WHERE t.type=1 ");
        sb.append(" ) as tmp ORDER BY lcount desc LIMIT 10 ");
        
        List<Object> param = new ArrayList<>();
        param.add(date);
        return dao.findMap(sb.toString(), param.toArray());
	}

	@Override
	public Page<Map<String, Object>> countFxMerchantServiceByDate(Map<String, Object> params, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
        sb.append("  SELECT ");
        sb.append("  t.id,t.unit_name,j.league_name, ");
        sb.append("  	( SELECT COUNT(DISTINCT s.vip_id) from tb_lib_vip_service s WHERE s.lib_id=t.lib_id AND date_format(s.start_time, '%Y-%m') =? 	) as count_1, ");
        sb.append("  	( SELECT COUNT(DISTINCT s.vip_id) from tb_lib_vip_service s WHERE s.lib_id=t.lib_id AND date_format(s.end_time, '%Y-%m') =? ) as count_2, ");
        sb.append("  	( SELECT COUNT(DISTINCT s.vip_id) from tb_lib_vip_service s WHERE s.lib_id=t.lib_id AND date_format(s.end_time, '%Y-%m') >=? ) as count_3, ");
        sb.append("  	( SELECT COUNT(DISTINCT s.vip_id) from tb_lib_vip_service s WHERE s.lib_id=t.lib_id AND date_format(s.end_time, '%Y-%m') >? ) as count_4");
        sb.append(" from fx_merchant t  ");
        sb.append(" LEFT JOIN  tb_lib_join j ON t.lib_id=j.id ");
        sb.append(" WHERE t.type=1 AND t.lib_id is NOT null ");
        List<Object> param = new ArrayList<>();
        param.add(params.get("date"));
        param.add(params.get("date"));
        param.add(params.get("date"));
        param.add(params.get("date"));
        if(params.get("unitName")!=null && StringUtils.isNotEmpty(params.get("unitName").toString())) {
        	 sb.append(" and t.unit_name like concat('%',?,'%')  ");
        	 param.add(params.get("unitName"));
        }
        sb.append(" ORDER BY t.create_time asc ");
        return dao.findPage(sb.toString(),param.toArray(),pageable);
	}
    
	@Override
	public Page<Map<String, Object>> countFxMerchantServiceByVipDate(Map<String, Object> params, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
		List<Object> param = new ArrayList<>();
		 
        String type=params.get("type")!=null ? params.get("type").toString() : "1";
       
        switch (type) {
			case "1":
				sb.append(" SELECT s.vip_id,u.wx_name,u.photo,v.parent_name,v.child_name,s.start_time,s.end_time from tb_lib_vip_service s  ");
				sb.append(" LEFT JOIN fx_merchant t ON s.lib_id=t.lib_id ");
				sb.append(" LEFT JOIN tb_lib_vip v ON s.vip_id=v.id ");
				sb.append(" LEFT JOIN tb_user u ON v.user_id=u.id ");
				sb.append(" WHERE date_format(s.start_time, '%Y-%m') =?");
				sb.append(" AND t.id=? ");
				param.add(params.get("date"));
				param.add(params.get("id"));
				if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
					sb.append(" AND v.child_name like concat('%',?,'%')  ");
					param.add(params.get("name"));
				}
				sb.append(" GROUP BY s.vip_id ORDER BY s.start_time DESC ");
				return dao.findPage(sb.toString(),param.toArray(),pageable);
			case "2":
				sb.append(" SELECT s.vip_id,u.wx_name,u.photo,v.parent_name,v.child_name,s.start_time,s.end_time from tb_lib_vip_service s  ");
				sb.append(" LEFT JOIN fx_merchant t ON s.lib_id=t.lib_id ");
				sb.append(" LEFT JOIN tb_lib_vip v ON s.vip_id=v.id ");
				sb.append(" LEFT JOIN tb_user u ON v.user_id=u.id ");
				sb.append(" WHERE date_format(s.end_time, '%Y-%m') =?");
				sb.append(" AND t.id=? ");
				param.add(params.get("date"));
				param.add(params.get("id"));
				if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
					sb.append(" AND v.child_name like concat('%',?,'%')  ");
					param.add(params.get("name"));
				}
				sb.append(" GROUP BY s.vip_id ORDER BY s.start_time DESC ");
				return dao.findPage(sb.toString(),param.toArray(),pageable);
			case "3":
				sb.append(" SELECT s.vip_id,u.wx_name,u.photo,v.parent_name,v.child_name,s.start_time,s.end_time from tb_lib_vip_service s  ");
				sb.append(" LEFT JOIN fx_merchant t ON s.lib_id=t.lib_id ");
				sb.append(" LEFT JOIN tb_lib_vip v ON s.vip_id=v.id ");
				sb.append(" LEFT JOIN tb_user u ON v.user_id=u.id ");
				sb.append(" WHERE date_format(s.end_time, '%Y-%m') >=?");
				sb.append(" AND t.id=? ");
				param.add(params.get("date"));
				param.add(params.get("id"));
				if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
					sb.append(" AND v.child_name like concat('%',?,'%')  ");
					param.add(params.get("name"));
				}
				sb.append(" GROUP BY s.vip_id ORDER BY s.start_time DESC ");
				return dao.findPage(sb.toString(),param.toArray(),pageable);
			case "4":
				sb.append(" SELECT s.vip_id,u.wx_name,u.photo,v.parent_name,v.child_name,s.start_time,s.end_time from tb_lib_vip_service s  ");
				sb.append(" LEFT JOIN fx_merchant t ON s.lib_id=t.lib_id ");
				sb.append(" LEFT JOIN tb_lib_vip v ON s.vip_id=v.id ");
				sb.append(" LEFT JOIN tb_user u ON v.user_id=u.id ");
				sb.append(" WHERE date_format(s.end_time, '%Y-%m') =?");
				sb.append(" AND t.id=? ");
				param.add(params.get("date"));
				param.add(params.get("id"));
				if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
					sb.append(" AND v.child_name like concat('%',?,'%')  ");
					param.add(params.get("name"));
				}
				sb.append(" GROUP BY s.vip_id ORDER BY s.start_time DESC ");
				return dao.findPage(sb.toString(),param.toArray(),pageable);
			default:
				return null;
		}
       
	}
    
	
	
	
	@Override
	public  Page<Map<String, Object>> countFxMerchantHomeworkByDay(Map<String, Object> params, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
        sb.append("  SELECT ");
        sb.append("  t.id,t.unit_name,j.league_name, ");
        sb.append("  	( SELECT COUNT(DISTINCT s.vip_id) from zyb_student_push s LEFT JOIN tb_lib_vip v ON s.vip_id=v.id WHERE v.lib_id=t.lib_id AND TO_DAYS(s.submit_time)=TO_DAYS(?) 	) as count_1, ");
        sb.append("  	( SELECT COUNT(DISTINCT s.vip_id) from scon_homework s LEFT JOIN tb_lib_vip v ON s.vip_id=v.id WHERE v.lib_id=t.lib_id AND s.start_time is NOT NULL AND s.end_time is NOT NULL AND TO_DAYS(s.create_time)=TO_DAYS(?) ) as count_2, ");
        sb.append("  	( SELECT COUNT(DISTINCT s.vip_id) from scon_homework_picture s LEFT JOIN tb_lib_vip v ON s.vip_id=v.id WHERE v.lib_id=t.lib_id AND TO_DAYS(s.create_time)=TO_DAYS(?) ) as count_3, ");
        sb.append("  	( SELECT COUNT(DISTINCT s.vip_id) from zyb_student_push s LEFT JOIN tb_lib_vip v ON s.vip_id=v.id WHERE v.lib_id=t.lib_id AND s.view_time IS not NULL AND TO_DAYS(s.submit_time)=TO_DAYS(?) ) as count_4");
        sb.append(" from fx_merchant t  ");
        sb.append(" LEFT JOIN  tb_lib_join j ON t.lib_id=j.id ");
        sb.append(" WHERE t.type=1 AND t.lib_id is NOT null ");
        List<Object> param = new ArrayList<>();
        param.add(params.get("date"));
        param.add(params.get("date"));
        param.add(params.get("date"));
        param.add(params.get("date"));
        if(params.get("unitName")!=null && StringUtils.isNotEmpty(params.get("unitName").toString())) {
        	 sb.append(" and t.unit_name like concat('%',?,'%')  ");
        	 param.add(params.get("unitName"));
        }
        sb.append(" ORDER BY t.create_time asc ");
        return dao.findPage(sb.toString(),param.toArray(),pageable);
	}

	@Override
	public Page<Map<String, Object>> countFxMerchantHomeworkByVipDay(Map<String, Object> params, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
		List<Object> param = new ArrayList<>();
		 
        String type=params.get("type")!=null ? params.get("type").toString() : "1";
       
        switch (type) {
			case "1":
				sb.append("  SELECT s.vip_id,u.wx_name,u.photo,v.parent_name,v.child_name,s.submit_time ");
				sb.append("  from zyb_student_push s ");
				sb.append("  LEFT JOIN fx_merchant t ON s.lib_id=t.lib_id ");
				sb.append("  LEFT JOIN tb_lib_vip v ON s.vip_id=v.id ");
				sb.append("  LEFT JOIN tb_user u ON v.user_id=u.id ");
				sb.append("  WHERE TO_DAYS(s.submit_time)=TO_DAYS(?) ");
				sb.append(" AND t.id=? ");
				param.add(params.get("date"));
				param.add(params.get("id"));
				if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
					sb.append(" AND v.child_name like concat('%',?,'%')  ");
					param.add(params.get("name"));
				}
				sb.append("  GROUP BY s.vip_id ORDER BY s.submit_time DESC ");
				return dao.findPage(sb.toString(),param.toArray(),pageable);
			case "2":
				sb.append("  SELECT s.vip_id,u.wx_name,u.photo,v.parent_name,v.child_name,SUM(s.used_time) as used_time ");
				sb.append("  from scon_homework s ");
				sb.append("  LEFT JOIN tb_lib_vip v ON s.vip_id=v.id ");
				sb.append("  LEFT JOIN fx_merchant t ON v.lib_id=t.lib_id ");
				sb.append("  LEFT JOIN tb_user u ON v.user_id=u.id ");
				sb.append("  WHERE s.start_time is NOT NULL AND s.end_time is NOT NULL ");
				sb.append("  AND TO_DAYS(s.create_time)=TO_DAYS(?) ");
				sb.append(" AND t.id=? ");
				param.add(params.get("date"));
				param.add(params.get("id"));
				if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
					sb.append(" AND v.child_name like concat('%',?,'%')  ");
					param.add(params.get("name"));
				}
				sb.append("  GROUP BY s.vip_id ORDER BY s.start_time DESC ");
				return dao.findPage(sb.toString(),param.toArray(),pageable);
			case "3":
				sb.append("  SELECT s.vip_id,u.wx_name,u.photo,v.parent_name,v.child_name,count(1) as count_num ");
				sb.append("  from scon_homework_picture s ");
				sb.append("  LEFT JOIN tb_lib_vip v ON s.vip_id=v.id ");
				sb.append("  LEFT JOIN fx_merchant t ON v.lib_id=t.lib_id ");
				sb.append("  LEFT JOIN tb_user u ON v.user_id=u.id ");
				sb.append("  WHERE TO_DAYS(s.create_time)=TO_DAYS(?) ");
				sb.append(" AND t.id=? ");
				param.add(params.get("date"));
				param.add(params.get("id"));
				if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
					sb.append(" AND v.child_name like concat('%',?,'%')  ");
					param.add(params.get("name"));
				}
				sb.append("  GROUP BY s.vip_id ORDER BY s.create_time DESC ");
				return dao.findPage(sb.toString(),param.toArray(),pageable);
			case "4":
				sb.append("  SELECT s.vip_id,u.wx_name,u.photo,v.parent_name,v.child_name,s.view_time ");
				sb.append("  from zyb_student_push s ");
				sb.append("  LEFT JOIN fx_merchant t ON s.lib_id=t.lib_id ");
				sb.append("  LEFT JOIN tb_lib_vip v ON s.vip_id=v.id ");
				sb.append("  LEFT JOIN tb_user u ON v.user_id=u.id ");
				sb.append("  WHERE s.view_time IS not NULL AND TO_DAYS(s.submit_time)=TO_DAYS(?) ");
				sb.append(" AND t.id=? ");
				param.add(params.get("date"));
				param.add(params.get("id"));
				if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
					sb.append(" AND v.child_name like concat('%',?,'%')  ");
					param.add(params.get("name"));
				}
				sb.append("  GROUP BY s.vip_id ORDER BY s.submit_time DESC ");
				return dao.findPage(sb.toString(),param.toArray(),pageable);
			default:
				return null;
		}
	}

	
	@Override
	public Page<Map<String, Object>> countFxMerchantCommonByDate(Map<String, Object> params, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
		String[] types=params.get("types").toString().split(";");
		List<Object> param = new ArrayList<>();
        sb.append("  SELECT ");
        sb.append("  t.id,t.unit_name,j.id as lib_id, j.league_name");
        for (int i = 0; i < types.length; i++) {
        	 sb.append(" , ( SELECT COUNT(DISTINCT a.vip_id) from tb_analysis a WHERE a.lib_id=t.lib_id AND a.type=? AND a.source=? AND TO_DAYS(a.create_time)>=TO_DAYS(?) AND TO_DAYS(a.create_time)<=TO_DAYS(?) ) as count_"+i );
        	 param.add(types[i]);
        	 param.add(params.get("source"));
        	 param.add(params.get("date"));
        	 param.add(params.get("date2"));
		}
        sb.append(" from fx_merchant t  ");
        sb.append(" LEFT JOIN  tb_lib_join j ON t.lib_id=j.id ");
        sb.append(" WHERE t.type=1 AND t.lib_id is NOT null ");
        if(params.get("unitName")!=null && StringUtils.isNotEmpty(params.get("unitName").toString())) {
        	 sb.append(" and t.unit_name like concat('%',?,'%')  ");
        	 param.add(params.get("unitName"));
        }
        sb.append(" ORDER BY t.create_time asc ");
        return dao.findPage(sb.toString(),param.toArray(),pageable);
	}
	
	
	@Override
	public Page<Map<String, Object>> countFxMerchantCommonByCountUserDate(Map<String, Object> params, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
		String[] types=params.get("types").toString().split(";");
		List<Object> param = new ArrayList<>();
        sb.append("  SELECT ");
        sb.append("  t.id,t.unit_name,j.id as lib_id, j.league_name");
        for (int i = 0; i < types.length; i++) {
        	 sb.append(" , ( SELECT COUNT(DISTINCT a.user_id) from tb_analysis a WHERE a.lib_id=t.lib_id AND a.type=? AND a.source=? AND TO_DAYS(a.create_time)=TO_DAYS(?)  AND TO_DAYS(a.create_time)<=TO_DAYS(?) ) as count_"+i );
        	 param.add(types[i]);
        	 param.add(params.get("source"));
        	 param.add(params.get("date"));
        	 param.add(params.get("date2"));
		}
        sb.append(" from fx_merchant t  ");
        sb.append(" LEFT JOIN  tb_lib_join j ON t.lib_id=j.id ");
        sb.append(" WHERE t.type=1 AND t.lib_id is NOT null ");
        if(params.get("unitName")!=null && StringUtils.isNotEmpty(params.get("unitName").toString())) {
        	 sb.append(" and t.unit_name like concat('%',?,'%')  ");
        	 param.add(params.get("unitName"));
        }
        sb.append(" ORDER BY t.create_time asc ");
        return dao.findPage(sb.toString(),param.toArray(),pageable);
	}

	
	@Override
	public Page<Map<String, Object>> countFxMerchantCommonByVipDate(Map<String, Object> params, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
		List<Object> param = new ArrayList<>();
        sb.append(" SELECT t.user_id,t.vip_id,u.wx_name,u.photo,v.parent_name,v.child_name,t.create_time ");
        sb.append(" from tb_analysis t  ");
        sb.append(" LEFT JOIN tb_lib_vip v ON t.vip_id=v.id ");
        sb.append(" LEFT JOIN tb_user u ON v.user_id=u.id ");
        sb.append(" WHERE t.lib_id=? AND t.type=? AND t.source =? AND TO_DAYS(t.create_time)>=TO_DAYS(?) AND TO_DAYS(t.create_time)<=TO_DAYS(?)  ");
        param.add(params.get("libId"));
        param.add(params.get("type"));
        param.add(params.get("source"));
        param.add(params.get("date"));
        param.add(params.get("date2"));
        if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
        	 sb.append(" and v.child_name like concat('%',?,'%')  ");
        	 param.add(params.get("name"));
        }
        sb.append(" GROUP BY TO_DAYS(t.create_time),t.vip_id ORDER BY t.create_time asc ");
        return dao.findPage(sb.toString(),param.toArray(),pageable);
	}
	
	
	
	@Override
	public Page<Map<String, Object>> countFxMerchantCommonByUserDate(Map<String, Object> params, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
		List<Object> param = new ArrayList<>();
        sb.append(" SELECT t.user_id,u.wx_name,u.photo,u.user_name,t.create_time ");
        sb.append(" from tb_analysis t  ");
        sb.append(" LEFT JOIN tb_user u ON t.user_id=u.id ");
        sb.append(" WHERE t.lib_id=? AND t.type=? AND t.source =? AND TO_DAYS(t.create_time)>=TO_DAYS(?) AND TO_DAYS(t.create_time)<=TO_DAYS(?)  ");
        param.add(params.get("libId"));
        param.add(params.get("type"));
        param.add(params.get("source"));
        param.add(params.get("date"));
        param.add(params.get("date2"));
        if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
        	 sb.append(" and u.user_name like concat('%',?,'%')  ");
        	 param.add(params.get("name"));
        }
        sb.append(" GROUP BY TO_DAYS(t.create_time),t.user_id ORDER BY t.create_time asc ");
        return dao.findPage(sb.toString(),param.toArray(),pageable);
	}



}
