<?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.railway.business.safetool.mapper.BaseSafetyToolMapper">

    <resultMap id="BaseResultMap" type="com.railway.business.safetool.domain.BaseSafetyTool">
                <result column="tool_id" property="toolId"/>
                <result column="dept_id" property="deptId"/>
                <result column="dept_name" property="deptName"/>
                <result column="store_place" property="storePlace"/>
                <result column="tool_name" property="toolName"/>
                <result column="tool_type" property="toolType"/>
                <result column="tool_code" property="toolCode"/>
                <result column="elec_level" property="elecLevel"/>
                <result column="unit" property="unit"/>
                <result column="test_cycle" property="testCycle"/>
                <result column="test_cycle_text" property="testCycleText"/>
                <result column="last_test_date" property="lastTestDate"/>
                <result column="next_test_date" property="nextTestDate"/>
                <result column="test_result" property="testResult"/>
                <result column="test_date" property="testDate"/>
                <result column="test_result_desc" property="testResultDesc"/>
                <result column="state" property="state"/>
                <result column="scrap_reason" property="scrapReason"/>
                <result column="scrap_date" property="scrapDate"/>
                <result column="scrap_user" property="scrapUser"/>
                <result column="replace_tool_id" property="replaceToolId"/>
                <result column="produce_factory" property="produceFactory"/>
                <result column="produce_date" property="produceDate"/>
                <result column="remark" property="remark"/>
                <result column="del_flag" property="delFlag"/>
                <result column="create_by" property="createBy"/>
                <result column="create_time" property="createTime"/>
                <result column="update_by" property="updateBy"/>
                <result column="update_time" property="updateTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        t.tool_id,
        t.dept_id,
        dept.dept_name,
        t.store_place,
        dict1.dict_label as tool_name,
        t.tool_type,
        t.tool_code,
        t.elec_level,
        dict1.remark unit,
        t.test_cycle,
        dict2.dict_label as test_cycle_text,
        t.last_test_date,
        t.next_test_date,
        t.test_result,
        t.test_date,
        t.test_result_desc,
        t.state,
        t.scrap_reason,
        t.scrap_date,
        t.scrap_user,
        t.replace_tool_id,
        t.produce_factory,
        t.produce_date,
        t.remark,
        t.del_flag,
        t.create_by,
        t.create_time,
        t.update_by,
        t.update_time
    </sql>

    <insert id="insert" parameterType="com.railway.business.safetool.domain.BaseSafetyTool"  useGeneratedKeys="true" keyProperty="toolId">
        INSERT INTO base_safety_tool
        <trim prefix="(" suffix=")" suffixOverrides=",">
                    <if test ='null != deptId'>
                    dept_id,
                    </if>
                    <if test ='null != storePlace'>
                    store_place,
                    </if>
                    <if test ='null != toolName'>
                    tool_name,
                    </if>
                    <if test ='null != toolType'>
                    tool_type,
                    </if>
                    <if test ='null != toolCode'>
                    tool_code,
                    </if>
                    <if test ='null != elecLevel'>
                    elec_level,
                    </if>
                    <if test ='null != unit'>
                    unit,
                    </if>
                    <if test ='null != testCycle'>
                    test_cycle,
                    </if>
                    <if test ='null != lastTestDate'>
                    last_test_date,
                    </if>
                    <if test ='null != nextTestDate'>
                    next_test_date,
                    </if>
                    <if test ='null != testResult'>
                    test_result,
                    </if>
                    <if test ='null != testDate'>
                    test_date,
                    </if>
                    <if test ='null != testResultDesc'>
                    test_result_desc,
                    </if>
                    <if test ='null != state'>
                    state,
                    </if>
                    <if test ='null != scrapReason'>
                    scrap_reason,
                    </if>
                    <if test ='null != scrapDate'>
                    scrap_date,
                    </if>
                    <if test ='null != scrapUser'>
                    scrap_user,
                    </if>
                    <if test ='null != replaceToolId'>
                    replace_tool_id,
                    </if>
                    <if test ='null != produceFactory'>
                    produce_factory,
                    </if>
                    <if test ='null != produceDate'>
                    produce_date,
                    </if>
                    <if test ='null != remark'>
                    remark,
                    </if>
                    <if test ='null != delFlag'>
                    del_flag,
                    </if>
                    <if test ='null != createBy'>
                    create_by,
                    </if>
                    <if test ='null != createTime'>
                    create_time,
                    </if>
                    <if test ='null != updateBy'>
                    update_by,
                    </if>
                    <if test ='null != updateTime'>
                    update_time
                    </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                    <if test ='null != deptId'>
                    #{deptId},
                    </if>
                    <if test ='null != storePlace'>
                    #{storePlace},
                    </if>
                    <if test ='null != toolName'>
                    #{toolName},
                    </if>
                    <if test ='null != toolType'>
                    #{toolType},
                    </if>
                    <if test ='null != toolCode'>
                    #{toolCode},
                    </if>
                    <if test ='null != elecLevel'>
                    #{elecLevel},
                    </if>
                    <if test ='null != unit'>
                    #{unit},
                    </if>
                    <if test ='null != testCycle'>
                    #{testCycle},
                    </if>
                    <if test ='null != lastTestDate'>
                    #{lastTestDate},
                    </if>
                    <if test ='null != nextTestDate'>
                    #{nextTestDate},
                    </if>
                    <if test ='null != testResult'>
                    #{testResult},
                    </if>
                    <if test ='null != testDate'>
                    #{testDate},
                    </if>
                    <if test ='null != testResultDesc'>
                    #{testResultDesc},
                    </if>
                    <if test ='null != state'>
                    #{state},
                    </if>
                    <if test ='null != scrapReason'>
                    #{scrapReason},
                    </if>
                    <if test ='null != scrapDate'>
                    #{scrapDate},
                    </if>
                    <if test ='null != scrapUser'>
                    #{scrapUser},
                    </if>
                    <if test ='null != replaceToolId'>
                    #{replaceToolId},
                    </if>
                    <if test ='null != produceFactory'>
                    #{produceFactory},
                    </if>
                    <if test ='null != produceDate'>
                    #{produceDate},
                    </if>
                    <if test ='null != remark'>
                    #{remark},
                    </if>
                    <if test ='null != delFlag'>
                    #{delFlag},
                    </if>
                    <if test ='null != createBy'>
                    #{createBy},
                    </if>
                    <if test ='null != createTime'>
                    #{createTime},
                    </if>
                    <if test ='null != updateBy'>
                    #{updateBy},
                    </if>
                    <if test ='null != updateTime'>
                    #{updateTime}
                    </if>
        </trim>
    </insert>

    <delete id="delete">
        UPDATE base_safety_tool
        set del_flag='1'
        WHERE tool_id = #{toolId}
    </delete>

    <update id="update" parameterType="com.railway.business.safetool.domain.BaseSafetyTool">
        UPDATE base_safety_tool
        <set>
                    <if test ='null != deptId'>dept_id = #{deptId},</if>
                    <if test ='null != storePlace'>store_place = #{storePlace},</if>
                    <if test ='null != toolName'>tool_name = #{toolName},</if>
                    <if test ='null != toolType'>tool_type = #{toolType},</if>
                    <if test ='null != toolCode'>tool_code = #{toolCode},</if>
                    <if test ='null != elecLevel'>elec_level = #{elecLevel},</if>
                    <if test ='null != unit'>unit = #{unit},</if>
                    <if test ='null != testCycle'>test_cycle = #{testCycle},</if>
                    <if test ='null != lastTestDate'>last_test_date = #{lastTestDate},</if>
                    <if test ='null != nextTestDate'>next_test_date = #{nextTestDate},</if>
                    <if test ='null != testResult'>test_result = #{testResult},</if>
                    <if test ='null != testDate'>test_date = #{testDate},</if>
                    <if test ='null != testResultDesc'>test_result_desc = #{testResultDesc},</if>
                    <if test ='null != state'>state = #{state},</if>
                    <if test ='null != scrapReason'>scrap_reason = #{scrapReason},</if>
                    <if test ='null != scrapDate'>scrap_date = #{scrapDate},</if>
                    <if test ='null != scrapUser'>scrap_user = #{scrapUser},</if>
                    <if test ='null != replaceToolId'>replace_tool_id = #{replaceToolId},</if>
                    <if test ='null != produceFactory'>produce_factory = #{produceFactory},</if>
                    <if test ='null != produceDate'>produce_date = #{produceDate},</if>
                    <if test ='null != remark'>remark = #{remark},</if>
                    <if test ='null != delFlag'>del_flag = #{delFlag},</if>
                    <if test ='null != createBy'>create_by = #{createBy},</if>
                    <if test ='null != createTime'>create_time = #{createTime},</if>
                    <if test ='null != updateBy'>update_by = #{updateBy},</if>
                    <if test ='null != updateTime'>update_time = #{updateTime}</if>
        </set>
        WHERE tool_id = #{toolId}
    </update>


    <select id="getInfo" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM base_safety_tool t
        LEFT JOIN sys_dept dept on t.dept_id = dept.dept_id
        LEFT JOIN sys_dict_data dict1 ON (t.tool_type = dict1.dict_value AND dict1.dict_type = 'tool_type')
        LEFT JOIN sys_dict_data dict2 ON (t.test_cycle = dict2.dict_value AND dict2.dict_type = 'test_cycle')
        WHERE t.del_flag='0' and t.tool_id = #{toolId}

    </select>

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM base_safety_tool t
        LEFT JOIN sys_dept dept on t.dept_id = dept.dept_id
        LEFT JOIN sys_dict_data dict1 ON (t.tool_type = dict1.dict_value AND dict1.dict_type = 'tool_type')
        LEFT JOIN sys_dict_data dict2 ON (t.test_cycle = dict2.dict_value AND dict2.dict_type = 'test_cycle')
        <where>
            t.del_flag='0'
                <if test="deptId!=null and deptId!=''">
                    and t.dept_id=#{deptId}
                </if>
                <if test="storePlace!=null and storePlace!=''">
                    and t.store_place=#{storePlace}
                </if>
                <if test="toolName!=null and toolName!=''">
                    and t.tool_name=#{toolName}
                </if>
                <if test="toolType!=null and toolType!=''">
                    and t.tool_type=#{toolType}
                </if>
                <if test="toolCode!=null and toolCode!=''">
                    and t.tool_code=#{toolCode}
                </if>
                <if test="elecLevel!=null and elecLevel!=''">
                    and t.elec_level=#{elecLevel}
                </if>
                <if test="unit!=null and unit!=''">
                    and t.unit=#{unit}
                </if>
                <if test="testCycle!=null and testCycle!=''">
                    and t.test_cycle=#{testCycle}
                </if>
                <if test="lastTestDate!=null">
                    and t.last_test_date=#{lastTestDate}
                </if>
                <if test="nextTestDate!=null">
                    and t.next_test_date=#{nextTestDate}
                </if>
                <if test="testResult!=null and testResult!=''">
                    and t.test_result=#{testResult}
                </if>
                <if test="testDate!=null">
                    and t.test_date=#{testDate}
                </if>
                <if test="testResultDesc!=null and testResultDesc!=''">
                    and t.test_result_desc=#{testResultDesc}
                </if>
                <if test="state!=null and state!=''">
                    and t.state=#{state}
                </if>
                <if test="scrapReason!=null and scrapReason!=''">
                    and t.scrap_reason=#{scrapReason}
                </if>
                <if test="scrapDate!=null and scrapDate!=''">
                    and t.scrap_date=#{scrapDate}
                </if>
                <if test="scrapUser!=null and scrapUser!=''">
                    and t.scrap_user=#{scrapUser}
                </if>
                <if test="replaceToolId!=null and replaceToolId!=''">
                    and t.replace_tool_id=#{replaceToolId}
                </if>
                <if test="produceFactory!=null and produceFactory!=''">
                    and t.produce_factory=#{produceFactory}
                </if>
                <if test="produceDate!=null">
                    and t.produce_date=#{produceDate}
                </if>
                <if test="remark!=null and remark!=''">
                    and t.remark=#{remark}
                </if>
                <if test="createBy!=null and createBy!=''">
                    and t.create_by=#{createBy}
                </if>
                <if test="createTime!=null">
                    and DATE_FORMAT(t.create_time, '%Y-%m-%d') = #{createTime}
                </if>
                <if test="updateBy!=null and updateBy!=''">
                    and t.update_by=#{updateBy}
                </if>
                <if test="updateTime!=null">
                    and DATE_FORMAT(t.update_time, '%Y-%m-%d') = #{updateTime}
                </if>
        </where>
    </select>

</mapper>