<?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.bonus.sgzb.material.mapper.RepairRecordMapper">
    <select id="getRepairRecordList" resultType="com.bonus.sgzb.material.domain.RepairRecord">
        SELECT tt.`code`,
               mt2.type_name as typeName,
               mt.type_name  as typeModelName,
               CASE rar.repair_type
                   WHEN 1 THEN
                       mpt2.pa_name
                   ELSE
                       rar.part_name
                   END          partName,
               CASE rar.repair_type
                   WHEN 1 THEN
                       rpd.part_num
                   ELSE
                       rar.part_num
                   END          partNum,
               CASE rar.repair_type
                   WHEN 1 THEN
                       rpd.part_cost
                   ELSE
                       rar.part_price
                   END          partPrice,
               bpl.lot_name  as proName,
               bui.unit_name as unitName

        FROM repair_apply_record rar
                 LEFT JOIN tm_task tt on tt.task_id = rar.task_id
                 LEFT JOIN ma_type mt on mt.type_id = rar.type_id
                 LEFT JOIN ma_type mt2 on mt2.type_id = mt.parent_id
                 LEFT JOIN repair_part_details rpd on rpd.task_id = rar.task_id
            and rpd.type_id = rar.type_id
            and rar.repair_type = '1'
                 LEFT JOIN ma_part_type mpt on mpt.pa_id = rpd.part_id
                 LEFT JOIN ma_part_type mpt2 on mpt2.pa_id = mpt.parent_id
                 LEFT JOIN repair_apply_details rad on rad.task_id = rar.task_id and rad.type_id = rar.type_id
                 LEFT JOIN back_apply_info bai on bai.id = rad.back_id
                 LEFT JOIN tm_task_agreement tta on tta.task_id = bai.task_id
                 LEFT JOIN bm_agreement_info baif on baif.agreement_id = tta.agreement_id
                 LEFT JOIN bm_project_lot bpl on bpl.lot_id = baif.project_id
                 LEFT JOIN bm_unit_info bui on bui.unit_id = baif.unit_id
        where 1 = 1
        <if test="keyWord != null and keyWord != ''">
            and (tt.`code` like concat('%',#{keyWord},'%') or
            mt2.type_name like concat('%',#{keyWord},'%') or
            mt.type_name like concat('%',#{keyWord},'%'))
        </if>
        <if test="unitId != null">
            and bui.unit_id = #{unitId}
        </if>
        <if test="proId != null">
            and bpl.lot_id = #{proId}
        </if>
    </select>
</mapper>