<?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.codeCollection.mapper.WsMaInfoMapper">

    <resultMap id="BaseResultMap" type="com.bonus.material.codeCollection.domain.WsMaInfo">
        <id column="id" property="id"/>
        <result column="ma_name" property="maName"/>
        <result column="ma_model" property="maModel"/>
        <result column="ma_code" property="maCode"/>
        <result column="supplier" property="supplier"/>
        <result column="this_check_time" property="thisCheckTime"/>
        <result column="next_check_time" property="nextCheckTime"/>
        <result column="repair_man" property="repairMan"/>
        <result column="check_man" property="checkMan"/>
        <result column="phone" property="phone"/>
        <result column="result" property="result"/>
        <result column="type" property="type"/>
        <result column="model_id" property="modelId"/>
        <result column="is_active" property="isActive"/>
        <result column="opt_user" property="optUser"/>
        <result column="opt_time" property="optTime"/>
    </resultMap>

    <select id="selectById" resultMap="BaseResultMap">
        SELECT id,
               ma_name,
               ma_model,
               ma_code,
               supplier,
               DATE(now()) as this_check_time,
               DATE(DATE_SUB(DATE_ADD(NOW(), INTERVAL 1 YEAR), INTERVAL 1 DAY)) as next_check_time,
               repair_man,
               check_man,
               phone,
               result,
               type,
               model_id,
               is_active,
               opt_user,
               opt_time
        FROM ws_ma_info
        WHERE id = #{id}
    </select>



    <select id="getMaTypeData" resultType="java.util.Map">
        SELECT type_id   AS id,
               type_name AS `name`
        FROM ma_type
        WHERE `level` = '3'
          AND del_flag = '0'
          and is_enter = '1'
    </select>

    <select id="getMaModeData" resultType="java.util.Map">
        SELECT type_id   AS id,
               type_name AS `name`
        FROM ma_type
        WHERE parent_id = #{parentId}
          AND del_flag = '0'
    </select>
    <select id="getSupplier" resultType="java.util.Map">
        SELECT supplier_id AS id,
               supplier    AS `name`
        FROM ma_supplier_info
        WHERE del_flag = '0'
    </select>
    <select id="existsByModelAndCode" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM ws_ma_info
        WHERE
          ma_code = #{maCode}
    </select>

    <select id="selectAll"  resultType="com.bonus.material.codeCollection.domain.WsMaInfo">
        SELECT * FROM (
        SELECT
        id AS id,
        ma_code AS maCode,
        ma_name AS maName,
        LEFT(next_check_time, 10) AS nextCheckTime,
        LEFT(this_check_time, 10) AS thisCheckTime,
        ma_model AS maModel,
        repair_man AS repairMan
        FROM
        ws_ma_info
        <where>
            <if test="maCode != null and maCode != ''">
                and (ma_code like concat('%', #{maCode}, '%')
                or qr_code = #{maCode})
            </if>
        </where>
        LIMIT 10
        ) AS t1

        UNION ALL

        SELECT * FROM (
        SELECT
        mm.ma_id AS id,
        mm.ma_code AS maCode,
        mt1.type_name AS maName,
        LEFT(mm.next_check_time, 10) AS nextCheckTime,
        LEFT(mm.this_check_time, 10) AS thisCheckTime,
        mt.type_name AS maModel,
        mm.check_man AS repairMan
        FROM
        ma_machine mm
        LEFT JOIN ma_type mt ON mm.type_id = mt.type_id
        LEFT JOIN ma_type mt1 ON mt1.type_id = mt.parent_id
        <where>
            <if test="maCode != null and maCode != ''">
                and (mm.ma_code like concat('%', #{maCode}, '%')
                or mm.qr_code = #{maCode})
            </if>
        </where>
        LIMIT 10
        ) AS t2

    </select>
    <select id="getGadgetList" resultType="com.bonus.material.codeCollection.domain.WsMaInfo">
        SELECT
        id,
        ma_name AS maName,
        ma_model AS maModel,
        ma_code AS maCode,
        this_check_time AS thisCheckTime,
        next_check_time AS nextCheckTime,
        repair_man AS repairMan,
        check_man AS checkMan,
        phone,
        result
        FROM ws_ma_info FORCE INDEX (idx_ws_ma_info_basic)
        WHERE ma_name IS NOT NULL
        <if test="keyWord != null and keyWord != ''">
            and ( ma_name like concat('%', #{keyWord}, '%') or
            ma_model like concat('%', #{keyWord}, '%') or
            ma_code like concat('%', #{keyWord}, '%') or
            repair_man like concat('%', #{keyWord}, '%') or
            check_man like concat('%', #{keyWord}, '%')
            )
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[
          and (
              DATE_FORMAT( this_check_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} or
              DATE_FORMAT( next_check_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
          )
            ]]>
        </if>
        ORDER BY ma_name, ma_model, this_check_time DESC
    </select>
    <select id="getGadgetInfo" resultType="com.bonus.material.codeCollection.domain.WsMaInfo">
        SELECT wmi.id,
               wmi.ma_name         AS maName,
               wmi.ma_model        AS maModel,
               wmi.ma_code         AS maCode,
               wmi.this_check_time AS thisCheckTime,
               wmi.next_check_time AS nextCheckTime,
               wmi.repair_man      AS repairMan,
               wmi.check_man       AS checkMan,
               mt.deviceTypeId,
               mt.deviceModelId,
               wmi.phone,
               wmi.result
        FROM ws_ma_info wmi
                 LEFT JOIN (
            SELECT
                mt.type_id as deviceModelId,
                mt2.type_id as deviceTypeId,
                mt.type_name as modelName,
                mt2.type_name as typeName
            FROM
                ma_type mt
                    LEFT JOIN ma_type mt2 on mt.parent_id=mt2.type_id
            WHERE
                mt.`level`='4'
        ) mt on wmi.ma_name=mt.typeName and wmi.ma_model=mt.modelName
        WHERE wmi.id = #{id}
    </select>
    <select id="selectCountByCode" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM ws_ma_info
        WHERE ma_code = #{maCode}
          and ma_name = #{maName}
          and ma_model = #{maModel}
    </select>
    <select id="getInfoByTypeAndModelAndCode" resultType="com.bonus.material.codeCollection.domain.WsMaInfo">
        SELECT id,
               ma_name AS maName,
               ma_model AS maModel,
               ma_code AS maCode,
               this_check_time AS thisCheckTime,
               next_check_time AS nextCheckTime,
               repair_man AS repairMan,
               check_man AS checkMan,
               phone,
               result
        FROM ws_ma_info
        WHERE ma_name = #{maName}
          and ma_model = #{maModel}
          and ma_code = #{maCode}
    </select>
    <select id="listByLevelAndName" resultType="com.bonus.material.ma.domain.Type">
        SELECT type_id AS typeId, type_name as typeName, level, parent_id as parentId
        FROM ma_type
        WHERE level = #{level}
          AND type_name = #{typeName}
    </select>
    <select id="selectTypeRelation" resultType="com.bonus.material.ma.domain.Type">
        SELECT
            mt3.type_id as parentId,
            mt4.type_id as typeId
        FROM ma_type mt3
                 INNER JOIN ma_type mt4 ON mt3.type_id = mt4.parent_id
        WHERE mt3.level = #{parentLevel}
          AND mt3.type_name = #{parentName}
          AND mt4.level = #{childLevel}
          AND mt4.type_name = #{childName}
            LIMIT 1
    </select>
    <insert id="insert" parameterType="com.bonus.material.codeCollection.domain.WsMaInfo" useGeneratedKeys="true"
            keyProperty="id">
        INSERT INTO ws_ma_info (ma_name, ma_model, ma_code, supplier, this_check_time, next_check_time,
                                repair_man, check_man, phone, result, type, model_id, is_active, opt_user, opt_time, qr_code)
        VALUES (#{maName}, #{maModel}, #{maCode}, #{supplier}, DATE(now()),
                DATE(DATE_SUB(DATE_ADD(NOW(), INTERVAL 1 YEAR), INTERVAL 1 DAY)),
                #{repairMan}, #{checkMan}, #{phone}, #{result}, #{type}, #{modelId}, #{isActive}, #{optUser}, now(), #{qrCode})
    </insert>
    <insert id="addWsMaInfoData">
        INSERT INTO ws_ma_info (ma_name, ma_model, ma_code, this_check_time, next_check_time,
                                repair_man, check_man, phone, result, model_id, opt_user, opt_time)
        VALUES (#{maName}, #{maModel}, #{maCode}, #{thisCheckTime},
                #{nextCheckTime},
                #{repairMan}, #{checkMan}, #{phone}, #{result}, #{modelId}, #{optUser}, now())
    </insert>

    <update id="update" parameterType="com.bonus.material.codeCollection.domain.WsMaInfo">
        UPDATE ws_ma_info
        SET ma_name         = #{maName},
            ma_model        = #{maModel},
            ma_code         = #{maCode},
            supplier        = #{supplier},
            this_check_time = #{thisCheckTime},
            next_check_time = #{nextCheckTime},
            repair_man      = #{repairMan},
            check_man       = #{checkMan},
            phone           = #{phone},
            result          = #{result},
            type            = #{type},
            model_id        = #{modelId},
            is_active       = #{isActive},
            opt_user        = #{optUser},
            opt_time        = #{optTime}
        WHERE id = #{id}
    </update>

        <update id="updateCheckTime">
        <!-- 更新 ws_ma_info 表 -->
        UPDATE ws_ma_info
        SET this_check_time = #{thisCheckTime},
        next_check_time = #{nextCheckTime},
        opt_time = NOW()
        WHERE ma_code = #{maCode};

        <!-- 更新 ma_machine 表 -->
        UPDATE ma_machine
        SET this_check_time = #{thisCheckTime},
        next_check_time = #{nextCheckTime},
        update_time = NOW()
        WHERE ma_code = #{maCode}
    </update>
    <update id="updateGadgetInfo">
        UPDATE ws_ma_info
        SET ma_name         = #{maName},
            ma_model        = #{maModel},
            ma_code         = #{maCode},
            this_check_time = #{thisCheckTime},
            next_check_time = #{nextCheckTime},
            repair_man      = #{repairMan},
            check_man       = #{checkMan},
            phone           = #{phone},
            result          = #{result},
            model_id        = #{modelId},
            opt_user        = #{optUser},
            opt_time        = NOW()
        WHERE id = #{id}
    </update>

    <delete id="deleteById" parameterType="int">
        DELETE
        FROM ws_ma_info
        WHERE id = #{id}
    </delete>
    <delete id="delGadget">
        DELETE
        FROM ws_ma_info
        WHERE id = #{id}
    </delete>


</mapper>
