<?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.ScrapApplyDetailsMapper">
    <resultMap type="com.bonus.material.scrap.domain.ScrapApplyDetails" id="ScrapApplyDetailsResult">
        <result property="id"    column="id"    />
        <result property="taskId"    column="task_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="maId"    column="ma_id"    />
        <result property="typeId"    column="type_id"    />
        <result property="scrapNum"    column="scrap_num"    />
        <result property="scrapSource"    column="scrap_source"    />
        <result property="scrapType"    column="scrap_type"    />
        <result property="status"    column="status"    />
        <result property="auditBy"    column="audit_by"    />
        <result property="auditTime"    column="audit_time"    />
        <result property="auditRemark"    column="audit_remark"    />
        <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="fileName"    column="file_name"    />
        <result property="fileUrl"    column="file_url"    />
    </resultMap>

    <sql id="selectScrapApplyDetailsVo">
        select id, task_id, parent_id, ma_id, type_id, scrap_num, scrap_source, scrap_type, status, audit_by, audit_time,
               audit_remark, create_by, create_time, update_by, update_time, remark, company_id, file_name, file_url
        from scrap_apply_details
    </sql>

    <select id="selectScrapApplyDetailsList" parameterType="com.bonus.material.scrap.domain.ScrapApplyDetails" resultMap="ScrapApplyDetailsResult">
        <include refid="selectScrapApplyDetailsVo"/>
        <where>  
            <if test="taskId != null "> and task_id = #{taskId}</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="maId != null "> and ma_id = #{maId}</if>
            <if test="typeId != null "> and type_id = #{typeId}</if>
            <if test="scrapNum != null "> and scrap_num = #{scrapNum}</if>
            <if test="scrapSource != null  and scrapSource != ''"> and scrap_source = #{scrapSource}</if>
            <if test="scrapType != null  and scrapType != ''"> and scrap_type = #{scrapType}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="auditBy != null "> and audit_by = #{auditBy}</if>
            <if test="auditTime != null "> and audit_time = #{auditTime}</if>
            <if test="auditRemark != null  and auditRemark != ''"> and audit_remark = #{auditRemark}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="fileName != null  and fileName != ''"> and file_name like concat('%', #{fileName}, '%')</if>
            <if test="fileUrl != null  and fileUrl != ''"> and file_url = #{fileUrl}</if>
        </where>
    </select>
    
    <select id="selectScrapApplyDetailsById" parameterType="Long" resultMap="ScrapApplyDetailsResult">
        <include refid="selectScrapApplyDetailsVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertScrapApplyDetails" parameterType="com.bonus.material.scrap.domain.ScrapApplyDetails" useGeneratedKeys="true" keyProperty="id">
        insert into scrap_apply_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="maId != null">ma_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="scrapNum != null">scrap_num,</if>
            <if test="scrapSource != null">scrap_source,</if>
            <if test="scrapType != null">scrap_type,</if>
            <if test="status != null">status,</if>
            <if test="auditBy != null">audit_by,</if>
            <if test="auditTime != null">audit_time,</if>
            <if test="auditRemark != null">audit_remark,</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="fileName != null">file_name,</if>
            <if test="fileUrl != null">file_url,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="maId != null">#{maId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="scrapNum != null">#{scrapNum},</if>
            <if test="scrapSource != null">#{scrapSource},</if>
            <if test="scrapType != null">#{scrapType},</if>
            <if test="status != null">#{status},</if>
            <if test="auditBy != null">#{auditBy},</if>
            <if test="auditTime != null">#{auditTime},</if>
            <if test="auditRemark != null">#{auditRemark},</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="fileName != null">#{fileName},</if>
            <if test="fileUrl != null">#{fileUrl},</if>
         </trim>
    </insert>

    <update id="updateScrapApplyDetails" parameterType="com.bonus.material.scrap.domain.ScrapApplyDetails">
        update scrap_apply_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="maId != null">ma_id = #{maId},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="scrapNum != null">scrap_num = #{scrapNum},</if>
            <if test="scrapSource != null">scrap_source = #{scrapSource},</if>
            <if test="scrapType != null">scrap_type = #{scrapType},</if>
            <if test="status != null">status = #{status},</if>
            <if test="auditBy != null">audit_by = #{auditBy},</if>
            <if test="auditTime != null">audit_time = #{auditTime},</if>
            <if test="auditRemark != null">audit_remark = #{auditRemark},</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="fileName != null">file_name = #{fileName},</if>
            <if test="fileUrl != null">file_url = #{fileUrl},</if>
        </trim>
        where id = #{id}
    </update>

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

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

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

    <select id="getGyoupRepairRecord" resultType="com.bonus.material.repair.domain.RepairRecord">
        select sum(scrap_num) as scrapNum,
        scrap_reason as scrapReason,
        scrap_type as scrapType,
        file_ids as fileIds
        from repair_apply_record rar
        where repair_type = '3'
        <if test="taskId != null and taskId != ''">
            and rar.task_id = #{taskId}
        </if>
        <if test="maId != null and maId != ''">
            and rar.ma_id = #{maId}
        </if>
        <if test="typeId != null and typeId != ''">
            and rar.type_id = #{typeId}
        </if>
        group by scrap_type
    </select>
</mapper>