--- map_did_to_user.lua.sql.2014110600	2014-10-31 10:35:06.934970867 +0600
+++ map_did_to_user.lua.sql	2014-11-07 03:09:50.321730716 +0600
@@ -621,6 +621,143 @@
             end
             repeat_cnt = repeat_cnt - 1
         end
+    elseif mgq.rule_id == "7" then	-- типо callcenter
+        session:execute("info")
+        local my_callcenter_query = string.format([[
+with
+x as (
+    select
+        id
+        ,name
+        ,name || '.sky.avantel.ru' full_name
+    from
+        companies
+    where
+        name = '%s')
+,y as (
+    select
+        gl1.line
+    from
+        groups_line gl1
+        ,x
+    where
+        gl1.company_id = x.id
+        and gl1.call_group = %s
+        and gl1.line not in (
+            select
+                sip_from_user
+            from
+                sip_dialogs
+            where
+                x.full_name in (
+                    sip_from_host
+                    ,sip_to_host)
+            union all
+            select
+                sip_to_user
+            from
+                sip_dialogs
+            where
+                x.full_name in (
+                    sip_from_host
+                    ,sip_to_host))
+        and gl1.line in (
+            select
+                sip_user
+            from
+                sip_registrations
+            where
+                x.full_name = sip_realm))
+,z as (
+    select
+        max(c1.start_stamp) max_start_stamp
+        ,c2.destination_number
+    from
+        cdr c1
+    join
+        x
+    on
+        c1.d_company = x.name
+    join
+        cdr c2
+    on
+        c1.bleg_uuid = c2.uuid
+    where
+        (c1.start_stamp >= now() - interval '1 hour'
+        and c2.start_stamp >= now() - interval '1 hour')
+        and length(c2.destination_number) = 4
+    group by
+        c2.destination_number)
+,zz as (
+    select
+        max_start_stamp
+        ,destination_number
+    from
+        z
+    union all
+    select
+        now()
+        ,sr.sip_user
+    from
+        sip_registrations sr
+    join
+        x
+    on
+        sr.sip_realm = x.full_name
+    where
+        sr.sip_user not in (
+            select
+                destination_number from z))
+select
+    y.line
+from
+    y
+join
+    zz
+on
+    y.line = zz.destination_number
+order by
+    zz.max_start_stamp desc
+    ,random();
+]]
+,_in_company_name
+,mgq.call_group)
+        session:setVariable("instant_ringback", "true")
+        session:setVariable("ringback", "local_stream://moh")
+        session:setVariable("transfer_ringback", "local_stream://moh")
+        session:setVariable("temp_hold_music", "local_stream://moh")
+        session:setVariable("ignore_early_media", "true")
+        session:setVariable("continue_on_fail", "UNALLOCATED_NUMBER,NO_ROUTE_DESTINATION,USER_BUSY,NO_USER_RESPONSE,NO_ANSWER,SUBSCRIBER_ABSENT,CALL_REJECTED,USER_NOT_REGISTERED")
+        session:answer()
+        session:sleep(500)
+        local _start_time = os.time()
+        while os.time() - _start_time < 60 * 30 and session:ready() and not session:bridged()
+        do
+            assert(dbh:query(my_callcenter_query, function(mcq)
+                freeswitch.consoleLog("notice", string.format("MCQ_LINE: %s\n", mcq.line))
+--                local session2 = null
+--                if session:ready() and not session:bridged() then
+--                    session2 = freeswitch.Session(string.format("{origination_caller_id_number=%s,originate_timeout=8}user/%s@%s.sky.avantel.ru", number, mcq.line, _in_company_name))
+--                    if session2:ready() and session2:answered() and session:ready() and not session:bridged() then
+--                        freeswitch.bridge(session, session2)
+--                    end
+--                end
+                if (session:getVariable("originate_disposition") == nil
+                    or session:getVariable("originate_disposition") == "UNALLOCATED_NUMBER" or session:getVariable("originate_disposition") == "NO_ROUTE_DESTINATION"
+                    or session:getVariable("originate_disposition") == "USER_BUSY" or session:getVariable("originate_disposition") == "NO_USER_RESPONSE"
+                    or session:getVariable("originate_disposition") == "NO_ANSWER" or session:getVariable("originate_disposition") == "SUBSCRIBER_ABSENT"
+                    or session:getVariable("originate_disposition") == "CALL_REJECTED" or session:getVariable("originate_disposition") == "USER_NOT_REGISTERED")
+                then
+                    session:execute("bridge", string.format("{originate_timeout=8}user/%s@%s.sky.avantel.ru", mcq.line, _in_company_name))
+                end
+            end))
+            freeswitch.consoleLog("notice", string.format("LOCAL_TIME: %s\n", os.time()))
+            freeswitch.consoleLog("notice", string.format("ORIGINATE_DISPOSITION: %s\n", session:getVariable("originate_disposition")))
+            freeswitch.consoleLog("notice", "START SLEEP\n")
+            session:execute("bridge", string.format("{originate_timeout=11}sofia/internal/x_wait@tld.jp:5080"))
+            freeswitch.consoleLog("notice", "END SLEEP\n")
+--            os.execute("sleep " .. 11)
+        end
 --        _loop_function(mfq.dest_call_group, mfq.l_company, 0, mfq.local_recording)
     end -- if mgq.rule_id == "1" then
 end)) -- assert(dbh:query(my_group_query, function(mgq)
