<?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.repair.mapper.RepairCollaborateRecordMapper">

    <resultMap type="com.repair.model.vo.CollaborateRecordVo" id="repairCollaborateRecordMap">
        <result property="id" column="id"/>
        <result property="recordId" column="record_id"/>
        <result property="state" column="state"/>
    </resultMap>

    <select id="queryCollaborateRecordList" resultType="com.repair.model.vo.CollaborateRecordVo" resultMap="repairCollaborateRecordMap">
        select id,record_id,'转单' as state from repair_transfer_record
        <where>
            and deleted = 0 and approval_statu = 2
            <if test="recordIds != null and recordIds.size() > 0">
                and record_id in
                <foreach collection="recordIds" item="recordId" index="index" open="(" close=")" separator=",">
                    ${recordId}
                </foreach>
            </if>
        </where>
        UNION ALL
        select id,record_id,'协作' as state from repair_collaborate_record
        <where>
            and deleted = 0 and approval_statu = 2
            <if test="recordIds != null and recordIds.size() > 0">
                and record_id in
                <foreach collection="recordIds" item="recordId" index="index" open="(" close=")" separator=",">
                    ${recordId}
                </foreach>
            </if>
        </where>
        UNION ALL
        select id,record_id,'转线下' as state from repair_offline_record
        <where>
            and deleted = 0 and approval_statu = 2
            <if test="recordIds != null and recordIds.size() > 0">
                and record_id in
                <foreach collection="recordIds" item="recordId" index="index" open="(" close=")" separator=",">
                    ${recordId}
                </foreach>
            </if>
        </where>
    </select>

    <resultMap type="com.repair.model.vo.CollaborateDetailVo" id="repairCollaborateDetailMap">
        <result property="id" column="id"/>
        <result property="userName" column="user_name"/>
        <result property="collaboratorName" column="collaborator_name"/>
        <result property="voice" column="voice"/>
        <result property="voiceLength" column="voice_length"/>
        <result property="remark" column="remark"/>
    </resultMap>
    <select id="queryCollaborateDetail" resultType="com.repair.model.vo.CollaborateDetailVo" resultMap="repairCollaborateDetailMap">
        select rcr.id,rcr.voice_length,rus.user_name,GROUP_CONCAT(CONCAT(ru.id,'-',ru.user_name)) as collaborator_name,rcr.voice,rcr.remark from repair_collaborate_record rcr
        left join repair_user ru on ru.deleted = 0 and find_in_set(ru.id,rcr.collaborator)
        left join repair_user rus on rus.deleted = 0 and rus.id = rcr.user_id
        where rcr.deleted = 0 and rcr.approval_statu = 2 and rcr.record_id = #{recordId}
        group by rcr.id
    </select>
</mapper>
