// 查找所有开启会话的顾问，并用逗号分隔
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,opi_sn,coli_sn,session_enable)
VALUES ('6282145142470',162,0,1);
/**
 * end 新增会话给顾问
 * ---------------------------------------------------------
 */
