package com.steering.jobface.pln;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 排产公共方法类(用于数据库操作)
 * @author peng
 */
public class TimeAdjustingBean {
    
    /**
     * 时间计算
     * 
     * @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(dateAddSecond(
                df.parse(beginTime), useSecond));
        return endTime;
    }
    
    /**
     * 时间增加秒
     * 
     * @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 con
     * @param gxProSeq
     * @param plineCode
     * @param planTimeE
     * @param processCode
     * @throws SQLException
     * @throws ParseException
     */
    public void timeIncrease(Connection con, int gxProSeq, String plineCode,
            String planTimeE, String processCode) throws SQLException,
            ParseException {
        String sql = "";
        String updateSql = "";
        if (processCode.equals("B")) {
            sql = "select t.pro_plan_id,t.gx_plan_no,t.plan_usetime,t.pro_month from PLN_ORDER_CAST_S t ";
            updateSql = "UPDATE PLN_ORDER_CAST_S T SET T.PLAN_TIME_B = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), T.PLAN_TIME_E = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') WHERE T.PRO_PLAN_ID = ? AND T.GX_PLAN_NO = ?";
        }
        else if (processCode.equals("C")) {
            sql = "select t.pro_plan_id,t.gx_plan_no,t.plan_usetime,t.pro_month from PLN_ORDER_DZ_S t";
            updateSql = "UPDATE PLN_ORDER_DZ_S T SET T.PLAN_TIME_B = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), T.PLAN_TIME_E = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') WHERE T.PRO_PLAN_ID = ? AND T.GX_PLAN_NO = ?";
        }
        else if (processCode.equals("D")) {
            sql = "select t.pro_plan_id,t.gx_plan_no,t.plan_usetime,t.pro_month from PLN_ORDER_ZG_S t";
            updateSql = "UPDATE PLN_ORDER_ZG_S T SET T.PLAN_TIME_B = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), T.PLAN_TIME_E = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') WHERE T.PRO_PLAN_ID = ? AND T.GX_PLAN_NO = ?";
        }
        else if (processCode.equals("E")) {
            sql = "select t.pro_plan_id,t.gx_plan_no,t.plan_usetime,t.pro_month from PLN_ORDER_DBK_S t";
            updateSql = "UPDATE PLN_ORDER_DBK_S T SET T.PLAN_TIME_B = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), T.PLAN_TIME_E = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') WHERE T.PRO_PLAN_ID = ? AND T.GX_PLAN_NO = ?";
        }
        else if (processCode.equals("F")) {
            sql = "select t.pro_plan_id,t.gx_plan_no,t.plan_usetime,t.pro_month from PLN_ORDER_RCL_S t";
            updateSql = "UPDATE PLN_ORDER_RCL_S T SET T.PLAN_TIME_B = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), T.PLAN_TIME_E = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') WHERE T.PRO_PLAN_ID = ? AND T.GX_PLAN_NO = ?";
        }
        else if (processCode.equals("G")) {
            sql = "select t.pro_plan_id,t.gx_plan_no,t.plan_usetime,t.pro_month from pln_order_jgx_s t";
            updateSql = "UPDATE PLN_ORDER_JGX_S T SET T.PLAN_TIME_B = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), T.PLAN_TIME_E = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') WHERE T.PRO_PLAN_ID = ? AND T.GX_PLAN_NO = ?";
        }
        else if (processCode.equals("H")) {
            sql = "select t.pro_plan_id,t.gx_plan_no,t.plan_usetime,t.pro_month from PLN_GX_JG_PROP_S t ";
            updateSql = "UPDATE PLN_GX_JG_PROP_S T SET T.PLAN_TIME_B = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'), T.PLAN_TIME_E = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') WHERE T.PRO_PLAN_ID = ? AND T.GX_PLAN_NO = ?";
        }
        sql += " where t.pline_code=? and to_number(t.gx_pro_seq)>to_number(?) order by to_number(t.gx_pro_seq)";
        List<String[]> strArrList = new ArrayList<String[]>();
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, plineCode);
        ps.setObject(2, gxProSeq);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            String[] strArr = new String[4];
            strArr[0] = rs.getString("plan_usetime");
            strArr[1] = rs.getString("pro_plan_id");
            strArr[2] = rs.getString("gx_plan_no");
            strArr[3] = rs.getString("pro_month");
            strArrList.add(strArr);
        }
        rs.close();
        ps.close();
        if (strArrList == null || strArrList.size() == 0) {
            return;
        }
        // 修改时间
        ps = con.prepareStatement(updateSql);
        final int batchSize = 1000;
        int count = 0;
        DateFormat df = new SimpleDateFormat("yyyy-MM");
        String planTimeB = planTimeE;
        for (int i = 0; i < strArrList.size(); i++) {
            String[] strArr = strArrList.get(i);
            if (processCode.equals("D")) {
                if (df.parse(strArr[3]).getTime() > df.parse(planTimeB)
                        .getTime()) {
                    planTimeB = strArr[3] + "-01 00:00:00";
                }
            }
            String planTimeE1 = addTime(planTimeB, strArr[0]);
            ps.setObject(1, planTimeB);
            ps.setObject(2, planTimeE1);
            ps.setObject(3, strArr[1]);
            ps.setObject(4, strArr[2]);
            ps.addBatch();
            if (++count % batchSize == 0) {
                ps.executeBatch();
            }
            planTimeB = planTimeE1;
        }
        ps.executeBatch();
        ps.close();
    }

  
}
