<?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,
        mt.jiju_type as jiJuType,
        mt.is_check as isCheck,
        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,
            jiju_type as jijuType
        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>
        <if test="manageType != null and manageType!=''">
            and manage_type=#{manageType}
        </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>
            <if test="materialModel != null">machine_name = #{materialModel},</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()
        <if test="record.thisCheckTime != null">
            ,this_check_time = #{record.thisCheckTime}
        </if>
        <if test="record.nextCheckTime != null">
            ,next_check_time = #{record.nextCheckTime}
        </if>
        <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},
            in_out_num = ifnull(in_out_num,0) + 1
        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 1 = 1
        <if test="maCode != null and maCode != ''">
            and ma.ma_code = #{maCode}
        </if>
        <if test="qrCode != null and qrCode != ''">
            and ma.qr_code = #{qrCode}
        </if>
        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_code like concat('%', #{maCode}, '%')
        <if test="maId != null">
          and mm.ma_id = #{maId}
        </if>
    </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,
            LEFT(mi.this_check_time, 10) AS thisCheckTime,
            LEFT(mi.next_check_time, 10) AS nextCheckTime,
            mi.repair_man as repairMan,
            mi.check_man as checkMan,
            mi.phone,
            mi.result,null AS reportCode,null AS reportUrl,
            mi.type,
            '1' as devType
        FROM
            ws_ma_info mi
        WHERE mi.ma_code LIKE CONCAT('%',#{maCode},'%')
        UNION
        SELECT
            mm.ma_id AS id,
            mt2.type_name as maName,
            mt.type_name as maModel,
            mm.ma_code as maCode,
            LEFT(mm.this_check_time, 10) AS thisCheckTime,
            LEFT(mm.next_check_time, 10) 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,
            '2' as devType
        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
    </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>

    <update id="batchUpdateSynchReport" parameterType="java.util.List">
        UPDATE ma_machine
        SET
        check_man = CASE ma_code
        <foreach collection="list" item="item">
            WHEN #{item.selfCode} THEN #{item.tester}
        </foreach>
        END,
        inspect_man = CASE ma_code
        <foreach collection="list" item="item">
            WHEN #{item.selfCode} THEN #{item.tester}
        </foreach>
        END,
        this_check_time = CASE ma_code
        <foreach collection="list" item="item">
            WHEN #{item.selfCode} THEN #{item.checkTimeSynch}
        </foreach>
        END,
        next_check_time = CASE ma_code
        <foreach collection="list" item="item">
            WHEN #{item.selfCode} THEN #{item.nextCheckTimeSynch}
        </foreach>
        END,
        inspect_status = CASE ma_code
        <foreach collection="list" item="item">
            WHEN #{item.selfCode} THEN #{item.result}
        </foreach>
        END,
        ex_code = CASE ma_code
        <foreach collection="list" item="item">
            WHEN #{item.selfCode} THEN #{item.reportNum}
        </foreach>
        END,
        ex_url = CASE ma_code
        <foreach collection="list" item="item">
            WHEN #{item.selfCode} THEN #{item.pdf}
        </foreach>
        END,
        ex_name = CASE ma_code
        <foreach collection="list" item="item">
            WHEN #{item.selfCode} THEN #{item.reportName}
        </foreach>
        END,
        ex_up_time = NOW()
        WHERE ma_code IN (
        <foreach collection="list" item="item" separator=",">
            #{item.selfCode}
        </foreach>
        )
    </update>
    <update id="editMachineStatus">
        update ma_machine
        set ma_status = #{maStatus}
        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
        1 = 1
        <if test="qrCode != null and qrCode != ''">
            and mm.qr_code = #{qrCode}
        </if>
    </select>

    <select id="getInfoByQrcode" resultType="com.bonus.material.ma.domain.Machine">
        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,
            msi.supplier 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,
            mt.jiju_type as devType
        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"
                LEFT JOIN ma_supplier_info msi on msi.supplier_id = mm.ma_vender
        where mm.qr_code = #{qrCode}

    </select>

    <select id="getInTimeByQrcode" resultType="com.bonus.material.ma.domain.Machine">
        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" resultType="com.bonus.material.ma.domain.Machine">
        SELECT
            COUNT(DISTINCT sai.agreement_id) as serviceNum
        FROM slt_agreement_info sai
        WHERE sai.ma_id =  #{maId}
    </select>

    <select id="getCheckNumByQrcode" resultType="com.bonus.material.ma.domain.Machine">

        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" resultType="com.bonus.material.ma.domain.Machine">
        SELECT
            sd.ma_id,
            sd.task_id,
            sd.ledger_time
        FROM
            scrap_apply_details sd
        WHERE sd.ma_id = #{maId}

    </select>

    <select id="getLeaseInfoByQrcode" resultType="com.bonus.material.ma.domain.Machine">
        SELECT
            sai.id,
            bu.unit_name AS leaseUnit,
            bp.pro_name AS leaseProject,
            lai.create_time AS leaseTime,
            IF(   sai.status = 1, bu.unit_name,"") AS backUnit,
            IF(   sai.status = 1,  bp.pro_name ,"") AS backProject,
            IF(   sai.status = 1, bai.create_time ,"") AS backTime,
            sai.status
        FROM
            slt_agreement_info sai
                LEFT JOIN bm_agreement_info ba on sai.agreement_id = ba.agreement_id
                LEFT JOIN bm_unit bu on ba.unit_id = bu.unit_id
                LEFT JOIN bm_project bp on ba.project_id = bp.pro_id
                LEFT JOIN lease_apply_info lai ON sai.lease_id = lai.id
                LEFT JOIN back_apply_info bai ON bai.id = sai.back_id
        WHERE sai.ma_id = #{maId}
        ORDER BY lai.create_time desc
            limit 1
    </select>

    <select id="getListByCode" resultType="com.bonus.material.ma.domain.Machine">
        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 1=1
        <if test="maCode != null">and mm.ma_code LIKE CONCAT('%',#{maCode},'%')</if>


        <if test="maId != null">and mm.ma_id = #{maId}</if>
       limit 100
    </select>

    <select id="getInfoByMaCode" resultType="com.bonus.material.ma.domain.Machine">
        SELECT
        ma.ma_id as maId,
        ma.type_id as typeId,
        mt2.type_name as materialName,
        CASE WHEN ma.machine_name IS NOT NULL THEN ma.machine_name ELSE mt.type_name END as materialModel,
        ma.ma_code as maCode,
        ma.pre_code as preCode,
        sda.dict_label as maStatus,
        ma.qr_code as qrCode,
        ma.buy_price as buyPrice,
        msi.supplier as maVender,
        msi.supplier_id AS supplierId,
        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.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,
        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 sys_dict_data sda on sda.dict_value = ma.ma_status
        and sda.dict_type = "ma_machine_status"
        LEFT JOIN ma_supplier_info msi ON msi.supplier_id = ma.ma_vender
        where
        1=1
        <if test="maCode != null and maCode != ''">
            and ma.ma_code LIKE CONCAT('%',#{maCode},'%')
        </if>
        <if test="qrCode != null and qrCode != ''">
            and ma.qr_code = #{qrCode}
        </if>
    </select>

    <select id="selectMaInfoByMaCode" resultType="com.bonus.material.ma.domain.vo.MachineVo">
        SELECT
            ma_name as materialName,
            ma_model as materialModel,
            ma_code as maCode,
            supplier as maVender,
            this_check_time as thisCheckTime,
            next_check_time as nextCheckTime,
            repair_man as inspectMan,
            check_man as checkMan,
            model_id as typeId,
            result as inspectStatus,
            phone as phone,
            id as maId
        FROM
            ws_ma_info
        where ma_code = #{maCode}
    </select>

    <select id="getInfoByTypeId" resultType="com.bonus.material.ma.domain.Machine">
        SELECT
        wsi.id as maId,
        wsi.model_id as typeId,
        wsi.ma_name as materialName,
        wsi.ma_model as materialModel,
        wsi.ma_code as maCode,
        wsi.qr_code as qrCode
        FROM
        ws_ma_info wsi
        LEFT JOIN ma_type mt ON wsi.model_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'
        WHERE
        1 = 1
        <if test="typeId != null ">
            and wsi.model_id = #{typeId}
        </if>
        <if test="maCode != null and maCode != ''">
            and wsi.ma_code LIKE CONCAT('%',#{maCode},'%')
        </if>
    </select>
    <select id="getHisByCodeNew" resultType="com.bonus.material.ma.domain.Machine">
        (
            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.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,
                mt.manage_type as manageType,
                mm.in_out_num as inOutNum,
                mt.jiju_type as jiJuType,
                mt.is_check as isCheck,
                '2' as devType
            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
                1=1
        <if test="maCode != null"> and mm.ma_code LIKE CONCAT('%',#{maCode}, '%') </if>
        <if test="maId != null">and mm.ma_id = #{maId}</if>
                LIMIT 100
        )
        UNION ALL
        (
            SELECT
                wmi.id as maId,
                wmi.ma_name as maName,
                wmi.ma_model as maModel,
                wmi.ma_code as maCode,
                wmi.qr_code as qrCode,
                wmi.model_id as typeId,
                '' as maStatus,
                wmi.supplier as maVender,
                '' as outFacTime,
                '' as outFacCode,
                '' as assetsCode,
                wmi.check_man as checkMan,
                wmi.this_check_time as thisCheckTime,
                wmi.next_check_time as nextCheckTime,
                1 as manageType,
                '' as inOutNum,
                '1' as jiJuType,
                '' as isCheck,
                '1' as devType
            FROM ws_ma_info wmi
            WHERE
                1=1
        <if test="maCode != null"> and wmi.ma_code LIKE CONCAT('%',#{maCode}, '%')</if>
        <if test="maId != null">and wmi.id = #{maId}</if>
                LIMIT 100
        )
    </select>

    <select id="getElectronicLabelByWsMaInfo" resultType="com.bonus.material.ma.domain.Machine">
        SELECT
            wmi.id as maId,
            wmi.ma_name AS materialName,
            wmi.ma_model AS materialModel,
            wmi.ma_code as maCode,
            wmi.this_check_time as thisCheckTime,
            wmi.next_check_time as nextCheckTime,
            wmi.check_man as inspectMan,
            wmi.repair_man as checkMan,
            wmi.phone,
            '合格' AS inspectStatus,
            wmi.supplier as maVender,
            '0' as isJj,
            wmi.model_id as typeId
        FROM
            ws_ma_info wmi
        WHERE
        wmi.ma_code like concat('%', #{maCode}, '%')
        <if test="maId != null">
            and wmi.id = #{maId}
        </if>
    </select>
    <select id="getHisByCodeNewByWsMaInfo" resultType="com.bonus.material.ma.domain.Machine">
        SELECT
        wmi.id as maId,
        wmi.ma_name as maName,
        wmi.ma_model as maModel,
        wmi.ma_code as maCode,
        wmi.qr_code as qrCode,
        wmi.model_id as typeId,
        '' as maStatus,
        wmi.supplier as maVender,
        '' as outFacTime,
        '' as outFacCode,
        '' as assetsCode,
        wmi.check_man as checkMan,
        wmi.this_check_time as thisCheckTime,
        wmi.next_check_time as nextCheckTime,
        '' as inOutNum,
        '1' as jiJuType,
        '1' as devType
        FROM ws_ma_info wmi
        WHERE
        1=1
        <if test="maCode != null"> and wmi.ma_code LIKE CONCAT('%',#{maCode}, '%')</if>
        <if test="maId != null">and wmi.id = #{maId}</if>
        LIMIT 100
    </select>
    <select id="getLeaseParentId" resultType="com.bonus.common.biz.domain.lease.LeaseApplyInfo">
        select parent_id as parentId,DATE_FORMAT(create_time, '%Y-%m-%d') as leaseTime from lease_out_details where ma_id = #{maId} order by create_time desc limit 1
    </select>
    <select id="getLeaseUnitAndProject" resultType="com.bonus.common.biz.domain.lease.LeaseApplyInfo">
        select
            bu.unit_name as unitName,
            bp.pro_name as projectName,
            DATE_FORMAT(lai.create_time, '%Y-%m-%d') as leaseTime
        from lease_apply_info lai
                 left join tm_task_agreement tta on lai.task_id = tta.task_id
                 left join bm_agreement_info bai on tta.agreement_id = bai.agreement_id
                 left join bm_unit bu on bai.unit_id = bu.unit_id
                 left join bm_project bp on bai.project_id = bp.pro_id
        where lai.id = #{parentId}
    </select>
    <select id="getBackParentId" resultType="com.bonus.common.biz.domain.lease.LeaseApplyInfo">
        select parent_id as parentId from back_check_details where ma_id = #{maId} order by create_time desc limit 1
    </select>
    <select id="getBackUnitAndProject" resultType="com.bonus.common.biz.domain.lease.LeaseApplyInfo">
        select
            bu.unit_name as unitName,
            bp.pro_name as projectName,
            DATE_FORMAT(ba.create_time, '%Y-%m-%d') as leaseTime
        from back_apply_info ba
                 left join tm_task_agreement tta on ba.task_id = tta.task_id
                 left join bm_agreement_info bai on tta.agreement_id = bai.agreement_id
                 left join bm_unit bu on bai.unit_id = bu.unit_id
                 left join bm_project bp on bai.project_id = bp.pro_id
        where ba.id = #{parentId}
    </select>

    <update id="updateMaStatus">
        update ma_machine set ma_status = 1 where ma_id = #{maId}
    </update>

    <select id="getNewByMaCode" 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,
            LEFT(mm.this_check_time, 10) AS thisCheckTime,
            LEFT(mm.next_check_time, 10) 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,
            mt.jiju_type as devType
        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
        UNION
        SELECT
            mi.id as maId,
            mi.ma_name as materialName,
            mi.ma_model as materialModel,
            mi.ma_code as maCode,
            LEFT(mi.this_check_time, 10) AS thisCheckTime,
            LEFT(mi.next_check_time, 10) AS nextCheckTime,
            mi.repair_man as repairMan,
            mi.check_man as checkMan,
            mi.phone,
            mi.result,null AS reportCode,null AS reportUrl,
            mi.type,
            '1' as devType
        FROM
            ws_ma_info mi
        WHERE mi.ma_code LIKE CONCAT('%',#{maCode},'%')

            LIMIT 60
    </select>

    <select id="findMaMsgById" 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,
            LEFT(mi.this_check_time, 10) AS thisCheckTime,
            LEFT(mi.next_check_time, 10) AS nextCheckTime,
            mi.repair_man as repairMan,
            mi.check_man as checkMan,
            mi.phone,
            mi.result,null AS reportCode,null AS reportUrl,
            mi.type,
            '1' as devType
        FROM
            ws_ma_info mi
        WHERE mi.id =#{maId}
    </select>
    <select id="getCountByMaCode" resultType="java.lang.Integer">
        SELECT count(1)
        FROM ma_machine
        WHERE ma_code = #{maCode}
          and type_id = #{typeId}
    </select>
    <select id="getSampleList" resultType="com.bonus.material.ma.domain.vo.SampleSync">
        select
            ma_code as maCode,
            ma_status as maStatus
        from ma_machine
        where ma_code = #{selfCode}
        limit 1
    </select>
    <select id="selectSynchDetailList" resultType="com.bonus.material.ma.domain.MachineSynch">
        select
            mcd.ma_code as maCode,
            mcd.check_man as checkMan,
            DATE_FORMAT(mcd.this_check_time, '%Y-%m-%d') as checkTimeSynch,
            DATE_FORMAT(mcd.next_check_time, '%Y-%m-%d') as nextCheckTimeSynch,
            mcd.is_success as isSuccess,
            mcd.reason as result,
            mcd.ex_code as reportNum,
            mt.type_id as typeId,
            mt2.type_name as typeModelName,
            mt.type_name as typeName
        from ma_check_details mcd
         left join ma_machine mm on mcd.ma_code = mm.ma_code
         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
            mcd.parent_id = #{parentId}
            <if test="keyWord != null and keyWord != ''">
                and (
                mcd.ma_code like concat('%', #{keyWord}, '%')
                or mcd.reason like concat('%', #{keyWord}, '%')
                or mcd.ex_code like concat('%', #{keyWord}, '%')
                or mt2.type_name like concat('%', #{keyWord}, '%')
                or mt.type_name like concat('%', #{keyWord}, '%')
                )
            </if>
    </select>

    <select id="selectSynchList" resultType="com.bonus.material.ma.domain.MachineSynch">
        select
        mci.id as id,
        mci.ex_code as reportNum,
        mci.create_time as createTime,
        su.nick_name as createBy
        from ma_check_info mci
        left join sys_user su on mci.create_by = su.user_id
        <where>
            <if test="keyWord != null and keyWord != ''">
                (
                mci.ex_code like concat('%', #{keyWord}, '%')
                or su.nick_name like concat('%', #{keyWord}, '%')
                )
            </if>
        </where>
        order by mci.create_time desc
    </select>

    <select id="getBigList" resultType="com.bonus.material.ma.domain.vo.SampleSync">
        select
             CONCAT_WS('-', mt2.type_name, mm.ma_code) as typeNameCode,
             mm.ma_code as maCode,
             mm.ma_status as maStatus
        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 mt.jiju_type = 2
    </select>

    <insert id="batchInsertSynchDetails" >
        INSERT INTO ma_check_details (
        parent_id,
        ma_code,
        check_man,
        this_check_time,
        next_check_time,
        is_success,
        reason,
        ex_code,
        type_name
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
             #{item.parentId},
            #{item.selfCode},
            #{item.tester},
            #{item.checkTimeSynch},
            #{item.nextCheckTimeSynch},
            #{item.isSuccess},
            #{item.result},
            #{item.reportNum},
            #{item.sampleName}
            )
        </foreach>
    </insert>
    <insert id="insertInfo" useGeneratedKeys="true" keyProperty="id">
        insert into ma_check_info(
        ex_code,
        create_by,
        create_time
        )
        values(
       #{reportNum},
       #{userId},
       NOW()
        )

    </insert>
</mapper>
