<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.dromara.business.mapper.CaseBaseInfoMapper">

    <select id="selectCaseReviewStatus" resultType="java.lang.String">
        select DISTINCT check_status
        from case_base_info
        where del_flag = '0'
    </select>

    <select id="queryCheckPageList" resultType="org.dromara.business.domain.vo.CaseBaseInfoVo">
        select t.*,
               case
                   when t.check_status = '0' then 0
                   when t.check_status = '1' then 2
                   when t.check_status = '2' then 1 end as sortStatus,
               a.nick_name                              as userName,
               b.nick_name                              as uploadUserName,
               c.nick_name                              as commentUseName
        from case_base_info t
                 left join sys_user a on t.user_id = a.user_id
                 left join sys_user b on t.upload_user_id = b.user_id
                 left join sys_user c on t.comment_user_id = c.user_id
            ${ew.getCustomSqlSegment}
        order by sortStatus asc, t.create_time desc
    </select>

    <select id="queryCommentPageList" resultType="org.dromara.business.domain.vo.CaseBaseInfoVo">
        select t.*,
               case
                   when t.comment_time is not null then TIMESTAMPDIFF(HOUR, check_time, comment_time) + 1
                   else TIMESTAMPDIFF(HOUR, check_time, now()) + 1 end as commentWaitingHours,
               a.nick_name                                             as userName,
               b.nick_name                                             as uploadUserName,
               c.nick_name                                             as commentUseName
        from case_base_info t
                 left join sys_user a on t.user_id = a.user_id
                 left join sys_user b on t.upload_user_id = b.user_id
                 left join sys_user c on t.comment_user_id = c.user_id ${ew.getCustomSqlSegment}
        and t.comment_status is not null
        order by t.comment_status desc, t.create_time desc
    </select>

    <select id="queryCommentAuditList" resultType="org.dromara.business.domain.vo.CaseBaseInfoVo">
        select t.*,
               a.nick_name as userName,
               b.nick_name as uploadUserName,
               c.nick_name as commentUseName
        from case_base_info t
                 left join sys_user a on t.user_id = a.user_id
                 left join sys_user b on t.upload_user_id = b.user_id
                 left join sys_user c on t.comment_user_id = c.user_id ${ew.getCustomSqlSegment}
          and t.comment_audit_status is not null
        order by t.comment_audit_status asc, t.create_time desc
    </select>

    <select id="queryAnalysisList" resultType="org.dromara.business.domain.vo.CaseBaseInfoVo">
        select t.*,
               a.nick_name as userName,
               b.nick_name as uploadUserName,
               c.nick_name as commentUseName
        from case_base_info t
                 left join sys_user a on t.user_id = a.user_id
                 left join sys_user b on t.upload_user_id = b.user_id
                 left join sys_user c on t.comment_user_id = c.user_id ${ew.getCustomSqlSegment}
          and t.case_analysis_status is not null
        order by t.case_analysis_status asc, t.create_time desc
    </select>

    <select id="commentAuditStatus" resultType="java.lang.String">
        select DISTINCT comment_audit_status
        from case_base_info
        where del_flag = '0'
    </select>

    <select id="caseAnalysisStatus" resultType="java.lang.String">
        select DISTINCT case_analysis_status
        from case_base_info
        where del_flag = '0'
    </select>

    <select id="commentStatus" resultType="java.lang.String">
        select DISTINCT comment_status
        from case_base_info
        where del_flag = '0'
    </select>

    <select id="commentDistributionExpert" resultType="org.dromara.system.domain.SysUser">
        SELECT user_id, nick_name, caseNumber
        FROM `sys_user` t
                 left join (select comment_user_id, count(1) caseNumber
                            from case_base_info
                            where comment_user_id is not null
                            GROUP BY comment_user_id) c on t.user_id = c.comment_user_id
        where t.login_identity = '2'
          and t.status = '0'
        ORDER BY caseNumber limit 1
    </select>
</mapper>
