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

import com.ssj.dao.weixin.sales.dao.LibCourseDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

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


/** 
 * @author  wuwen 
 * @version 2017年4月24日 上午11:59:29 
 * 类说明 
 */
@Repository
public class LibCourseDaoImpl implements LibCourseDao {

	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public List<Map<String, Object>> getByLibIdLibCourse(String libId,String type) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append(" SELECT * from tb_lib_course t where t.c_state=1 and t.lib_id =? ");
	    queryParams.add(libId);
	    if (StringUtils.isNotEmpty(type)) {
	       selSQL.append(" AND t.c_type= ?");
	       queryParams.add(type);
	    }
	    selSQL.append(" ORDER BY t.c_course_time asc  ");
		return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getByLibIdLibCourseDay(String libId,String type, String day) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append(" SELECT * from tb_lib_course t where t.c_state=1 and t.lib_id =?  AND  TO_DAYS(?)=TO_DAYS(t.c_course_time) ");
	    queryParams.add(libId);
	    queryParams.add(day);
	    if (StringUtils.isNotEmpty(type)) {
	       selSQL.append(" AND t.c_type= ?");
	       queryParams.add(type);
	    }
	    selSQL.append(" ORDER BY t.c_course_time asc  ");
		return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getVipService(String[] vipIds, String[] species, int state) {
		if (vipIds == null || vipIds.length == 0 || species == null || species.length == 0) return null;
		StringBuilder sb = new StringBuilder();
		StringBuilder sb1 = new StringBuilder();
		StringBuilder sb2 = new StringBuilder();

		for (int i = 0; i < vipIds.length; i++){
			sb1.append("'").append(vipIds[i]).append("',");
		}
		sb1.deleteCharAt(sb1.length() - 1);

		for (int i = 0; i < species.length; i++){
			sb2.append("'").append(species[i]).append("',");
		}
		sb2.deleteCharAt(sb2.length() - 1);

		sb.append("select vip_id, species from tb_lib_vip_service where vip_id in (").append(sb1.toString())
				.append(") and species in (").append(sb2.toString())
				.append(") and stat = ").append(state);
		return dao.findMap(sb.toString(), null);
	}
}
