<?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.soss.system.mapper.MachineMapper">
    
    <resultMap type="Machine" id="MachineResult">
        <result property="id"    column="id"    />
        <result property="shopId"    column="shop_id"    />
        <result property="code"    column="code"    />
        <result property="type"    column="type"    />
        <result property="state"    column="state"    />
        <result property="bindTime"    column="bind_time"    />
        <result property="createdAt"    column="created_at"    />
        <result property="updatedAt"    column="updated_at"    />
        <result property="register" column="register"></result>
    </resultMap>

    <sql id="selectMachineVo">
        select id, shop_id, code, type, state, bind_time, created_at, updated_at,register from machine
    </sql>

    <select id="selectMachineList" parameterType="Machine" resultMap="MachineResult">
        <include refid="selectMachineVo"/>
        <where>  
            <if test="shopId != null  and shopId != ''"> and shop_id = #{shopId}</if>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
            <if test="state != null  and state != ''"> and state = #{state}</if>
            <if test="bindTime != null "> and bind_time = #{bindTime}</if>
            <if test="createdAt != null "> and created_at = #{createdAt}</if>
            <if test="updatedAt != null "> and updated_at = #{updatedAt}</if>
        </where>
    </select>
    
    <select id="selectMachineById" parameterType="String" resultMap="MachineResult">
        <include refid="selectMachineVo"/>
        where id = #{id}
    </select>
    <select id="selectMachineByCode" parameterType="String" resultMap="MachineResult">
        <include refid="selectMachineVo"/>
        where code = #{code}
    </select>
        
    <insert id="insertMachine" parameterType="Machine" useGeneratedKeys="true" keyProperty="id">
        insert into machine
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="shopId != null and shopId != ''">shop_id,</if>
            <if test="code != null and code != ''">code,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="state != null and state != ''">state,</if>
            <if test="bindTime != null">bind_time,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="updatedAt != null">updated_at,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="shopId != null and shopId != ''">#{shopId},</if>
            <if test="code != null and code != ''">#{code},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="state != null and state != ''">#{state},</if>
            <if test="bindTime != null">#{bindTime},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="updatedAt != null">#{updatedAt},</if>
         </trim>
    </insert>

    <update id="updateMachine" parameterType="Machine">
        update machine
        <trim prefix="SET" suffixOverrides=",">
            <if test="shopId != null and shopId != ''">shop_id = #{shopId},</if>
            <if test="code != null and code != ''">code = #{code},</if>
            <if test="type != null and type != ''">type = #{type},</if>
            <if test="state != null and state != ''">state = #{state},</if>
            <if test="bindTime != null">bind_time = #{bindTime},</if>
            <if test="updatedAt != null">updated_at = #{updatedAt},</if>
            <if test="register != null">register = #{register},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMachineById" parameterType="String">
        update machine set shop_id = null  where id = #{id}
    </delete>

    <delete id="deleteMachineByIds" parameterType="String">
        delete from machine where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <update id="unbound">
        update machine set shopId =null where code =#{id}
    </update>
    <select id="selectRegister" resultType="string">
        select register from machine where code =#{id}
    </select>
</mapper>