select
	sm.id,
	sm.msg_title as msgTitle,
	sm.msg_content as msgContent,
	sm.send_id as sendId,
	sm.send_status as sendStatus,
	sm.create_date as createDate,
	sm.send_time as sendTime,
	sm.is_pushapp as isPushapp,
	sm.received_range as receivedRange,
	sm.busid as busid,
	smd.read_status as readStatus 
FROM
	t_bus_sys_msg sm
LEFT JOIN t_bus_sys_msg_detail smd ON sm.id = smd.msg_id
WHERE
    sm.send_status='1'
<#if (sysMsgDto.receivedId )?? && sysMsgDto.receivedId ?length gt 0>
	AND (smd.received_id like CONCAT('%', :sysMsgDto.receivedId ,'%') or  (smd.received_id LIKE CONCAT('%', (select org_id from t_s_user_org where user_id = :sysMsgDto.receivedId limit 1),'%') and smd.received_id LIKE CONCAT('%', (select in_postid from t_bus_user_personnel where userid = :sysMsgDto.receivedId limit 1),'%')))
<#else>
	AND smd.received_id = null
</#if>

<#if (sysMsgDto.readStatus )?? && sysMsgDto.readStatus ?length gt 0>
	and smd.read_status = :sysMsgDto.readStatus
</#if>

<#if (sysMsgDto.msgTitle )?? && sysMsgDto.msgTitle ?length gt 0>
	and sm.msg_title like CONCAT('%', :sysMsgDto.msgTitle ,'%') 
</#if>
		
<#if (sysMsgDto.sendTime_begin1 )?? >
	and sm.send_time >= :sysMsgDto.sendTime_begin1
</#if>

<#if (sysMsgDto.sendTime_end2 )?? >
	and sm.send_time <= :sysMsgDto.sendTime_end2
</#if>
ORDER BY
    smd.read_status,
    sm.send_time desc, sm.id