package com.steering.pss.judge.DAL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

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.comm.tools.SqlBuilder;
import com.steering.pss.judge.model.QcmZbsInfoEntity;
import com.steering.pss.plnsaleord.steelMarkingPlan.FrmSteelMakingPlanChange;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.ydm.entity.YdmZcBillCEntity;

public class ZbsInfoInput extends DBCommon {

	/***
	 * 查询登陆人对应的销售组织
	 * 
	 * @param areaNo
	 * @return
	 */
	public CoreReturnObject getBillInfo(String BillNo,String judgeNo,String startTime,String endTime) {
		return this.getDataBySqlID("ZbsInfoInput.getBillInfo",
				new Object[] { BillNo,judgeNo,startTime,endTime });
	}
	
	/***
	 * 查询天淮产线
	 * 
	 * @param areaNo
	 * @return
	 */
	public CoreReturnObject getPlineInfo() {
		return this.getDataBySqlID("ZbsInfoInput.getPlineInfo");
	}
	
    /**
     * 新增质保书主信息
     * @param
     * @param parm
     * @param orderNo
     * @param saleOrg
     * @return
     */
    public CoreReturnObject addZbsInfo(ArrayList<String> list,String billNo) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            //获取质保书id
            String sql = CoreXmlSqlParsersFactory
                    .getSql("ZbsInfoInput.getMaxZbsId");
            PreparedStatement ps = con.prepareStatement(sql);
            String zbsId="";
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
            	zbsId=rs.getString(1);
            }
            rs.close();
            ps.close();
            list.set(0, zbsId);
            sql = CoreXmlSqlParsersFactory
                    .getSql("ZbsInfoInput.addZbsInfo");
            ps = con.prepareStatement(sql);
            for (int j = 0; j < list.size(); j++) {
                ps.setObject(j + 1, list.get(j));
            }
            String sql1 = CoreXmlSqlParsersFactory
                    .getSql("ZbsInfoInput.updateBillZbs");
            PreparedStatement ps1 = con.prepareStatement(sql1);
            ps1.setObject(1,billNo);
            ps1.setObject(2,zbsId);
            ps1.setObject(3,billNo);
            ps.executeUpdate();
            ps1.executeUpdate();
            ps.close();
            ps1.close();
            con.commit();
            cro.setV_errMsg("新增成功!");
        }
        catch (SQLException e) {
            try {
                if (con != null && !con.isClosed()) {
                    con.rollback();
                    cro.setV_errMsg(e.getMessage());
                    cro.setV_errCode(-1);
                    e.printStackTrace();
                }
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cro;
    }
    
	/***
	 * 查询登陆人对应的销售组织
	 * 
	 * @param areaNo
	 * @return
	 */
	public CoreReturnObject getZbsInfo(String judgeNo,String startTime,String endTime) {
		return this.getDataBySqlID("ZbsInfoInput.getZbsInfo",
				new Object[] { judgeNo,startTime,endTime });
	}
    
	 /**
     * 保存质保书主信息
     * @param
     * @param parm
     * @param orderNo
     * @param saleOrg
     * @return
     */
    public CoreReturnObject saveZbsInfo(ArrayList<String> list) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("ZbsInfoInput.saveZbsInfo");
            PreparedStatement ps = con.prepareStatement(sql);
            for (int j = 0; j < list.size(); j++) {
                ps.setObject(j + 1, list.get(j));
            }
            ps.executeUpdate();
            ps.close();
            con.commit();
            cro.setV_errMsg("保存成功!");
        }
        catch (SQLException e) {
            try {
                if (con != null && !con.isClosed()) {
                    con.rollback();
                    cro.setV_errMsg(e.getMessage());
                    cro.setV_errCode(-1);
                    e.printStackTrace();
                }
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cro;
    }
    
    /**
     * 删除质保书主信息
     * @param
     * @param parm
     * @param orderNo
     * @param saleOrg
     * @return
     */
    public CoreReturnObject doDeleteZbsInfo(String zbsId) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("ZbsInfoInput.doDeleteZbsInfo");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, zbsId);
            ps.executeUpdate();
            ps.close();
            //删除成分信息
            sql = CoreXmlSqlParsersFactory
                    .getSql("ZbsInfoInput.DeleteChemInfo");
            ps = con.prepareStatement(sql);
            ps.setObject(1, zbsId);
            ps.setObject(2, "");
            ps.setObject(3, "");
            ps.executeUpdate();
            ps.close();
            //删除理化信息
            sql = CoreXmlSqlParsersFactory
                    .getSql("ZbsInfoInput.DeletePhyInfo");
            ps = con.prepareStatement(sql);
            ps.setObject(1, zbsId);
            ps.setObject(2, "");
            ps.setObject(3, "");
            ps.executeUpdate();
            ps.close();
            //清空提单信息
            String sql1 = CoreXmlSqlParsersFactory
                    .getSql("ZbsInfoInput.clearBillZbs");
            PreparedStatement ps1 = con.prepareStatement(sql1);
            ps1.setObject(1,zbsId);
            ps1.executeUpdate();
            ps1.close();
            con.commit();
            cro.setV_errMsg("删除成功!");
        }
        catch (SQLException e) {
            try {
                if (con != null && !con.isClosed()) {
                    con.rollback();
                    cro.setV_errMsg(e.getMessage());
                    cro.setV_errCode(-1);
                    e.printStackTrace();
                }
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cro;
    }
    
	/***
	 * 查询成分信息
	 * 
	 * @param areaNo
	 * @return
	 */
	public CoreReturnObject getChemInfo(String zbsId) {
		return this.getDataBySqlID("ZbsInfoInput.getChemInfo",
				new Object[] { zbsId });
	}
    
    /**
     * 成分信息保存
     * @param parm 合同头参数
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject saveChemInfo(ArrayList<Object> parm,String zbsId) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            //删除成分信息
            String sql = CoreXmlSqlParsersFactory
                    .getSql("ZbsInfoInput.DeleteChemInfo");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, zbsId);
            ps.setObject(2, "");
            ps.setObject(3, "");
            ps.executeUpdate();
            ps.close();
        	for (int i = 0; i < parm.size(); i++) {
                sql = CoreXmlSqlParsersFactory
                        .getSql("ZbsInfoInput.saveChemInfo");
                ArrayList<Object> list = (ArrayList<Object>) parm.get(i);
                ps = con.prepareStatement(sql);
                for (int j = 0; j < list.size(); j++) {
                    ps.setObject(j + 1, list.get(j));
                }
                ps.executeUpdate();
                ps.close();
            }
        	
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
        }
        return cro;
    }
    
    /**
     * 删除质保书成分信息
     * @param
     * @param parm
     * @param orderNo
     * @param saleOrg
     * @return
     */
    public CoreReturnObject DeleteChemInfo(String zbsId,String zbsSeq) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("ZbsInfoInput.DeleteChemInfo");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, zbsId);
            ps.setObject(2, zbsSeq);
            ps.setObject(3, zbsSeq);
            ps.executeUpdate();
            ps.close();
            con.commit();
            cro.setV_errMsg("删除成功!");
        }
        catch (SQLException e) {
            try {
                if (con != null && !con.isClosed()) {
                    con.rollback();
                    cro.setV_errMsg(e.getMessage());
                    cro.setV_errCode(-1);
                    e.printStackTrace();
                }
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cro;
    }

	
	
	/***
	 * 查询理化信息
	 * 
	 * @param areaNo
	 * @return
	 */
	public CoreReturnObject getPhyInfo(String zbsId) {
		return this.getDataBySqlID("ZbsInfoInput.getPhyInfo",
				new Object[] { zbsId });
	}
	
    /**
     * 理化信息保存
     * @param parm 合同头参数
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject savePhyInfo(ArrayList<Object> parm,String zbsId) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
        	con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            //先删除原理化信息
            String sql = CoreXmlSqlParsersFactory
                    .getSql("ZbsInfoInput.DeletePhyInfo");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, zbsId);
            ps.setObject(2, "");
            ps.setObject(3, "");
            ps.executeUpdate();
            ps.close();
        	for (int i = 0; i < parm.size(); i++) {
                sql = CoreXmlSqlParsersFactory
                        .getSql("ZbsInfoInput.savePhyInfo");
                ArrayList<Object> list = (ArrayList<Object>) parm.get(i);
                ps = con.prepareStatement(sql);
                for (int j = 0; j < list.size(); j++) {
                    ps.setObject(j + 1, list.get(j));
                }
                ps.executeUpdate();
                ps.close();
            }
        	
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            cro.setV_errMsg(e.getMessage());
            cro.setV_errCode(-1);
        }
        return cro;
    }
    
    /**
     * 删除质保书理化信息
     * @param
     * @param parm
     * @param orderNo
     * @param saleOrg
     * @return
     */
    public CoreReturnObject DeletePhyInfo(String zbsId,String zbsSeq) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("ZbsInfoInput.DeletePhyInfo");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, zbsId);
            ps.setObject(2, zbsSeq);
            ps.setObject(3, zbsSeq);
            ps.executeUpdate();
            ps.close();
            con.commit();
            cro.setV_errMsg("删除成功!");
        }
        catch (SQLException e) {
            try {
                if (con != null && !con.isClosed()) {
                    con.rollback();
                    cro.setV_errMsg(e.getMessage());
                    cro.setV_errCode(-1);
                    e.printStackTrace();
                }
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cro;
    }
	
}
