<?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.repair.mapper.RepairApplyDetailsMapper">
    <resultMap type="com.bonus.material.repair.domain.RepairApplyDetails" id="RepairApplyDetailsResult">
        <result property="id"    column="id"    />
        <result property="taskId"    column="task_id"    />
        <result property="maId"    column="ma_id"    />
        <result property="typeId"    column="type_id"    />
        <result property="repairNum"    column="repair_num"    />
        <result property="repairedNum"    column="repaired_num"    />
        <result property="scrapNum"    column="scrap_num"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="companyId"    column="company_id"    />
        <result property="backId"    column="back_id"    />
        <result property="repairer"    column="repairer"    />
    </resultMap>

    <sql id="selectRepairApplyDetailsVo">
        select id, task_id, ma_id, type_id, repair_num, repaired_num, scrap_num, status, create_by, create_time,
               update_by, update_time, remark, company_id, back_id, repairer
        from repair_apply_details
    </sql>

    <select id="selectRepairApplyDetailsList" parameterType="com.bonus.material.repair.domain.RepairApplyDetails" resultMap="RepairApplyDetailsResult">
        <include refid="selectRepairApplyDetailsVo"/>
        <where>  
            <if test="taskId != null "> and task_id = #{taskId}</if>
            <if test="maId != null "> and ma_id = #{maId}</if>
            <if test="typeId != null "> and type_id = #{typeId}</if>
            <if test="repairNum != null "> and repair_num = #{repairNum}</if>
            <if test="repairedNum != null "> and repaired_num = #{repairedNum}</if>
            <if test="scrapNum != null "> and scrap_num = #{scrapNum}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="backId != null "> and back_id = #{backId}</if>
            <if test="repairer != null  and repairer != ''"> and repairer = #{repairer}</if>
        </where>
    </select>
    
    <select id="selectRepairApplyDetailsById" parameterType="Long" resultMap="RepairApplyDetailsResult">
        <include refid="selectRepairApplyDetailsVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertRepairApplyDetails" parameterType="com.bonus.material.repair.domain.RepairApplyDetails" useGeneratedKeys="true" keyProperty="id">
        insert into repair_apply_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="maId != null">ma_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="repairNum != null">repair_num,</if>
            <if test="repairedNum != null">repaired_num,</if>
            <if test="scrapNum != null">scrap_num,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
            <if test="backId != null">back_id,</if>
            <if test="repairer != null">repairer,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="maId != null">#{maId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="repairNum != null">#{repairNum},</if>
            <if test="repairedNum != null">#{repairedNum},</if>
            <if test="scrapNum != null">#{scrapNum},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="backId != null">#{backId},</if>
            <if test="repairer != null">#{repairer},</if>
         </trim>
    </insert>

    <update id="updateRepairApplyDetails" parameterType="com.bonus.material.repair.domain.RepairApplyDetails">
        update repair_apply_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="maId != null">ma_id = #{maId},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="repairNum != null">repair_num = #{repairNum},</if>
            <if test="repairedNum != null">repaired_num = #{repairedNum},</if>
            <if test="scrapNum != null">scrap_num = #{scrapNum},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="backId != null">back_id = #{backId},</if>
            <if test="repairer != null">repairer = #{repairer},</if>
            <if test="thisRepairedNum != null">this_repaired_num = #{thisRepairedNum},</if>
            <if test="thisScrapNum != null">this_scrap_num = #{thisScrapNum},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateRepairApplyDetailsAfterReject">
        update repair_apply_details
        set repaired_num = repaired_num - #{rejectRepairedNum},
            scrap_num = scrap_num - #{rejectScrapNum},
            update_time = NOW()
        where id = #{id}
    </update>

    <update id="updateStatus">
        update repair_apply_details
        set status = #{status},
            update_time = NOW()
        where id = #{repairId}
    </update>
    <update id="updateRepairApplyDetailsAfterAudit">
        update repair_apply_record
        set status = 1,
            update_time = NOW()
        where task_id = #{taskId}
    </update>

    <select id="getRepairDetailsWhichNotSent" resultType="com.bonus.material.repair.domain.RepairTaskDetails">
        SELECT
        rad.id AS id,
        rad.ma_id AS maId,
        rad.type_id AS typeId,
        IFNULL( rad.repair_num, 0 ) AS repairNum,
        IFNULL( rad.repaired_num, 0 ) AS repairedNum,
        IFNULL( rad.this_repaired_num, 0 ) AS thisRepairedNum,
        IFNULL( rad.scrap_num, 0 ) AS scrapNum,
        IFNULL( rad.this_scrap_num, 0 ) AS thisScrapNum,
        rad.company_id AS companyId,
        tt.task_id AS taskId,
        tt.CODE AS taskCode,
        rad.back_id AS backId,
        rad.STATUS AS STATUS,
        rad.LEVEL AS LEVEL,
        tt.CODE AS taskCode
        FROM
        repair_apply_details rad
        LEFT JOIN tm_task tt ON rad.task_id = tt.task_id
        WHERE
        rad.task_id = #{taskId}
        AND rad.is_ds =0
