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

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

/**
 * @author ioriwuhj
 *
 */

public class CorePriceFactorRange extends DBCommon {

	 /**
     * 根据范围名称查询
     * @param facRName
     * @return
     */
    public CoreReturnObject queryFacRNameByName(String facRName) {

        Object[] obj = new Object[] { facRName };
        return this.getDataBySqlID("CorePriceFactorRange.queryFacRNameByName", obj);
    }

    /**
     * 根据范围ID查询
     * @param RangeId
     * @return
     */
    public CoreReturnObject queryFacRNameById(String RangeId) {

        Object[] obj = new Object[] { RangeId };
        return this.getDataBySqlID("CorePriceFactorRange.queryFacRNameById", obj);

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

        Object[] obj = new Object[] {  };
        return this.getDataBySqlID("CorePriceFactorRange.queryFacName", obj);

    }
    
  
    /**
     * @param RangeId
     * @param RangeName
     * @param valid
     * @return
     */
    public CoreReturnObject query(String RangeId, String RangeName, String valid) {
        String sql = CoreXmlSqlParsersFactory.getSql("CorePriceFactorRange.query");
        sql = sql.replaceAll("#CLASS_D_ID#", RangeId);
        sql = sql.replaceAll("#CLASS_D_NAME#", RangeName);
        sql = sql.replaceAll("#VALIDFLAG#", valid);
        return this.getData(sql);
    }
    
    
    /**
     * 查询COM_BASE_INFO数据
     * @param sortCode
     * @return
     */
    public CoreReturnObject queryBaseInfo(String sortCode) {
        Object[] obj = new Object[] { sortCode };
        return this.getDataBySqlID("CorePriceFactorRange.queryBaseInfo", obj);
    }
    
    
    /**
     * 保存数据
     * @param parm
     * @return
     */
    public CoreReturnObject saveData(ArrayList<String> parm,
            String name) {
   
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            PreparedStatement ps = null;
            for (String str : parm) {
            	SlmPriceFactorClassifyEntity spf = EntityTool.getEntityByJSONStr(
            			str, SlmPriceFactorClassifyEntity.class);
                if (spf.getValidflag().equals("")) { // 新增
                    String sqlAdd = CoreXmlSqlParsersFactory
                            .getSql("CorePriceFactorRange.insert");
                    spf.setCreateName(name);
                    sqlAdd = EntityTool.fillSqlByEntity(sqlAdd, spf);
                    ps = con.prepareStatement(sqlAdd);
                    ps.executeUpdate();
                    ps.close();
                }
                else {
                    String sqlUpdate = CoreXmlSqlParsersFactory
                            .getSql("CorePriceFactorRange.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 CLASS_D_ID
     * @param CLASS_D_NAME
     * @return
     */
    public CoreReturnObject canResume(String CLASS_D_ID, String CLASS_D_NAME) {
        Object[] obj = new Object[] { CLASS_D_ID, CLASS_D_NAME };
        return this.getDataBySqlID("CorePriceFactorRange.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 CLASS_D_ID : parm) {
            if (flag) {
                sqlList.add(CoreXmlSqlParsersFactory
                        .getSql("CorePriceFactorRange.Delete"));
            }
            else {
                sqlList.add(CoreXmlSqlParsersFactory
                        .getSql("CorePriceFactorRange.Resume"));
            }
            params.add(new Object[] { name, CLASS_D_ID });
        }
        cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqlList,
                params);
        return cro;
    }
	
	
}
