package com.steering.pss.qcm;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.SqlBuilder;

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

/**
 * 生产产线基础信息管理类
 * @author siy 2014/10/13
 * @version v1.0
 * @modify
 */
public class ProductionLineInfo extends DBCommon {
	
	/**
	 * 判断描述信息是否重复
	 * @param name
	 * @return
	 */
	public CoreReturnObject GetProcess() {
		return this.getDataBySqlID("ProductionLineInfo.GetProcess");
	}
	
	/**
	 * 判断描述信息是否重复
	 * @param name
	 * @return
	 */
	public CoreReturnObject GetName(String name) {
		Object[] obj = new Object[] { name };
		return this.getDataBySqlID("ProductionLineInfo.GetName", obj);
	}
	
	
	/**
	 * 获取产线代码最大值
	 * @param name
	 * @return
	 */
	public CoreReturnObject GetMaxCode() {
		return this.getDataBySqlID("ProductionLineInfo.GetMaxCode");
	}
	
	
	/**
     * 生产产线基础信息查询
     * @param includeDeleted 是否包含已删除数据
     * @param productionLineName 产线名称
     * @param departmentDesc 管理部门描述
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject getLineList(Boolean includeDeleted,
            String productionLineName) {
        String deletedFlag = includeDeleted ? "0" : "";
        Object[] params = new Object[] { deletedFlag, productionLineName};
        return this.getDataBySqlID("ProductionLineInfo.QUERY", params);
    }
    
    public CoreReturnObject QueryAll(String processCode){
    	SqlBuilder b= new SqlBuilder("ProductionLineInfo.QueryAll");
    	b.Build("PROCESS_CODE", processCode, SqlBuilder.Empty);
    	return this.getData(b.toString());
    }

    /**
     * 查询核算中心基础信息
     * @return
     */
    public CoreReturnObject getAccountingBaseInfo(){
        String strSql = CoreXmlSqlParsersFactory.getSql("ProductionLineInfo.getAccountingBaseInfo");
        return this.getData(strSql);
    }

    /**
     * 生产产线基础信息新增
     * @param plineCode 产线代码
     * @param plineName 产线名称
     * @param departCode 管理部门代码
     * @param departDesc 管理部门描述
     * @param memo 备注
     * @param operator 操作人
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject addLineInfo(ArrayList<Object> parm) {
        Object[] params = parm.toArray();
        CoreReturnObject coreReturnObject = this.setDataBySqlID("ProductionLineInfo.ADD", params);

        // 同步财务中间表
        FmisBasePline pline = new FmisBasePline();
        pline.syncData(parm.get(0).toString());
        return coreReturnObject;
    }

    /**
     * 生产产线基础信息修改
     * @param productLineObjStr 产线信息对象JSON字符串
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
//    public CoreReturnObject updateLineInfo(String productLineObjStr) {
//        JSONObject json = JSONObject.fromObject(productLineObjStr);
//        ProcuctionLineObject plObj = (ProcuctionLineObject) JSONObject.toBean(
//                json, ProcuctionLineObject.class);
//
//        Object[] params = new Object[] { plObj.getProLineName(),
//                plObj.getDepartmentCode(), plObj.getDepartmentDesc(),
//                plObj.getMemo(), plObj.getUpdateName(), plObj.getProLineCode() };
//        return this.setDataBySqlID("ProductionLineInfo.UPDATE", params);
//    }
    
    /**
     * 扣型基础信息修改
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject updateLineInfo(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory.getSql("ProductionLineInfo.UPDATE");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());

            // 同步财务中间表
            FmisBasePline pline = new FmisBasePline();
            pline.syncData( parm.get(parm.size()-1).toString());
        } 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("ProductionLineInfo.DELETE");
                params.add(new Object[] { operator,
                        plineCodes.get(i).toString() });
                // 同步财务中间表
                FmisBasePline pline = new FmisBasePline();
                pline.syncData(plineCodes.get(i).toString());
            }
            else {
                sqlIDList.add("ProductionLineInfo.RESUME");
                params.add(new Object[] { plineCodes.get(i).toString() });
                // 同步财务中间表
                FmisBasePline pline = new FmisBasePline();
                pline.syncData(plineCodes.get(i).toString());
            }
        }
        return this.setDataById(sqlIDList, params);
    }
    
    /**
     * 查询部门
     * @return
     */
    public CoreReturnObject queryDepartment(){
    	String sql = CoreXmlSqlParsersFactory.getSql("ProductionLineInfo.queryDepartment");
    	return this.getData(sql);
    }

    /**
     * 查询厂库信息
     * @return
     */
    public CoreReturnObject getStorageInfo(){
        String sql = CoreXmlSqlParsersFactory.getSql("ProductionLineInfo.getStorageInfo");
        return this.getData(sql);
    }
}
