<?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.ma.mapper.MachineMapper">
    <resultMap type="com.bonus.material.ma.domain.Machine" id="MachineResult">
        <result property="maId"    column="ma_id"    />
        <result property="typeId"    column="type_id"    />
        <result property="maCode"    column="ma_code"    />
        <result property="preCode"    column="pre_code"    />
        <result property="maStatus"    column="ma_status"    />
        <result property="qrCode"    column="qr_code"    />
        <result property="buyPrice"    column="buy_price"    />
        <result property="maVender"    column="ma_vender"    />
        <result property="outFacTime"    column="out_fac_time"    />
        <result property="outFacCode"    column="out_fac_code"    />
        <result property="assetsCode"    column="assets_code"    />
        <result property="checkMan"    column="check_man"    />
        <result property="thisCheckTime"    column="this_check_time"    />
        <result property="nextCheckTime"    column="next_check_time"    />
        <result property="gpsCode"    column="gps_code"    />
        <result property="rfidCode"    column="rfid_code"    />
        <result property="erpCode"    column="erp_code"    />
        <result property="transferCode"    column="transfer_code"    />
        <result property="inOutNum"    column="in_out_num"    />
        <result property="buyTask"    column="buy_task"    />
        <result property="ownHouse"    column="own_house"    />
        <result property="companyId"    column="company_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="inspectMan"    column="inspect_man"    />
        <result property="inspectStatus"    column="inspect_status"    />
        <result property="phone"    column="phone"    />
    </resultMap>

    <sql id="selectMachineVo">
        select ma_id, type_id, ma_code, pre_code, ma_status, qr_code, buy_price, ma_vender, out_fac_time,
               out_fac_code, assets_code, check_man, this_check_time, next_check_time, gps_code, rfid_code,
               erp_code, transfer_code, in_out_num, buy_task, own_house, company_id, create_time, update_time,
               inspect_man, inspect_status, phone from ma_machine
    </sql>

    <select id="selectMachineList" resultType="com.bonus.material.ma.domain.vo.MachineVo">
        select a.*
        from (
        SELECT
        ma.ma_id as maId,
        ma.type_id as typeId,
        mt4.type_name as itemType,
        mt3.type_name as materialType,
        mt2.type_name as materialName,
        mt.type_name as materialModel,
        ma.ma_code as maCode,
        ma.pre_code as preCode,
        ma.ma_status as maStatus,
        ma.qr_code as qrCode,
        ma.buy_price as buyPrice,
        msi.supplier as maVender,
        ma.out_fac_time as outFacTime,
        ma.out_fac_code as outFacCode,
        ma.assets_code as assetsCode,
        ma.check_man as checkMan,
        ma.this_check_time as thisCheckTime,
        ma.next_check_time as nextCheckTime,
        ma.gps_code as gpsCode,
        ma.rfid_code as rfidCode,
        ma.erp_code as erpCode,
        ma.transfer_code as transferCode,
        ma.in_out_num as inOutNum,
        tt.code as buyTask,
        ma.own_house as ownHouse,
        ma.company_id as companyId,
        ma.create_time as createTime,
        ma.update_time as updateTime,
        ma.inspect_man as inspectMan,
        ma.inspect_status as inspectStatus,
        ma.phone as phone,
        CASE
        WHEN RIGHT(ma.ma_code, 4) REGEXP '^[0-9]{4}$' THEN CAST(RIGHT(ma.ma_code, 4) AS UNSIGNED)
        WHEN RIGHT(ma.ma_code, 3) REGEXP '^[0-9]{3}$' THEN CAST(RIGHT(ma.ma_code, 3) AS UNSIGNED)
        WHEN RIGHT(ma.ma_code, 2) REGEXP '^[0-9]{2}$' THEN CAST(RIGHT(ma.ma_code, 2) AS UNSIGNED)
        WHEN RIGHT(ma.ma_code, 1) REGEXP '^[0-9]$' THEN CAST(RIGHT(ma.ma_code, 1) AS UNSIGNED)
        ELSE '0'
        END AS result
        FROM
        ma_machine ma
        LEFT JOIN ma_type mt ON ma.type_id = mt.type_id
        and mt.`level` = '4' and mt.del_flag = '0'
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        and mt2.`level` = '3' and mt2.del_flag = '0'
        LEFT JOIN ma_type mt3 ON mt2.parent_id = mt3.type_id
        and mt3.`level` = '2' and mt3.del_flag = '0'
        LEFT JOIN ma_type mt4 ON mt3.parent_id = mt4.type_id
        and mt4.`level` = '1' and mt4.del_flag = '0'
        LEFT JOIN ma_supplier_info msi ON msi.supplier_id = ma.ma_vender
        LEFT JOIN tm_task tt on tt.task_id=ma.buy_task
        where
        1 = 1
        <if test="keyWord != null and keyWord != ''">
            and (
            ma.ma_code like concat('%', #{keyWord}, '%')
            or ma.assets_code like concat('%', #{keyWord}, '%')
            or ma.pre_code like concat('%', #{keyWord}, '%')
            or ma.buy_task like concat('%', #{keyWord}, '%')
            or ma.own_house like concat('%', #{keyWord}, '%')
            or mt4.type_name like concat('%', #{keyWord}, '%')
            or mt3.type_name like concat('%', #{keyWord}, '%')
            or mt2.type_name like concat('%', #{keyWord}, '%')
            or mt.type_name like concat('%', #{keyWord}, '%')
            )
        </if>
        <if test="typeId != null">
            and ma.type_id = #{typeId}
        </if>
        <if test="maStatus != null and maStatus != ''">
            and ma.ma_status = #{maStatus}
        </if>
        <if test="maCode != null and maCode != ''">
            and ma.ma_code like concat('%', #{maCode}, '%')
        </if>
        <if test="qrCode != null and qrCode != ''">
            and ma.qr_code like concat('%', #{qrCode}, '%')
        </if>
        <if test="materialType != null and materialType != ''">
            and mt3.type_name like concat('%', #{materialType}, '%')
        </if>
        <if test="materialName != null and materialName != ''">
            and mt2.type_name like concat('%', #{materialName}, '%')
        </if>
        <if test="materialModel != null and materialModel != ''">
            and mt.type_name like concat('%', #{materialModel}, '%')
        </if>
        <if test="isAssets != null">
            <if test="isAssets == 0">
                AND ma.assets_code IS NOT NULL
                AND ma.assets_code != ''
            </if>
            <if test="isAssets == 1">
                AND (ma.assets_code IS NULL OR ma.assets_code = '')
            </if>
        </if>

        ) a
        where
        1=1
        <if test="prefix != null and suffix !=null">
            and a.result BETWEEN #{prefix} and #{suffix}
        </if>

        order by a.createTime desc
    </select>

    <select id="selectMachineByMaId" resultType="com.bonus.material.ma.domain.vo.MachineVo">
        SELECT
            ma.ma_id as maId,
            ma.type_id as typeId,
            mt4.type_name as itemType,
            mt3.type_name as materialType,
            mt2.type_name as materialName,
            mt.type_name as materialModel,
            ma.ma_code as maCode,
            ma.pre_code as preCode,
            ma.ma_status as maStatus,
            ma.qr_code as qrCode,
            ma.buy_price as buyPrice,
            ma.ma_vender as maVender,
            ma.out_fac_time as outFacTime,
            ma.out_fac_code as outFacCode,
            ma.assets_code as assetsCode,
            ma.check_man as checkMan,
            ma.this_check_time as thisCheckTime,
            ma.next_check_time as nextCheckTime,
            ma.gps_code as gpsCode,
            ma.rfid_code as rfidCode,
            ma.erp_code as erpCode,
            ma.transfer_code as transferCode,
            ma.in_out_num as inOutNum,
            tt.code as buyTask,
            ma.own_house as ownHouse,
            ma.company_id as companyId,
            ma.create_time as createTime,
            ma.update_time as updateTime,
            ma.inspect_man as inspectMan,
            ma.inspect_status as inspectStatus,
            ma.phone as phone,
            GROUP_CONCAT( DISTINCT su1.user_id ORDER BY su1.user_id SEPARATOR ',' ) AS keeperId,
            GROUP_CONCAT( DISTINCT su1.nick_name ORDER BY su1.user_id SEPARATOR ',' ) AS keeperName,
            GROUP_CONCAT( DISTINCT su2.user_id ORDER BY su2.user_id SEPARATOR ',' ) AS repairId,
            GROUP_CONCAT( DISTINCT su2.nick_name ORDER BY su2.user_id SEPARATOR ',' ) AS repairName,
            baa.asset_name as assetName,
            ma.assets_id as assetsId,
            ma.remark as remark
        FROM
            ma_machine ma
                LEFT JOIN ma_type mt ON ma.type_id = mt.type_id
                and  mt.`level` = '4' and mt.del_flag = '0'
                LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
                and mt2.`level` = '3' and mt2.del_flag = '0'
                LEFT JOIN ma_type mt3 ON mt2.parent_id = mt3.type_id
                and mt3.`level` = '2' and mt3.del_flag = '0'
                LEFT JOIN ma_type mt4 ON mt3.parent_id = mt4.type_id
                and mt4.`level` = '1' and mt4.del_flag = '0'
                LEFT JOIN ma_type_keeper mtk on ma.type_id = mtk.type_id
                LEFT JOIN sys_user su1 on mtk.user_id = su1.user_id
                LEFT JOIN ma_type_repair mtr on ma.type_id = mtr.type_id
                LEFT JOIN sys_user su2 on mtr.user_id = su2.user_id
                LEFT JOIN bm_asset_attributes baa on ma.assets_id = baa.id
                LEFT JOIN tm_task tt on tt.task_id=ma.buy_task
        where ma.ma_id = #{maId}
    </select>

    <select id="selectByTypeList" resultType="com.bonus.material.ma.domain.Type">
        SELECT
            type_id as typeId,
            type_name as typeName,
            parent_id as parentId,
            storage_num as storageNum,
            type_code as typeCode,
            level as level
        FROM
            ma_type
        WHERE del_flag = '0'
        <if test="level != null and level != ''">
            and level = #{level}
        </if>
        <if test="typeId != null">
            and parent_id = #{typeId}
        </if>
    </select>

    <insert id="insertMachine" parameterType="com.bonus.material.ma.domain.Machine" useGeneratedKeys="true" keyProperty="maId">
        insert into ma_machine
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="typeId != null">type_id,</if>
            <if test="maCode != null">ma_code,</if>
            <if test="preCode != null">pre_code,</if>
            <if test="maStatus != null">ma_status,</if>
            <if test="qrCode != null">qr_code,</if>
            <if test="buyPrice != null">buy_price,</if>
            <if test="maVender != null">ma_vender,</if>
            <if test="outFacTime != null">out_fac_time,</if>
            <if test="outFacCode != null">out_fac_code,</if>
            <if test="assetsCode != null">assets_code,</if>
            <if test="checkMan != null">check_man,</if>
            <if test="thisCheckTime != null">this_check_time,</if>
            <if test="nextCheckTime != null">next_check_time,</if>
            <if test="gpsCode != null">gps_code,</if>
            <if test="rfidCode != null">rfid_code,</if>
            <if test="erpCode != null">erp_code,</if>
            <if test="transferCode != null">transfer_code,</if>
            <if test="inOutNum != null">in_out_num,</if>
            <if test="buyTask != null">buy_task,</if>
            <if test="ownHouse != null">own_house,</if>
            <if test="companyId != null">company_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="inspectMan != null">inspect_man,</if>
            <if test="inspectStatus != null">inspect_status,</if>
            <if test="phone != null">phone,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="typeId != null">#{typeId},</if>
            <if test="maCode != null">#{maCode},</if>
            <if test="preCode != null">#{preCode},</if>
            <if test="maStatus != null">#{maStatus},</if>
            <if test="qrCode != null">#{qrCode},</if>
            <if test="buyPrice != null">#{buyPrice},</if>
            <if test="maVender != null">#{maVender},</if>
            <if test="outFacTime != null">#{outFacTime},</if>
            <if test="outFacCode != null">#{outFacCode},</if>
            <if test="assetsCode != null">#{assetsCode},</if>
            <if test="checkMan != null">#{checkMan},</if>
            <if test="thisCheckTime != null">#{thisCheckTime},</if>
            <if test="nextCheckTime != null">#{nextCheckTime},</if>
            <if test="gpsCode != null">#{gpsCode},</if>
            <if test="rfidCode != null">#{rfidCode},</if>
            <if test="erpCode != null">#{erpCode},</if>
            <if test="transferCode != null">#{transferCode},</if>
            <if test="inOutNum != null">#{inOutNum},</if>
            <if test="buyTask != null">#{buyTask},</if>
            <if test="ownHouse != null">#{ownHouse},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="inspectMan != null">#{inspectMan},</if>
            <if test="inspectStatus != null">#{inspectStatus},</if>
            <if test="phone != null">#{phone},</if>
         </trim>
    </insert>

    <update id="updateMachine" parameterType="com.bonus.material.ma.domain.Machine">
        update ma_machine
        <trim prefix="SET" suffixOverrides=",">
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="maCode != null">ma_code = #{maCode},</if>
            <if test="preCode != null">pre_code = #{preCode},</if>
            <if test="maStatus != null">ma_status = #{maStatus},</if>
            <if test="qrCode != null">qr_code = #{qrCode},</if>
            <if test="buyPrice != null">buy_price = #{buyPrice},</if>
            <if test="maVender != null">ma_vender = #{maVender},</if>
            <if test="outFacTime != null">out_fac_time = #{outFacTime},</if>
            <if test="outFacCode != null">out_fac_code = #{outFacCode},</if>
            <if test="assetsCode != null">assets_code = #{assetsCode},</if>
            <if test="checkMan != null">check_man = #{checkMan},</if>
            <if test="thisCheckTime != null">this_check_time = #{thisCheckTime},</if>
            <if test="nextCheckTime != null">next_check_time = #{nextCheckTime},</if>
            <if test="gpsCode != null">gps_code = #{gpsCode},</if>
            <if test="rfidCode != null">rfid_code = #{rfidCode},</if>
            <if test="erpCode != null">erp_code = #{erpCode},</if>
            <if test="transferCode != null">transfer_code = #{transferCode},</if>
            <if test="inOutNum != null">in_out_num = #{inOutNum},</if>
            <if test="buyTask != null">buy_task = #{buyTask},</if>
            <if test="ownHouse != null">own_house = #{ownHouse},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="inspectMan != null">inspect_man = #{inspectMan},</if>
            <if test="inspectStatus != null">inspect_status = #{inspectStatus},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="assetsId != null">assets_id = #{assetsId},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where ma_id = #{maId}
    </update>

    <delete id="deleteMachineByMaId" parameterType="Long">
        delete from ma_machine where ma_id = #{maId}
    </delete>


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

    <select id="getMachineStatus" resultType="java.lang.String">
        select ma_status from ma_machine where ma_id = #{maId}
    </select>

    <update id="updateMaMachineStatus">
        UPDATE
            ma_machine
        SET
            ma_status = #{status},create_time = NOW()
        <where>
            type_id = #{record.typeId}
            <if test="record.maId != null and record.maId != ''">
                and ma_id = #{record.maId}
            </if>
        </where>
    </update>

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

    <select id="selectMachineByMaCode" resultType="com.bonus.material.ma.domain.vo.MachineVo">
        SELECT
            ma.ma_id as maId,ma.type_id as typeId,ma.ma_code as maCode,ma.pre_code as preCode,
            mt4.type_name as itemType,
            mt3.type_name as materialType,
            mt2.type_name as materialName,
            mt.type_name as materialModel,
            ma.ma_status as maStatus,ma.qr_code as qrCode,ma.buy_price as buyPrice,
            ma.ma_vender as maVender,ma.out_fac_time as outFacTime,ma.out_fac_code as outFacCode,
            ma.assets_code as assetsCode,ma.check_man as checkMan,ma.this_check_time as thisCheckTime,
            ma.next_check_time as nextCheckTime,ma.gps_code as gpsCode,ma.rfid_code as rfidCode,
            ma.erp_code as erpCode,ma.transfer_code as transferCode,ma.in_out_num as inOutNum,
            ma.buy_task as buyTask,ma.own_house as ownHouse,ma.company_id as companyId,ma.remark as remark,
            ma.create_time as createTime,ma.update_time as updateTime,ma.inspect_man as inspectMan,ma.assets_id as assetsId,
            ma.inspect_status as inspectStatus,ma.phone as phone,
            mtk.user_id as keeperId,
            mtr.user_id as repairId,
            baa.asset_name as assetName
        FROM
            ma_machine ma
        LEFT JOIN
            ma_type mt ON ma.type_id = mt.type_id and  mt.`level` = '4' and mt.del_flag = '0'
        LEFT JOIN
            ma_type mt2 ON mt2.type_id = mt.parent_id and mt2.`level` = '3' and mt2.del_flag = '0'
        LEFT JOIN
            ma_type mt3 ON mt2.parent_id = mt3.type_id and mt3.`level` = '2' and mt3.del_flag = '0'
        LEFT JOIN
            ma_type mt4 ON mt3.parent_id = mt4.type_id and mt4.`level` = '1' and mt4.del_flag = '0'
        LEFT JOIN
            ma_type_keeper mtk on ma.type_id = mtk.type_id
        LEFT JOIN
            ma_type_repair mtr on ma.type_id = mtr.type_id
        LEFT JOIN
            bm_asset_attributes baa on ma.assets_id = baa.id
        where
            ma.qr_code = #{qrCode}
        group by
            ma.ma_id
        limit 1
    </select>

    <select id="getMachineList" resultType="com.bonus.material.ma.domain.Machine">
        SELECT
            mt1.type_name as materialName,
            mt.type_name as materialModel,
            mt.type_id as typeId,
            mm.ma_id as maId,
            mm.ma_code as maCode
        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'
        WHERE
            mm.ma_status = '1' and mt.type_id = #{typeId}
    </select>

    <select id="getElectronicLabel" resultType="com.bonus.material.ma.domain.Machine">
        SELECT
            mm.ma_id as maId,
            mt2.type_name as materialName,
            mt.type_name as materialModel,
            mm.ma_code as maCode,
            mm.this_check_time as thisCheckTime,
            mm.next_check_time as nextCheckTime,
            mm.check_man as checkMan,
            mm.inspect_man as inspectMan,
            mm.phone,
            '合格' as inspectStatus,
            mm.ma_vender as maVender,
            mm.ex_code as exCode,
            mm.ex_url as exUrl,
            mm.is_jj as isJj,
            mm.type_id as typeId
        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_status = '2' and
            mm.ma_code like concat('%', #{maCode}, '%')
    </select>

    <select id="selectMaId" resultType="java.lang.Long">
        select ma_id
        from ma_machine
        WHERE ma_code = #{maCode}
    </select>

    <select id="selectHouse" resultType="com.bonus.material.ma.domain.Machine">
        SELECT
            whi.house_name as ownHouse
        FROM
            ma_type mt
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
                AND mt1.del_flag = '0'
                LEFT JOIN ma_type mt2 ON mt1.parent_id = mt2.type_id
                AND mt2.del_flag = '0'
                LEFT JOIN ma_type mt3 ON mt2.parent_id = mt3.type_id
                AND mt3.del_flag = '0'
                LEFT JOIN wh_house_set whs ON mt3.type_id = whs.type_id
                LEFT JOIN wh_house_info whi ON whs.house_id = whi.house_id
        WHERE
            mt.type_id = #{typeId}
    </select>

    <select id="getByMaCode" resultType="com.bonus.material.ma.domain.Machine">
        SELECT
            mi.id as maId,
            mi.ma_name as materialName,
            mi.ma_model as materialModel,
            mi.ma_code as maCode,
            mi.this_check_time as thisCheckTime,
            mi.next_check_time as nextCheckTime,
            mi.repair_man as repairMan,
            mi.check_man as checkMan,
            mi.phone,
            mi.result,null AS reportCode,null AS reportUrl,
            mi.type
        FROM
            ws_ma_info mi
        WHERE mi.ma_code LIKE CONCAT('%',#{maCode},'%') and mi.is_active = 1 and DATEDIFF(mi.next_check_time,NOW()) > 0
        UNION
        SELECT
            mm.ma_id AS id,
            mt2.type_name as maName,
            mt.type_name as maModel,
            mm.ma_code as maCode,
            mm.this_check_time as thisCheckTime,
            mm.next_check_time as nextCheckTime,
            mm.check_man as repairMan,
            ifnull(mm.inspect_man,"高民")   as checkMan,
            "0551-63703966" as phone,
            "合格" as result,mm.ex_code AS reportCode,mm.ex_url AS reportUrl,
            is_jj as type
        FROM ma_machine mm
                 LEFT JOIN ma_type mt on mm.type_id = mt.type_id
                 LEFT JOIN ma_type mt2 on mt.parent_id = mt2.type_id
        WHERE mm.ma_code LIKE CONCAT('%',#{maCode},'%') and mt.MANAGE_TYPE =0 and DATEDIFF(mm.next_check_time,NOW()) > 0
    </select>

    <delete id="deleteMachineByMaCodeAndTypeId">
        delete from ma_machine where ma_code = #{maCode} and type_id = #{typeId}
            and ma_status = '0'
    </delete>

    <update id="updateSynchReport" >
        update ma_machine
        <trim prefix="SET" suffixOverrides=",">
            <if test="tester != null">check_man = #{tester},inspect_man = #{tester},</if>
            <if test="checkTimeSynch != null">this_check_time = #{checkTimeSynch},</if>
            <if test="nextCheckTimeSynch != null">next_check_time = #{nextCheckTimeSynch},</if>
            <if test="result != null">inspect_status = #{result},</if>
            <if test="reportNum != null">ex_code = #{reportNum},</if>
            <if test="pdf != null">ex_url = #{pdf},</if>
            <if test="reportName != null">ex_name = #{reportName},</if>
            ex_up_time = NOW(),
        </trim>
        where ma_code = #{selfCode}
    </update>

    <update id="editAssetsCode" parameterType="com.bonus.material.ma.domain.Machine">
        update ma_machine
        SET
        assets_code = #{assetsCode}
        where ma_id = #{maId}
    </update>


    <select id="selectMachineByQrCode" resultType="com.bonus.material.ma.domain.Machine">
        SELECT
            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,
            mm.buy_price as buyPrice,
            mm.ma_vender as maVender,
            mm.out_fac_time as outFacTime,
            mm.out_fac_code as outFacCode,
            mm.assets_code as assetsCode,
            mm.check_man as checkMan,
            mm.this_check_time as thisCheckTime
        FROM
            ma_machine mm
        WHERE
            mm.qr_code = #{qrCode}

    </select>

    <select id="getInfoByQrcode" resultMap="MachineResult">
        SELECT
            mm.ma_id as maId,
            mt2.type_name as maName,
            mt.type_name as maModel,
            mm.ma_code as maCode,
            mm.qr_code as qrCode,
            mm.type_id as typeId,
            sd.dict_label as maStatus,
            mm.qr_code as qrCode,
            mm.ma_vender as maVender,
            mm.out_fac_time as outFacTime,
            mm.out_fac_code as outFacCode,
            mm.assets_code as assetsCode,
            mm.check_man as checkMan,
            mm.this_check_time as thisCheckTime,
            mm.next_check_time as nextCheckTime,
            mm.in_out_num as inOutNum
        FROM
            ma_machine mm
                LEFT JOIN ma_type mt ON mt.type_id = mm.type_id
                LEFT JOIN ma_type mt2 ON mt.parent_id = mt2.type_id
                LEFT JOIN sys_dict_data sd on mm.ma_status = sd.dict_value and sd.dict_type = "ma_machine_status"
        where mm.qr_code = #{qrCode}

    </select>

    <select id="getInTimeByQrcode" resultMap="MachineResult">
        SELECT
            bqi.qr_code as qrCode,
            bqi.task_id as taskId,
            LEFT(bqi.create_time,10) as inTime
        FROM
            bm_qrcode_info bqi
        WHERE bqi.qr_code = #{qrCode}
    </select>

    <select id="getServiceNumByQrcode" resultMap="MachineResult">
        SELECT
            COUNT(DISTINCT sai.agreement_id) as serviceNum
        FROM slt_agreement_info sai
        WHERE sai.ma_id =  #{maId}
    </select>

    <select id="getCheckNumByQrcode" resultMap="MachineResult">

        SELECT
            COUNT(rp.ma_id ) as checkNum
        FROM
            repair_part_details rp
        WHERE rp.ma_id =  #{maId} and rp.is_ds = 0
    </select>

    <select id="getScrapTimeByQrcode" resultMap="MachineResult">
        SELECT
            sd.ma_id,
            sd.task_id,
            sd.ledger_time
        FROM
            scrap_apply_details sd
        WHERE sd.ma_id = #{maId}

    </select>

    <select id="getLeaseInfoByQrcode" resultMap="MachineResult">
    </select>
</mapper>
