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

import java.util.ArrayList;
import java.util.HashMap;
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.train.dao.TrainQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;

@Repository
public class TrainQueryDaoImpl  implements TrainQueryDao {

    @Autowired
    private PagingHibernateJdbcDao dao;
    
    @Override
	public Page<Map<String, Object>> findTrainVideoInfoByPage(Map<String, Object> params, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
		List<Object> param = new ArrayList<>();
        sb.append(" SELECT *,(SELECT count(1) from tr_exam_info e WHERE e.status=1 and e.video_id=t.id ) as exam_num from tr_video_info t WHERE 1=1 ");
        if(params.get("roleType")!=null && StringUtils.isNotEmpty(params.get("roleType").toString())) {
       	 	sb.append(" and t.role_type=?  ");
       	 	param.add(params.get("roleType"));
        }
        if(params.get("module")!=null && StringUtils.isNotEmpty(params.get("module").toString())) {
        	sb.append(" and t.module like concat('%',?,'%')  ");
        	param.add(params.get("module"));
        }
        if(params.get("title")!=null && StringUtils.isNotEmpty(params.get("title").toString())) {
       	  	sb.append(" and t.title like concat('%',?,'%')  ");
       	  	param.add(params.get("title"));
        }
        if(params.get("status")!=null && StringUtils.isNotEmpty(params.get("status").toString())) {
       	  	sb.append(" and t.status =? ");
       	  	param.add(params.get("status"));
        }
        sb.append(" ORDER BY t.role_type ASC,t.indes ASC ");
        return dao.findPage(sb.toString(),param.toArray(),pageable);
	}

