package com.steering.pss.sale.order;

import CoreFS.SA06.CoreReturnObject;
import java.sql.SQLException;
import java.util.ArrayList;
 
import CoreFS.SA08.CoreXmlSqlParsersFactory;

import com.steering.comm.db.DBCommon;

public class CoreOrderModel extends DBCommon {
	/**
	 * 下移功能
	 * @param modelIndex
	 * @param orderModelNo
	 * @param modelSeqNo
	 */
	public CoreReturnObject doNext(String modelIndex,String orderModelNo,String modelSeqNo )
	{
		ArrayList<String> sqlIDList = new ArrayList<String>();    
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        
        sqlIDList.add("CoreOrderModel.UpdateNextX");
        params.add(new Object[] { modelIndex,modelIndex,orderModelNo });
        
        sqlIDList.add("CoreOrderModel.UpdateNextY");
        params.add(new Object[] { modelIndex,orderModelNo,modelSeqNo });
        
		return this.setDataById(sqlIDList, params);
		
	}
	
	/**
	 * 上移功能
	 * @param modelIndex
	 * @param orderModelNo
	 * @param modelSeqNo
	 */
	public CoreReturnObject doLase(String modelIndex,String orderModelNo,String modelSeqNo )
	{
		ArrayList<String> sqlIDList = new ArrayList<String>();    
        ArrayList<Object[]> params = new ArrayList<Object[]>();
        
        sqlIDList.add("CoreOrderModel.UpdateLaseX");
        params.add(new Object[] { modelIndex,modelIndex,orderModelNo });
        
        sqlIDList.add("CoreOrderModel.UpdateLaseY");
        params.add(new Object[] { modelIndex,orderModelNo,modelSeqNo });
        
		return this.setDataById(sqlIDList, params);
		
	}
	
	/**
	 * 自动生成编号
	 * @param name
	 * @return
	 */
	public CoreReturnObject GetMaxCode(String syNo) {
		Object[] obj = new Object[] { syNo };
		return this.getDataBySqlID("CoreOrderModel.GetMaxCode", obj);
	}
	
	/**
	 * 获取最大编号
	 * @param name
	 * @return
	 */
	public CoreReturnObject GetMaxCode_Item(String orderModelNo) {
		Object[] obj = new Object[] { orderModelNo };
		return this.getDataBySqlID("CoreOrderModel.GetMaxCode_Item",obj);
	}
	
	/**
	 * 获取最大顺序号
	 * @param name
	 * @return
	 */
	public CoreReturnObject GetIndex(String orderModelNo) {
		Object[] obj = new Object[] { orderModelNo };
		return this.getDataBySqlID("CoreOrderModel.GetIndex",obj);
	}
	
	
	
	
	/**
	 * 判断是否重复
	 * @param name
	 * @return
	 */
	public CoreReturnObject isRepeatName(String name) {
		Object[] obj = new Object[] { name };
		return this.getDataBySqlID("CoreOrderModel.isRepeatName", obj);
	}
	
