<?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.neusoft.smart.pos.org.persistent.mapper.TableGroupsMapper">
    <resultMap id="BaseResultMap" type="com.neusoft.smart.pos.org.persistent.domain.TableGroups">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="shop_id" jdbcType="INTEGER" property="shopId"/>
        <result column="group_name" jdbcType="VARCHAR" property="groupName"/>
    </resultMap>

    <sql id="columns">
        id, shop_id, group_name
    </sql>

    <insert id="insert">
        INSERT INTO table_groups
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="shopId != null">
                shop_id,
            </if>
            <if test="groupName != null">
                group_name,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="shopId != null">
                #{shopId,jdbcType=INTEGER},
            </if>
            <if test="groupName != null">
                #{groupName,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>

    <select id="selectById" resultMap="BaseResultMap">
        SELECT
        <include refid="columns"/>
        FROM table_groups
        WHERE id = #{id,jdbcType=INTEGER}
    </select>
    <select id="selectByShopId" resultMap="BaseResultMap">
        SELECT
        <include refid="columns"/>
        FROM table_groups
        WHERE shop_id = #{shopId,jdbcType=INTEGER}
        ORDER BY id DESC
    </select>

    <update id="updateById">
        UPDATE table_groups
        <set>
            <if test="shopId != null">
                shop_id = #{shopId,jdbcType=INTEGER},
            </if>
            <if test="groupName != null">
                group_name = #{groupName,jdbcType=VARCHAR},
            </if>
        </set>
        WHERE id = #{id,jdbcType=INTEGER}
    </update>

    <delete id="deleteById">
        DELETE
        FROM table_groups
        WHERE id = #{id,jdbcType=INTEGER}
    </delete>

    <select id="countByGroupName" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM table_groups
        WHERE group_name = #{groupName,jdbcType=VARCHAR} AND shop_id = #{shopId,jdbcType=INTEGER}
    </select>

    <select id="countByGroupName2" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM table_groups
        WHERE group_name = #{groupName,jdbcType=VARCHAR} AND shop_id = #{shopId,jdbcType=INTEGER} AND id &lt;&gt;
        #{id, jdbcType=INTEGER}
    </select>
</mapper>