/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2017.11.27
 * 描述               :                   Slm提供其他系统调用的公共接口
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
 ***********************************************************/

package market.Api.OA.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import market.Api.OA.service.OaApiService;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Logistics.vo.SpmDelivery;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

@SuppressWarnings("rawtypes")
@RemoteService(path="OaApiService")
public class OaApiServiceImpl extends BaseServiceImpl implements OaApiService {
	
	
	public ResultModel doQuaryOaLeaveInfo(CommonPage cp) {
		ResultModel rm = new ResultModel();
		String startTime = cp.getStartTime();//查询开始时间
		String endTime = cp.getEndTime();//查询结束时间
		Connection connection = getConnection();
		List<HashMap<String,String>> infoList = new ArrayList<>();//返回的结果集
		//准备sql语句
		String sql = "select "
				+ " (IFNULL(TYPE_B,0)+IFNULL(TYPE_C,0)+IFNULL(TYPE_H,0)+IFNULL(TYPE_N,0)+IFNULL(TYPE_Q,0)+IFNULL(TYPE_S,0)) TYPE_ALL,"
				+ " tmp.* from ("
				+ " select t.department_name,t.person_name, "
				+ " sum(case when t.leave_type_name = '病假'and t.isall = '1' then t.leave_time / 8 end ) TYPE_B,"
				+ " sum(case when t.leave_type_name = '产假'and t.isall = '1' then t.leave_time / 8 end ) TYPE_C,"
				+ " sum(case when t.leave_type_name = '婚假'and t.isall = '1' then t.leave_time / 8 end ) TYPE_H,"
				+ " sum(case when t.leave_type_name = '年假'and t.isall = '1' then t.leave_time / 8 end ) TYPE_N,"
				+ " sum(case when t.leave_type_name = '其它'and t.isall = '1' then t.leave_time / 8 end ) TYPE_Q,"
				+ " sum(case when t.leave_type_name = '事假'and t.isall = '1' then t.leave_time / 8 end ) TYPE_S,"
				+ " sum(case when t.isall = '2' then t.leave_time end ) TYPE_NOT"
				+ " from oa_leave_info t"
				+ " where t.leave_start_time >= ? and t.leave_end_time <= ?"
				+ " group by t.department_name,t.person_name"
				+ " order by t.department_name,t.person_name) tmp";
		//创建语句传输对象
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, startTime);
			preparedStatement.setString(2, endTime);
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				HashMap<String, String> info = new HashMap<String, String>();
				info.put("DEPARTMENT_NAME", resultSet.getString("DEPARTMENT_NAME"));//申请部门
				info.put("PERSON_NAME", resultSet.getString("PERSON_NAME"));//申请人
				info.put("TYPE_B", resultSet.getString("TYPE_B"));//病假
				info.put("TYPE_C", resultSet.getString("TYPE_C"));//产假
				info.put("TYPE_H", resultSet.getString("TYPE_H"));//婚假
				info.put("TYPE_N", resultSet.getString("TYPE_N"));//年假
				info.put("TYPE_Q", resultSet.getString("TYPE_Q"));//其它
				info.put("TYPE_S", resultSet.getString("TYPE_S"));//事假
				info.put("TYPE_ALL", resultSet.getString("TYPE_ALL"));//合计
				info.put("TYPE_NOT", resultSet.getString("TYPE_NOT"));//非整天
				infoList.add(info);	            		   	    	 		    	 
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(resultSet, connection, preparedStatement);
		}
		rm.setData(infoList);
		rm.setState("200");
		rm.setMsgInfo("查询成功");
		return  rm;
 	
 	}
	
	
	/**
	  * 建立OA的连接
	  * @return
	  */
	 public Connection getConnection(){
		  Connection conn=null;		  
		  try {
			String url="jdbc:mysql://6.6.6.6:3306/oa_xinsteel_prod?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&setting useSSL=true";
			  String user="root";
			  String password="Hnst2017@";			  
			  Class.forName("com.mysql.jdbc.Driver");//加载数据驱动
			  conn = DriverManager.getConnection(url, user, password);// 连接数据库
			  
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.out.println("加载数据库驱动失败");
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("连接数据库失败");
		}
		  return conn;
	 }
	 
	 
	 /**
	  * 关闭连接
	  * @param rs
	  * @param conn
	  * @param stmt
	  */
	 public void close(ResultSet rs, Connection conn, Statement stmt) {
		try {
			if (rs != null)
					rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (stmt != null)
				stmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (conn != null)
				conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	 
}
