package com.steering.pss.sale.order;

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

import net.sf.json.JSONObject;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.model.SlmOrdDesignMscPlGxPrlEntity;

public class CorePlineSelected extends DBCommon {
    /**
     * 查询产线
     * @param ordLnPk 合同号
     * @return
     */
    public CoreReturnObject getPlineByOrdLnPk(String ordLnPk) {
        Object[] params = new Object[] { ordLnPk };
        return this.getDataBySqlID("CorePlineSelected.getPlineByOrdLnPk",
                params);
    }

    /**
     * 操作产线
     * @param strs 实体类字符串集合
     * @param flag 标识
     * @return CoreReturnObject
     */
    public CoreReturnObject doOperate(ArrayList<String> strs, String flag) {
        CoreReturnObject core = new CoreReturnObject();
        /**
         * 获取数据库连接
         */
        Connection con = null;
        try {
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            String sql = "";
            PreparedStatement ps = null;
            ResultSet rs = null;
            for (String str : strs) {
                JSONObject obj = JSONObject.fromObject(str);
                SlmOrdDesignMscPlGxPrlEntity line = (SlmOrdDesignMscPlGxPrlEntity) JSONObject
                        .toBean(obj, SlmOrdDesignMscPlGxPrlEntity.class);
                line.setValidflag(flag);
                sql = CoreXmlSqlParsersFactory
                        .getSql("CorePlineSelected.getLineValig");
                sql = EntityTool.fillSqlByEntity(sql, line);
                ps = con.prepareStatement(sql);
                String lineValig = "";
                rs = ps.executeQuery();
                if (rs.next()) {
                    lineValig = rs.getString("VALIDFLAG");
                }
                if (flag.equals("1") && lineValig.equals("1")) {
                    core.setV_errMsg(line.getPlineName() + "已选,不能恢复操作");
                    return core;
                }
                if (flag.equals("0") && lineValig.equals("0")) {
                    core.setV_errMsg(line.getPlineName() + "未选,不能删除操作");
                    return core;
                }
                if (flag.equals("0")) {
                    sql = CoreXmlSqlParsersFactory
                            .getSql("CorePlineSelected.getValigLine");
                    sql = EntityTool.fillSqlByEntity(sql, line);
                    ps = con.prepareStatement(sql);
                    rs = ps.executeQuery();
                    int count = 0;
                    if (rs.next()) {
                        count = rs.getInt("COUNT");
                    }
                    if (count <= 1) {
                        core.setV_errMsg("每一道工序必需保留一条产线,不能删除");
                        return core;
                    }
                }
                sql = CoreXmlSqlParsersFactory
                        .getSql("CorePlineSelected.operateLine");
                sql = EntityTool.fillSqlByEntity(sql, line);
                ps = con.prepareStatement(sql);
                ps.executeUpdate();
            }
            if (flag.equals("1")) {
                core.setV_errMsg("恢复成功");
            }
            else {
                core.setV_errMsg("删除成功");
            }
            con.commit();
        }
        catch (SQLException e) {
            try {
                con.rollback();
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null) {
                    con.close();
                }

            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return core;
    }
}
