<?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.hw.mes.mapper.MesBaseFactoryInfoMapper">

    <resultMap type="MesBaseFactoryInfo" id="MesBaseFactoryInfoResult">
        <result property="factoryId" column="factory_id"/>
        <result property="companyName" column="company_name"/>
        <result property="factoryCode" column="factory_code"/>
        <result property="factoryName" column="factory_name"/>
        <result property="factoryStatus" column="factory_status"/>
        <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="selectMesBaseFactoryInfoVo">
        select factory_id,
               company_name,
               factory_code,
               factory_name,
               factory_status,
               remark,
               create_by,
               create_time,
               update_by,
               update_time
        from mes_base_factory_info
    </sql>

    <select id="selectMesBaseFactoryInfoList" parameterType="MesBaseFactoryInfo" resultMap="MesBaseFactoryInfoResult">
        <include refid="selectMesBaseFactoryInfoVo"/>
        <where>
            <if test="companyName != null  and companyName != ''">and company_name like concat('%', #{companyName},
                '%')
            </if>
            <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="factoryStatus != null  and factoryStatus != ''">and factory_status = #{factoryStatus}</if>
        </where>
    </select>

    <select id="selectMesBaseFactoryInfoByFactoryId" parameterType="Long" resultMap="MesBaseFactoryInfoResult">
        <include refid="selectMesBaseFactoryInfoVo"/>
        where factory_id = #{factoryId}
    </select>

    <insert id="insertMesBaseFactoryInfo" parameterType="MesBaseFactoryInfo" useGeneratedKeys="true"
            keyProperty="factoryId">
        insert into mes_base_factory_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyName != null">company_name,</if>
            <if test="factoryCode != null">factory_code,</if>
            <if test="factoryName != null and factoryName != ''">factory_name,</if>
            <if test="factoryStatus != null and factoryStatus != ''">factory_status,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyName != null">#{companyName},</if>
            <if test="factoryCode != null">#{factoryCode},</if>
            <if test="factoryName != null and factoryName != ''">#{factoryName},</if>
            <if test="factoryStatus != null and factoryStatus != ''">#{factoryStatus},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateMesBaseFactoryInfo" parameterType="MesBaseFactoryInfo">
        update mes_base_factory_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyName != null">company_name = #{companyName},</if>
            <if test="factoryCode != null">factory_code = #{factoryCode},</if>
            <if test="factoryName != null and factoryName != ''">factory_name = #{factoryName},</if>
            <if test="factoryStatus != null and factoryStatus != ''">factory_status = #{factoryStatus},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where factory_id = #{factoryId}
    </update>

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

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