package com.steering.pss.sale.order;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.model.SlmOrderAuthenticationEntity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 * 合同认证
 * @author WenHB
 */
public class CoreOrderAuthentication extends DBCommon {

    /**
     * 数据查询
     * @param ordNo 合同号
     * @param dtStart 开始时间
     * @param dtEnd 结束时间
     * @return
     */
    public CoreReturnObject queryAuthedOrd(String ordNo, String strArea,String dtStart,
            String dtEnd,String timeType) {
        Object[] obj = new Object[] { ordNo,strArea };
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderAuthentication.QueryAuthedOrd");
        if (!dtStart.isEmpty() && !dtEnd.isEmpty()) {
        	if(timeType.equals("1")){
        		sql += " AND T.UPDATE_TIME BETWEEN TO_DATE('" + dtStart
                + "','YYYY-MM-DD HH24:MI:SS') AND TO_DATE('" + dtEnd
                + "','YYYY-MM-DD HH24:MI:SS')";
        	}else if(timeType.equals("2")){
        		sql += " AND T1.CREATE_TIME BETWEEN TO_DATE('" + dtStart
                + "','YYYY-MM-DD HH24:MI:SS') AND TO_DATE('" + dtEnd
                + "','YYYY-MM-DD HH24:MI:SS')";
        	}
            
        }

        return this.getData(sql, obj);
    }
    
    /**
     * 获取合同数据至合同认证表
     * @return
     */
    public CoreReturnObject getOrdData() {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderAuthentication.getOrdData");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.executeUpdate();
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errMsg(e.getMessage());
                cro.setV_errCode(-1);
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 设置返回时间
     * @param parm
     * @param name 操作人
     * @return
     */
    public CoreReturnObject returnTime(String[] parm, String name) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderAuthentication.ReturnTime");
            PreparedStatement ps = con.prepareStatement(sql);
            for (int i = 0; i < parm.length; i++) {
                ps.setObject(1, name);
                ps.setObject(2, parm[i]);
                ps.addBatch();
                if (i % 100 == 0)
                    ps.executeBatch();
            }
            ps.executeBatch();
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cro;
    }

    /**
     * 已保存在SLM_ORDER_AUTHENTICATION表中的数据的修改 只修改些许字段
     * @param parm
     * @return
     */
    public CoreReturnObject saveWrite(
            ArrayList<SlmOrderAuthenticationEntity> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            Statement stmt = con.createStatement();
            int i = 0;
            for (SlmOrderAuthenticationEntity soa : parm) {
                String sql1 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderAuthentication.UpdateAuthOrdWrite");
                sql1 = EntityTool.fillSqlByEntity(sql1, soa);
                stmt.addBatch(sql1);
                i++;
                if (i % 100 == 0)
                    stmt.executeBatch();
            }
            stmt.executeBatch();
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
                con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 后台更新数据
     * @param ordArr 合同号集合
     * @return
     */
    public CoreReturnObject updateData(String[] ordArr) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderAuthentication.UpdateData");
            String arr = CoreConverter.toSqlConditionString("B.ORDER_NO",ordArr);
            sql = sql.replace("#ORDARRAY#", arr);
            PreparedStatement ps = con.prepareStatement(sql);
            ps.executeUpdate();
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errMsg(e.getMessage());
                cro.setV_errCode(-1);
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 查询合同号
     * @return
     */
    public CoreReturnObject queryOrdNo() {
        return this.getDataBySqlID("CoreOrderAuthentication.QueryOrdNo");
    }

    /**
     * 更新合同文本
     * @param ordPk
     * @param ordFile
     * @return
     */
    public CoreReturnObject updateOrdFile(String ordPk, String ordFile) {
        Object[] obj = new Object[] { ordFile, ordPk, ordFile, ordPk };
        return this.setDataBySqlID("CoreOrderAuthentication.updateOrdFile",
                obj);
    }

    /**
     * 更新合同文本
     * @param ordPk
     * @param ordFile
     * @return
     */
    public CoreReturnObject updateUserOrdFile(String ordPk, String ordFile) {
        Object[] obj = new Object[] { ordFile, ordPk, ordFile, ordPk };
        return this.setDataBySqlID("CoreOrderAuthentication.updateUserOrdFile",
                obj);
    }
}
