<?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.shkj.wms.mapper.WcsPlcConnectMapper">
    <resultMap type="WcsPlcConnect" id="WcsPlcConnectResult">
        <result property="id"    column="id"    />
        <result property="connectName"    column="connect_name"    />
        <result property="devCode"    column="dev_Code"    />
        <result property="plcFactory"    column="plc_factory"    />
        <result property="plcType"    column="plc_type"    />
        <result property="plcIp"    column="plc_ip"    />
        <result property="port"    column="port"    />
        <result property="plcProtocol"    column="plc_protocol"    />
    </resultMap>

    <sql id="selectWcsPlcConnectVo">
        select id, connect_name, dev_code, plc_factory, plc_type, plc_ip, port, plc_protocol from wcs_plc_connect
    </sql>

    <select id="selectWcsPlcConnectList" parameterType="WcsPlcConnect" resultType="com.shkj.wms.vo.WcsPlcConnectVo">
        SELECT
        plc.* ,dev.dev_no,dev.dev_type,dev.dev_name,dev.tunnel
        FROM
        wcs_plc_connect plc
        LEFT JOIN wcs_device dev ON plc.dev_code = dev.id
        <where>
            <if test="connectName != null  and connectName != ''"> and connect_name like concat('%', #{connectName}, '%')</if>
            <if test="devCode != null  and devCode != ''"> and dev_code = #{devCode}</if>
            <if test="plcFactory != null  and plcFactory != ''"> and plc_factory = #{plcFactory}</if>
            <if test="plcType != null  and plcType != ''"> and plc_type = #{plcType}</if>
            <if test="plcIp != null  and plcIp != ''"> and plc_ip = #{plcIp}</if>
            <if test="port != null  and port != ''"> and port = #{port}</if>
            <if test="plcProtocol != null  and plcProtocol != ''"> and plc_protocol = #{plcProtocol}</if>
            <if test="isActive != null  and isActive != ''"> and  ifnull(plc.is_active, 0) = #{isActive}</if>

        </where>
    </select>

    <select id="selectWcsPlcConnectById" parameterType="Long" resultMap="WcsPlcConnectResult">
        <include refid="selectWcsPlcConnectVo"/>
        where id = #{id}
    </select>

    <insert id="insertWcsPlcConnect" parameterType="WcsPlcConnect" useGeneratedKeys="true" keyProperty="id" >
        insert into wcs_plc_connect
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="connectName != null">connect_name,</if>
            <if test="devCode != null">dev_code,</if>
            <if test="plcFactory != null">plc_factory,</if>
            <if test="plcType != null">plc_type,</if>
            <if test="plcIp != null">plc_ip,</if>
            <if test="port != null">port,</if>
            <if test="plcProtocol != null">plc_protocol,</if>
            <if test="resetPoint != null">reset_point,</if>
            <if test="pointTables != null">point_tables,</if>
            <if test="isActive != null">is_active,</if>
            <if test="branchId != null">branch_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="connectName != null">#{connectName},</if>
            <if test="devCode != null">#{devCode},</if>
            <if test="plcFactory != null">#{plcFactory},</if>
            <if test="plcType != null">#{plcType},</if>
            <if test="plcIp != null">#{plcIp},</if>
            <if test="port != null">#{port},</if>
            <if test="plcProtocol != null">#{plcProtocol},</if>
            <if test="resetPoint != null">#{resetPoint},</if>
            <if test="pointTables != null">#{pointTables},</if>
            <if test="isActive != null">#{isActive},</if>
            <if test="branchId != null">#{branchId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
        <selectKey resultType="java.lang.Long" keyProperty="id">
            SELECT LAST_INSERT_ID()
        </selectKey>

    </insert>

    <update id="updateWcsPlcConnect" parameterType="WcsPlcConnect">
        update wcs_plc_connect
        <trim prefix="SET" suffixOverrides=",">
            <if test="connectName != null">connect_name = #{connectName},</if>
            <if test="devCode != null">dev_code = #{devCode},</if>
            <if test="plcFactory != null">plc_factory = #{plcFactory},</if>
            <if test="plcType != null">plc_type = #{plcType},</if>
            <if test="plcIp != null">plc_ip = #{plcIp},</if>
            <if test="port != null">port = #{port},</if>
            <if test="plcProtocol != null">plc_protocol = #{plcProtocol},</if>
            <if test="resetPoint != null">reset_point = #{resetPoint},</if>
            <if test="isActive != null">is_ative = #{isActive},</if>
            <if test="branchId != null">branch_id = #{branchId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteWcsPlcConnectById" parameterType="Long">
        delete from wcs_plc_connect where id = #{id}
    </delete>

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