package com.steering.mes.mcp.coup;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import net.sf.json.JSONObject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
public class BaseHelper extends DBCommon {

   
 

    /**
     * 时间增加秒
     * @param beginTime
     * @param second
     * @return
     */
    public static Date dateAddSecond(Date beginTime, int second) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(beginTime);
        rightNow.add(Calendar.SECOND, second);
        Date dt = rightNow.getTime();
        return dt;
    }
    /**
     * 时间计算
     * @param beginTime 开始时间
     * @param useMinute 用时(分)
     * @return String 结束时间
     * @throws ParseException
     */
    public static String addTime(String beginTime, String useMinute)
            throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int useSecond = (int) (Double.parseDouble(useMinute) * 60);
        String endTime = df.format(BaseHelper.dateAddSecond(
                df.parse(beginTime), useSecond));
        return endTime;
    }

    /**
     * 时间比较(date1=<date2 返回TRUE)
     * @param date1
     * @param date2
     * @return boolean
     * @throws ParseException
     */
    public static boolean compareTime(String date1, String date2)
            throws ParseException {
        java.text.DateFormat df = new java.text.SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        return compareTime(df.parse(date1), df.parse(date2));
    }

    /**
     * 时间比较(date1=<date2 返回TRUE)
     * @param date1
     * @param date2
     * @return boolean
     * @throws ParseException
     */
    public static boolean compareTime(Date date1, Date date2)
            throws ParseException {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(date1);
        c2.setTime(date2);
        int result = c1.compareTo(c2);
        if (result == 0) {
            return true;
        }
        else if (result < 0) {
            return true;
        }
        else {
            return false;
        }
    }

    public static boolean equalTime(String date1, String date2)
            throws ParseException {
        java.text.DateFormat df = new java.text.SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(df.parse(date1));
        c2.setTime(df.parse(date2));
        int result = c1.compareTo(c2);
        if (result == 0) {
            return true;
        }
        else if (result < 0) {
            return false;
        }
        else {
            return false;
        }
    }

    /**
     * 获取某时间　当前月的第一天
     * @param dateTime yyyy-mm-dd
     * @return
     * @throws ParseException
     */
    public static String getMonthFristDay(String dateTime)
            throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(dateTime);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
        Date beginTime = cal.getTime();
        String beginTime1 = format.format(beginTime) + " 00:00:00";
        return beginTime1;
    }

    /**
     * 获取某时间　当前月的最后一天
     * @param dateTime yyyy-mm-dd
     * @return
     * @throws ParseException
     */
    public static String getMonthLastDay(String dateTime) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(dateTime);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);
        cal.roll(Calendar.DATE, -1);
        Date beginTime = cal.getTime();
        String beginTime1 = format.format(beginTime) + " 23:59:59";
        return beginTime1;
    }

    /**
     * 某时间　加月份
     * @param dateTime 　时间
     * @param num 　　月数
     * @return　String
     * @throws ParseException
     */
    public static String addMonth(String dateTime, int num)
            throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(dateTime);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.MONTH, num);
        Date date1 = rightNow.getTime();
        String newMonth = format.format(date1);
        return newMonth;
    }

    /**
     * 某时间　加月份
     * @param dateTime 　时间
     * @param num 　　月数
     * @return　String
     * @throws ParseException
     */
    public static String addMonth2(String dateTime, int num)
            throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date date = format.parse(dateTime);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.MONTH, num);
        Date date1 = rightNow.getTime();
        String newMonth = format.format(date1);
        return newMonth;
    }

    /**
     * json转化为实体类
     * @param c 实体类类型
     * @param jsonStr 字符串
     * @return T
     */
    public static <T> T jsonToEntity(Class<?> c, String jsonStr) {
        if (jsonStr.equals(""))
            return null;
        JSONObject obj = JSONObject.fromObject(jsonStr);
        @SuppressWarnings("unchecked")
        T t = (T) JSONObject.toBean(obj, c);
        return t;
    }

    /**
     * 查询实体类
     * @param con 连接字符串
     * @param params 参数数组(按顺序)
     * @param sql SQL ID
     * @param supSql 补充SQL
     * @param cls 实体类
     * @return
     * @throws SQLException
     */
    public static <T> T getEntityData(Connection con, String[] params,
            String sqlId, String supSql, Class<T> cls) throws SQLException {
        String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
        sql = sql + "  " + supSql;
        PreparedStatement ps = con.prepareStatement(sql);
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
        }
        ResultSet rs = ps.executeQuery();
        T t = EntityTool.getEntityByRs(rs, cls);
        rs.close();
        ps.close();
        return t;
    }

    /**
     * 查询实体类集合
     * @param con 连接字符串
     * @param params 参数数组(按顺序)
     * @param sql SQLID
     * @param supSql 补充SQL
     * @param cls 实体类
     * @return
     * @throws SQLException
     */
    public static <T> List<T> getEntityDatas(Connection con, String[] params,
            String sqlId, String supSql, Class<T> cls) throws SQLException {
        String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
        sql = sql + "  " + supSql;
        PreparedStatement ps = con.prepareStatement(sql);
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
        }
        ResultSet rs = ps.executeQuery();
        List<T> t = EntityTool.getEntitysByRs(rs, cls);
        rs.close();
        ps.close();
        return t;
    }

    /**
     * 新增实体类
     * @param con
     * @param sqlId SQL ID
     * @param obj 实体类
     * @throws SQLException
     */
    public static void addEntityData(Connection con, String sqlId, Object obj)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
        sql = EntityTool.fillSqlByEntity(sql, obj);
        PreparedStatement ps = con.prepareStatement(sql);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 删除数据
     * @param con
     * @param sqlId
     * @param supSql
     * @param params
     * @throws SQLException
     */
    public static void deleteOrUpdateData(Connection con, String sqlId,
            String supSql, Object[] params) throws SQLException {
        String sql = CoreXmlSqlParsersFactory.getSql(sqlId);
        sql += supSql;
        PreparedStatement ps = con.prepareStatement(sql);
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
        }
        ps.executeUpdate();
        ps.close();
    }
    /**
     * 显示编辑区支数
     */
    public CoreReturnObject doQueryPiece(String woidId,String[] plinCode)
    {
    	String sql = CoreXmlSqlParsersFactory
				.getSql("CoupLoadingService.doQueryPiece");
    	if(!woidId.equals(""))
		{
			sql += " AND T.WO_ID = '" + woidId + "'";
		}	
		sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plinCode);
	return this.getData(sql);
    }
}
