package com.steering.pss.sale.price.server;

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

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.price.entity.SlmPriceFactorEntity;

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

/**
 * 价格因素管理
 * @author WenHB
 */
public class CorePriceFactor extends DBCommon {

    /**
     * 查询表名
     * @return 平台对象
     */
    public CoreReturnObject queryTableName() {
        return this.getDataBySqlID("CorePriceFactor.queryTableName");
    }

    /**
     * 查询某表的列名
     * @param tableName
     * @return 平台对象
     */
    public CoreReturnObject queryColumnName(String tableName) {
        Object[] obj = new Object[] { tableName };
        return this.getDataBySqlID("CorePriceFactor.queryColumnName", obj);
    }

    /**
     * 查询COM_BASE_INFO数据
     * @param sortCode
     * @return
     */
    public CoreReturnObject queryBaseInfo(String sortCode) {
        Object[] obj = new Object[] { sortCode };
        return this.getDataBySqlID("CorePriceFactor.queryBaseInfo", obj);
    }

    /**
     * 获取字段的数据类型
     * @param tabName
     * @param colName
     * @return
     */
    public CoreReturnObject queryColumnType(String tabName, String colName) {
        Object[] obj = new Object[] { tabName, colName };
        return this.getDataBySqlID("CorePriceFactor.queryColumnType", obj);
    }

    /**
     * 根据因素名称查询
     * @param facName
     * @return
     */
    public CoreReturnObject queryFacNameByName(String facName) {

        Object[] obj = new Object[] { facName };
        return this.getDataBySqlID("CorePriceFactor.queryFacNameByName", obj);
    }

    /**
     * 根据因素ID查询
     * @param facId
     * @return
     */
    public CoreReturnObject queryFacNameById(String facId) {

        Object[] obj = new Object[] { facId };
        return this.getDataBySqlID("CorePriceFactor.queryFacNameById", obj);

    }

    /**
     * 保存数据
     * @param parm
     * @return
     */
    public CoreReturnObject saveData(ArrayList<SlmPriceFactorEntity> parm,
            String name) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            PreparedStatement ps = null;
            for (SlmPriceFactorEntity spf : parm) {
                if (spf.getValidflag().equals("")) { // 新增
                    String sqlAdd = CoreXmlSqlParsersFactory
                            .getSql("CorePriceFactor.insert");
                    spf.setCreateName(name);
                    sqlAdd = EntityTool.fillSqlByEntity(sqlAdd, spf);
                    ps = con.prepareStatement(sqlAdd);
                    ps.executeUpdate();
                    ps.close();
                }
                else {
                    String sqlUpdate = CoreXmlSqlParsersFactory
                            .getSql("CorePriceFactor.update");
                    spf.setUpdateName(name);
                    sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, spf);
                    ps = con.prepareStatement(sqlUpdate);
                    ps.executeUpdate();
                    ps.close();
                }
            }
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 查询数据
     * @param facId
     * @param facName
     * @param valid
     * @return
     */
    public CoreReturnObject query(String facId, String facName, String valid) {
        String sql = CoreXmlSqlParsersFactory.getSql("CorePriceFactor.query");
        sql = sql.replaceAll("#FACTOR_ID#", facId);
        sql = sql.replaceAll("#FACTOR_NAME#", facName);
        sql = sql.replaceAll("#VALIDFLAG#", valid);
        return this.getData(sql);
    }

    /**
     * 查询数据是否能够恢复
     * @param facId
     * @param facName
     * @return
     */
    public CoreReturnObject canResume(String facId, String facName) {
        Object[] obj = new Object[] { facId, facName };
        return this.getDataBySqlID("CorePriceFactor.canResume", obj);
    }

    /**
     * 删除或者恢复数据
     * @param parm
     * @param name
     * @param flag true 删除 false 恢复
     * @return
     */
    public CoreReturnObject deleteOrResume(ArrayList<String> parm, String name,
            Boolean flag) {
        CoreReturnObject cro = new CoreReturnObject();
        ArrayList<String> sqlList = new ArrayList<String>();
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        for (String facId : parm) {
            if (flag) {
                sqlList.add(CoreXmlSqlParsersFactory
                        .getSql("CorePriceFactor.Delete"));
            }
            else {
                sqlList.add(CoreXmlSqlParsersFactory
                        .getSql("CorePriceFactor.Resume"));
            }
            params.add(new Object[] { name, facId });
        }
        cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqlList,
                params);
        return cro;
    }
}
