<?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.material.scrap.mapper.ScrapReasonMapper">

    <select id="levelTwolist"  resultType="com.bonus.material.scrap.domain.ScrapReason">
        select type_id as parentId,type_name as parentName
        from ma_type mt
        where level = 2 and del_flag = 0
    </select>

    <select id="levelThreelist"  resultType="com.bonus.material.scrap.domain.ScrapReason">
        select type_id as typeId,type_name as typeName
        from ma_type mt
        where parent_id = #{parentId} and del_flag = 0
    </select>



    <select id="selectScrapReasonList" resultType="com.bonus.material.scrap.domain.ScrapReason">
        select
        sc.id as id,sc.type_id as typeId,mt.type_name as typeName,mt1.type_id as parentId,mt1.type_name as parentName,
        sc.update_time as updateTime,su.nick_name as updateBy,sc.reason as reason
        from scrap_reason sc
        left join ma_type mt on sc.type_id = mt.type_id and mt.del_flag = 0
        left join ma_type mt1 on mt.parent_id = mt1.type_id and mt1.del_flag = 0
        left join sys_user su on sc.update_by  = su.user_id and su.del_flag = 0
        where  sc.del_flag = 0

        <if test="keyWord != null  and keyWord != ''">
            AND (
            mt1.type_name LIKE CONCAT('%',#{keyWord},'%')
            OR mt.type_name LIKE CONCAT('%',#{keyWord},'%')
            )
        </if>
    </select>


        <insert id="insertScrapReasonInfo">
            insert into scrap_reason
            (
            <if test="typeId != null">
                type_id,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="createBy != null and createBy != ''">
                create_by,
            </if>
            <if test="updateBy != null and updateBy != ''">
                update_by,
            </if>
            <if test="remark != null and remark != ''">
                remark,
            </if>
            <if test="reason != null and reason != ''">
                reason,
            </if>
            del_flag
            )
            values (
            <if test="typeId != null">
                #{typeId},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="createBy != null and createBy != ''">
                #{createBy},
            </if>
            <if test="updateBy != null and updateBy != ''">
                #{updateBy},
            </if>
            <if test="remark != null and remark != ''">
                #{remark},
            </if>
            <if test="reason != null and reason != ''">
                #{reason},
            </if>
            0
            )
        </insert>

    <select id="selectScrapReasonById" resultType="com.bonus.material.scrap.domain.ScrapReason">
        select
        sc.id as id,sc.type_id as typeId,mt.type_name as typeName,mt1.type_id as parentId,mt1.type_name as parentName,
        sc.update_time as updateTime,sc.update_by as updateBy,sc.reason as reason
        from scrap_reason sc
        left join ma_type mt on sc.type_id = mt.type_id and mt.del_flag = 0
        left join ma_type mt1 on mt.parent_id = mt1.type_id and mt1.del_flag = 0
        where  sc.del_flag = 0

        <if test="id != null  ">
            AND sc.id = #{id}
        </if>
    </select>

    <select id="getList" resultType="com.bonus.material.scrap.domain.ScrapReason">
        SELECT
            sc.id AS id,
            sc.type_id AS typeId,
            mt.type_name AS typeName,
            mt1.type_id AS parentId,
            mt1.type_name AS parentName,
            sc.update_time AS updateTime,
            sc.reason AS reason
        FROM
            scrap_reason sc
                LEFT JOIN ma_type mt ON sc.type_id = mt.type_id
                AND mt.del_flag = 0
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
                AND mt1.del_flag = 0
                LEFT JOIN ma_type mt2 ON mt2.parent_id = mt.type_id
                AND mt2.del_flag = 0
        WHERE
            sc.del_flag = 0
          <if test="typeId != null">
              AND mt2.type_id = #{typeId}
          </if>
        GROUP BY sc.id
    </select>

    <select id="getRepairRecord" resultType="com.bonus.material.repair.domain.RepairPartDetails">
        SELECT
            task_id as taskId,
            ma_id as maId,
            scrap_id as scrapId,
            scrap_reason as scrapReason
        FROM
            repair_apply_record
        WHERE
            scrap_id = #{id}
    </select>

    <update id="updateScrapReasonInfo">
        update scrap_reason
        set type_id = #{typeId},
            update_by = #{updateBy},
            update_time = #{updateTime},
            reason = #{reason}
        where id = #{id} and del_flag = 0
    </update>

    <update id="deleteScrapReasonInfo">
        update scrap_reason
        set del_flag = 1
        where id = #{id} and del_flag = 0
    </update>

    <update id="updateRepairRecord">
        update
            repair_apply_record
        set
            scrap_reason = #{scrapReason}
        where scrap_id = #{id}
    </update>


    <!--    <delete id="deleteScrapApplyDetailsById" parameterType="Long">-->
<!--        delete from scrap_apply_details where id = #{id}-->
<!--    </delete>-->




</mapper>