<?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.RepairTestInputMapper">
    <!--新增入库任务详细表-数据-->
    <insert id="addInputApplyDetails">
        INSERT INTO input_apply_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null and taskId != ''">task_id,</if>
            <if test="maId != null and maId!=''">ma_id,</if>
            <if test="typeId != null and typeId !=''">type_id,</if>
            <if test="repairNum != null and repairNum!=''">input_num,</if>
            input_type,
            <if test="updateBy != null and updateBy != ''">create_by,</if>
            <if test="updateTime != null and updateTime != ''">create_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null and updateTime != ''">update_time,</if>
            <if test="companyId != null">company_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null and taskId != ''">#{taskId},</if>
            <if test="maId != null and maId!=''">#{maId},</if>
            <if test="typeId != null and typeId !=''">#{typeId},</if>
            <if test="repairNum != null and repairNum!=''">#{repairNum},</if>
            '3',
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null and updateTime != ''">#{updateTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null and updateTime != ''">#{updateTime},</if>
            <if test="companyId != null">#{companyId},</if>
        </trim>
    </insert>
    <!--更新修试后入库-数据-->
    <update id="updateRepairInputDetails">
        UPDATE repair_input_details
        <set>
            <if test="repairNum != null and repairNum != ''">input_num = #{repairNum},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null and updateTime != ''">update_time = #{updateTime},</if>
            <if test="checkType != null and checkType != ''">`status` = #{checkType},</if>
            <if test="remark != null and remark != ''">`remark` = #{remark}</if>
        </set>
        WHERE id = #{id}
    </update>
    <!--更新机具类型-库存数量-->
    <update id="updateMaTypeNum">
        UPDATE ma_type SET num = #{num} WHERE type_id = #{typeId}
    </update>
    <!--管理方式为编号的需更新机具设备的机具状态-->
    <update id="updateMaMachineStatus">
        UPDATE ma_machine SET ma_status = #{dicId} WHERE ma_id = #{maId}
    </update>
    <!--更新任务表状态-->
    <update id="updateTmTaskStatus">
        UPDATE tm_task SET task_status = #{dictId},update_by = #{params.updateBy},update_time = #{params.updateTime} WHERE task_id = #{params.taskId}
    </update>

    <!--获取修试后入库列表-->
    <select id="getRepairedList" resultType="com.bonus.sgzb.material.domain.RepairTestInputVo">
        SELECT b.id,
               b.repairCode,
               b.maTypeName,
               b.projectName,
               b.unitName,
               b.wxName,
               b.wxTime,
               b.taskStatus
        FROM (
                SELECT ANY_VALUE(rid.task_id) AS id,
                ANY_VALUE(tt.`code`) AS repairCode,
                ANY_VALUE(GROUP_CONCAT(DISTINCT a.typeName2)) AS maTypeName,
                ANY_VALUE(GROUP_CONCAT(DISTINCT a.typeId2)) AS typeId2,
                bpl.lot_name projectName,
                bui.unit_name unitName,
                ANY_VALUE(us.user_name) AS wxName,
                ANY_VALUE(tt.create_time) AS wxTime,
                ANY_VALUE(IFNULL(sd.`name`,'入库进行中')) AS taskStatus
                FROM repair_input_details rid
                LEFT JOIN repair_apply_details rad on rid.repair_id=rad.id
                LEFT JOIN sys_user us on us.user_id=rad.repairer
                LEFT JOIN tm_task tt ON rid.task_id = tt.task_id
                LEFT JOIN tm_task_agreement tta ON tt.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 sys_dic sd ON tt.task_status = sd.id
                LEFT JOIN tm_task tt2 ON rad.task_id = tt2.task_id
                LEFT JOIN (
                    SELECT mt.type_id AS typeId,mt.type_name AS typeName,mt.manage_type,mt2.type_id AS typeId2,mt2.type_name AS typeName2
                    FROM ma_type mt
                    LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id AND mt2.`level` = '3'
                    LEFT JOIN ma_type mt3 ON mt2.parent_id = mt3.type_id AND mt3.`level` = '2'
                    WHERE mt.`level` = '4'
                )a ON rid.type_id = a.typeId
                GROUP BY rid.task_id
                ORDER BY tt.create_time DESC
            ) b
        <where>
            <if test="deviceTypeId!=null and deviceTypeId!=''">
               b.typeId2 = #{deviceTypeId}
            </if>
            <if test="wxTime!=null and wxTime!=''">
               AND b.wxTime BETWEEN CONCAT(#{wxTime},' 00:00:00') AND CONCAT(#{wxTime},' 23:59:59')
            </if>
            <if test="keyWord!=null and keyWord!=''">
               AND (
                INSTR(b.repairCode,#{keyWord}) > 0 OR
                INSTR(b.wxName,#{keyWord}) > 0
                )
            </if>
        </where>
    </select>
    <!--获取修试后入库-详情列表-->
    <select id="getRepairedDetailList" resultType="com.bonus.sgzb.material.domain.RepairTestInputDetailVo">
        SELECT rid.id,
               rid.ma_id AS maId,
               rid.type_id AS typeId,
               a.typeName,
               a.typeName2,
               rid.repair_num AS repairNum,
               a.manage_type AS manageType,
               mm.ma_code AS maCode,
               rid.update_by AS updateBy,
               rid.update_time AS updateTime,
               rid.remark,
               CASE rid.`status` WHEN '0' THEN '进行中' WHEN '1' THEN '已入库' WHEN '2' THEN '驳回' ELSE '进行中' END AS `status`
        FROM repair_input_details rid
        LEFT JOIN ma_machine mm ON rid.ma_id = mm.ma_id
        LEFT JOIN (
            SELECT mt.type_id AS typeId,mt2.type_id AS typeId2,mt.type_name AS typeName,mt2.type_name AS typeName2,mt.manage_type
            FROM ma_type mt
            LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id AND mt2.`level` = '3'
            LEFT JOIN ma_type mt3 ON mt2.parent_id = mt3.type_id AND mt3.`level` = '2'
            LEFT JOIN ma_type mt4 ON mt3.parent_id = mt4.type_id AND mt4.`level` = '1'
            WHERE mt.`level` = '4'
        )a ON rid.type_id = a.typeId
        WHERE rid.task_id = #{taskId}
        <if test="keyWord!=null and keyWord!=''">
            AND (
            INSTR(rid.update_by,#{keyWord}) > 0 OR
            INSTR(mm.ma_code,#{keyWord}) > 0
            )
        </if>
        <if test="deviceTypeId!=null and deviceTypeId!=''">
            AND a.typeId2 = #{deviceTypeId}
        </if>
        <if test="typeId!=null and typeId!=''">
            AND a.typeId = #{typeId}
        </if>
        ORDER BY rid.create_time DESC
    </select>
    <!--查询机具类型-库存数量-->
    <select id="getMaTypeByNum" resultType="java.util.Map">
        SELECT type_name AS typeName,
               num
        FROM ma_type WHERE type_id = #{typeId}
    </select>

    <!--查询机具状态-在库的id-->
    <select id="getDicByMaStatusId" resultType="java.lang.Integer">
        SELECT sd2.id,sd2.`name`
        FROM sys_dic sd
        LEFT JOIN sys_dic sd2 ON sd.id = sd2.p_id AND sd2.`name` = #{status}
        WHERE sd.`value` = #{maStatus} AND sd.p_id = 0
    </select>

    <!--查询修试后入库的状态是否全部更新-->
    <select id="getIsAllUpdate" resultType="java.util.Map">
        SELECT COUNT(IF(status = '0' OR status IS NULL OR status = '',1,NULL)) AS noCheckNum,
               COUNT(IF(status = '1',1,NULL)) AS passNum,
               COUNT(IF(status = '2',1,NULL)) AS noPassNum,
               COUNT(*) AS totalNum
        FROM repair_input_details WHERE task_id = #{taskId}
    </select>
    <select id="exportList" resultType="com.bonus.sgzb.material.domain.RepairTestInputDetailVo">
        SELECT rid.id,
        tt2.`code` as repairCode,
        tt2.create_by as wxName,
        tt2.create_time as wxTime,
        rid.ma_id AS maId,
        rid.type_id AS typeId,
        a.typeName,
        a.typeName2,
        rid.repair_num AS repairNum,
        a.manage_type AS manageType,
        mm.ma_code AS maCode,
        rid.update_by AS updateBy,
        rid.update_time AS updateTime,
        rid.remark,
        CASE rid.`status` WHEN '0' THEN '进行中' WHEN '1' THEN '已入库' WHEN '2' THEN '驳回' ELSE '进行中' END AS `status`
        FROM repair_input_details rid
        LEFT JOIN tm_task tt2 ON rid.repair_id = tt2.task_id
        LEFT JOIN ma_machine mm ON rid.ma_id = mm.ma_id
        LEFT JOIN (
        SELECT mt.type_id AS typeId,mt2.type_id AS typeId2,mt.type_name AS typeName,mt2.type_name AS
        typeName2,mt.manage_type
        FROM ma_type mt
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id AND mt2.`level` = '3'
        WHERE mt.`level` = '4'
        )a ON rid.type_id = a.typeId
        <where>
            <if test="deviceTypeId!=null and deviceTypeId!=''">
                INSTR(b.typeId2,#{deviceTypeId}) > 0
            </if>
            <if test="wxTime!=null and wxTime!=''">
                AND b.wxTime BETWEEN CONCAT(#{wxTime},' 00:00:00') AND CONCAT(#{wxTime},' 23:59:59')
            </if>
            <if test="keyWord!=null and keyWord!=''">
                AND (
                INSTR(b.repairCode,#{keyWord}) > 0 OR
                INSTR(b.wxName,#{keyWord}) > 0
                )
            </if>
        </where>
        ORDER BY rid.create_time DESC

    </select>

    <insert id="insertRepairInputDetails" parameterType="com.bonus.sgzb.material.domain.RepairInputDetails">
        insert into repair_input_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <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>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <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="id != null">#{id},</if>
            <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>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
        </trim>
    </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>

    <select id="getCountOfMachineByTypeId" resultType="java.lang.Integer">
        SELECT
            count(mm.ma_id)
        FROM ma_machine mm
                 LEFT JOIN ma_type mt ON mt.type_id = mm.type_id
                 LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        WHERE mm.ma_code is not null and mm.ma_status in (15)
          AND mt.type_id = #{typeId}
    </select>
</mapper>