package com.steering.pss.plnsaleord;

import java.util.ArrayList;
import java.util.List;

import CoreFS.SA06.CoreReturnObject;

import com.steering.comm.db.DBCommon;

public class FrmPlnOrderDownLog extends DBCommon {
	/**
	 * 查询合同下发及回退日志
	 * 
	 * @param beginTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param vrsionNo
	 *            版次号
	 * @param orderno
	 *            合同号
	 * @param type
	 *            类型
	 * @param ordersource
	 *            来源
	 * @return
	 */
	public CoreReturnObject queryOrderDownLog(String beginTime, String endTime,
			String vrsionNo, String orderno, String type, String ordersource) {
		String sql = "SELECT DL.VERSION_NO DOWN_VERSION_NO,decode(DL.version_type,0,'下发','取消下发') VERSION_TYPE,"
		        +" DECODE(T2.ORDER_SOURCE,'补量需求',R.ACCEPT_NO,T.ACCEPT_NO) ACCEPT_NO,"
		        +" M.DESGIN_KEY,"
                        +"DL.ROLL_WELL_MONTH,DECODE(DL.IF_PASS_ZG,'1','是','否')IF_PASS_ZG,DECODE(DL.IF_PASS_JGX,'1','是','否')IF_PASS_JGX,"   
		        +" (SELECT A.PLINE_NAME FROM COM_BASE_PLINE A WHERE A.PLINE_CODE = T2.GROUP_ROLL AND A.VALIDFLAG = '1') GROUP_ROLL,"
		        +" (SELECT A.PRODUC_JX FROM COM_BASE_PRODUCT A WHERE A.PRODUCCODE = T.PRODUCCODE) PRODUC_JX,"
		        +" (DECODE(T.STD_STYLE_DESC || ' ', ' ', '', T.STD_STYLE_DESC || ' ') || T.STEELNAME) STEELNAME,"
		        +" T.OUTDIAMETER,"
		        +" T.WALLTHICK,"
		        +" T.LENGTHDISC,"
		        +" T.MODEL_DESC,"
		        +" T2.PLAN_P_MAIN ORDERWEIGHT,"
		        +" DECODE(T.TOTLENGTH, 0, '', NULL, '', T2.PLAN_TOT_LEN_D) TOTLENGTH,"
		        +" T.STD_NAME,"
		        +" (DL.ORDER_NO || decode(DL.ORDER_SEQ,null,'', '/' || DL.ORDER_SEQ)) ORDER_SEQNO,"
		        +" NVL((SELECT A.ABRVITION FROM SLM_BASE_CUSTOMER A WHERE A.CUSTOMER_NO = DECODE(T.FINAL_USER,NULL,T.BUYER_ID, '/',T.BUYER_ID,T.FINAL_USER)),"
		        +"  DECODE(T.FINAL_USER_DESC,NULL,T.BUYER_NAME, '/',T.BUYER_NAME,T.FINAL_USER_DESC)) FINAL_USER,"
		        +" L.ORDER_ADD_DESC,"
		        +" (SELECT A.BASENAME FROM COM_BASE_INFO A WHERE A.BASECODE = T.ORDERFROM) ORDERFROM,"
		        +" T.PRODUCE_SORT,"
		        +" (TO_CHAR(DECODE(T.DELVRY_RANGE_TPE,'绝对值',T.DELVRY_RANGE_MAX * 100 / T.ORDER_QTY, T.DELVRY_RANGE_MAX), 'FM9990.00')) RANGE_MAX,"
		        +" DECODE(T.DELVRY_RANGE_MIN,NULL,'',('-' || TO_CHAR(ABS(DECODE(T.DELVRY_RANGE_TPE,'绝对值',T.DELVRY_RANGE_MIN * 100 / T.ORDER_QTY,T.DELVRY_RANGE_MIN)),'FM9990.00'))) RANGE_MIN,"
		        +" TO_CHAR(DECODE(T.DELVRY_RANGE_TPE,'绝对值',T.DELVRY_RANGE_MAX / T.ORDER_QTY,T.DELVRY_RANGE_MAX / 100),'FM90.0000') DELVRY_RANGE_MAX,"
		        +" DECODE(T.DELVRY_RANGE_MIN,NULL,'',('-' || TO_CHAR(ABS(DECODE(T.DELVRY_RANGE_TPE,'绝对值',T.DELVRY_RANGE_MIN / T.ORDER_QTY,T.DELVRY_RANGE_MIN / 100)),'FM90.0000'))) DELVRY_RANGE_MIN,"
		        +" TO_CHAR(T.DELVRY_EDATE, 'YYYY-MM-DD') DELVRY_EDATE,"
		        +" T.ORDERCONTACT,"
		        +" (DECODE(T.TRANSIT_TYP || ' ', ' ', '', T.TRANSIT_TYP || ' ') || T.STATION_NM) STATION_NM,"
		        +" (SELECT A.BASENAME FROM COM_BASE_INFO A WHERE A.BASECODE = T.PRD_CLS) PRD_CLS,"
		        +" TO_CHAR(TO_DATE(T2.ROLL_MONTH, 'YYYY-MM'), 'YYYYMM') ROLL_MONTH,"
		        +" (SELECT A.BASENAME FROM COM_BASE_INFO A WHERE A.BASECODE = TRIM(T.OUTS_BILLET_FL)) OUTS_BILLET_FL,"
		        +" T.SPEC_NAME,"
		        +" (SELECT A.BASENAME FROM COM_BASE_INFO A WHERE A.BASECODE = T.MONITORDEMAND) MONITORDEMAND,"
		        +" T2.PRDCRPRO,"
		        +" T.PRDCR_NO,"
		        +" DECODE(T.LENGTHMIN,NULL,'',(TO_CHAR(T.LENGTHMIN, 'FM99990.00') || '-' || TO_CHAR(T.LENGTHMAX, 'FM99990.00') || 'M')) TECHNICALLEN,"
		        +" T.ORDER_SPECIAL_DESC,"
		        +" T2.ORDER_QTY_D ORDER_QTY,"
		        +" T2.ORDER_UNIT,"
		        +" T2.PLAN_WT_D,"
		        +" T2.BJ_TYPE,"
		        +" DECODE(T2.ORDER_SOURCE, '补量需求', NULL, T.PAYDOWNWEIGHT) PAYDOWNWEIGHT,"
		        +" DECODE(T2.ORDER_SOURCE, '补量需求', NULL, T.PAYDOWN_NUM) PAYDOWN_NUM,"
		        +" DECODE(T.ORDER_UNIT,'米','吨','英尺','吨','英磅','吨',T.ORDER_UNIT) DW,"
		        +" DECODE(T.ORDER_UNIT,'米','吨','英尺','吨','英磅','吨',T.ORDER_UNIT) DW2,"
		        +" T.SHRT_STOVE,"
		        +" T.SIGL_LEN,"
		        +" DECODE(T.ACCEPT_NO,NULL,'',DECODE(T.ORDER_SPECIAL_DESC, NULL, '普通', '特殊')) ORDER_TYPE,"
		        +" T.ENDLEN_SPECSTD,"
		        +" T2.STUFF_PROPERTY,"
		        +" T2.GRADECODE_ALL,"
		        +" (SELECT MAX(D.CRAFT_NO) FROM CRAFT_ORD_DESIGN D WHERE D.ORD_LN_PK = T.ORD_LN_PK AND D.VALIDFLAG = '1') CRAFT_FILE_NO_NEW,"
		        +" T2.CRAFT_FILE_NO,"
		        +" (SELECT M1.CRAFT_NAME FROM CRAFT_ORD_DESIGN_APPOINT M1 WHERE M1.ORD_LN_PK = M.ORD_LN_PK) CRAFT_FILE_MAN,"
		        +" T2.HEIGHT_NK_MIN / 100 HEIGHT_NK_MIN,"
		        +" T2.HEIGHT_NK_MAX / 100 HEIGHT_NK_MAX,"
		        +" (SELECT MAX(T1.PROCESS_DESC_C) FROM CRAFT_ORD_DESIGN_MSC_PL_PRC T1 WHERE T1.PROCESS_CODE = 'D' AND T1.DESGIN_KEY = M.DESGIN_KEY AND T1.MSC_PLINE = T2.INDEX_SEQ) CRAFT_PATH,"
		        +" (SELECT MAX(T1.PROCESS_DESC_C) FROM CRAFT_ORD_DESIGN_MSC_PL_PRC T1 WHERE T1.PROCESS_CODE = 'F' AND T1.DESGIN_KEY = M.DESGIN_KEY AND T1.MSC_PLINE = T2.INDEX_SEQ) RCL_INFO,"
		        +" T2.CRAFT_NO_V VERSION_NO,"
		        +" DL.ORDER_SOURCE ORDERCONTRACT,"
		        +" DECODE(T2.PLANSTATUS, '1', '已接收','2','已下发', '3','生产中','4', '生产完成','5', '缴库完成') PORDERSTATUS,"
		        +" T.DELIVERY_NO,"
		        +" (SELECT A.BASENAME FROM COM_BASE_INFO A WHERE A.BASECODE = T2.GROUP_RCL) GROUP_RCL,"
		        +" (SELECT A.BASENAME FROM COM_BASE_INFO A WHERE A.BASECODE = T2.GROUP_JGX) GROUP_JGX,"
		        +" (SELECT A.BASENAME FROM COM_BASE_INFO A WHERE A.BASECODE = T2.PRIORITY) PRIORITY,"
		        +" T2.PRIORITY_MEMO,"
		        +" DECODE(T.JGORD_IS_OK, '0', '否', '1', '是', '2', '/') JGORD_IS_OK,"
		        +" DECODE(T.DJBL_REQ_IS_OK, '0', '否', '1', '是', '2', '/') DJBL_REQ_IS_OK,"
		        +" (DL.ORD_LN_DLY_PK || '-' ||DL.PLN_DIVIDE_ID) ORD_LN_DLY_PK,"
		        +" T2.ABC,"
		        +" T2.IF_EXPORT,"
		        +" L.AW,"
		        +" T2.LEN_ROLL_MAX LEN_TOEND_MAX,"
		        +" T2.LEN_ROLL_MIN LEN_TOEND_MIN,"
		        +" T2.DIMATER_YLG,"
		        +" T2.HEIGHT_YLG,"
		        +" Z1.Z_ORDERWEIGHT,"
		        +" Z1.Z_ORDERNUM,Z1.STATION_ROAD,"
		        +" DECODE(T.ACCEPT_NO,NULL,'',(T.STD_NAME ||'，'|| T.USE_DESC)) USE_STAND,"
		        +" (SELECT P.WHOLE_BACKLOG_DESC FROM CRAFT_ORD_DESIGN_MSC_PL P WHERE P.DESGIN_KEY = M.DESGIN_KEY AND P.MSC_PLINE = T2.INDEX_SEQ) WHOLE_BACKLOG_DESC,"
		        +" DL.OPERATE_NAME,DL.OPERATE_TIME,ZGS.INWL_DESC_ZG,ZGS.OUTWL_DESC_ZG,ZGS.PASS_NAME_ZG,ZGS.AIMWALLTHICK_ZG,ZGS.DIAMETER_GP_ZG,ZGS.DIAMETER_MANDREL, "
		        +"TO_NUMBER(Z1.Z_ORDERWEIGHT*(1-0.9*DECODE(T.DELVRY_RANGE_MAX,NULL,0,ABS(DECODE(T.DELVRY_RANGE_TPE,'绝对值',T.DELVRY_RANGE_MAX / T.ORDER_QTY,T.DELVRY_RANGE_MAX / 100))) "
                +" -0.9*DECODE(T.DELVRY_RANGE_MIN,NULL,0,ABS(DECODE(T.DELVRY_RANGE_TPE,'绝对值',T.DELVRY_RANGE_MIN / T.ORDER_QTY,T.DELVRY_RANGE_MIN / 100))))) ZG_WT_MIN "
		        +" FROM PLN_ORDER_DOWN_LOG DL "
		        +" LEFT JOIN PLN_SALEORD T ON T.ORD_LN_DLY_PK = DL.ORD_LN_DLY_PK "
		        +" LEFT JOIN SLM_ORDER_LINE L ON L.ORD_LN_PK = T.ORD_LN_PK AND L.VALIDFLAG = '1' "
		        +" LEFT JOIN PLN_SALEORD_PROC T2 ON T2.ORD_LN_DLY_PK=DL.ORD_LN_DLY_PK and T2.PLN_DIVIDE_ID =DL.PLN_DIVIDE_ID "
		        +" LEFT JOIN (SELECT Z.ORD_LN_DLY_PK,Z.PLN_DIVIDE_ID,SUM(Z.ORDERWEIGHT) Z_ORDERWEIGHT,SUM(Z.ORDERNUM) Z_ORDERNUM,MAX(Z.STATION_ROAD) STATION_ROAD FROM PLN_ORDER_ZG Z GROUP BY Z.ORD_LN_DLY_PK, Z.PLN_DIVIDE_ID) Z1 "
		        +" ON Z1.ORD_LN_DLY_PK = T2.ORD_LN_DLY_PK AND Z1.PLN_DIVIDE_ID = T2.PLN_DIVIDE_ID "
		        +" LEFT JOIN PLN_SALEORD E2 ON E2.ORD_LN_DLY_PK = T2.ORD_LN_DLY_PK "
		        +" LEFT JOIN CRAFT_ORD_DESIGN M ON M.CRAFT_SEQ = T2.CRAFT_SEQ AND M.ORD_LN_PK = E2.ORD_LN_PK "
		        +" LEFT JOIN PLN_SALEORDER_R_NUM R ON R.R_NUM_ID = T2.R_NUM_ID "
		        +" LEFT JOIN SLM_ORDER_LINE_DELIVERY D ON D.ORD_LN_DLY_PK = T2.ORD_LN_DLY_PK "
		        +" LEFT JOIN (select S.ORD_LN_DLY_PK,S.PLN_DIVIDE_ID,max(S.INWL_DESC) INWL_DESC_ZG,max(S.OUTWL_DESC) OUTWL_DESC_ZG,max(S.PASS_NAME) PASS_NAME_ZG,max(S.AIMWALLTHICK) AIMWALLTHICK_ZG," 
		        +" max(S.DIAMETER_GP) DIAMETER_GP_ZG,max(S.DIAMETER_MANDREL) DIAMETER_MANDREL from PLN_ORDER_ZG_S  S GROUP BY S.ORD_LN_DLY_PK, S.PLN_DIVIDE_ID) ZGS "
		        +" ON ZGS.ORD_LN_DLY_PK = T2.ORD_LN_DLY_PK AND ZGS.PLN_DIVIDE_ID = T2.PLN_DIVIDE_ID where DL.IF_PASS_ZG='1' ";
		List<String> params = new ArrayList<String>();
		if (!beginTime.equals("") && !endTime.equals("")) {
			sql += " and DL.operate_time between to_date(?,'yyyy-mm-dd hh24:mi:ss') and to_date(?,'yyyy-mm-dd hh24:mi:ss')";
			params.add(beginTime);
			params.add(endTime);
		}
		if (!vrsionNo.equals("")) {
			sql += " and DL.version_no like '%'||?||'%' ";
			params.add(vrsionNo);
		}
		if (!orderno.equals("")) {
			sql += " and (DL.ORDER_NO || '/' || DL.ORDER_SEQ) like '%'||?||'%' ";
			params.add(orderno);
		}
		if (!type.equals("")) {
			sql += " and DL.version_type =? ";
			params.add(type);
		}
		if (!ordersource.equals("")) {
			sql += " and DL.order_source=? ";
			params.add(ordersource);
		}
		sql += " order by DL.operate_time";
		return this.getData(sql, params.toArray());
	}
}
