package com.yonyou.iuap.example.repository;

import java.util.HashMap;
import java.util.Iterator;
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.PageRequest;
import org.springframework.stereotype.Repository;

import com.yonyou.iuap.example.entity.CourseJdbcDemo;
import com.yonyou.iuap.persistence.bs.dao.BaseDAO;
import com.yonyou.iuap.persistence.bs.dao.DAOException;
import com.yonyou.iuap.persistence.jdbc.framework.SQLParameter;
import com.yonyou.iuap.persistence.vo.pub.VOStatus;
//import com.yonyou.iuap.tenant.entity.CourseSource;
//import com.yonyou.iuap.tenant.sdk.CourseCenter;

@Repository
public  class CourseJdbcDemoDao {
	
	
	//---------------------------租户中心did	
	String tenantId = "fyk2bpxl";	
	//---------------------------租户中心did

	@Autowired
	private BaseDAO dao;

	public CourseJdbcDemo queryByPK(String pk) throws DAOException {
		return dao.queryByPK(CourseJdbcDemoDao.class, pk);
	}

	public Page<CourseJdbcDemo> queryPage(Map<String, Object> searchParams, PageRequest pageRequest) throws DAOException {
		StringBuffer sqlBuffer = new StringBuffer("select * from boot_course where 1=1 ");
		SQLParameter sqlParameter = new SQLParameter();
		buildSql(searchParams, sqlBuffer, sqlParameter);
		String sql = sqlBuffer.toString();
		return dao.queryPage(sql, sqlParameter, pageRequest, CourseJdbcDemo.class);
	}

	public void save(CourseJdbcDemo vo) throws DAOException {
		
		
//		//同步用户中心用户的接口的sdk代码--------------------------------------
//		//此代码的来源对应开发人员：卞腾，bianteng@yonyou.com;对应测试人员：徐诗云 xushy1@yonyou.com
//		Map<String, String> params = new HashMap<String, String>();
//		params.put("courseName", vo.getCourse_name());
//		params.put("courseCode", vo.getCourse_code());
//		params.put("courseMobile", vo.getCourse_tele());
//		params.put("courseEmail",vo.getCourse_email());
//		params.put("tenantId", tenantId);
//		
//		String result = null;
//		if(vo.getStatus().equals(VOStatus.UPDATED)){
//			 vo = dao.queryByPK(CourseJdbcDemo.class, vo.getCourse_id());
//			params.put("courseId", vo.getCourseid());
//			result = CourseCenter.updateCourse(params);		
//		}else{
//			result = CourseCenter.addCourse(params, CourseSource.LOCAL);			
//		}
//		System.out.println(result);
//		int indexID = result.indexOf("courseId");
//		int indexStatus = result.indexOf("status");
//		String status = result.substring(indexStatus + 10, indexStatus + 11);
//		if(status.equals("0")){
//			vo.setCourseid("0");
//		}else{
//			String courseID = result.substring(indexID + 11, indexID + 47);
//			System.out.println("courseid:" + courseID);				
//			vo.setCourseid(courseID);
//		}	
//		//========================================================同步用户中心用户的接口的sdk代码结束
		
		dao.save(vo);
	}

	public void remove(CourseJdbcDemo vo) throws DAOException {
		
//		//========================================================删除用户中心用户的接口的sdk代码开始
//        vo = dao.queryByPK(CourseJdbcDemo.class, vo.getCourse_id());
//		System.out.println("del course by id :" + vo.getCourseid());
//		String result = CourseCenter.delCourse(vo.getCourseid(), "ipu");
//		System.out.println(result);
//		//========================================================删除用户中心用户的接口的sdk代码结束
		
		dao.remove(vo);
	}

	/*
	 * 
	 * 	public void remove(List<StudentJdbcDemo> vos) throws DAOException {
		for (Iterator<StudentJdbcDemo> iterator = vos.iterator(); iterator.hasNext();) {
			StudentJdbcDemo studentJdbcDemo = (StudentJdbcDemo) iterator.next();
			remove(studentJdbcDemo);
		}
	//	dao.remove(vos);
	 * 
	 * 
	 * 
	 * 
	 */
	
	
	
	
	public void remove(List<CourseJdbcDemo> vos) throws DAOException {
		
		for (Iterator<CourseJdbcDemo> iterator = vos.iterator(); iterator.hasNext();) {
			CourseJdbcDemo courseJdbcDemo = (CourseJdbcDemo) iterator.next();
			remove(courseJdbcDemo);
		}
		
		
		//dao.remove(vos);
	}

	//业务开发根据自己的需求，修改查询条件的拼接方式
	private void buildSql(Map<String, Object> searchParams, StringBuffer sqlBuffer, SQLParameter sqlParameter) {
		
		int index = 0;
		StringBuffer sb = new StringBuffer();
		for (Map.Entry<String, Object> entry : searchParams.entrySet()) {
			String[] keySplit = entry.getKey().split("__");    //xzn
			if (keySplit.length == 2) {
				String columnName = keySplit[1];
				String compartor = keySplit[0];
				Object value = entry.getValue();
				if (value != null && StringUtils.isNotBlank(value.toString())) {
					
					sb.append(columnName).append(" ").append(compartor).append(" ? ");
					// 处理模糊查询
					value = "like".equalsIgnoreCase(compartor) ? "%" + value + "%" : value;
					sqlParameter.addParam(value);
					index ++;
					
					if(index != searchParams.keySet().size()){
						sb.append(" or ");
					}
				}
			}
		}
		
		String conditionSql = sb.toString();
		if(StringUtils.isNoneBlank(conditionSql)){
			sqlBuffer.append(" and (" + conditionSql.toString() + ");");
		}
		
	}
}
