<?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="com.hz.pm.api.meeting.mapper.MeetingExpertMapper">

    <select id="selectByExpertIdAndStatus"
            resultType="com.hz.pm.api.meeting.entity.dto.ExpertInvitedRecordDTO">
        SELECT em.meeting_id meetingId, em.status, em.ID  meetingExpertId
        FROM (SELECT ROW_NUMBER() OVER ( PARTITION BY meeting_id ORDER BY update_on DESC ) rowNumber, ID, expert_id,
        status, meeting_id
        FROM meeting_expert
        where expert_id = #{expertId}
        <if test="meetingIds != null and meetingIds.size > 0">
            and meeting_id
            <foreach collection="meetingIds" open="in (" close=")" item="item">#{item}</foreach>
        </if>
        ) em
        WHERE rowNumber = 1
        <if test="status == 3">
            AND em.status = 6
        </if>
        <if test="status == null">
            and em.status in (3,6)
        </if>
    </select>

    <select id="selectByExpertIdAndTime"
            resultType="com.hz.pm.api.meeting.entity.dto.ExpertInvitedRecordDTO">
        SELECT em.meeting_id meetingId, em.status, em.ID  meetingExpertId, em.create_on createOn
        FROM (SELECT ROW_NUMBER() OVER ( PARTITION BY me.meeting_id ORDER BY me.update_on DESC ) rowNumber, me.ID, me.expert_id,
        me.status, me.meeting_id, m.create_on
        FROM meeting_expert me inner join meeting m on m.id = me.meeting_id
        where expert_id = #{expertId}
        <if test="sTime != null and eTime != null">
            and m.start_time between #{sTime} and #{eTime}
        </if>
        ) em
        WHERE rowNumber = 1
    </select>

    <select id="selectExpertByStatusAndMeetingId"
            resultType="com.hz.pm.api.meeting.entity.domain.MeetingExpert">
        SELECT * FROM (SELECT ROW_NUMBER() OVER ( PARTITION BY expert_id ORDER BY update_on DESC ) rowNumber,
         ID, expert_id, status, meeting_id, invite_type, mobile, expert_name, update_on FROM meeting_expert
        where meeting_id = #{meetingId}
        <if test="inviteType != null">
            and invite_type = #{inviteType}
        </if>
        ) em
        WHERE rowNumber = 1
        <if test="status != null">
            and status = #{status}
        </if>
    </select>

    <select id="selectExpertByStatusAndMeetingIds"
            resultType="com.hz.pm.api.meeting.entity.domain.MeetingExpert">
        SELECT * FROM (SELECT ROW_NUMBER() OVER ( PARTITION BY expert_id, meeting_id ORDER BY update_on DESC ) rowNumber,
        ID, expert_id, status, meeting_id, invite_type, mobile, expert_name, update_on FROM meeting_expert
        where meeting_id <foreach collection="meetingIds" separator="," close=")" open=" in (" item="item">
        #{item}</foreach>) em
        WHERE rowNumber = 1
        <if test="status != null">
            and status = #{status}
        </if>
    </select>

    <select id="listAgreeExpertIdByRecentDaysAndAgreeCount" resultType="long">
        SELECT expert_id FROM (SELECT ROW_NUMBER() OVER ( PARTITION BY expert_id, meeting_id ORDER BY update_on DESC )
        rowNumber,expert_id, status FROM meeting_expert
        where meeting_id in (select id from meeting m where m.status = 1
        and ((m.start_time &gt;= #{startTime} and m.start_time &lt; #{endTime})
            or (m.end_time &gt;= #{startTime} and m.end_time &lt; #{endTime})) )) em
        WHERE rowNumber = 1 and status = 3 group by expert_id having count(1) &gt;= #{agreeCount}
    </select>

    <select id="countExpertByStatusAndMeetingId"
            resultType="int">
        SELECT count(1) FROM (SELECT ROW_NUMBER() OVER ( PARTITION BY expert_id, meeting_id ORDER BY update_on DESC )
        rowNumber, status FROM meeting_expert
        where meeting_id = #{meetingId}
        <if test="inviteType != null">
            and invite_type = #{inviteType}
        </if>
        ) em
        WHERE rowNumber = 1
        <if test="status != null">
            and status = #{status}
        </if>
    </select>

    <select id="listExpertLastByMeetingIds" resultType="com.hz.pm.api.meeting.entity.domain.MeetingExpert">
        SELECT * FROM (SELECT ROW_NUMBER() OVER ( PARTITION BY expert_id, meeting_id ORDER BY update_on DESC ) rowNumber,
        ID, expert_id, status, meeting_id, invite_type, mobile, expert_name, update_on, rule_id, create_on FROM meeting_expert
        where meeting_id <foreach collection="meetingIds" separator="," close=")" open=" in (" item="item">
        #{item}</foreach>) em WHERE rowNumber = 1
    </select>

    <sql id="reviewedByHeadman">
        <if test="p.reviewed">
            exists(select 1 from nd_expert_review ner where ner.meeting_id = m.id and ner.project_id = np.id and ner.create_by =
            me.expert_id and is_final = 1)
        </if>
        <if test="!p.reviewed">
            not exists(select 1 from nd_expert_review ner where ner.meeting_id = m.id and ner.project_id = np.id and ner.create_by =
            me.expert_id and is_final = 1)
        </if>
    </sql>

    <sql id="reviewedByNotHeadman">
        <if test="p.reviewed">
            exists(select 1 from nd_expert_review ner where ner.meeting_id = m.id and ner.project_id = np.id and ner.create_by =
            me.expert_id)
        </if>
        <if test="!p.reviewed">
            not exists(select 1 from nd_expert_review ner where ner.meeting_id = m.id and ner.project_id = np.id and ner.create_by =
            me.expert_id)
        </if>
    </sql>

    <select id="pageReviewProjectList" resultType="com.hz.pm.api.meeting.entity.dto.ReviewProjectDTO">
        select mip.project_id,np.project_code, np.project_name, np.project_type, np.project_year, np.build_org_name, m.type review_type,
        np.biz_domain, np.declare_amount declared_amount, mip.meeting_id, m.type meetingType, m.start_time reviewTime,m.end_time endReviewTime,
        m.connecter, m.contact, me.is_headman, (select count(1) from nd_expert_review ner where ner.meeting_id = m.id and ner.project_id = np.id
        and ner.create_by = me.expert_id) reviewed
        from nd_project np inner join meeting_inner_project mip on mip.project_id = np.id
        inner join meeting m on m.id = mip.meeting_id
        inner join meeting_expert me on m.id = me.meeting_id
        where m.is_inner_project = 1 and m.status != 3 and me.status = 3 and m.confirmed_roster = 1
        <if test="p.reviewed != null">
            and if(me.is_headman,'<include refid="reviewedByHeadman"/>','<include refid="reviewedByNotHeadman"/>')
        </if>
        and me.expert_id = #{p.userId}
        <if test="p.projectName != null and p.projectName.length > 0">
            and np.project_name like concat('%', #{p.projectName}, '%')
        </if>
        <if test="p.buildOrgName != null and p.buildOrgName.length > 0">
            and np.build_org_name like concat('%', #{p.buildOrgName}, '%')
        </if>
        <if test="p.reviewTimeMin != null">
            and m.start_time &gt;= #{p.reviewTimeMin}
        </if>
        <if test="p.reviewTimeMax != null">
            and m.start_time &lt;= #{p.reviewTimeMax}
        </if>
        order by m.start_time desc
    </select>

</mapper>
