<?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 *
        FROM ws_ma_info
        WHERE id = #{id}
    </select>

    <select id="selectAll" resultMap="BaseResultMap">
        SELECT *
        FROM ws_ma_info
    </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 id
        FROM ws_ma_info
        WHERE ma_model = #{maModel}
          AND ma_code = #{maCode}
        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)
        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())
    </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">
        UPDATE ws_ma_info
        SET this_check_time = DATE(now()),
            next_check_time =  DATE(DATE_SUB(DATE_ADD(NOW(), INTERVAL 1 YEAR), INTERVAL 1 DAY)),
            opt_time        = DATE(now())
        WHERE id = #{id}
    </update>

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


</mapper>
