create procedure proc_customer_tqjq()
BEGIN
	DECLARE eb_id VARCHAR(32);
	DECLARE eb_mayope_money DECIMAL(22,2);
	DECLARE eb_client_name VARCHAR(200);
	DECLARE eb_client_acc_number VARCHAR(200);
	DECLARE eb_fact_date VARCHAR(32);
	DECLARE eb_match VARCHAR(1000) DEFAULT '';
	DECLARE ct INT DEFAULT 0;
	DECLARE sql_error INT DEFAULT 0;
	DECLARE done BOOLEAN DEFAULT FALSE;
	DECLARE ebank_data CURSOR FOR SELECT id,mayope_money,client_name,client_acc_number,fact_date FROM lc_ebank WHERE invalid<>'Y' AND id NOT IN(SELECT ebank_number FROM lc_ebank_process);
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
	DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET sql_error=1;
	OPEN ebank_data;
	myLoop:LOOP
		FETCH ebank_data INTO eb_id,eb_mayope_money,eb_client_name,eb_client_acc_number,eb_fact_date;
		IF done = TRUE THEN LEAVE myLoop;
		END IF;
		BEGIN
			DECLARE eb_contract_id VARCHAR(50);
			DECLARE eb_payment_number VARCHAR(50);
			DECLARE eb_rent_list VARCHAR(50);
			DECLARE num INT DEFAULT 0;
			DECLARE flag BOOLEAN DEFAULT FALSE;
			SELECT COUNT(*) num,MIN(lci.id),MIN(lr.PAYMENT_NUMBER),MIN(lr.PLAN_LIST)
			INTO num,eb_contract_id,eb_payment_number,eb_rent_list
			FROM LC_RENT_PLAN lr
			LEFT JOIN lb_contract_info lci ON lci.ID=lr.CONTRACT_ID
			LEFT JOIN(SELECT contract_id,client_acc_number FROM lb_tenantry_info WHERE client_acc_number=eb_client_acc_number) AS lti ON lci.id=lti.CONTRACT_ID 
			LEFT JOIN(SELECT contract_id,customer_name FROM lb_union_lessee WHERE customer_name=eb_client_name) AS lul ON lul.CONTRACT_ID=lci.ID
			left join lc_calc_condition_status lccs on lccs.payment_number=lr.payment_number 
			WHERE (lr.payment_number,lr.plan_list) IN (
								SELECT MIN(lrp.PAYMENT_NUMBER) payment_number,MIN(lrp.PLAN_LIST) plan_list FROM lc_rent_plan lrp 
								WHERE lrp.PAYMENT_NUMBER NOT IN 
								(SELECT payment_number FROM vi_lc_rent_plan WHERE fact_rent>0 AND fact_rent<rent)
								AND lrp.ID NOT IN 
								(SELECT id FROM vi_lc_rent_plan WHERE fact_rent>=rent) GROUP BY payment_number)
			AND lr.RENT=eb_mayope_money AND (CASE WHEN (lti.CLIENT_ACC_NUMBER IS NULL OR lti.CLIENT_ACC_NUMBER='') AND (lul.CUSTOMER_NAME IS NULL OR lul.CUSTOMER_NAME='') THEN FALSE ELSE TRUE END)
			and lr.PAYMENT_NUMBER not in(select payment_number from lc_occupy_rent_list) and lccs.plan_status=31;
			IF num=1 AND (eb_match LIKE CONCAT('%',eb_payment_number,eb_rent_list,'%'))=0 THEN
			START TRANSACTION;
			UPDATE lc_ebank SET contract_id=eb_contract_id,payment_number=eb_payment_number,rent_list_income=eb_rent_list WHERE id=eb_id;
			INSERT INTO lc_ebank_process (ID,EBANK_NUMBER,PROCESS_MONEY,FLOW_NAME)
			VALUES(REPLACE(UUID(),'-',''),eb_id,eb_mayope_money,'网银批量核销');
			insert into lc_occupy_rent_list values(REPLACE(UUID(),'-',''),eb_payment_number,eb_rent_list,'网银批量核销','');
			SELECT CONCAT(eb_match,',',eb_payment_number,eb_rent_list) INTO eb_match;
			set ct = ct + 1;
			END IF;
			IF sql_error=1 THEN ROLLBACK;
			ELSE COMMIT;
			END IF;
		END;
	END LOOP myLoop;
	CLOSE ebank_data;
	SELECT ct FROM DUAL;
END;

