## 查找使用邮件功能人数
select group_concat(opi_sn_value separator ''',''') as 'opi_list' from (
SELECT 
  SUBSTRING_INDEX(SUBSTRING_INDEX(request_uri, 'opi_sn=', -1), '&', 1) AS opi_sn_value,
  COUNT(*) AS count
FROM log_message 
WHERE request_uri LIKE '%/v3/dir_count%'
GROUP BY opi_sn_value
) a

select DISTINCT OPI_RealName from V_Operator_Info voi 
where OPI2_OPI_SN  in ('513','5130','466','621','404','622','383','609','510','512','582','586','633','0','415','639','641','640','577','654','601','602','599','535','568','496','648','691','690','525','540','626','162','634','487','585','594','628','611','624','674','637','522','676','606','631','451','551','489','583','495','503','719','698','644','605','587','588','589','509','552','526','227','501','515','581','216','575','484','687','679','370','580','490','617','618','619','261','600','603','604','114','579','481','387','629','354','492','632','414','660','574','79','486','663','391','584','482','252','264','265','376','453','649','651','650','210','212','343','565','143','591','590','328','476','593','514','576','595','536','543','564','178','528','541','625','119','571','598','573','332','413','155','330','627','550','742','612','444','360','519','421','146','553','441')



## 查找出掉线的 WhatsApp
select *
from whatsapp_individual.connections 
where status IN ('offline')
and wa_id not in ('8618777396951', '8613557032060','8613317835586')
and wa_id <> 'null'

// 查找所有开启会话的顾问，并用逗号分隔
SELECT  group_concat(opi_sn separator ',') as 'sn_list' FROM (
SELECT
  opi_sn,COUNT(*)
FROM whatsapp_session
  where opi_sn is not null and opi_sn not in (29,383,404,227)
  AND (last_send_time >= '2024-4-15' AND last_send_time <= '2024-4-21')
  GROUP by opi_sn
) session_count

SELECT
  OPI_SN, OPI_Code,OPI_Name,OPI_DEI_SN,OPI_FirstName,OPI_RealName
FROM
  dbo.OperatorInfo
  where
  DeleteFlag = 0 and
  OPI_SN in (162,31,32,33,34,599,600,353,352,350,351,35,606,495,293,525,114,587,585,519,522,476,354,451,216,143,586,539,370,512)

// 查找钉钉绑定 HT 情况
SELECT opi.OPI_Code,
  opi.OPI_Name, tpa.* FROM dbo.ThirdPlatformAccount_Bind tpa
  left join OperatorInfo opi
    on opi.opi_sn = tpa.TPA_OPI_SN
where opi.DeleteFlag = 0

// 查找没关联订单的会话
SELECT *
FROM whatsapp_session
WHERE coli_sn  = 0 AND last_received_time IS NOT NULL ## AND opi_sn = 599
ORDER BY last_received_time DESC

// 查找 WA 号码不一致
SELECT *
FROM whatsapp_session
WHERE whatsapp_phone_number_bak IS not NULL

/**
 * ---------------------------------------------------------
 * 分配会话到某个订单
 */
 -- title: query order info
SELECT cli.COLI_ID,cli.COLI_SN
  ,cli.COLI_GRI_SN
  ,mi.MEI_SN
--  ,cl.CUL_SN
  ,oi.OPI2_Name AS 顾问
  ,'#-------#'
  ,cli.COLI_OrderStartDate
  ,(mi.MEI_FirstName + ' | '+ mi.MEI_LastName) AS member_name
  ,cli.*
  ,'#-------#',cl.*
  ,'#-------#', mi.*
FROM ConfirmLineInfo AS cli
LEFT JOIN CUstomerList AS cl ON cli.COLI_SN = cl.CUL_COLI_SN
LEFT JOIN MEmberInfo AS mi ON mi.MEI_SN = cl.CUL_CUI_SN
LEFT JOIN OperatorInfo2 AS oi ON oi.opi2_opi_sn = cli.COLI_OPI_ID  AND oi.OPI2_LGC =2
WHERE 1 = 1
  --  AND mi.MEI_FirstName = 'CHEN WANG'
    AND COLI_ID = 'Lib231120146'
--  AND CUL_COLI_SN = 1086888
  --
  ;

UPDATE whatsapp_session
SET coli_sn = 1075451
WHERE sn = 57 AND whatsapp_phone_number = '14048496022';

UPDATE whatsapp_inboundmessage msg
INNER JOIN whatsapp_session s ON s.whatsapp_phone_number = msg.froms
  AND msg.msg_direction = 'inbound'
SET msg.coli_sn = s.coli_sn
WHERE ifnull(msg.coli_sn, 0) = 0
  AND msg.msg_direction = 'inbound'
  AND msg.froms = '393343998897';


UPDATE whatsapp_inboundmessage msg
INNER JOIN whatsapp_session s ON s.whatsapp_phone_number = msg.tos
  AND msg.msg_direction = 'outbound'
