package com.steering.jobface.qcm;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.model.qcm.PipeCheckconsignDetailEntity;
import com.steering.model.qcm.QcmPhyRclInfoEntity;

import java.sql.*;
import java.util.ArrayList;

/**
 * Created by 朱少波 on 2018/8/3.
 */
public class SampleHeatTreatmentToQcm extends DBCommon {

    public   void   InsertData(){


        try {
            ArrayList<String> CheckNos = getCheckNo();

            for (String CheckNo : CheckNos
                    ) {
                try {
                    ArrayList<String> sqlList = new ArrayList<String>();
                    ArrayList<Object[]> paraList = new  ArrayList<Object[]>();

                    ArrayList<String> sqlList1 = new ArrayList<String>();
                    ArrayList<Object[]> paraList1 = new  ArrayList<Object[]>();



                    sqlList.add( CoreXmlSqlParsersFactory.getSql("SampleHeatTreatmentToQcm.DeleteQcmPhyRclInfo"));
                    paraList.add(new Object[]{CheckNo});



                    ArrayList<QcmPhyRclInfoEntity> list = getQcmPhyRclInfoEntity(CheckNo);
                    for (QcmPhyRclInfoEntity PhyRclInfo : list
                            ) {
                        String sqlAddInfo = CoreXmlSqlParsersFactory.getSql("SampleHeatTreatmentToQcm.InsertQcmPhyRclInfo");
                        sqlAddInfo = EntityTool.fillSqlByEntity(sqlAddInfo, PhyRclInfo);
                        sqlList.add(sqlAddInfo);
                        paraList.add(new Object[]{});
                    }

                    sqlList.add( CoreXmlSqlParsersFactory.getSql("SampleHeatTreatmentToQcm.DeleteQcmPhyRclDetail"));
                    paraList.add(new Object[]{CheckNo});

                    ArrayList<PipeCheckconsignDetailEntity> list1 = getPipeCheckConsignDetail(CheckNo);
                    for (PipeCheckconsignDetailEntity PhyRclDetail : list1
                            ) {
                        String sqlAddInfo = CoreXmlSqlParsersFactory.getSql("SampleHeatTreatmentToQcm.InsertQcmPhyRclDetail");
                        sqlAddInfo = EntityTool.fillSqlByEntity(sqlAddInfo, PhyRclDetail);
                        sqlList.add(sqlAddInfo);
                        paraList.add(new Object[]{});
                    }


                    String sqlAddInfo = CoreXmlSqlParsersFactory.getSql("SampleHeatTreatmentToQcm.getUpdateFlag");
                    sqlList1.add(sqlAddInfo);
                    paraList1.add(new Object[]{CheckNo});


                    setData("TgDao",sqlList,paraList);
                    setData("JhyDao",sqlList1,paraList1);

                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }


        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }



    private  ArrayList<String> getCheckNo() {
        Connection jhyCon = new DBCommon().getDao("JhyDao").getConnection();
        PreparedStatement jhyStmt = null;
        ResultSet rs = null;
        ArrayList<String> checkList = new ArrayList<String>();
        try {
            String sql = CoreXmlSqlParsersFactory.getSql("SampleHeatTreatmentToQcm.getCheckNo");
            jhyStmt = jhyCon
                    .prepareStatement(sql);
            rs = jhyStmt.executeQuery();
            while (rs.next()) {
                checkList.add(rs.getString("CHECK_NO"));
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                jhyCon.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            if (jhyStmt != null) {
                try {
                    jhyStmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (jhyCon != null) {
                try {
                    jhyCon.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return checkList;
    }

    private  ArrayList<PipeCheckconsignDetailEntity> getPipeCheckConsignDetail(String CheckNo) {
        Connection jhyCon = new DBCommon().getDao("JhyDao").getConnection();
        ArrayList<PipeCheckconsignDetailEntity> list = new ArrayList<PipeCheckconsignDetailEntity>();
        try {
            String sql = CoreXmlSqlParsersFactory.getSql("SampleHeatTreatmentToQcm.getPipeCheckConsignDetail");
            PreparedStatement ps = jhyCon.prepareStatement(sql);
            ps.setObject(1, CheckNo);
            ResultSet rs = ps.executeQuery();
            list= EntityTool.getEntitysByRs(rs,
                    PipeCheckconsignDetailEntity.class);
            rs.close();
            ps.close();

        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                jhyCon.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            if (jhyCon != null) {
                try {
                    jhyCon.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    private  ArrayList<QcmPhyRclInfoEntity> getQcmPhyRclInfoEntity(String CheckNo) {
        Connection jhyCon = new DBCommon().getDao("JhyDao").getConnection();
        ArrayList<QcmPhyRclInfoEntity> list = new ArrayList<QcmPhyRclInfoEntity>();
        try {

            String sql = CoreXmlSqlParsersFactory
                    .getSql("SampleHeatTreatmentToQcm.getPipePhyInfo");
            PreparedStatement ps = jhyCon.prepareStatement(sql);
            ps.setObject(1, CheckNo);
            ResultSet rs = ps.executeQuery();
            list= EntityTool.getEntitysByRs(rs,
                    QcmPhyRclInfoEntity.class);
            rs.close();
            ps.close();


        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                jhyCon.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            if (jhyCon != null) {
                try {
                    jhyCon.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }


}
