package com.ssj.dao.kmt.kmt.dao.impl;


import com.ssj.dao.kmt.kmt.dao.KmtArrangeQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
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 java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Repository
public class KmtArrangeQueryDaoImpl implements KmtArrangeQueryDao{
	
	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public List<Map<String, Object>> findHaveTeacherList(String parentTeacherId, String id, String teacherName) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT tmp.*,  ");
        selSQL.append("   (SELECT SUM(a.attend_type) from kmt_arrange_allot a where a.`status`=1 AND a.teacher_id=tmp.teacher_id AND DATE_FORMAT(a.arrange_date, '%Y%m' ) = DATE_FORMAT(tmp.arrange_date, '%Y%m')  AND TO_DAYS(a.arrange_date)<TO_DAYS(NOW())) as attend_sum , ");
        selSQL.append("   (SELECT COUNT(1) from kmt_arrange_allot a where a.`status`=1 AND a.teacher_id=tmp.teacher_id AND DATE_FORMAT(a.arrange_date, '%Y%m' ) = DATE_FORMAT(tmp.arrange_date, '%Y%m') AND TO_DAYS(a.arrange_date)<TO_DAYS(NOW())) as count ,");
        selSQL.append("   (SELECT COUNT(1) from kmt_arrange_allot a where a.`status`=1 AND a.teacher_id=tmp.teacher_id AND DATE_FORMAT(a.arrange_date, '%Y%m' ) = DATE_FORMAT(tmp.arrange_date, '%Y%m')) as fullcount");
        selSQL.append(" from ( ");
	        selSQL.append(" SELECT t.*,m.`name`,m.parent_id,m.t_photo from kmt_arrange_allot t  ");
	        selSQL.append(" LEFT JOIN tb_lib_manager m ON t.teacher_id=m.user_id AND m.type IN (8,13)  ");
	        selSQL.append(" where t.`status`=1 ");
	        selSQL.append(" AND t.arrange_detail_id=?  ");
	        queryParams.add(id);
	        if(StringUtils.isNotEmpty(teacherName)){
	        	selSQL.append(" AND m.`name` LIKE CONCAT(CONCAT('%',?,'%')) ");
	        	queryParams.add(teacherName);
	        }
	        selSQL.append(" GROUP BY t.teacher_id ");
	        