SET msg.coli_sn = s.coli_sn
WHERE ifnull(msg.coli_sn, 0) = 0
  AND msg.msg_direction = 'outbound'
  AND msg.tos = '393343998897';
/**
 * end 分配会话到某个订单
 * ---------------------------------------------------------
 */

/**
 * ---------------------------------------------------------
 * 新增会话给顾问
 */
INSERT INTO sale_system.whatsapp_session (
  whatsapp_phone_number,whatsapp_name, opi_sn,coli_sn,session_enable)
VALUES ('6282145142470','',162,0,1);
/**
 * end 新增会话给顾问
 * ---------------------------------------------------------
 */

/**
 * ---------------------------------------------------------
 * 会话 合并
 * 含有特别呼叫代码的 会话, 合并WhatsApp号码
 * 1. 更新目标会话的 coli_sn, coli_id, whatsapp_name
 * 2. 更新需合并的源会话的消息记录: tos, coli_sn
 */
UPDATE sale_system.whatsapp_session
SET whatsapp_name='Luiz Ribeiro', coli_sn=1092511, coli_id='L240509014'
WHERE whatsapp_phone_number='553499923993' AND opi_sn = 587;

UPDATE sale_system.whatsapp_inboundmessage
SET tos='553499923993', coli_sn=1092511
WHERE tos = '5534999923993' AND opi_sn = 587;
/**
 * end 会话 合并
 * ---------------------------------------------------------
 */



/**
个人 WhatsApp
*/

select * from whatsapp_user
##where wau_opi_sn in (252, 261,264,265,330,360,376,413,421,453,605,620) ## 国际部
where wau_opi_sn in (495, 143, 370, 114, 513, 514, 517, 522, 550, 587,354, 414, 599, 606, 639, 648, 654, 662, 674, 676,391, 451, 476, 501, 512, 525, 528, 585, 586, 644) ## GH

-- 查找每个服务器在线的 WA 数量
select connect_name, count(*)
from whatsapp_individual.connections
where status IN ('open')
group by connect_name

set SESSION group_concat_max_len=4294967295;

-- 查找已经配置 WAI 服务的顾问
select group_concat(wau_whatsapp separator ''',''') as 'sn_list'
from sale_system.whatsapp_user
where wau_wai_server is not null

## 查找在线的 WhatsApp
SELECT group_concat(sesson_id separator ''',''') as 'sesson_list'
FROM whatsapp_individual.connections
where status = 'open'

-- 查找掉线的顾问
select group_concat(sesson_id separator ''',''') as 'sesson_list' from whatsapp_individual.connections
where status IN ('offline')
and wa_id not in ('8618777396951', '8613557032060','8613317835586')

## 查找 GH 没有扫码登录的顾问
select group_concat(wau_opi_sn separator ',') as 'sn_list'
from whatsapp_user
where wau_whatsapp not in ('8613317835586','8617607732272','8613978392676','8618378304803','8617607730629','8619107833371','8619107835971','8617607731491','8615080129281','8617607737720','8618777396951','8618078444860','8615778462307','8617774702925','8615078398450','8619178340224','8617607731153','8617607735120','8617607737646','8618877388203','8615778493040','8613617733956','8618290167273','8617776515283','8617607736381','8613557032060','8613667839691','8618378388403','8613635132972','8617607734598','8617607732512','8615878340720')
and wau_opi_sn in (495, 143, 370, 114, 513, 514, 517, 522, 550, 587,354, 414, 599, 606, 639, 648, 654, 662, 674, 676,391, 451, 476, 501, 512, 525, 528, 585, 586, 644)

-- 查找使用 WhatsApp 顾问信息
SELECT
  OPI_SN, OPI_Code,OPI_Name,OPI_DEI_SN,OPI_FirstName,OPI_RealName, DeleteFlag
FROM
  dbo.OperatorInfo WHERE
  OPI_SN in (143,476,528,391)
  --OPI_SN in (495, 143, 370, 114, 513, 514, 517, 522, 550, 587)



SELECT OPI_RealName + '(' + CAST(OPI_SN AS VARCHAR(100)) + ')',OPI_SN
FROM
  dbo.OperatorInfo
  where
  --DeleteFlag = 0 and
  OPI_SN in (79,85,114,119,135,143,155,162,178,210,216,222,225,252,261,264,265,273,293,296,311,330,343,347,348,354,360,370,376,387,391,412,413,414,421,441,444,451,453,456,466,468,476,495,497,501,509,512,513,514,517,519,522,525,527,528,539,550,573,585,586,587,592,599,600,605,606,611,617,620,639,644,648,654,656,659,662,663,674,676,690,691)
  and OPI_RealName in ('兰芬','孙俊垚','王继伟','曾君','潘宏宇','郑美珍','张丽娟','张倩倩','赵泽菲','王影','陆力影','吕燕珍','何秋云','沈慧香')