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

import com.ssj.dao.weixin.sales.dao.LibVipServiceQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.framework.weixin.util.DateUtil;
import com.ssj.service.conch.teacher.library.dto.AllStudentDto;
import com.ssj.service.conch.teacher.library.dto.StudentDto;
import org.apache.commons.collections.CollectionUtils;
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.*;

@Repository
public class LibVipServiceQueryDaoImpl implements LibVipServiceQueryDao {

    @Autowired
    private PagingHibernateJdbcDao pageDao;

    @Override
    public Page<String> findEffectiveLibVipIdPage(HashMap<String, Object> params, Pageable pageable) {
        StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<>();

        //服务过期5天内仍然可以签到
        String currentDateTime = params.get("currentDateTime").toString();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.parse_yyyyMMddHHmmss(currentDateTime));
        calendar.add(Calendar.DAY_OF_MONTH, -5);
        Date minEndTime = calendar.getTime();
        String minEndTimeStr = DateUtil.format_yyyyMMddHHmmss(minEndTime);

        selSQL.append("select vip_id value from tb_lib_vip_service, tb_lib_vip ");
        selSQL.append("where vip_id = tb_lib_vip.id ");
        selSQL.append("and lib_id = ? ");queryParams.add(params.get("libId"));
        selSQL.append("and tb_lib_vip.user_id is not null ");
        selSQL.append("and species = ? ");queryParams.add("S003");
        selSQL.append("and tb_lib_vip_service.stat = 1 ");
        selSQL.append("and tb_lib_vip.child_name like concat('%', ?, '%') ");queryParams.add(params.get("studentName"));
        selSQL.append("and start_time <= ? ");
        queryParams.add(currentDateTime);
        selSQL.append("and end_time >= ? ");queryParams.add(minEndTimeStr);
        selSQL.append("and not exists (select tb_lib_subscribe.vip_id from tb_lib_subscribe where stat = '1' and flow_stat = '3' and date(ryke_time) = date(?) and tb_lib_vip.id = tb_lib_subscribe.vip_id) ");queryParams.add(currentDateTime);
        selSQL.append("order by tb_lib_vip_service.create_time asc ");

        return pageDao.findPage(selSQL.toString(), queryParams.toArray(), pageable, String.class);
    }
    
    
    
    /**
     *分页查询馆下的所有的服务用户列表
     * String libId
     * String species
     **/
	@Override
	public Page<Map<String, Object>> findLibAllUserServiceList(Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append(" SELECT t.*,s.lib_id,s.start_time,s.end_time from tb_lib_vip t   ");
		selSQL.append(" LEFT JOIN tb_lib_vip_service s ON t.id=s.vip_id where t.stat=1  and t.user_id is not NULL ");
		if(searchParams.containsKey("libId")){
			selSQL.append(" AND s.lib_id=? ");
			queryParams.add(searchParams.get("libId").toString());
		}
		if(searchParams.containsKey("species")){
			selSQL.append(" AND s.species=?  ");
			queryParams.add(searchParams.get("species").toString());
		}
		selSQL.append(" ORDER BY s.end_time ASC ");
		return pageDao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

    @Override
    public List<StudentDto> findStudentList4SignIn(String libId, String studentName, boolean isOpen) {
        //服务过期5天内仍然可以签到
        Date currentDateTime = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDateTime);
//        calendar.add(Calendar.DAY_OF_MONTH, -5);
        Date minEndTime = calendar.getTime();
        String minEndTimeStr = DateUtil.format_yyyyMMddHHmmss(minEndTime);

        String sql =
                "select tb_lib_vip.id         vip_id,\n" +
                "       tb_lib_vip.child_name student_name,\n" +
                "       tb_lib_vip.school,\n" +
                "       tb_lib_vip.grade,\n" +
                "       tb_lib_vip.img_url\n" +
                "from tb_lib_vip,\n" +
                "     tb_lib_vip_service\n" +
                "where tb_lib_vip.id = tb_lib_vip_service.vip_id\n" +
                "  and tb_lib_vip.lib_id = ?\n" +
                "  and tb_lib_vip.id not in\n" +
                "      (select vip_id\n" +
                "       from tb_lib_subscribe\n" +
                "       where lib_id = ?\n" +
                "         and flow_stat = '3'\n" +
                "         and tb_lib_subscribe.stat = '1'\n" +
                "         and to_days(ryke_time) = to_days(now()))\n" +
                "  and tb_lib_vip_service.stat = 1\n" +
                "  and tb_lib_vip_service.species in ('S003','S999')\n" +
                "  and unix_timestamp(tb_lib_vip_service.start_time) < unix_timestamp(?)\n" +
                "  and ? < tb_lib_vip_service.end_time\n";
        sql += "and tb_lib_vip_service.lib_id = ?\n";
        if (StringUtils.isNotBlank(studentName)) {
            sql += "  and tb_lib_vip.child_name like '%" + studentName + "%'\n";
        }
        sql += "group by tb_lib_vip.id \n";

        List<Object> paramList = new ArrayList<>();
        paramList.add(libId);
        paramList.add(libId);
        paramList.add(DateUtil.format_yyyyMMddHHmmss(currentDateTime));
        paramList.add(minEndTimeStr);
        paramList.add(libId);

        return pageDao.findList(sql, paramList.toArray(), StudentDto.class);
    }

    @Override
    public List<StudentDto> findStudentList4SignOff(String libId, String studentName) {
	    String sql =
                "select tb_lib_vip.id         vip_id,\n" +
                "       tb_lib_vip.child_name student_name,\n" +
                "       tb_lib_vip.school,\n" +
                "       tb_lib_vip.grade,\n" +
                "       tb_lib_vip.img_url\n" +
                "from tb_lib_vip, tb_lib_subscribe\n" +
                "where tb_lib_vip.id = tb_lib_subscribe.vip_id\n" +
                "  and tb_lib_vip.lib_id = ?\n" +
                "  and tb_lib_subscribe.lib_id = ?\n" +
                "  and tb_lib_subscribe.flow_stat = '3'\n" +
                "  and tb_lib_subscribe.stat = '1'\n" +
                "  and to_days(tb_lib_subscribe.ryke_time) = to_days(now())\n" +
                "  and tb_lib_vip.user_id is not null\n";
        if (StringUtils.isNotBlank(studentName)) {
            sql += "  and tb_lib_vip.child_name like '%" + studentName + "%'";
        }
        return pageDao.findList(sql, new Object[]{libId,libId}, StudentDto.class);
    }

    @Override
    public StudentDto findByVipId(String vipId) {
	    String sql =
                "select id vip_id, child_name student_name, school, grade, img_url\n" +
                "from tb_lib_vip\n" +
                "where id = ?\n" +
                "  and stat = 1 " ;
        List<StudentDto> studentDtoList = pageDao.findList(sql, new Object[]{vipId}, StudentDto.class);
        if (CollectionUtils.isNotEmpty(studentDtoList)) {
            return studentDtoList.get(0);
        }
        return null;
    }

    @Override
    public List<AllStudentDto> findByVipIdInAndChildNameLike(List<String> vipIdList, String studentName) {
	    String sql =
                "select id vip_id, child_name student_name, school, grade, img_url, 0 as checked_in, 1 as exited\n" +
                "from tb_lib_vip\n" +
                "where id in ('" + StringUtils.join(vipIdList, "','") + "')\n" +
                "  and stat = 1\n";

        if (StringUtils.isNotBlank(studentName)) {
            sql += "  and child_name like '%" + studentName + "%'\n";
        }

        return pageDao.findList(sql, AllStudentDto.class);
    }


}
