package com.steering.mes.mcp.heatTreatment;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.HttReportDefaultEntity;

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

/**
 * Created by 朱少波 on 2018/7/6.
 */
public class FrmReportDefault  extends DBCommon {
    /**
     * 查询
     * @param planNo
     * @return
     */
        public CoreReturnObject Query(String PlineCode,String ReportNo) {
        return this.getDataBySqlID("FrmReportDefault.Query",
                new Object[] { PlineCode, ReportNo});
    }

    public CoreReturnObject Query(String PlineCode,String ReportNo,String MachineNo) {
        return this.getDataBySqlID("FrmReportDefault.Query1",
                new Object[] { PlineCode, ReportNo,MachineNo});
    }


    public CoreReturnObject QueryAllTotal() {
        return this.getDataBySqlID("FrmReportDefault.QueryAll");
    }

    public CoreReturnObject Copy(String ReportNo, String PlineCode,String MachineNo, String TReportNo, String TPlineCode,String TMachineNo) {

        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();


        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            ArrayList<String> SqlID = new ArrayList<String>();
            ArrayList<Object[]> Para = new ArrayList<Object[]>();
            SqlID.add("FrmReportDefault.DeleteDefault1");
            Para.add(new Object[]{TPlineCode,TReportNo,TMachineNo });

            SqlID.add("FrmReportDefault.Copy");
            Para.add(new Object[]{TPlineCode,TReportNo,TMachineNo,PlineCode,ReportNo,MachineNo});

           setDataById(SqlID,Para);

            core.setV_errMsg("复制成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;



    }


    public CoreReturnObject UpdateDefault(ArrayList<String> list) {

        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();


        try {
            connection.setAutoCommit(false); // 取消自动提交事务

            for (int i = 0; i < list.size(); i++) {
                HttReportDefaultEntity HttReportDefaultEntity = EntityTool.getEntityByJSONStr(list.get(i),HttReportDefaultEntity.class);


                String sqlUpdateDefault = CoreXmlSqlParsersFactory
                        .getSql("FrmReportDefault.UpdateDefault");
                sqlUpdateDefault = com.steering.common.EntityTool.fillSqlByEntity(sqlUpdateDefault, HttReportDefaultEntity);
                PreparedStatement patUpdateDefault = connection.prepareStatement(sqlUpdateDefault);
                patUpdateDefault.addBatch();
                patUpdateDefault.executeBatch();
            }

            core.setV_errMsg("修改成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;



    }

    public CoreReturnObject AddDefault(ArrayList<String> list) {

        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();


        try {
            connection.setAutoCommit(false); // 取消自动提交事务

            for (int i = 0; i < list.size(); i++) {
                HttReportDefaultEntity HttReportDefaultEntity = EntityTool.getEntityByJSONStr(list.get(i),HttReportDefaultEntity.class);


                String sqlUpdateDefault = CoreXmlSqlParsersFactory
                        .getSql("FrmReportDefault.AddDefault");
                sqlUpdateDefault = com.steering.common.EntityTool.fillSqlByEntity(sqlUpdateDefault, HttReportDefaultEntity);
                PreparedStatement patUpdateDefault = connection.prepareStatement(sqlUpdateDefault);
                patUpdateDefault.addBatch();
                patUpdateDefault.executeBatch();
            }

            core.setV_errMsg("新增成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;


    }


    public CoreReturnObject DeleteDefault(ArrayList<String> list) {

        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();


        try {
            connection.setAutoCommit(false); // 取消自动提交事务

            for (int i = 0; i < list.size(); i++) {
                HttReportDefaultEntity HttReportDefaultEntity = EntityTool.getEntityByJSONStr(list.get(i),HttReportDefaultEntity.class);


                String sqlUpdateDefault = CoreXmlSqlParsersFactory
                        .getSql("FrmReportDefault.DeleteDefault");
                sqlUpdateDefault = com.steering.common.EntityTool.fillSqlByEntity(sqlUpdateDefault, HttReportDefaultEntity);
                PreparedStatement patUpdateDefault = connection.prepareStatement(sqlUpdateDefault);
                patUpdateDefault.addBatch();
                patUpdateDefault.executeBatch();
            }

            core.setV_errMsg("删除成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;



    }


    public CoreReturnObject getReportColumns() {
        return this.getDataBySqlID("FrmReportDefault.getReportColumns");
    }

}
