<?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.ScrapApplyDetailsMapper">
    
    <resultMap type="com.bonus.sgzb.material.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="fileUrl"    column="file_url"    />
        <result property="companyId"    column="company_id"    />
        <result property="fileName"    column="file_name"    />
    </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, file_url, company_id, file_name from scrap_apply_details
    </sql>

    <select id="selectScrapApplyDetailsList" parameterType="com.bonus.sgzb.material.domain.ScrapApplyDetails" resultType="com.bonus.sgzb.material.vo.ScrapApplyDetailsVO">
        SELECT
        tk.CODE scrapNum,
        tk.task_status taskStatus,
        dic.NAME taskStatusName,
        bui.unit_name unitName,
        bpl.lot_name projectName,
        su.nick_name createBy,
        tk.create_time createTime,
        tk.remark,
        tk1.CODE repairNum,
        sad.scrap_num as scrapNum2,
        sad.scrap_source as scrapSource,
        sad.file_url as fileUrl,
        tk.task_id taskId
        FROM
        tm_task tk
        LEFT JOIN tm_task_agreement tta ON tk.task_id = tta.task_id
        LEFT JOIN bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
        LEFT JOIN bm_project_lot bpl ON bai.project_id = bpl.lot_id
        LEFT JOIN bm_unit_info bui ON bai.unit_id = bui.unit_id
        LEFT JOIN scrap_apply_details sad ON tk.task_id = sad.task_id
       /* LEFT JOIN repair_audit_details rad ON sad.parent_id = rad.id
        LEFT JOIN repair_apply_details rad2 ON rad.repair_id = rad2.id*/
        LEFT JOIN scrap_apply_details sad1 ON sad.parent_id = sad1.id
        LEFT JOIN tm_task tk1 ON sad1.task_id = tk1.task_id
        LEFT JOIN (
        SELECT
        id,
        p_id,
        NAME
        FROM
        sys_dic
        WHERE
        p_id IN ( SELECT id FROM sys_dic WHERE VALUE = 'bf_task' )) dic ON tk.task_status = dic.id
        LEFT JOIN ma_type mt ON sad.type_id = mt.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        LEFT JOIN sys_user su ON su.user_id = tk.create_by
        WHERE
        sad.scrap_source IN ( '1', '2' )
        AND tk.task_type = 57
        <if test="keyword != null and keyword != ''">
            AND (locate(#{keyword}, tk.create_by) > 0
            or locate(#{keyword}, tk.CODE) > 0
            or locate(#{keyword}, bui.unit_name) > 0
            or locate(#{keyword}, bpl.lot_name) > 0
            or locate(#{keyword}, tk1.CODE) > 0)
        </if>
        <if test="backUnit != null and backUnit != ''">
            and bui.unit_id = #{backUnit}
        </if>
        <if test="repairStatus != null and repairStatus != ''">
            and tk.task_status = #{repairStatus}
        </if>
        <if test="backPro != null and backPro != ''">
            and bpl.lot_id = #{backPro}
        </if>
        <if test="type != null and type != ''">
            and mt1.type_id = #{type}
        </if>
        <if test="backCode != null and backCode != ''">
            and locate(#{backCode}, tk.code) > 0
        </if>
        <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''">
            and tk.create_time between #{params.beginTime} and #{params.endTime}
        </if>
        GROUP BY tk.CODE
        order by tk.create_time desc
    </select>

    <select id="exportScrapApplyDetailsList" resultType="com.bonus.sgzb.material.vo.ScrapApplyDetailsVO">
        SELECT
            tk.CODE scrapNum,
            tk.task_status taskStatus,
            dic.NAME taskStatusName,
            bui.unit_name unitName,
            bpl.lot_name projectName,
            su.nick_name createBy,
            tk.create_time createTime,
            tk.remark,
            tk1.CODE repairNum,
            tk.task_id taskId,
            mt1.type_name AS type,
            mt.type_name AS typeName,
            mma.ma_code AS maCode,
            sad.scrap_num AS scrapNum2
        FROM
            tm_task tk
                LEFT JOIN tm_task_agreement tta ON tk.task_id = tta.task_id
                LEFT JOIN bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
                LEFT JOIN bm_project_lot bpl ON bai.project_id = bpl.lot_id
                LEFT JOIN bm_unit_info bui ON bai.unit_id = bui.unit_id
                LEFT JOIN scrap_apply_details sad ON tk.task_id = sad.task_id
                LEFT JOIN tm_task tk1 ON sad.parent_id = tk1.task_id
                LEFT JOIN (
                SELECT
                    id,
                    p_id,
                    NAME
                FROM
                    sys_dic
                WHERE
                    p_id IN ( SELECT id FROM sys_dic WHERE VALUE = 'bf_task' )) dic ON tk.task_status = dic.id
                LEFT JOIN ma_type mt ON sad.type_id = mt.type_id
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
                left join ma_machine mma on sad.ma_id= mma.ma_id
                LEFT JOIN sys_user su ON su.user_id = tk.create_by
        WHERE
            sad.scrap_source IN ( '1', '2' )
          AND tk.task_type = 57
        <if test="keyword != null and keyword != ''">
            AND (locate(#{keyword}, tk.create_by) > 0
            or locate(#{keyword}, tk.CODE) > 0
            or locate(#{keyword}, bui.unit_name) > 0
            or locate(#{keyword}, bpl.lot_name) > 0
            or locate(#{keyword}, tk1.CODE) > 0)
        </if>
        <if test="backUnit != null and backUnit != ''">
            and bui.unit_id = #{backUnit}
        </if>
        <if test="repairStatus != null and repairStatus != ''">
            and tk.task_status = #{repairStatus}
        </if>
        <if test="backPro != null and backPro != ''">
            and bpl.lot_id = #{backPro}
        </if>
        <if test="type != null and type != ''">
            and mt1.type_id = #{type}
        </if>
        <if test="backCode != null and backCode != ''">
            and locate(#{backCode}, tk.code) > 0
        </if>
        <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''">
            and tk.create_time between #{params.beginTime} and #{params.endTime}
        </if>
        GROUP BY tk.CODE
        order by tk.create_time desc
    </select>
    
    <select id="selectScrapApplyDetailsById" parameterType="Long" resultMap="ScrapApplyDetailsResult">
        <include refid="selectScrapApplyDetailsVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertScrapApplyDetails" parameterType="com.bonus.sgzb.material.domain.ScrapApplyDetails">
        insert into scrap_apply_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <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="fileUrl != null">file_url,</if>
            <if test="companyId != null">company_id,</if>
            <if test="fileName != null">file_name,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <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="fileUrl != null">#{fileUrl},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="fileName != null">#{fileName},</if>
         </trim>
    </insert>
    <insert id="addBackDetails">
        insert into back_apply_details (parent_id,type_id,pre_num,audit_num,status,create_by,create_time,update_by,update_time,company_id)
        values (#{parentId},#{typeId},#{scrapNum},#{scrapNum},#{status},#{createBy},now(),#{updateBy},now(),#{companyId});
    </insert>
    <insert id="addBackInfo" useGeneratedKeys="true" keyProperty="id">
        insert into back_apply_info (code,task_id,back_person,phone,direct_audit_by,direct_audit_time,direct_audit_remark,create_by,create_time,update_by,update_time,company_id,back_time,status)
        values (#{code},#{taskId},#{backPerson},#{phone},#{directAuditBy},#{directAuditTime},#{directAuditRemark},#{createBy},now(),#{updateBy},now(),#{companyId},#{backTime},#{status});
    </insert>
    <insert id="addRepaieAudit">
        insert into repair_audit_details (task_id,repair_id,ma_id,type_id,repair_num,repaired_num,scrap_num,status,create_by,create_time,update_by,update_time,company_id)
        values (#{taskId},#{repairId},#{maId},#{typeId},#{repairNum},#{repairedNum},#{scrapNum},#{status},#{createBy},now(),#{updateBy},now(),#{companyId});
    </insert>

    <update id="updateScrapApplyDetails" parameterType="com.bonus.sgzb.material.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="fileUrl != null">file_url = #{fileUrl},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="fileName != null">file_name = #{fileName},</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="selectScrapAuditList" resultMap="ScrapApplyDetailsResult">
        select sad.* ,mt.type_name specificationType, mt1.type_name machineTypeName, mma.ma_code maCode
        from scrap_apply_details sad
         left join ma_type mt on sad.type_id = mt.type_id
         left join ma_type mt1 on mt.parent_id = mt1.type_id
        left join ma_machine mma on sad.ma_id= mma.ma_id
        where sad.task_id = #{taskId}
        <if test="keyword != null and keyword != ''">
            AND (locate(#{keyword}, mma.ma_code) > 0
            or locate(#{keyword}, mt.type_name) > 0
            or locate(#{keyword}, mt1.type_name) > 0)
        </if>
        <if test="type != null and type != ''">
            AND mt.type_id = #{type}
        </if>
        <if test="typeName != null and typeName != ''">
            AND mt1.type_id = #{typeName}
        </if>
        <if test="status != null and status != ''">
            AND sad.status = #{status}
        </if>
        order by sad.create_time desc
    </select>

    <select id="selectScrapDetailsByTaskId" resultMap="ScrapApplyDetailsResult">
        <include refid="selectScrapApplyDetailsVo"/>
        where task_id = #{taskId}
    </select>

    <update id="updateMachine">
        update ma_machine set ma_status = 22 where ma_id = #{maId}
    </update>
    <update id="updateStatus">
        update scrap_apply_details
        set audit_by = #{auditBy},
            audit_time = now(),
            status = #{status},
            update_by = #{updateBy},
            update_time = now(),
            audit_remark = #{auditRemark}
        where id = #{id}
    </update>

    <select id="selectTypeNameByTaskId" resultType="java.lang.String">
        select GROUP_CONCAT(type_name) typeName from
                (select distinct sad.task_id, mt1.type_name
                 from scrap_apply_details sad
                              left join ma_type mt on sad.type_id = mt.type_id
                              left join ma_type mt1 on mt.parent_id = mt1.type_id
                 where sad.task_id = #{taskId}
                        ) t
        GROUP BY task_id
    </select>

    <select id="selectnotScrapByTaskId" resultType="com.bonus.sgzb.material.domain.ScrapApplyDetails">
        <include refid="selectScrapApplyDetailsVo"/>
        where task_id = #{taskId} and status = '0'
    </select>

    <select id="getBackApplyInfo" resultType="com.bonus.sgzb.base.api.domain.BackApplyInfo">
        select id,
               code,
               task_id as taskId,
               back_person as backPerson,
               phone,
               direct_audit_by as directAuditBy,
               direct_audit_time as directAuditTime,
               direct_audit_remark as directAuditRemark,
               company_id as companyId,
               back_time as backTime,
               status
        from back_apply_info
        where id = #{parentId}
    </select>

    <select id="getRepairAudit" resultType="com.bonus.sgzb.material.domain.RepairAuditDetails">
        select id,
               task_id as taskId,
               repair_id as repairId,
               ma_id as maId,
               type_id as typeId,
               repair_num as repairNum,
               scrap_num as scrapNum,
               company_id as companyId
        from repair_audit_details
        where id = #{parentId}
    </select>

    <select id="getRepairId" resultType="java.lang.String">
        select repair_id
        from repair_audit_details
        where id = #{parentId}
    </select>

    <select id="getByRepairId" resultType="com.bonus.sgzb.material.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="getRepairRecord" resultType="com.bonus.sgzb.material.domain.RepairRecord">
        select 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>
    </select>

    <select id="getFileUrl" resultType="java.lang.String">
        select file_url
        from sys_file_info
        where id = #{s}
    </select>

    <select id="getGyoupRepairRecord" resultType="com.bonus.sgzb.material.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>
    <select id="getScrapApplyList" resultType="com.bonus.sgzb.material.vo.ScrapApplyDetailsVO">

        SELECT
        tk.CODE scrapNum,
        tk.task_status taskStatus,
        dic.NAME taskStatusName,
        bui.unit_name unitName,
        bpl.lot_name projectName,
        su.nick_name createBy,
        tk.create_time createTime,
        tk.remark,
        tk1.CODE repairNum,
        sad.scrap_num as scrapNum2,
        sad.scrap_source as scrapSource,
        rad3.CODE repairCode,
        tk.task_id taskId
        FROM
        tm_task tk
        LEFT JOIN tm_task_agreement tta ON tk.task_id = tta.task_id
        LEFT JOIN bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
        LEFT JOIN bm_project_lot bpl ON bai.project_id = bpl.lot_id
        LEFT JOIN bm_unit_info bui ON bai.unit_id = bui.unit_id
        LEFT JOIN scrap_apply_details sad ON tk.task_id = sad.task_id
        LEFT JOIN repair_audit_details rad ON sad.parent_id = rad.id
        LEFT JOIN back_apply_info rad3 ON sad.parent_id = rad3.id
        LEFT JOIN repair_apply_details rad2 ON rad.repair_id = rad2.id
        LEFT JOIN tm_task tk1 ON rad2.task_id = tk1.task_id
        LEFT JOIN (
        SELECT
        id,
        p_id,
        NAME
        FROM
        sys_dic
        WHERE
        p_id = '20') dic ON tk.task_status = dic.id
        LEFT JOIN ma_type mt ON sad.type_id = mt.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        LEFT JOIN sys_user su ON su.user_id = tk.create_by
        WHERE
        sad.scrap_source IN ( '1', '2' )
        AND tk.task_type IN ('20' , '21')
        <if test="keyword != null and keyword != ''">
            AND (locate(#{keyword}, tk.create_by) > 0
            or locate(#{keyword}, tk.CODE) > 0
            or locate(#{keyword}, bui.unit_name) > 0
            or locate(#{keyword}, bpl.lot_name) > 0
            or locate(#{keyword}, tk1.CODE) > 0)
        </if>
        <if test="backUnit != null and backUnit != ''">
            and bui.unit_id = #{backUnit}
        </if>
        <if test="repairStatus != null and repairStatus != ''">
            and tk.task_status = #{repairStatus}
        </if>
        <if test="taskIdList != null and taskIdList != ''">
            and tk.task_id in #{taskIdList}
        </if>
        <if test="backPro != null and backPro != ''">
            and bpl.lot_id = #{backPro}
        </if>
        <if test="type != null and type != ''">
            and mt1.type_id = #{type}
        </if>
        <if test="backCode != null and backCode != ''">
            and locate(#{backCode}, tk.code) > 0
        </if>
        <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''">
            and tk.create_time between #{params.beginTime} and #{params.endTime}
        </if>
        GROUP BY tk.CODE
        order by tk.create_time desc

    </select>
    <select id="scrapTaskReviewList" resultType="com.bonus.sgzb.material.vo.ScrapApplyDetailsVO">
        SELECT
        tk.CODE scrapNum,
        tk.task_status taskStatus,
        dic.NAME taskStatusName,
        bui.unit_name unitName,
        bpl.lot_name projectName,
        su.nick_name createBy,
        tk.create_time createTime,
        tk.remark,
        tk1.CODE repairNum,
        sad.scrap_num as scrapNum2,
        sad.scrap_source as scrapSource,
        sad.file_url as fileUrl,
        tk.task_id taskId
        FROM
        tm_task tk
        LEFT JOIN tm_task_agreement tta ON tk.task_id = tta.task_id
        LEFT JOIN bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
        LEFT JOIN bm_project_lot bpl ON bai.project_id = bpl.lot_id
        LEFT JOIN bm_unit_info bui ON bai.unit_id = bui.unit_id
        LEFT JOIN scrap_apply_details sad ON tk.task_id = sad.task_id
        /* LEFT JOIN repair_audit_details rad ON sad.parent_id = rad.id
        LEFT JOIN repair_apply_details rad2 ON rad.repair_id = rad2.id*/
        LEFT JOIN scrap_apply_details sad1 ON sad.parent_id = sad1.id
        LEFT JOIN tm_task tk1 ON sad1.task_id = tk1.task_id
        LEFT JOIN (
        SELECT
        id,
        p_id,
        NAME
        FROM
        sys_dic
        WHERE
        p_id IN ( SELECT id FROM sys_dic WHERE VALUE = 'bf_task' )) dic ON tk.task_status = dic.id
        LEFT JOIN ma_type mt ON sad.type_id = mt.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        LEFT JOIN sys_user su ON su.user_id = tk.create_by
        WHERE
        sad.scrap_source IN ( '1', '2' )
        AND tk.task_type = 57  AND tk.task_status in ('58','59','61')
        <if test="keyword != null and keyword != ''">
            AND (locate(#{keyword}, tk.create_by) > 0
            or locate(#{keyword}, tk.CODE) > 0
            or locate(#{keyword}, bui.unit_name) > 0
            or locate(#{keyword}, bpl.lot_name) > 0
            or locate(#{keyword}, tk1.CODE) > 0)
        </if>
        <if test="backUnit != null and backUnit != ''">
            and bui.unit_id = #{backUnit}
        </if>
        <if test="repairStatus != null and repairStatus != ''">
            and tk.task_status = #{repairStatus}
        </if>
        <if test="backPro != null and backPro != ''">
            and bpl.lot_id = #{backPro}
        </if>
        <if test="type != null and type != ''">
            and mt1.type_id = #{type}
        </if>
        <if test="backCode != null and backCode != ''">
            and locate(#{backCode}, tk.code) > 0
        </if>
        <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''">
            and tk.create_time between #{params.beginTime} and #{params.endTime}
        </if>
        GROUP BY tk.CODE
        order by tk.create_time desc
    </select>

</mapper>