Delimiter //
/**
 * Caculate the matching code state of the entries :
 * ce batch determine le mode de lettrage en fonction de l'option du schema d'import de l'entitié
 * Il lance les procédures adequate
 * 
 * @bcmcs_ticket #8114
 */ 
Drop Procedure if Exists BUSINESS_CALCULATE_MATCHING_CODE_STATE//
 
Create Procedure BUSINESS_CALCULATE_MATCHING_CODE_STATE(
    In idacounts Int unsigned
    , In identities Int unsigned
    , In maxDelay Smallint Unsigned
)
Begin
	bcmcs_Main: Begin
        bcmcs_initvars: Begin
            Set @bcmcs_idacounts = idacounts;
            Set @bcmcs_identities = identities;

            if @bcmcs_idacounts Is Null Then

                Set @bcmcs_accMinVal = 0;
                Set @bcmcs_accMaxVal = 4e7;

                Set @bcmcs_idEntities_min = Coalesce(@bcmcs_identities, 0);
                Set @bcmcs_idEntities_max = Coalesce(@bcmcs_identities, 10000);
            Else

                Set @bcmcs_accMinVal = @bcmcs_idacounts;
                Set @bcmcs_accMaxVal = @bcmcs_idacounts;

                Set @bcmcs_idEntities_min = Coalesce(
                            (
                                Select ENTITIES_ID_ENTITIES 
                                From ACCOUNTS 
                                where ID_ACCOUNTS = @bcmcs_idacounts
                            )
                            , 0
                        )
                    ;
                Set @bcmcs_idEntities_max = @bcmcs_idEntities_min;
            End If;

        End;

-- 
-- -- 
-- -- --         select @bcmcs_idEntities_min,  @bcmcs_idEntities_max ; 
-- -- --         leave bcmcs_Main;
-- -- 
-- -- 		set @bcmcs_letteringModeAccounts = 'MTCHNG-IN-ACCNT';
-- -- 		set @bcmcs_letteringModeEntities = 'MTCHNG-IN-ENTTS';
		set @bcmcs_fastStatusModeCode = 'MTCHNG-FAST-STT' ;
		set @bcmcs_avgStatusModeCode = 'MTCHNG-AVG-STT' ;
		
		bcmcs_entitiesBuild:  Begin
			Declare targetIdEntities Int unsigned;
			Declare matchingMode Varchar(50);
			Declare fastStatusMode Int;
-- 			-- obtention des entités à traiter et de leur mode de lettrage respectif
-- 			-- par defaut intra-compte
			Declare bcmcs_cursor Cursor For
					Select
						ENTT.ID_ENTITIES
						, Coalesce(TMPLOPT_MDLT.TEMPLATE_OPTIONS_CODE, @bcmcs_letteringModeAccounts)
-- 						, Case When Coalesce(TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE, 'FULL') = @bcmcs_fastStatusModeCode Then 1 Else 0 End
						, Case Coalesce(TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE, 'FULL') 
								When @bcmcs_fastStatusModeCode Then 1 
								When @bcmcs_avgStatusModeCode Then 2 
								Else 0 
							End
					From
						ENTITIES ENTT
						Left Join (
							TEMPLATE_LOADER TMPLDR_MDLT
							Inner Join TEMPL_LOADER_has_TEMPL_OPT TMPLDR_MDLTHASOPT
								On TMPLDR_MDLT.ID_TEMPLATE_LOADER = TMPLDR_MDLTHASOPT.TEMPL_LOADER_ID_TEMPL_LOADER
							Inner Join TEMPLATE_OPTIONS TMPLOPT_MDLT
								On TMPLDR_MDLTHASOPT.TEMPL_OPT_ID_TEMPL_OPT = TMPLOPT_MDLT.ID_TEMPLATE_OPTIONS
								And TMPLOPT_MDLT.TEMPLATE_OPTIONS_CODE = @bcmcs_letteringModeEntities
						) On ENTT.TEMPL_LOADER_ID_TEMPL_LOADER = TMPLDR_MDLT.ID_TEMPLATE_LOADER
						Left Join (
								TEMPLATE_LOADER TMPLDR_FSLT
								Inner Join TEMPL_LOADER_has_TEMPL_OPT TMPLDR_FSLTHASOPT
									On TMPLDR_FSLT.ID_TEMPLATE_LOADER = TMPLDR_FSLTHASOPT.TEMPL_LOADER_ID_TEMPL_LOADER
								Inner Join TEMPLATE_OPTIONS TMPLOPT_FSLT
									On TMPLDR_FSLTHASOPT.TEMPL_OPT_ID_TEMPL_OPT = TMPLOPT_FSLT.ID_TEMPLATE_OPTIONS
									And TMPLOPT_FSLT.TEMPLATE_OPTIONS_CODE In(@bcmcs_fastStatusModeCode, @bcmcs_avgStatusModeCode)
							) On ENTT.TEMPL_LOADER_ID_TEMPL_LOADER = TMPLDR_FSLT.ID_TEMPLATE_LOADER
					where 
						ENTT.ID_ENTITIES Between @bcmcs_idEntities_min  And @bcmcs_idEntities_max 
				;
			Open bcmcs_cursor;
			bcmcs_entities_read: Begin
				Declare Exit Handler for NOT FOUND 
                Begin
                    Close bcmcs_cursor;
                End ;
				bcmcs_entities_loop: Loop
					-- parcour des entités
					Fetch bcmcs_cursor Into targetIdEntities, matchingMode, fastStatusMode;
					IF fastStatusMode = 1 Then
                        call BUSINESS_CALCULATE_ENTRIES_MATCHING_CODE_STATE_FAST(idacounts, targetIdEntities, maxDelay);
                    Else
                        call BUSINESS_CALCULATE_ENTRIES_MATCHING_CODE_STATE_FULL(idacounts, targetIdEntities, maxDelay);
                    End If;
-- -- 					if matchingMode = @bcmcs_letteringModeEntities Then
-- -- 						-- mode de letrage inter-compte (intra entitié)
-- -- 						Call BUSINESS_CALCULATE_ENTITIES_MATCHING_CODE_STATE(targetIdEntities, fastStatusMode, maxDelay);
-- -- 					Else 
-- -- 						-- mode de lettrage intra-compte (defaut)
-- -- 						Call BUSINESS_CALCULATE_ENTRIES_MATCHING_CODE_STATE(idacounts, fastStatusMode, maxDelay);
-- -- 					End if;
				End Loop bcmcs_entities_loop;
			End ;
		End;

	End ;

End //
Delimiter ;