<?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.ruoyi.system.mapper.TableQrcodeMapper">

    <resultMap type="TableQrcode" id="TableQrcodeResult">
        <result property="id"    column="id"    />
        <result property="tableNo"    column="table_no"    />
        <result property="type"    column="type"    />
        <result property="storeId"    column="store_id"    />
        <result property="nightMarketId"    column="night_market_id"    />
        <result property="qrCode"    column="qr_code"    />
        <result property="status"    column="status"    />
        <result property="useStatus"    column="use_status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="storeName"    column="store_name"    />
    </resultMap>

    <sql id="selectTableQrcodeVo">
        select tq.*, ps.pos_name as store_name
        from table_qrcode tq
        left join pos_store ps on tq.store_id = ps.id
    </sql>

    <select id="selectTableQrcodeList" parameterType="TableQrcode" resultMap="TableQrcodeResult">
        <include refid="selectTableQrcodeVo"/>
        <where>
            <if test="tableNo != null and tableNo != ''"> and tq.table_no like concat('%', #{tableNo}, '%')</if>
            <if test="type != null"> and tq.type = #{type}</if>
            <if test="storeId != null"> and tq.store_id = #{storeId}</if>
            <if test="nightMarketId != null"> and tq.night_market_id = #{nightMarketId}</if>
            <if test="status != null"> and tq.status = #{status}</if>
            <if test="useStatus != null"> and tq.use_status = #{useStatus}</if>
        </where>
        order by tq.id desc
    </select>

    <select id="selectTableQrcodeById" parameterType="Long" resultMap="TableQrcodeResult">
        <include refid="selectTableQrcodeVo"/>
        where tq.id = #{id}
    </select>

    <insert id="insertTableQrcode" parameterType="TableQrcode" useGeneratedKeys="true" keyProperty="id">
        insert into table_qrcode
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tableNo != null and tableNo != ''">table_no,</if>
            <if test="type != null">type,</if>
            <if test="storeId != null">store_id,</if>
            <if test="nightMarketId != null">night_market_id,</if>
            <if test="qrCode != null">qr_code,</if>
            <if test="status != null">status,</if>
            <if test="useStatus != null">use_status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tableNo != null and tableNo != ''">#{tableNo},</if>
            <if test="type != null">#{type},</if>
            <if test="storeId != null">#{storeId},</if>
            <if test="nightMarketId != null">#{nightMarketId},</if>
            <if test="qrCode != null">#{qrCode},</if>
            <if test="status != null">#{status},</if>
            <if test="useStatus != null">#{useStatus},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateTableQrcode" parameterType="TableQrcode">
        update table_qrcode
        <trim prefix="SET" suffixOverrides=",">
            <if test="tableNo != null and tableNo != ''">table_no = #{tableNo},</if>
            <if test="storeId != null">store_id = #{storeId},</if>
            <if test="nightMarketId != null">night_market_id = #{nightMarketId},</if>
            <if test="qrCode != null">qr_code = #{qrCode},</if>
            <if test="status != null">status = #{status},</if>
            <if test="useStatus != null">use_status = #{useStatus},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTableQrcodeById" parameterType="Long">
        delete from table_qrcode where id = #{id}
    </delete>

    <select id="checkTableNoUnique" parameterType="TableQrcode" resultType="int">
        select count(1) from table_qrcode
        <where>
            table_no = #{tableNo}
            <if test="type == 0 and storeId != null"> and store_id = #{storeId}</if>
            <if test="type == 1 and nightMarketId != null"> and night_market_id = #{nightMarketId} and type = 1</if>
            <if test="id != null"> and id != #{id}</if>
        </where>
    </select>

</mapper>
