<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc="计量衡预报">

	<query id="PreTrackNotice.query" desc="查询">
		<![CDATA[
			 SELECT
				   T.NOTICE_NO,
				   T.MATTER_NO,
				   T.MATTER_NAME,
				   T.CONTRACT_NO,
				   T.BATCH_NO,
				   T.CUSTOMER_SUPPLIER_NO,
				   T.CUSTOMER_SUPPLIER_NAME,
				   T.SOURCE_AREA,
				   T.FORWARDING_UNIT_NO,
				   T.FORWARDING_UNIT_NAME,
				   T.RECEIVING_UINT_NO,
				   T.RECEIVING_UINT_NAME,
				   T.MATERIAL_NO,
				   T.MATERIAL_NAME,
				   T.SPEC_NO,
				   T.SPEC_NAME,
				   T.LOAD_POINT_NO,
				   T.LOAD_POINT_NAME,
				   T.CARRIER_UNIT_NO,
				   T.CARRIER_UNIT_NAME,
				   T.METER_TYPE_NO,
				   T.METER_TYPE_NAME,
				   T.VALUE_FLAG,
				   T.CREATE_MAN_NO,
				   T.CREATE_MAN_NAME,
				   TO_CHAR(T.CREATE_TIME, 'YYYY-MM-DD HH24:MI:SS') CREATE_TIME,
				   T.READ_FLAG,
				   T.TRANSPORT_TYPE_NAME,
				   T.TOTAL,
				   T.TOLERANCE_RATIO,
				   T.MEMO,
				   T.INDEX_CODE,
				   T.UPDATE_MAN_NO,
				   T.UPDATE_MAN_NAME,
				   TO_CHAR(T.UPDATE_TIME, 'YYYY-MM-DD HH24:MI:SS') UPDATE_TIME
			 FROM PRE_TRACK_NOTICE T
		 	 WHERE
				T.METER_TYPE_NO = '001006002' AND T.VALUE_FLAG = 0
				AND T.INDEX_CODE IS NOT NULL
		]]>
	</query>

	<query id="PreTrackNotice.insert" desc="添加">
		<![CDATA[
			 INSERT INTO PRE_TRACK_NOTICE(
					NOTICE_NO,
					MATTER_NO,
					MATTER_NAME,
					CONTRACT_NO,
					BATCH_NO,
					CUSTOMER_SUPPLIER_NO,
					CUSTOMER_SUPPLIER_NAME,
					SOURCE_AREA,
					FORWARDING_UNIT_NO,
					FORWARDING_UNIT_NAME,
					RECEIVING_UINT_NO,
					RECEIVING_UINT_NAME,
					MATERIAL_NO,
					MATERIAL_NAME,
					SPEC_NO,
					SPEC_NAME,
					LOAD_POINT_NO,
					LOAD_POINT_NAME,
					CARRIER_UNIT_NO,
					CARRIER_UNIT_NAME,
					METER_TYPE_NO,
					METER_TYPE_NAME,
					VALUE_FLAG,
					CREATE_MAN_NO,
					CREATE_MAN_NAME,
					CREATE_TIME,
					READ_FLAG,
					TRANSPORT_TYPE_NAME,
					TOTAL,
					TOLERANCE_RATIO,
					MEMO,
					INDEX_CODE,
					UPDATE_MAN_NO,
					UPDATE_MAN_NAME,
					UPDATE_TIME)
			 VALUES(
					( SELECT
						to_char( SYSDATE, 'YYYYMMDD' ) || LPAD( NVL( SUBSTR( MAX( NOTICE_NO ), 9 ), 0 ) + 1, 4, '0' )
					FROM
						PRE_TRACK_NOTICE),
					( SELECT MATTER_NO FROM METER_BASE_MATTER_INFO WHERE INDEX_CODE = '#MATTER_NO#' ),
					( SELECT MATTER_NAME FROM METER_BASE_MATTER_INFO WHERE INDEX_CODE = '#MATTER_NO#' ),
					'#CONTRACT_NO#',
					'#BATCH_NO#',
					( SELECT CUSTOMER_SUPPLIER_NO from METER_BASE_CUSTOMER_SUPPLIER where INDEX_CODE =  '#CUSTOMER_SUPPLIER_NO#' ),
					( SELECT CUSTOMER_SUPPLIER_NAME from METER_BASE_CUSTOMER_SUPPLIER where INDEX_CODE =  '#CUSTOMER_SUPPLIER_NO#' ),
					'#SOURCE_AREA#',
					( SELECT CUSTOMER_SUPPLIER_NO from METER_BASE_CUSTOMER_SUPPLIER where INDEX_CODE =  '#FORWARDING_UNIT_NO#' ),
					( SELECT CUSTOMER_SUPPLIER_NAME from METER_BASE_CUSTOMER_SUPPLIER where INDEX_CODE =  '#FORWARDING_UNIT_NO#' ),
					( SELECT CUSTOMER_SUPPLIER_NO from METER_BASE_CUSTOMER_SUPPLIER where INDEX_CODE =  '#RECEIVING_UINT_NO#' ),
					( SELECT CUSTOMER_SUPPLIER_NAME from METER_BASE_CUSTOMER_SUPPLIER where INDEX_CODE =  '#RECEIVING_UINT_NO#' ),
					'#MATERIAL_NO#',
					'#MATERIAL_NAME#',
					'#SPEC_NO#',
					'#SPEC_NAME#',
					'#LOAD_POINT_NO#',
					'#LOAD_POINT_NAME#',
					'#CARRIER_UNIT_NO#',
					'#CARRIER_UNIT_NAME#',
					'#METER_TYPE_NO#',
					'#METER_TYPE_NAME#',
					'#VALUE_FLAG#',
					'#CREATE_MAN_NO#',
					'#CREATE_MAN_NAME#',
					SYSDATE,
					'#READ_FLAG#',
					'#TRANSPORT_TYPE_NAME#',
					'#TOTAL#',
					'#TOLERANCE_RATIO#',
					'#MEMO#',
					'#INDEX_CODE#',
					'#UPDATE_MAN_NO#',
					'#UPDATE_MAN_NAME#',
					SYSDATE)
					]]>
	</query>


	<query id="PreTrackNotice.delete" desc="删除">
		<![CDATA[
			UPDATE PRE_TRACK_NOTICE T SET
				T.VALUE_FLAG = '1',
				T.UPDATE_MAN_NO = '#UPDATE_MAN_NO#',
				T.UPDATE_MAN_NAME = '#UPDATE_MAN_NAME#',
				T.UPDATE_TIME = SYSDATE
			WHERE
				T.INDEX_CODE = '#INDEX_CODE#'
		]]>
	</query>

	<query id = "PreTrackNotice.getPreDictNo" desc = "查询预报编号">
		SELECT TO_CHAR(SYSDATE, 'YYYYMMDD') ||
		LPAD(NVL(MAX(SUBSTR(PREDICTION_NO, 9)), 0) + 1, 4, 0) PREDICTION_NO
		FROM PRE_TRACK_SCALE T
		WHERE T.PREDICTION_NO LIKE TO_CHAR(SYSDATE, 'YYYYMMDD')|| '____'
	</query>

	<query id = "PreTrackNotice.InsertPreDictNo" desc = "插入计量预报信息">
		<![CDATA[
		INSERT INTO PRE_TRACK_SCALE(
			PREDICTION_NO,
			NOTICE_NO,
			CAR_NO,
			MATTER_NO,
			MATTER_NAME,
			CONTRACT_NO,
			FORWARDING_UNIT_NO,
			FORWARDING_UNIT_NAME,
			RECEIVING_UINT_NO,
			RECEIVING_UINT_NAME,
			MATERIAL_NO,
			MATERIAL_NAME,
			SPEC_NO,
			SPEC_NAME,
			PREDICTION_TYPE,
			METER_TYPE_NO,
			METER_TYPE_NAME,
			USED_TYPE,
			EFFECTIVE_NUM,
			VALUE_FLAG,
			CREATE_MAN_NO,
			CREATE_MAN_NAME,
			CREATE_TIME,
			HEAT_NO,
			CAR_ALLOCATION_TIME,
			EFFECTIVE_START_TIME,
			EFFECTIVE_END_TIME)
		VALUES(
			(SELECT TO_CHAR(SYSDATE, 'YYYYMMDD') ||
        LPAD(NVL(MAX(SUBSTR(PREDICTION_NO, 9)), 0) + 1, 4, 0) PREDICTION_NO
        FROM PRE_TRACK_SCALE T
        WHERE T.PREDICTION_NO LIKE TO_CHAR(SYSDATE, 'YYYYMMDD')|| '____'),
			'#NOTICE_NO#',
			'#CAR_NO#',
			( SELECT MATTER_NO FROM METER_BASE_MATTER_INFO WHERE INDEX_CODE = '#MATTER_NO#' ),
			( SELECT MATTER_NAME FROM METER_BASE_MATTER_INFO WHERE INDEX_CODE = '#MATTER_NO#' ),
			'#CONTRACT_NO#',
			'#FORWARDING_UNIT_NO#',
			'#FORWARDING_UNIT_NAME#',
			( SELECT CUSTOMER_SUPPLIER_NO from METER_BASE_CUSTOMER_SUPPLIER where INDEX_CODE =  '#RECEIVING_UINT_NO#' ),
			( SELECT CUSTOMER_SUPPLIER_NAME from METER_BASE_CUSTOMER_SUPPLIER where INDEX_CODE =  '#RECEIVING_UINT_NO#' ),
			'#MATERIAL_NO#',
			'#MATERIAL_NAME#',
			'#SPEC_NO#',
			'#SPEC_NAME#',
			'#PREDICTION_TYPE#',
			'#METER_TYPE_NO#',
			'#METER_TYPE_NAME#',
			'#USED_TYPE#',
			'#EFFECTIVE_NUM#',
			'#VALUE_FLAG#',
			'#CREATE_MAN_NO#',
			'#CREATE_MAN_NAME#',
			TO_DATE('#CREATE_TIME#','YYYY-MM-DD HH24:MI:SS'),
			'#HEAT_NO#',
			TO_DATE('#CAR_ALLOCATION_TIME#','YYYY-MM-DD HH24:MI:SS'),
			TO_DATE('#EFFECTIVE_START_TIME#','YYYY-MM-DD HH24:MI:SS'),
			TO_DATE('#EFFECTIVE_END_TIME#','YYYY-MM-DD HH24:MI:SS'))
		]]>
	</query>
	<query id = "PreTrackNotice.InsertPreProcess_LINK" desc = "插入计量流程环节信息">
		<![CDATA[
			DECLARE
			  V_PRE_PROCESS_NO METER_WORK_PRE_PROCESS.PRE_PROCESS_NO%TYPE := NULL;
			BEGIN
			  SELECT TO_CHAR(SYSDATE, 'YYYYMMDD') ||
					 (SELECT LPAD(NVL(MAX(TO_NUMBER(SUBSTR(PRE_PROCESS_NO,
														   LENGTH(PRE_PROCESS_NO) - 4))),
									  0) + 1,
								  5,
								  '0') PRE_PROCESS_NO
						FROM METER_WORK_PRE_PROCESS
					   where instr(PRE_PROCESS_NO, to_char(sysdate, 'yyyyMMdd')) > 0)
				into V_PRE_PROCESS_NO
				FROM DUAL;

			  INSERT INTO METER_WORK_PRE_PROCESS
				SELECT V_PRE_PROCESS_NO,
					   T2.PREDICTION_NO   PREDICTION_NO,
					   T1.PROCESS_NO      PROCESS_NO,
					   T2.CAR_NO          CAR_NO,
					   T1.METER_TYPE_NO   METER_TYPE_NO,
					   T1.METER_TYPE_NAME METER_TYPE_NAME,
					   T2.MATTER_NO       MATTER_NO,
					   T2.MATTER_NAME     MATTER_NAME,
					   '1',
					   T2.CREATE_MAN_NO,
					   T2.CREATE_MAN_NAME,
					   SYSDATE
				  FROM METER_WORK_METER_PROCESS T1, PRE_TRACK_SCALE T2
				 WHERE T1.METER_TYPE_NO = '001006002'
				   AND T1.METER_TYPE_NAME = '销售出厂'
				   AND T2.PREDICTION_NO = '#PREDICTION_NO#'
				   AND T1.VALUE_FLAG = '0';

			  INSERT INTO METER_WORK_PRE_LINK
				(PRE_LINK_NO,
				 PREDICTION_NO,
				 PROCESS_NO,
				 PRE_PROCESS_NO,
				 LINK_CODE,
				 LINK_NAME,
				 SEQ_NO,
				 LINK_NO,
				 VALUE_FLAG,
				 CREATE_MAN_NO,
				 CREATE_MAN_NAME,
				 CREATE_TIME)
				SELECT TO_CHAR(SYSDATE, 'YYYYMMDD') ||
					   (SELECT LPAD(NVL(MAX(TO_NUMBER(SUBSTR(PRE_LINK_NO,
															 LENGTH(PRE_LINK_NO) - 3))),
										0),
									4,
									'0') PRE_LINK_NO
						  FROM METER_WORK_PRE_LINK
						 where instr(PRE_LINK_NO, TO_CHAR(SYSDATE, 'YYYYMMDD')) > 0) +
					   ROWNUM,
					   T1.PREDICTION_NO PREDICTION_NO,
					   T.PROCESS_NO PROCESS_NO,
					   T1.PRE_PROCESS_NO PRE_PROCESS_NO,
					   T.LINK_CODE LINK_CODE,
					   T.LINK_NAME LINK_NAME,
					   T.LINK_ORDER_NUM SEQ_NO,
					   T.LINK_NO LINK_NO,
					   DECODE(T.LINK_CODE, '001021001', '1', '0'),
					   T1.CREATE_MAN_NO,
					   T1.CREATE_MAN_NAME,
					   SYSDATE
				  FROM METER_WORK_METER_LINK T, METER_WORK_PRE_PROCESS T1
				 WHERE T.PROCESS_NO = '202108200002'
				   AND T1.PRE_PROCESS_NO = V_PRE_PROCESS_NO;
			END;

		]]>
	</query>

	<query id = "PreTrackNotice.getNetWeightSql" desc = "查询过磅重量">
		SELECT
			(NET_WEIGHT / 1000) NET_WEIGHT,ACTUAL_NO
		FROM
			METER_WORK_CAR_ACTUAL
		WHERE
			NOTICE_NO = ?
			AND CAR_NO = ?
	</query>
</queryMap>