<?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.RepairInputDetailsMapper">
    <resultMap type="com.bonus.common.biz.domain.repair.RepairInputDetails" id="RepairInputDetailsResult">
        <result property="id"    column="id"    />
        <result property="taskId"    column="task_id"    />
        <result property="auditId"    column="audit_id"    />
        <result property="repairId"    column="repair_id"    />
        <result property="maId"    column="ma_id"    />
        <result property="typeId"    column="type_id"    />
        <result property="repairNum"    column="repair_num"    />
        <result property="inputNum"    column="input_num"    />
        <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="status"    column="status"    />
        <result property="remark"    column="remark"    />
        <result property="companyId"    column="company_id"    />
    </resultMap>

    <sql id="selectRepairInputDetailsVo">
        select id, task_id, audit_id, repair_id, ma_id, type_id, repair_num, input_num, create_by, create_time, update_by, update_time, status, remark, company_id from repair_input_details
    </sql>



    <select id="selectRepairInputDetailsList" resultType="com.bonus.common.biz.domain.repair.RepairInputDetails">
        SELECT * FROM (
        SELECT
        rd.task_id AS taskId,
        tt1.CODE AS repairCode,
        bai.`code` AS backCode,
        rd.create_time AS createTime,
        tt.task_status AS taskStatus,
        CASE tt.task_status
        WHEN '0' THEN '入库进行中'
        WHEN '1' THEN '入库完成'
        WHEN '2' THEN '入库驳回'
        ELSE '未知状态'
        END AS statusName,
        rd.remark AS remark,
        bui.unit_name AS backUnit,
        bpi.pro_name AS backPro,
        su.nick_name AS createBy,
        tt.CODE AS inputCode,
        SUM(IFNULL(rd.repair_num, 0)) as pendingInputNum,
        SUM(IFNULL(rd.input_num, 0)) as inputNum,
        GROUP_CONCAT(DISTINCT mt2.type_name) AS materialType,
        tta.agreement_id as agreementId,
        GROUP_CONCAT( DISTINCT mt4.type_id) as firstId
        FROM
        repair_input_details rd
        LEFT JOIN ma_type mt on rd.type_id = mt.type_id
        LEFT JOIN ma_type mt2 on mt.parent_id = mt2.type_id
        LEFT JOIN tm_task tt on rd.task_id = tt.task_id
        LEFT JOIN tm_task tt1 on rd.repair_id = tt1.task_id
        LEFT JOIN (
        SELECT
        rad.task_id,
        rad.back_id,
        rad.create_time
        FROM
        repair_apply_details rad
        GROUP BY rad.task_id
        ) rad ON rad.task_id = rd.repair_id
        LEFT JOIN back_apply_info bai ON rad.back_id = bai.id
        LEFT JOIN tm_task_agreement tta ON rd.repair_id = tta.task_id
        LEFT JOIN bm_agreement_info bai2 ON tta.agreement_id = bai2.agreement_id
        LEFT JOIN bm_unit bui ON bai2.unit_id = bui.unit_id
        LEFT JOIN bm_project bpi ON bai2.project_id = bpi.pro_id and bpi.del_flag = '0'
        left join sys_user su on rd.create_by = su.user_id
        left join ma_type mt3 ON mt2.parent_id = mt3.type_id and mt3.del_flag = '0'
        left join ma_type mt4 ON mt3.parent_id = mt4.type_id and mt4.del_flag = '0'
        <if test="userId != null">
            JOIN ma_type_keeper mtr ON mtr.type_id = rd.type_id AND mtr.user_id = #{userId}
        </if>
        <where>
            rad.create_time &lt; '2025-08-20 00:00:00'
            <if test="inputCode != null and inputCode != ''">
                AND tt.CODE = #{inputCode}
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                <![CDATA[and DATE_FORMAT( rd.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
            </if>
        </where>
        GROUP BY rd.task_id

        UNION

        SELECT
        rd.task_id AS taskId,
        tt1.CODE AS repairCode,
        bai.`code` AS backCode,
        rd.create_time AS createTime,
        tt.task_status AS taskStatus,
        CASE tt.task_status
        WHEN '0' THEN '入库进行中'
        WHEN '1' THEN '入库完成'
        WHEN '2' THEN '入库驳回'
        ELSE '未知状态'
        END AS statusName,
        rd.remark AS remark,
        bui.unit_name AS backUnit,
        bpi.pro_name AS backPro,
        su.nick_name AS createBy,
        tt.CODE AS inputCode,
        SUM(IFNULL(rd.repair_num, 0)) as pendingInputNum,
        SUM(IFNULL(rd.input_num, 0)) as inputNum,
        GROUP_CONCAT(DISTINCT mt2.type_name) AS materialType,
        tta.agreement_id as agreementId,
        GROUP_CONCAT( DISTINCT mt4.type_id) as firstId
        FROM
        repair_input_details rd
        LEFT JOIN ma_type mt on rd.type_id = mt.type_id
        LEFT JOIN ma_type mt2 on mt.parent_id = mt2.type_id
        LEFT JOIN tm_task tt on rd.task_id = tt.task_id
        LEFT JOIN repair_apply_details rad ON rad.id = rd.repair_id
        LEFT JOIN back_apply_info bai ON rad.back_id = bai.id
        LEFT JOIN tm_task tt1 on rad.task_id = tt1.task_id
        LEFT JOIN tm_task_agreement tta ON rd.task_id = tta.task_id
        LEFT JOIN bm_agreement_info bai2 ON tta.agreement_id = bai2.agreement_id
        LEFT JOIN bm_unit bui ON bai2.unit_id = bui.unit_id
        LEFT JOIN bm_project bpi ON bai2.project_id = bpi.pro_id and bpi.del_flag = '0'
        left join sys_user su on rd.create_by = su.user_id
        left join ma_type mt3 ON mt2.parent_id = mt3.type_id and mt3.del_flag = '0'
        left join ma_type mt4 ON mt3.parent_id = mt4.type_id and mt4.del_flag = '0'
        <if test="userId != null">
            JOIN ma_type_keeper mtr ON mtr.type_id = rd.type_id AND mtr.user_id = #{userId}
        </if>
        <where>
            rad.create_time &gt;= '2025-08-20 00:00:00'
            <if test="inputCode != null and inputCode != ''">
                AND tt.CODE = #{inputCode}
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                <![CDATA[and DATE_FORMAT( rd.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
            </if>
        </where>
        GROUP BY rd.task_id
        ) AS combined_results
        ORDER BY
        taskStatus,
        createTime DESC
    </select>

    <select id="selectRepairInputDetailsById" resultType="com.bonus.material.repair.domain.RepairInputInfo">
        select
            rid.id as id,
            rid.task_id as taskId,
            tt.`code` as inputCode,
            rid.ma_id as maId,
            rid.type_id as typeId,
            SUM(IFNULL(rid.repair_num, 0)) as repairNum,
            SUM(IFNULL(rid.input_num, 0)) as inputNum,
            (SUM(IFNULL(rid.repair_num, 0)) - SUM(IFNULL(rid.input_num, 0)) - SUM(IFNULL(rid.reject_num, 0)))
                AS pendingInputNum,
            SUM(IFNULL(rid.reject_num, 0)) as rejectNum,
            su.nick_name as createBy,
            rid.create_time as createTime,
            su1.nick_name as auditBy,
            rid.audit_time as auditTime,
            rid.remark as remark,
            mt1.type_name AS typeName,
            mt.type_name AS typeModelName,
            mt.unit_name AS unitName,
            mt.unit_value AS unitValue,
            mt.manage_type AS manageType,
            tta.agreement_id as agreementId,
            rid.reject_reason as rejectReason,
            mt4.type_id as firstId
        from repair_input_details rid
        LEFT JOIN tm_task tt on tt.task_id=rid.task_id
        LEFT JOIN ma_type mt on rid.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 tm_task_agreement tta ON rid.task_id = tta.task_id
        LEFT JOIN sys_user su on rid.create_by = su.user_id
        LEFT JOIN sys_user su1 on rid.audit_by = su1.user_id
        left join ma_type mt3 ON mt1.parent_id = mt3.type_id and mt3.del_flag = '0'
        left join ma_type mt4 ON mt3.parent_id = mt4.type_id and mt4.del_flag = '0'
        <if test="userId != null">
            JOIN ma_type_keeper mtr ON mtr.type_id = rid.type_id AND mtr.user_id = #{userId}
        </if>
        where rid.task_id = #{taskId}
        <if test="keyWord != null and keyWord != ''">
            AND (
            locate(#{keyWord}, mt1.type_name) > 0
            or locate(#{keyWord}, mt.type_name) > 0
            or locate(#{keyWord}, mt.unit_name) > 0
            )
        </if>
        GROUP BY rid.type_id
    </select>

    <select id="selectStatusByTaskId" resultType="java.lang.String">
        select rd.status from repair_input_details rd
        <if test="userId != null">
            JOIN ma_type_keeper mtr ON mtr.type_id = rd.type_id AND mtr.user_id = #{userId}
        </if>
        where task_id = #{taskId}
    </select>

    <select id="getCodeList" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT
            rid.ma_id as maId,
            mt1.type_name as typeName,
            mt.type_name as materialName,
            mm.ma_code as maCode,
            mm.ma_status as maStatus,
            rid.reject_reason as rejectReason,
            su.nick_name as auditByName,
            rid.audit_time as auditTime,
            rid.reject_num as rejectNum
        FROM
            repair_input_details rid
                LEFT JOIN ma_machine mm ON rid.ma_id = mm.ma_id
                LEFT JOIN ma_type mt ON rid.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 rid.audit_by = su.user_id
        WHERE
           rid.task_id = #{taskId}
          AND rid.type_id = #{typeId}
        <if test="keyWord != null and keyWord != ''">
            AND (
            locate(#{keyWord}, mt1.type_name) > 0
            or locate(#{keyWord}, mt.type_name) > 0
            or locate(#{keyWord}, mm.ma_code) > 0
            )
        </if>
    </select>

    <select id="selectRepairDetailsById" resultType="com.bonus.material.repair.domain.RepairInputInfo">
        SELECT
            rd.id as id,
            rd.task_id as taskId,
            rd.repair_id as repairId,
            rd.ma_id as maId,
            rd.type_id as typeId,
            rd.repair_num as repairNum,
            rd.input_num as inputNum,
            (IFNULL(rd.repair_num, 0) - IFNULL(rd.input_num, 0) - IFNULL(rd.reject_num, 0))
                AS pendingInputNum,
            rd.reject_num as rejectNum,
            rd.create_by as createBy,
            rd.create_time as createTime,
            rd.status as status,
            rd.remark as remark,
            mt1.type_name AS typeName,
            mt.type_name AS typeModelName,
            mt.unit_name AS unitName,
            mt.unit_value AS unitValue,
            mt.manage_type AS manageType
        FROM
            repair_input_details rd
                LEFT JOIN ma_type mt on rd.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
            rd.task_id = #{taskId}
        <if test="typeId != null">
            and rd.type_id = #{typeId}
        </if>
    </select>

    <select id="selectBackIdByTaskId" resultType="java.lang.Long">
        SELECT
            rad.back_id
        FROM
            repair_input_details rid
                LEFT JOIN repair_apply_details rad ON rid.repair_id = rad.id
        WHERE
            rid.task_id = #{taskId}
            LIMIT 1
    </select>

    <select id="selectRepairDetails" resultType="com.bonus.common.biz.domain.repair.RepairInputDetails">
        SELECT
            rd.task_id AS taskId,
            tt.`code` as inputCode,
            rd.type_id AS typeId,
            rd.repair_num AS repairNum,
            rd.input_num AS inputNum,
            SUM((
                    IFNULL( rd.repair_num, 0 ) - IFNULL( rd.input_num, 0 ) - IFNULL( rd.reject_num, 0 ))) AS pendingInputNum,
            rd.reject_num AS rejectNum,
            mt.manage_type AS manageType,
            GROUP_CONCAT( mm.ma_code ) AS maCode
        FROM
            repair_input_details rd
        LEFT JOIN tm_task tt on tt.task_id=rd.task_id
                LEFT JOIN ma_type mt ON rd.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_machine mm ON mm.ma_id = rd.ma_id
        WHERE
           (mm.ma_status != 1 or mm.ma_status is null) and
            rd.`status` = '0' and
            rd.task_id IN
          <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
          </foreach>
        GROUP BY
            mt.type_id
    </select>

    <select id="getInfoByQrcode" resultType="com.bonus.common.biz.domain.repair.RepairInputDetails">
        SELECT
            mt1.type_name as typeName,
            mt.type_name  as typeModelName,
            mm.ma_id      as maId,
            mm.ma_code    as maCode,
            mm.type_id    as typeId,
            mm.ma_status  as maStatus,
            mm.qr_code    as qrCode,
            case when mm.ma_status = '5' then '修试后待入库'
                 else ''
                end as statusName,
            mt.manage_type as manageType,
            rid.task_id as taskId,
            bp.pro_name as backPro,
            bu.unit_name as backUnit,
            tt.CODE AS inputCode,
            tt1.CODE AS repairCode,
            bai.agreement_id as agreementId
        FROM ma_machine mm
                 LEFT JOIN ma_type mt ON mm.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 repair_input_details rid ON mm.ma_id = rid.ma_id
                 LEFT JOIN tm_task_agreement tta ON rid.task_id = tta.task_id
                 LEFT JOIN bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
                 LEFT JOIN bm_project bp ON bai.project_id = bp.pro_id
                 LEFT JOIN bm_unit bu ON bai.unit_id = bu.unit_id
                 LEFT JOIN tm_task tt on rid.task_id = tt.task_id
                 LEFT JOIN repair_apply_details rad ON rad.id = rid.repair_id
                 LEFT JOIN tm_task tt1 on rad.task_id = tt1.task_id
        WHERE
            rid.`status` = '0' and mm.ma_status = '5'
        <if test="qrCode != null and qrCode != ''">
            and mm.qr_code = #{qrCode}
        </if>
        <if test="maCode != null and maCode != ''">
            and mm.ma_code like concat('%',#{maCode},'%')
        </if>
    </select>

    <insert id="batchInsertRepairInputDetails" parameterType="com.bonus.common.biz.domain.repair.RepairInputDetails">
        insert into repair_input_details
        (task_id, audit_id, repair_id, ma_id, type_id, repair_num, input_num, create_by, create_time,
        status, remark, company_id)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.taskId,jdbcType=INTEGER}, #{item.auditId,jdbcType=INTEGER}, #{item.repairId,jdbcType=INTEGER},
            #{item.maId,jdbcType=INTEGER}, #{item.typeId,jdbcType=INTEGER}, #{item.repairNum,jdbcType=INTEGER},
            #{item.inputNum,jdbcType=INTEGER},
            #{item.createBy,jdbcType=VARCHAR}, NOW(), #{item.status,jdbcType=VARCHAR},
            #{item.remark,jdbcType=VARCHAR}, #{item.companyId,jdbcType=INTEGER})
        </foreach>
    </insert>

    <insert id="insertRepairInputDetails" parameterType="com.bonus.common.biz.domain.repair.RepairInputDetails" useGeneratedKeys="true" keyProperty="id">
        insert into repair_input_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="auditId != null">audit_id,</if>
            <if test="repairId != null">repair_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="inputNum != null">input_num,</if>
            <if test="createBy != null">create_by,</if>
            create_time,
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="auditId != null">#{auditId},</if>
            <if test="repairId != null">#{repairId},</if>
            <if test="maId != null">#{maId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="repairNum != null">#{repairNum},</if>
            <if test="inputNum != null">#{inputNum},</if>
            <if test="createBy != null">#{createBy},</if>
            NOW(),
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
         </trim>
    </insert>

    <insert id="insertRad">
        insert into repair_apply_details
        (
        <if test="taskId != null">
            task_id,
        </if>
        <if test="maId != null">
            ma_id,
        </if>
        <if test="typeId != null">
            type_id,
        </if>
        <if test="rejectNum != null">
            repair_num,
        </if>
            status,
        <if test="createBy != null and createBy != ''">
            create_by,
        </if>
        <if test="remark != null and remark != ''">
            remark,
        </if>
        <if test="companyId != null">
            company_id,
        </if>
        <if test="backId != null">
         back_id,
        </if>
        create_time,
        is_ds
        )
        values (
        <if test="taskId != null">
            #{taskId},
        </if>
        <if test="maId != null">
            #{maId},
        </if>
        <if test="typeId != null">
            #{typeId},
        </if>
        <if test="rejectNum != null">
            #{rejectNum},
        </if>
        3,
        <if test="createBy != null and createBy != ''">
            #{createBy},
        </if>
        <if test="remark != null and remark != ''">
            #{remark},
        </if>
        <if test="companyId != null">
            #{companyId},
        </if>
        <if test="backId != null">
                #{backId},
        </if>
        NOW(),
        1
        )
    </insert>

    <update id="updateRepairInputDetails" parameterType="com.bonus.common.biz.domain.repair.RepairInputDetails">
        UPDATE repair_input_details
        SET
            <if test="inputNum != null">input_num = IFNULL(input_num, 0) + #{inputNum},</if>
            <if test="rejectNum != null">reject_num = IFNULL(reject_num, 0) + #{rejectNum},</if>
            <if test="rejectReason != null and rejectReason != ''">reject_reason = #{rejectReason},</if>
            update_by   = #{updateBy},
            update_time = #{updateTime},
            audit_by    = #{auditBy},
            audit_time = #{auditTime},
            status      = #{status}
        WHERE task_id = #{taskId}
          and type_id = #{typeId}
      <if test="maId != null">
          and ma_id = #{maId}
      </if>
    </update>

    <update id="updateNum">
        UPDATE ma_type
        SET storage_num = #{inputNum} + IFNULL(storage_num, 0)
        WHERE
            type_id = #{typeId}
    </update>

    <update id="updateMaStatus">
        UPDATE ma_machine
        SET ma_status = #{status}
        WHERE
            ma_id = #{maId}
    </update>

    <delete id="deleteRepairInputDetailsById" parameterType="Long">
        delete from repair_input_details where id = #{id}
    </delete>

    <delete id="deleteRepairInputDetailsByTaskId" parameterType="Long">
        delete from repair_input_details where task_id = #{taskId}
    </delete>

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

    <select id="selectInputInfoByMaId" resultMap="RepairInputDetailsResult">
        SELECT
            rid.id,
            rid.task_id,
            tt.`code` as inputCode,
            rid.ma_id,
            rid.type_id,
            rid.input_num,
            rid.create_by,
            rid.create_time,
            rid.update_by,
            rid.update_time,
            rid.remark,
            rid.company_id
        FROM
            repair_input_details  rid
                LEFT JOIN tm_task tt on tt.task_id=rid.task_id
        WHERE
            rid.STATUS = '0'
          AND rid.ma_id = #{maId}

            LIMIT 1
    </select>

    <update id="updateInputNum">
        update repair_input_details set input_num = 1,audit_by = #{auditBy},status  = 1,audit_time = now()
        where task_id = #{taskId} and ma_id = #{maId}
    </update>

    <update id="updateStorageNum">
        update ma_type  set storage_num = storage_num + #{inputNum}
        where type_id = #{typeId}
    </update>

    <update id="updateTaskStatus">
        update tm_task  set task_status = 1
        where task_id = #{taskId}
    </update>

    <select id="selectInputTaskStatus" resultMap="RepairInputDetailsResult">

        SELECT
            task_id,
            SUM(repair_num) - SUM(inputNum) as waitNum
        FROM
            (

                SELECT
                    id,
                    task_id,
                    ma_id,
                    type_id,
                    repair_num,
                    IFNULL(input_num,0) as inputNum,
                    create_by,
                    create_time,
                    update_by,
                    update_time,
                    remark,
                    company_id
                FROM
                    repair_input_details
                WHERE
                    task_id =#{taskId}
            ) res
        HAVING waitNum > 0

    </select>

    <select id="selectRepairListByBoxId" resultMap="RepairInputDetailsResult">
        SELECT
            bbb.box_id AS boxId,
            bbb.ma_id as maId,
            mm.qr_code as qrCode
        FROM
            bm_qrcode_box bqb
                LEFT JOIN bm_qrcode_box_bind bbb on bqb.box_id = bbb.box_id
                LEFT JOIN ma_machine mm on bbb.ma_id  = mm.ma_id
        WHERE bqb.box_code = #{boxCode}
    </select>
    <select id="selectRepairInputDetailsByAuditId" resultType="java.lang.Integer">
        select count(1)
        from repair_input_details
        where audit_id = #{auditId}
    </select>

    <update id="updateBoxStatus">
        update bm_qrcode_box set box_status = 6
        where  box_code = #{boxCode}
    </update>
</mapper>