	        selSQL.append("	ORDER BY ");
	        if(StringUtils.isNotEmpty(parentTeacherId)){
	        	selSQL.append("	FIELD(m.parent_id,?), ");
	        	queryParams.add(parentTeacherId);
	        }
	        selSQL.append("	t.create_time DESC  ");
        selSQL.append(" ) as tmp  ");
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findNoHaveTeacherList(List<String> teacherIds, Date month) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT m.user_id as teacher_id, m.t_photo,m.`name`,m.parent_id,  ");
        selSQL.append(" (SELECT IFNULL(SUM(a.attend_type),0)  from kmt_arrange_allot a where a.`status`=1 AND a.teacher_id=m.user_id AND DATE_FORMAT(a.arrange_date, '%Y%m' ) = DATE_FORMAT(?, '%Y%m') AND TO_DAYS(a.arrange_date)<TO_DAYS(NOW())) as attend_sum , ");
        selSQL.append(" (SELECT COUNT(1) from kmt_arrange_allot a where a.`status`=1 AND a.teacher_id=m.user_id AND DATE_FORMAT(a.arrange_date, '%Y%m' ) = DATE_FORMAT(?, '%Y%m') AND TO_DAYS(a.arrange_date)<TO_DAYS(NOW())) as count, ");
        selSQL.append(" (SELECT COUNT(1) from kmt_arrange_allot a where a.`status`=1 AND a.teacher_id=m.user_id AND DATE_FORMAT(a.arrange_date, '%Y%m' ) = DATE_FORMAT(?, '%Y%m')) as fullcount");
        selSQL.append(" from tb_lib_manager m WHERE m.type IN (8,13)  AND m.user_id in(");
        selSQL.append("'");
        if(teacherIds.size()>1){
        	selSQL.append(String.join("','", teacherIds));
        }else{
        	selSQL.append(String.join("", teacherIds));
        }
        selSQL.append("'");
        selSQL.append(" )  ");
        selSQL.append(" GROUP BY m.user_id ORDER BY m.create_time DESC ");
        queryParams.add(month);
        queryParams.add(month);
        queryParams.add(month);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}
	
	
	@Override
	public Page<Map<String, Object>> findTeamTeacherList(String parentTeacherId,String type,String tempId,String arrangeDate,String teacherName,Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        //当查询时段为空的时候直接用少关联查询，性能快
        if(StringUtils.isEmpty(tempId) && StringUtils.isEmpty(arrangeDate)){
        	selSQL.append(" SELECT m.user_id as teacher_id, m.t_photo, m.`name`,m.parent_id,m.course_name, ");
	        selSQL.append("  (SELECT SUM(a.attend_type) from kmt_arrange_allot a where a.`status`=1 AND a.teacher_id=m.user_id AND DATE_FORMAT(a.arrange_date, '%Y%m' ) = DATE_FORMAT(CURDATE(), '%Y%m') AND TO_DAYS(a.arrange_date)<TO_DAYS(NOW())) as attend_sum , ");
	        selSQL.append("  (SELECT COUNT(1) from kmt_arrange_allot a where a.`status`=1 AND a.teacher_id=m.user_id AND DATE_FORMAT(a.arrange_date, '%Y%m' ) = DATE_FORMAT(CURDATE(), '%Y%m') AND TO_DAYS(a.arrange_date)<TO_DAYS(NOW())) as count , ");
	        selSQL.append("  (SELECT COUNT(1) from kmt_arrange_allot a where a.`status`=1 AND a.teacher_id=m.user_id AND DATE_FORMAT(a.arrange_date, '%Y%m' ) = DATE_FORMAT(CURDATE(), '%Y%m')) as fullcount");
	        selSQL.append("  from tb_lib_manager m WHERE m.parent_id=? AND m.state=1  AND m.is_cancelled != 1 AND m.type=? ");
	        queryParams.add(parentTeacherId);
	        queryParams.add(type);
	        if(StringUtils.isNotEmpty(teacherName)){
	        	selSQL.append(" AND m.`name` LIKE CONCAT(CONCAT('%',?,'%')) ");
	        	queryParams.add(teacherName);
	        }
	        selSQL.append(" ORDER BY fullcount desc,teacher_id ASC ");
        }else{
        	selSQL.append(" SELECT m.user_id AS teacher_id, m.t_photo,m.`name`,m.parent_id,m.course_name, ");
	        selSQL.append("  (SELECT SUM(a.attend_type) from kmt_arrange_allot a where a.`status`=1 AND a.teacher_id=m.user_id AND DATE_FORMAT(a.arrange_date, '%Y%m' ) = DATE_FORMAT(CURDATE(), '%Y%m') AND TO_DAYS(a.arrange_date)<TO_DAYS(NOW())) as attend_sum , ");
	        selSQL.append("  (SELECT COUNT(1) from kmt_arrange_allot a where a.`status`=1 AND a.teacher_id=m.user_id AND DATE_FORMAT(a.arrange_date, '%Y%m' ) = DATE_FORMAT(CURDATE(), '%Y%m') AND TO_DAYS(a.arrange_date)<TO_DAYS(NOW())) as count , ");
	        selSQL.append("  (SELECT COUNT(1) from kmt_arrange_allot a where a.`status`=1 AND a.teacher_id=m.user_id AND DATE_FORMAT(a.arrange_date, '%Y%m' ) = DATE_FORMAT(CURDATE(), '%Y%m')) as fullcount ");
	        selSQL.append("  FROM `kmt_arrange_allot` t ");
	        selSQL.append("  LEFT JOIN kmt_arrange_detail d ON t.arrange_detail_id = d.id  AND t.`status`=1 ");
	        selSQL.append("  LEFT JOIN tb_lib_manager m ON t.teacher_id = m.user_id ");
	        selSQL.append("  WHERE 1=1 ");
	        selSQL.append("  AND m.parent_id = ? ");
	        selSQL.append("  AND m.type = ? ");
	        queryParams.add(parentTeacherId);
	        queryParams.add(type);
	        if(StringUtils.isNotEmpty(arrangeDate)){
	        	selSQL.append(" AND d.arrange_date =? ");
	        	queryParams.add(arrangeDate);
	        }
	        if(StringUtils.isNotEmpty(tempId)){
	        	 selSQL.append("  AND d.arrange_template_id = ? ");
	        	queryParams.add(tempId);
	        }
	        if(StringUtils.isNotEmpty(teacherName)){
	        	selSQL.append(" AND m.`name` LIKE CONCAT(CONCAT('%',?,'%')) ");
	        	queryParams.add(teacherName);
	        }
	        selSQL.append("  AND m.state = 1 AND m.is_cancelled != 1 ");
	        selSQL.append("  GROUP BY t.teacher_id ");
	        selSQL.append("  ORDER BY fullcount DESC, t.teacher_id ASC ");
        }
        return dao.findPage(selSQL.toString(),queryParams.toArray(),pageable);
	}
	

	@Override
	public List<Map<String, Object>> findGTTeacherByMonthList(String parentTeacherId, Date month) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT t.teacher_id,COUNT(DISTINCT(t.id)) as count from kmt_arrange_allot t ");
        selSQL.append(" LEFT JOIN tb_lib_manager m ON t.teacher_id=m.user_id  ");
        selSQL.append(" where t.`status`=1 AND m.state=1 AND m.parent_id=? ");
        selSQL.append(" AND DATE_FORMAT(t.arrange_date, '%Y%m' ) = DATE_FORMAT(?, '%Y%m') ");
        selSQL.append(" GROUP BY t.teacher_id HAVING count>=30 ");
        queryParams.add(parentTeacherId);
        queryParams.add(month);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> findKmtArrangeAllotByArrangeTemplateIdList(String arrangeTemplateId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT t.teacher_id,u.wx_account,d.arrange_date,d.start_interval,d.end_interval,d.type,d.`subject` from kmt_arrange_allot t  ");
        selSQL.append(" LEFT JOIN kmt_arrange_detail d ON t.arrange_detail_id=d.id ");
        selSQL.append(" LEFT JOIN tb_user u ON t.teacher_id=u.id ");
        selSQL.append(" where t.`status`=1 AND d.arrange_template_id=? AND TO_DAYS(t.arrange_date)=TO_DAYS(NOW()) ");
        selSQL.append(" GROUP BY t.teacher_id ");
        queryParams.add(arrangeTemplateId);
        return dao.findMap(selSQL.toString(),queryParams.toArray());
	}
	

	
}
