<?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.liquidnet.service.smile.mapper.SmileVolunteersMapper">


    <resultMap id="volunteersExportDto" type="com.liquidnet.service.smile.entity.dto.VolunteersExportDto">
        <result column="img" property="img"/>
        <result column="mid" property="mid"/>
        <result column="name" property="name"/>
        <result column="id_card" property="idCard"/>
        <result column="sex" property="sex"/>
        <result column="status" property="status"/>
        <result column="school" property="school"/>
        <result column="school_address" property="schoolAddress"/>
        <result column="phone" property="phone"/>
        <result column="project_name" property="projectName"/>
        <result column="created_at" property="createdAt"/>
        <result column="specialty" property="specialty"/>
        <result column="specialty2" property="specialty2"/>
        <result column="team1Name" property="team1"/>
        <result column="team2Name" property="team2"/>
        <result column="team3Name" property="team3"/>
        <result column="teamAudit" property="teamAudit"/>
        <result column="introduce" property="introduce"/>
    </resultMap>

    <select id="expertVolunteer" parameterType="java.util.Map" resultMap="volunteersExportDto">
        select
        sv.mid,
        sv.img,
        sv.name,
        id_card,
        (CASE
        WHEN sex = 0 THEN '未知'
        WHEN sex = 1 THEN '男'
        WHEN sex = 2 THEN '女'
        ELSE '其他' END) as 'sex',
        (CASE
        WHEN sv.status = 0 THEN '待审核'
        WHEN sv.status = 1 THEN '审核通过'
        WHEN sv.status = 2 THEN '审核拒绝'
        ELSE '其他' END) as 'status',
        school,
        school_address,
        phone,
        svp.title as 'project_name',
        sv.created_at,
        specialty,
        specialty2,
        svt1.name as 'team1Name',
        svt2.name as 'team2Name',
        svt3.name as 'team3Name',
        t.name as 'teamAudit',
        sv.introduce
        from smile_volunteers as sv
        inner join smile_volunteers_project as svp on svp.project_id = sv.project_id
        left join smile_volunteers_team as svt1 on svt1.team_id = sv.team_id1
        left join smile_volunteers_team as svt2 on svt2.team_id = sv.team_id2
        left join smile_volunteers_team as svt3 on svt3.team_id = sv.team_id3
        left join (select a.uid,b.name,a.project_id from smile_volunteers_team_relation as a inner join smile_volunteers_team as b on a.team_id = b.team_id) as t
        on sv.uid = t.uid and sv.project_id = t.project_id
        <where>
            <if test="title!=''">
                AND svp.title like concat('%',#{title},'%')
            </if>
            <if test="projectId!=''">
                AND svp.project_id = #{projectId}
            </if>

            <if test="name!=''">
                AND sv.name like concat('%',#{name},'%')
            </if>

            <if test="phone!=''">
                AND phone like concat('%',#{phone},'%')
            </if>
            <if test="status!='-1'">
                AND sv.status = #{status}
            </if>
            <if test="sex!='-1'">
                AND sex = #{sex}
            </if>
            <if test="team!=''">
                AND (svt1.name = #{team} OR svt2.name = #{team} OR svt3.name = #{team})
            </if>
            <if test="teamAudit!=''">
                AND t.name = #{teamAudit}
            </if>
        </where>
        ORDER BY sv.created_at desc
    </select>

</mapper>
