package com.steering.pss.sale.order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

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.SlmOrdDesignDeptEntity;

public class CoreDesignDept extends DBCommon {
    /**
     * 保存管理科室
     * @param jsonStr
     * @return
     */
    public CoreReturnObject saveDept(String jsonStr) {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            JSONObject obj = JSONObject.fromObject(jsonStr);
            SlmOrdDesignDeptEntity deptEntity = (SlmOrdDesignDeptEntity) JSONObject
                    .toBean(obj, SlmOrdDesignDeptEntity.class);
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreDesignDept.deleteDept");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, deptEntity.getOrdLnPk());
            ps.executeUpdate();
            sql = CoreXmlSqlParsersFactory.getSql("CoreDesignDept.saveDept");
            sql = EntityTool.fillSqlByEntity(sql, deptEntity);
            ps = con.prepareStatement(sql);
            ps.executeUpdate();
            ps.close();
            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;
    }

    /**
     * 查询MSC管理科室
     * @param ordLnPk 合同行号
     * @return CoreReturnObject
     */
    public CoreReturnObject getMscDept(String ordLnPk) {
        Object[] params = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreDesignDept.getMscDept", params);
    }
}
