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

import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.framework.core.util.MapGetterTool;
import com.ssj.framework.core.util.StringUtil;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yanghuan
 * @title: LibVipQueryDaoImpl
 * @projectName ssj
 * @description: TODO
 * @date 2019/9/5 10:57
 */
@Repository
public class LibVipQueryDaoImpl implements LibVipQueryDao{
    @Autowired
    private PagingHibernateJdbcDao dao;


    @Override
   public Page<Map<String,Object>> findStudentList(Map map, Pageable pageable){
        List<String> queryParams=new ArrayList<>();
        StringBuilder sql=new StringBuilder();
        MapGetterTool mapGetterTool=new MapGetterTool(map);
        String searchKey=mapGetterTool.getString("searchkey");
        String libId=mapGetterTool.getString("libId");
        sql.append("select t1.start_time,t1.end_time,t1.species, t2.id,t2.child_name,t2.img_url,t2.school,t2.grade from tb_lib_vip_service t1 left join tb_lib_vip t2");
        sql.append(" on t1.vip_id=t2.id where (t1.species='S999' or t1.species='S003') ");
        if(StringUtil.isNotBlank(libId)){
            sql.append("and t1.lib_id regexp ?");
            queryParams.add(libId);
        }
        if(StringUtil.isNotBlank(searchKey)){
            sql.append("and t2.child_name regexp ?");
            queryParams.add(searchKey);
        }
        sql.append(" order by t1.end_time desc");
        Page<Map<String,Object>> page=  dao.findPage(sql.toString(),queryParams.toArray(),pageable);
        return page;
    }

   @Override
    public Page<Map<String,Object>> findInnerStudentList(Map map, Pageable pageable){
       List<String> queryParams=new ArrayList<>();
       StringBuilder sql=new StringBuilder();
       MapGetterTool mapGetterTool=new MapGetterTool(map);
       String searchKey=mapGetterTool.getString("searchkey");
       String libId=mapGetterTool.getString("libId");
       sql.append("select t1.start_time,t1.end_time, t2.id,t2.child_name,t2.img_url,t2.school,t2.grade from tb_lib_vip_service t1 left join tb_lib_vip t2");
       sql.append(" on t1.vip_id=t2.id where t1.species='S003' ");
       if(StringUtil.isNotBlank(libId)){
           sql.append("and t1.lib_id regexp ?");
           queryParams.add(libId);
       }
       if(StringUtil.isNotBlank(searchKey)){
           sql.append("and t2.child_name regexp ?");
           queryParams.add(searchKey);
       }
       sql.append(" order by t1.end_time desc");
       Page<Map<String,Object>> page=  dao.findPage(sql.toString(),queryParams.toArray(),pageable);
       return page;
   }

  @Override
    public Map<String,Object> findStudentInfo(String vipId){
      List<String> queryParams=new ArrayList<>();
      StringBuilder sql=new StringBuilder();
      Map<String,Object> map=new HashMap<>(16);
      sql.append("SELECT\n" +
              "\tt1.start_time,\n" +
              "\tt1.end_time,\n" +
              "\tt2.child_name,\n" +
              "\tt2.img_url,\n" +
              "\tt2.school,\n" +
              "\tt2.grade,\n" +
              "\tt2.derver\n" +
              "FROM\n" +
              "\ttb_lib_vip_service t1\n" +
              "LEFT JOIN tb_lib_vip t2 ON t1.vip_id = t2.id\n" +
              "WHERE\n" +
              "\t1=1\n" +
              "AND t1.species IN ('S003', 'S999')\n" +
              "and t1.vip_id=?\n" +
              "ORDER BY t1.end_time desc LIMIT 1");
      queryParams.add(vipId);
      List<Map<String,Object>> list=dao.findMap(sql.toString(),queryParams.toArray());
      if(list.size()>0){
          map=list.get(0);
      }
        return map;
  }
  
  
  
  
  @Override
  public Page<Map<String,Object>> findTeacherList(Map map, Pageable pageable){
     List<String> queryParams=new ArrayList<>();
     StringBuilder sql=new StringBuilder();
     MapGetterTool mapGetterTool=new MapGetterTool(map);
     String libId=mapGetterTool.getString("libId");
     sql.append(" SELECT  t.user_id as teacherid,t.lib_id as libid,t.`name`,t.telephone,t.is_correct as iscorrect,t.t_photo as imgurl  from tb_lib_manager t where t.lib_id=? AND t.type=15 AND t.state=1 ORDER BY t.create_time desc");
     queryParams.add(libId);
     Page<Map<String,Object>> page=  dao.findPage(sql.toString(),queryParams.toArray(),pageable);
     return page;
 }



}