	/**
	 * 判断条目序号是否已存在
	 * @param name
	 * @return
	 */
	public CoreReturnObject isRepeatName_Item(String name,String no) {
		Object[] obj = new Object[] { name ,no};
		return this.getDataBySqlID("CoreOrderModel.isRepeatName_Item", obj);
	}
	
	
	/**
	 * 适用单位数据源绑定
	 * @param saleRegion
	 * @return
	 */
	public CoreReturnObject GetSy(String saleRegion) {
		Object[] obj = new Object[] { saleRegion };
		CoreReturnObject cro =  this.getDataBySqlID("CoreOrderModel.GetSy",obj);
		return cro;
	}
	
	
	/**
	 * 获取范本条目数据源
	 * @return
	 */
	public CoreReturnObject GetOrderModelItem(String orderModelNo) {
		Object[] obj = new Object[] { orderModelNo };
		CoreReturnObject cro =  this.getDataBySqlID("CoreOrderModel.GetOrderModelItem",obj);
		return cro;
	}
	
	
	/**
     * 专用线基础信息查询
     * @param includeDeleted 是否包含已删除数据
     * @param name 专用线描述
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
	public CoreReturnObject isRepeat(String lenDesc) {
		Object[] obj = new Object[] { lenDesc };
        return this.getDataBySqlID("CoreOrderLen.isRepeat",obj);
	}
	
	/**
     * 专用线基础信息查询
     * @param includeDeleted 是否包含已删除数据
     * @param name 专用线描述
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
	public CoreReturnObject doQuery(String orderModelDesc, String userUnit) {
		Object[] obj =new Object[]{userUnit,userUnit,userUnit,userUnit,orderModelDesc};
        return this.getDataBySqlID("CoreOrderModel.doQuery",obj);
	}
	
	/**
     * 查询
     * @param includeDeleted 是否包含已删除数据
     * @param name 专用线描述
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
	public CoreReturnObject doQueryNew(String orderModelDesc, String userUnit) {
		Object[] obj =new Object[]{orderModelDesc,userUnit};
        return this.getDataBySqlID("CoreOrderModel.doQueryNew",obj);
	}
	
	/**
     * 专用线基础信息修改
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
	public CoreReturnObject doUpdate(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("CoreOrderModel.doUpdate");
		try {
			cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	
	/**
     * 专用线基础信息修改
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
	public CoreReturnObject doUpdate_Item(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("CoreOrderModel.doUpdate_Item");
		try {
			cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	/**
     * 专用线基础信息新增
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
	public CoreReturnObject doAdd(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("CoreOrderModel.doAdd");
		try {
			cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	
	/**
     * 专用线基础信息新增
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
	public CoreReturnObject doAdd_Item(ArrayList<Object> parm) {
		CoreReturnObject cro = new CoreReturnObject();
		String strSql = CoreXmlSqlParsersFactory.getSql("CoreOrderModel.doAdd_Item");
		try {
			cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
		} catch (SQLException e) {
			cro = cro.SetErrInfo(e.getMessage());
		}
		return cro;
	}
	
	/**
     * 专用线基础信息删除或恢复
     * @param plineCodes 产线代码集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject deleteLineInfo(ArrayList<Object> plineCodes,
            String operator, Boolean isDelete) {  	
        ArrayList<String> sqlIDList = new ArrayList<String>();    
        ArrayList<Object[]> params = new ArrayList<Object[]>();       
        for (int i = 0; i < plineCodes.size(); i++) {
            if (isDelete) {
                sqlIDList.add("CoreOrderModel.DELETE");
                params.add(new Object[] { operator,
                        plineCodes.get(i).toString() });
            }
            else {
                sqlIDList.add("CoreOrderModel.RESUME");
                params.add(new Object[] { plineCodes.get(i).toString() });
            }
        }
        
        //删除从表记录
        deleteLineInfoAllItem(plineCodes,operator,isDelete);
        
		return this.setDataById(sqlIDList, params);
	}
    
    /**
     * 删除从表全部数据
     * @param plineCodes
     * @param operator
     * @param isDelete
     */
    public void deleteLineInfoAllItem(ArrayList<Object> plineCodes,
            String operator, Boolean isDelete){
    	ArrayList<String> sqlIDList = new ArrayList<String>();    
        ArrayList<Object[]> params = new ArrayList<Object[]>();       
        for (int i = 0; i < plineCodes.size(); i++) {
            if (isDelete) {
                sqlIDList.add("CoreOrderModel.DELETE_ITEM");
                params.add(new Object[] { operator,
                        plineCodes.get(i).toString() });
            }
            else {
                sqlIDList.add("CoreOrderModel.RESUME_ITEM");
                params.add(new Object[] { plineCodes.get(i).toString() });
            }
        }
        
        this.setDataById(sqlIDList, params);
    	
    }
	
    
    /**
     * 专用线基础信息删除或恢复
     * @param plineCodes 产线代码集合
     * @param operator 操作人
     * @param isDelete true删除 false恢复
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject deleteLineInfo_ITEM(ArrayList<Object> plineCodes,
            String operator, Boolean isDelete) {  	
        ArrayList<String> sqlIDList = new ArrayList<String>();    
        ArrayList<Object[]> params = new ArrayList<Object[]>();       
            if (isDelete) {
                sqlIDList.add("CoreOrderModel.DELETE_ITEMNo");
                params.add(new Object[] { operator,
                        plineCodes.get(0).toString(),plineCodes.get(1).toString() });
            }
            else {
                sqlIDList.add("CoreOrderModel.RESUME_ITEMNo");
                params.add(new Object[] { 
                		plineCodes.get(0).toString(),
                		plineCodes.get(1).toString() 
                		});
            }
		return this.setDataById(sqlIDList, params);
	}
}