	@Override
	public Page<Map<String, Object>> findMerchantTeaNumByPage(Map<String, Object> params, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
		List<Object> param = new ArrayList<>();
        sb.append(" SELECT 	t.unit_name,t.id,t.pos_code,t.lib_id, ");
        sb.append(" (SELECT COUNT(DISTINCT m.id) from tr_video_info m WHERE m.role_type=1 AND m.`status`=1 ) as role_num_1, ");
        sb.append(" (SELECT COUNT(DISTINCT m.id) from tr_video_info m WHERE m.role_type=2 AND m.`status`=1 ) as role_num_2, ");
        sb.append(" (SELECT COUNT(DISTINCT m.id) from tr_video_info m WHERE m.role_type=3 AND m.`status`=1 ) as role_num_3, ");
        sb.append(" (SELECT COUNT(DISTINCT m.id) from tr_video_info m WHERE m.role_type=4 AND m.`status`=1 ) as role_num_4, ");
        sb.append(" (SELECT COUNT(DISTINCT m.id) from tr_train_user m WHERE m.role_type=1 AND m.lib_id=t.lib_id) as pal_num, ");
        sb.append(" (SELECT COUNT(DISTINCT m.id) from tr_train_user m WHERE m.lib_id=t.lib_id) as tea_num ");
        sb.append(" from fx_merchant t WHERE 1=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"));
        }
        if(params.get("id")!=null && StringUtils.isNotEmpty(params.get("id").toString())) {
       	 	sb.append(" AND t.id=? ");
       	 	param.add(params.get("id"));
        }
        sb.append(" ORDER BY t.create_time desc ");
        return dao.findPage(sb.toString(),param.toArray(),pageable);
	}

	@Override
	public Page<Map<String, Object>> findTeaInfoNumByPage(Map<String, Object> params, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
		List<Object> param = new ArrayList<>();
        sb.append(" SELECT   *, ");
        sb.append(" ( SELECT IFNULL(COUNT(DISTINCT u.user_id),0) from tr_video_user u LEFT JOIN tr_video_info i ON u.video_id=i.id WHERE i.`status`=1 AND u.`status`=1 AND u.role_type=1 AND u.user_id=t.user_id 	) as role_count_1,");
        sb.append(" ( SELECT IFNULL(COUNT(DISTINCT u.user_id),0) from tr_video_user u LEFT JOIN tr_video_info i ON u.video_id=i.id WHERE i.`status`=1 AND u.`status`=1 AND u.role_type=2 AND u.user_id=t.user_id 	) as role_count_2,");
        sb.append(" ( SELECT IFNULL(COUNT(DISTINCT u.user_id),0) from tr_video_user u LEFT JOIN tr_video_info i ON u.video_id=i.id WHERE i.`status`=1 AND u.`status`=1 AND u.role_type=3 AND u.user_id=t.user_id 	) as role_count_3,");
        sb.append(" ( SELECT IFNULL(COUNT(DISTINCT u.user_id),0) from tr_video_user u LEFT JOIN tr_video_info i ON u.video_id=i.id WHERE i.`status`=1 AND u.`status`=1 AND u.role_type=4 AND u.user_id=t.user_id 	) as role_count_4,");
       
        sb.append(" (SELECT COUNT(DISTINCT m.id) from tr_video_info m WHERE m.role_type=1 AND m.`status`=1) as role_num_1, ");
        sb.append(" (SELECT COUNT(DISTINCT m.id) from tr_video_info m WHERE m.role_type=2 AND m.`status`=1) as role_num_2, ");
        sb.append(" (SELECT COUNT(DISTINCT m.id) from tr_video_info m WHERE m.role_type=3 AND m.`status`=1) as role_num_3, ");
        sb.append(" (SELECT COUNT(DISTINCT m.id) from tr_video_info m WHERE m.role_type=4 AND m.`status`=1) as role_num_4  ");
        
        sb.append(" from tr_train_user t WHERE 1=1 ");
        sb.append(" AND t.lib_id=?   ");
        param.add(params.get("libId"));
       
        if(params.get("type")!=null && StringUtils.isNotEmpty(params.get("type").toString())) {
        	 sb.append(" AND t.role_type=? ");
        	 param.add(params.get("type"));
        }
        if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
        	sb.append(" AND t.name like concat('%',?,'%') ");
       	 	param.add(params.get("name"));
        }
        sb.append(" GROUP BY t.id ");
        sb.append(" ORDER BY t.create_time desc ");
        return dao.findPage(sb.toString(),param.toArray(),pageable);
	}
	
	
	
	@Override
	public Map<String, Object> findTrainVideoUserByPage(Map<String, Object> params) {
		StringBuilder sb = new StringBuilder();
		List<Object> param = new ArrayList<>();
		sb.append(" SELECT  ");
		sb.append(" 	(SELECT COUNT(*)  from ( ");
		sb.append(" 		SELECT  u.user_id,COUNT(DISTINCT u.video_id) AS count from tr_video_user u");
		sb.append(" 		LEFT JOIN tr_video_info i ON u.video_id=i.id");
		sb.append(" 		LEFT JOIN tr_train_user m ON u.user_id=m.id");
		sb.append(" 		WHERE i.`status`=1 AND u.`status`=1 AND u.role_type=1 AND m.lib_id=? ");
		sb.append(" 		GROUP BY u.user_id ");
		sb.append(" 		HAVING count>=? ");
		sb.append(" 	) as tmp) as role_count_1, ");
		param.add(params.get("lib_id"));
	    param.add(params.get("role_num_1"));
		
		sb.append(" 	(SELECT COUNT(*)  from ( ");
		sb.append(" 		SELECT  u.user_id,COUNT(DISTINCT u.video_id) AS count from tr_video_user u");
		sb.append(" 		LEFT JOIN tr_video_info i ON u.video_id=i.id");
		sb.append(" 		LEFT JOIN tr_train_user m ON u.user_id=m.id");
		sb.append(" 		WHERE i.`status`=1 AND u.`status`=1 AND u.role_type=2 AND m.lib_id=? ");
		sb.append(" 		GROUP BY u.user_id ");
		sb.append(" 		HAVING count>=? ");
		sb.append(" 	) as tmp) as role_count_2, ");
		param.add(params.get("lib_id"));
	    param.add(params.get("role_num_2"));
	    
	    
	    sb.append(" 	(SELECT COUNT(*)  from ( ");
		sb.append(" 		SELECT  u.user_id,COUNT(DISTINCT u.video_id) AS count from tr_video_user u");
		sb.append(" 		LEFT JOIN tr_video_info i ON u.video_id=i.id");
		sb.append(" 		LEFT JOIN tr_train_user m ON u.user_id=m.id");
		sb.append(" 		WHERE i.`status`=1 AND u.`status`=1 AND u.role_type=3 AND m.lib_id=? ");
		sb.append(" 		GROUP BY u.user_id ");
		sb.append(" 		HAVING count>=? ");
		sb.append(" 	) as tmp) as role_count_3, ");
		param.add(params.get("lib_id"));
	    param.add(params.get("role_num_3"));
	    
	    sb.append(" 	(SELECT COUNT(*)  from ( ");
  		sb.append(" 		SELECT  u.user_id,COUNT(DISTINCT u.video_id) AS count from tr_video_user u");
  		sb.append(" 		LEFT JOIN tr_video_info i ON u.video_id=i.id");
  		sb.append(" 		LEFT JOIN tr_train_user m ON u.user_id=m.id");
  		sb.append(" 		WHERE i.`status`=1 AND u.`status`=1 AND u.role_type=4 AND m.lib_id=? ");
  		sb.append(" 		GROUP BY u.user_id ");
  		sb.append(" 		HAVING count>=? ");
  		sb.append(" 	) as tmp) as role_count_4 ");
  		param.add(params.get("lib_id"));
  	    param.add(params.get("role_num_4"));
  	    
  	    List<Map<String, Object>> list= dao.findMap(sb.toString(),param.toArray());
  	    
        return list!=null && list.size()>0 ? list.get(0): new HashMap<String, Object>();
	}

	@Override
	public List<Map<String, Object>> findTrainVideoUserByUserId(String userId) {
		StringBuilder sb = new StringBuilder();
		List<Object> param = new ArrayList<>();
        sb.append(" SELECT ");
        sb.append(" t.role_type as role_type, ");
        sb.append(" COUNT(*) as count_num,  ");
        sb.append(" ( ");
        sb.append("  	SELECT COUNT(DISTINCT u.video_id)  from tr_video_user u ");
        sb.append("  	LEFT JOIN tr_video_info i ON u.video_id=i.id");
        sb.append("  	WHERE i.`status`=1 AND u.`status`=1 AND t.role_type=u.role_type AND u.user_id=? ");
        sb.append(" ) as success_num ");
        sb.append(" from tr_video_info t WHERE t.`status`=1 GROUP BY t.role_type ORDER BY t.role_type ");
        param.add(userId);
        return dao.findMap(sb.toString(),param.toArray());
	}

	@Override
	public List<Map<String, Object>> findTrainVideoUserByRoleType(String userId, Integer roleType, String module) {
		StringBuilder sb = new StringBuilder();
		List<Object> param = new ArrayList<>();
        sb.append(" SELECT *, ");
        sb.append(" (SELECT count(DISTINCT u.video_id) from tr_video_user u WHERE u.video_id=t.id AND u.user_id=? AND u.`status`=1  ) as un_lock ");
        sb.append(" from tr_video_info t   ");
        sb.append(" WHERE t.`status`=1  ");
        param.add(userId);
        if(roleType!=null) {
	        sb.append("  AND t.role_type=?  ");
	     	param.add(roleType);
        }
        if(StringUtils.isNotEmpty(module)) {
        	sb.append("  AND t.module=?  ");
        	param.add(module);
        }
        sb.append("  ORDER BY t.indes asc ");
        return dao.findMap(sb.toString(),param.toArray());
	}

	@Override
	public List<Map<String, Object>> findVideoFileByType() {
		StringBuilder sb = new StringBuilder();
		List<Object> param = new ArrayList<>();
        sb.append(" SELECT t.role_type,	t.url, ");
        sb.append(" (SELECT count(1) from `tr_video_file` f WHERE f.`status`=1 AND f.type=2 AND f.role_type=t.role_type) as num  ");
        sb.append(" FROM  `tr_video_file` t WHERE  t.`status`=1 AND t.type=1 GROUP BY t.role_type  ORDER BY t.role_type ASC ");
        return dao.findMap(sb.toString(),param.toArray());
	}

	@Override
	public Page<Map<String, Object>> findVideoFileBySysPage(Map<String, Object> params, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
		List<Object> param = new ArrayList<>();
        sb.append(" SELECT * from tr_video_file t WHERE t.`status`=1  ");
        if(params.get("type")!=null && StringUtils.isNotEmpty(params.get("type").toString())) {
       	 	sb.append(" AND t.type=?  ");
       	 	param.add(params.get("type"));
        }
        if(params.get("roleType")!=null && StringUtils.isNotEmpty(params.get("roleType").toString())) {
       	 	sb.append(" AND t.role_type=?  ");
       	 	param.add(params.get("roleType"));
        }
        if(params.get("module")!=null && StringUtils.isNotEmpty(params.get("module").toString())) {
        	sb.append(" AND t.module=?  ");
        	param.add(params.get("module"));
        }
        if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
       	  	sb.append(" AND t.title like concat('%',?,'%')  ");
       	  	param.add(params.get("name"));
        }
        sb.append("  ORDER BY t.type ASC,t.create_time DESC ");
        return dao.findPage(sb.toString(),param.toArray(),pageable);
	}
	
	@Override
	public Page<Map<String, Object>> findVideoFileByPage(Map<String, Object> params, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
		List<Object> param = new ArrayList<>();
        sb.append(" SELECT * from tr_video_file t WHERE t.`status`=1  ");
        if(params.get("type")!=null && StringUtils.isNotEmpty(params.get("type").toString())) {
       	 	sb.append(" AND t.type=?  ");
       	 	param.add(params.get("type"));
        }
        if(params.get("roleType")!=null && StringUtils.isNotEmpty(params.get("roleType").toString())) {
       	 	sb.append(" AND t.role_type=?  ");
       	 	param.add(params.get("roleType"));
        }
        if(params.get("module")!=null && StringUtils.isNotEmpty(params.get("module").toString())) {
        	sb.append(" AND t.module=?  ");
        	param.add(params.get("module"));
        }
        if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
       	  	sb.append(" AND t.title like concat('%',?,'%')  ");
       	  	param.add(params.get("name"));
        }
        sb.append("  ORDER BY t.create_time DESC ");
        return dao.findPage(sb.toString(),param.toArray(),pageable);
	}

	@Override
	public Page<Map<String, Object>> findTrainUserBySysPage(Map<String, Object> params, Pageable pageable) {
		StringBuilder sb = new StringBuilder();
		List<Object> param = new ArrayList<>();
        sb.append(" SELECT * from tr_train_user t WHERE t.status=1   ");
        if(params.get("roleType")!=null && StringUtils.isNotEmpty(params.get("roleType").toString())) {
       	 	sb.append(" and t.role_type=?  ");
       	 	param.add(params.get("roleType"));
        }
        if(params.get("mobile")!=null && StringUtils.isNotEmpty(params.get("mobile").toString())) {
        	sb.append(" and t.mobile like concat('%',?,'%')  ");
        	param.add(params.get("mobile"));
        }
        if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
       	  	sb.append(" and t.`name` like concat('%',?,'%')  ");
       	  	param.add(params.get("name"));
        }
        if(params.get("libId")!=null && StringUtils.isNotEmpty(params.get("libId").toString())) {
       	  	sb.append(" and t.libId =? ");
       	  	param.add(params.get("libId"));
        }
        sb.append(" ORDER BY t.create_time DESC ");
        return dao.findPage(sb.toString(),param.toArray(),pageable);
	}


	
	
}
