<?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.ximai.mes.md.mapper.MdFactoryMapper">

    <resultMap type="MdFactory" id="MdFactoryResult">
        <result property="factoryId" column="factory_id"/>
        <result property="factoryCode" column="factory_code"/>
        <result property="factoryName" column="factory_name"/>
        <result property="enableFlag" column="enable_flag"/>
        <result property="remark" column="remark"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectMdFactoryVo">
        select factory_id,
               factory_code,
               factory_name,
               enable_flag,
               remark,
               create_by,
               create_time,
               update_by,
               update_time
        from md_factory
    </sql>

    <select id="selectMdFactoryList" parameterType="MdFactory" resultMap="MdFactoryResult">
        <include refid="selectMdFactoryVo"/>
        <where>
            <if test="factoryCode != null  and factoryCode != ''">and factory_code = #{factoryCode}</if>
            <if test="factoryName != null  and factoryName != ''">and factory_name like concat('%', #{factoryName},
                '%')
            </if>
            <if test="enableFlag != null  and enableFlag != ''">and enable_flag = #{enableFlag}</if>
        </where>
    </select>

    <select id="selectMdFactoryByFactoryId" parameterType="Long" resultMap="MdFactoryResult">
        <include refid="selectMdFactoryVo"/>
        where factory_id = #{factoryId}
    </select>
    <select id="selectMdFactoryByFactoryCode" resultMap="MdFactoryResult"
            parameterType="java.lang.String">
        <include refid="selectMdFactoryVo"/>
        where factory_code = #{factoryCode}
    </select>
    <select id="selectLikeByName" resultType="com.ximai.mes.md.domain.MdFactory">
        select * from md_factory where factory_name like '%${factoryName}%'
    </select>

    <insert id="insertMdFactory" parameterType="MdFactory" useGeneratedKeys="true" keyProperty="factoryId">
        insert into md_factory
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="factoryCode != null">factory_code,</if>
            <if test="factoryName != null">factory_name,</if>
            <if test="enableFlag != null">enable_flag,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="factoryCode != null">#{factoryCode},</if>
            <if test="factoryName != null">#{factoryName},</if>
            <if test="enableFlag != null">#{enableFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            sysdate()
        </trim>
    </insert>

    <update id="updateMdFactory" parameterType="MdFactory">
        update md_factory
        <trim prefix="SET" suffixOverrides=",">
            <if test="factoryCode != null">factory_code = #{factoryCode},</if>
            <if test="factoryName != null">factory_name = #{factoryName},</if>
            <if test="enableFlag != null">enable_flag = #{enableFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = sysdate()
        </trim>
        where factory_id = #{factoryId}
    </update>

    <delete id="deleteMdFactoryByFactoryId" parameterType="Long">
        delete
        from md_factory
        where factory_id = #{factoryId}
    </delete>

    <delete id="deleteMdFactoryByFactoryIds" parameterType="String">
        delete from md_factory where factory_id in
        <foreach item="factoryId" collection="array" open="(" separator="," close=")">
            #{factoryId}
        </foreach>
    </delete>
</mapper>