<!--          and rad.status = '0'-->
    </select>

    <select id="getList" resultType="com.bonus.material.repair.domain.RepairTaskDetails">
        select rad.id,
               rad.ma_id as maId,
               rad.type_id as typeId,
               rad.repair_num as repairNum,
               rad.repaired_num as repairedNum,
               rad.this_repaired_num as thisRepairedNum,
               rad.scrap_num as scrapNum,
               rad.this_scrap_num as thisScrapNum,
               rad.company_id as companyId,
               rad.back_id as backId,
               rad.status as status,
               rad.level as level,
               rad.parent_id as parentId
        from repair_apply_details rad
        where rad.id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="getRepairDetails" resultType="com.bonus.material.repair.domain.RepairTaskDetails">
        SELECT
            rad.id AS id,
            rad.ma_id AS maId,
            rad.type_id AS typeId,
            rad.repair_num AS repairNum,
            rad.this_repaired_num,
            rad.this_scrap_num,
            rad.repaired_num AS repairedNum,
            rad.this_repaired_num AS thisRepairedNum,
            rad.scrap_num AS scrapNum,
            rad.this_scrap_num AS thisScrapNum,
            rad.company_id AS companyId,
            rad.back_id AS backId,
            rad.STATUS AS STATUS ,
            rad.`level` as level,
            rd.repair_num as preRepairNum,
            rd.repaired_num as preRepairedNum,
            rd.scrap_num as preScrapNum,
            rd.parent_id as parentId,
            rd.task_id as preTaskId
        FROM
            repair_apply_details rad
            LEFT JOIN repair_apply_details rd on rad.parent_id = rd.id
        WHERE
            rad.task_id = #{oldWxTaskId}
    </select>

    <select id="selectById" resultType="com.bonus.material.repair.domain.RepairAuditDetails">
        select task_id as taskId,
               type_id as typeId,
               ma_id as maId,
               level as level
               from repair_apply_details
        where id = #{repairId}
    </select>

    <select id="getMaxLevel" resultType="com.bonus.material.repair.domain.RepairTaskDetails">
        SELECT
            rad.id AS id,
            rad.ma_id AS maId,
            rad.type_id AS typeId,
            rad.repair_num AS repairNum,
            rad.this_repaired_num,
            rad.this_scrap_num,
            rad.repaired_num AS repairedNum,
            rad.this_repaired_num AS thisRepairedNum,
            rad.scrap_num AS scrapNum,
            rad.this_scrap_num AS thisScrapNum,
            rad.company_id AS companyId,
            tt.task_id AS taskId,
            tt.CODE AS taskCode,
            rad.back_id AS backId,
            rad.STATUS AS STATUS,
            rad.LEVEL AS LEVEL,
            tt.CODE AS taskCode
        FROM
            repair_apply_details rad
                LEFT JOIN tm_task tt ON rad.task_id = tt.task_id
        WHERE
            rad.back_id = #{backId}
          AND rad.is_ds = 0
        ORDER BY
        rad.`level` DESC
        LIMIT 1
    </select>

    <delete id="deleteRepairApplyDetailsById" parameterType="Long">
        delete from repair_apply_details where id = #{id}
    </delete>

    <delete id="deleteRepairApplyDetailsByTaskIdAndStatus">
        delete from repair_apply_details where task_id = #{taskId} and status = #{status}
    </delete>

    <delete id="deleteRepairApplyDetailsByIds" parameterType="String">
        delete from repair_apply_details where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectRepairCodeByIds" resultType="com.bonus.material.repair.domain.RepairAuditDetails">
        select
               id as repairId,
               task_id as taskId,
               type_id as typeId,
               ma_id as maId,
               level as level
        from repair_apply_details
        where id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <update id="updateRepairApplyDetailsAfterAuditBatch">
        update repair_apply_record
        set status = 1,
            update_time = NOW()
        where task_id in
        <foreach item="obj" collection="list" open="(" separator="," close=")">
            #{obj.taskId}
        </foreach>
    </update>
</mapper>