package com.steering.mes.mcp.Mch;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.db.DBCommon;

/**
 * 加工分组与工序点对应关系
 * @author lzh 2015-7-13
 */
public class FrmProcGroupPoint extends DBCommon {

    /**
     * 查询加工分组
     * @param sortCode
     * @return
     */
    public CoreReturnObject getComBaseInfo(String sortCode) {
        return this.getDataBySqlID("FrmProcGroupPoint.getComBaseInfo",
                new Object[] { sortCode });
    }

    /**
     * 查询加工分组与工序点对应关系
     * @param sortCode
     * @return
     */
    public CoreReturnObject getMchProcPoint(String proce) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmProcGroupPoint.getMchProcPoint");
        if (!proce.equals("")) {
            sql += " AND PROCE_GROUP_NO = '" + proce + "'";
        }
        sql+=" ORDER BY PROCE_NO ASC";
        return this.getData(sql);
    }

    /**
     *新增加工分组与工序点对应关系
     * @param result
     * @return
     */
    public CoreReturnObject getAdd(ArrayList<String> list) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String addSourc = CoreXmlSqlParsersFactory
                    .getSql("FrmProcGroupPoint.getAdd");// 新增
            PreparedStatement pstaAddSourc = con.prepareStatement(addSourc);

            String proceNo = list.get(0);
            String proceName = list.get(1);
            String pointNo = list.get(2);
            String pointNmae = list.get(3);
            String name = list.get(4);

            int count = 0;
            String  sql = CoreXmlSqlParsersFactory
                    .getSql("FrmProcGroupPoint.getQueryAdd");// 查询是否主键冲突
            PreparedStatement psta = con.prepareStatement(sql);
            psta.setObject(1, proceNo + pointNo);
            ResultSet rs  = psta.executeQuery();
            if (rs.next()) {
                count = Integer.parseInt(rs.getString("COUNT"));
            }
            rs.close();
            psta.close();
            if (count != 0) {
                cor.setV_errMsg("您选择的加工分组与工序点对应关系已存在,请重新选择！!");
                return cor;
            }

            // 修改审批状态
            pstaAddSourc.setObject(1, proceNo + pointNo);
            pstaAddSourc.setObject(2, proceNo);
            pstaAddSourc.setObject(3, proceName);
            pstaAddSourc.setObject(4, pointNo);
            pstaAddSourc.setObject(5, pointNmae);
            pstaAddSourc.setObject(6, name);
            pstaAddSourc.addBatch();
            pstaAddSourc.executeBatch();

            cor.setV_errMsg("新增成功！");
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    cor.setV_errMsg(e.getMessage());
                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cor;
    }

    /**
     *修改加工分组与工序点对应关系
     * @param result
     * @return
     */
    public CoreReturnObject getUpdate(ArrayList<String> list) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        String sql = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String addSourc = CoreXmlSqlParsersFactory
                    .getSql("FrmProcGroupPoint.getUpdate");// 修改
            PreparedStatement pstaAddSourc = con.prepareStatement(addSourc);

            String proceNo = list.get(0);
            String proceName = list.get(1);
            String pointNo = list.get(2);
            String pointNmae = list.get(3);
            String name = list.get(4);
            String procePoiNo = list.get(5);
            int count = 0;
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmProcGroupPoint.getQueryAdd");// 查询是否主键冲突
            psta = con.prepareStatement(sql);
            psta.setString(1, proceNo + pointNo);
            rs = psta.executeQuery();
            if (rs.next()) {
                count = Integer.parseInt(rs.getString("COUNT"));
            }
            rs.close();
            psta.close();
            if (count != 0) {
                cor.setV_errMsg("您选择的加工分组与工序点对应关系已存在,请重新选择！!");
                return cor;
            }

            // 修改
            pstaAddSourc.setObject(1, proceNo + pointNo);
            pstaAddSourc.setObject(2, proceNo);
            pstaAddSourc.setObject(3, proceName);
            pstaAddSourc.setObject(4, pointNo);
            pstaAddSourc.setObject(5, pointNmae);
            pstaAddSourc.setObject(6, name);
            pstaAddSourc.setObject(7, procePoiNo);
            pstaAddSourc.addBatch();
            pstaAddSourc.executeBatch();

            cor.setV_errMsg("修改成功！");
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    cor.setV_errMsg(e.getMessage());
                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cor;
    }

    /**
     *删除加工分组与工序点对应关系
     * @param result
     * @return
     */
    public CoreReturnObject getDelete(ArrayList<String> list) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            String addSourc = CoreXmlSqlParsersFactory
                    .getSql("FrmProcGroupPoint.getDelete");// 删除
            PreparedStatement pstaAddSourc = con.prepareStatement(addSourc);
            String procePoiNo = list.get(0);

            pstaAddSourc.setObject(1, procePoiNo);
            pstaAddSourc.addBatch();
            pstaAddSourc.executeBatch();

            cor.setV_errMsg("删除成功！");
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    cor.setV_errMsg(e.getMessage());
                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cor;
    }
}
