<?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.foreverwin.mesnac.common.mapper.NextNumberCodeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.foreverwin.mesnac.common.model.NextNumberCode">
        <id column="HANDLE" property="handle" />
        <result column="SITE" property="site" />
        <result column="NEXT_NUMBER_BO" property="nextNumberBo" />
        <result column="CODE" property="code" />
        <result column="CURRENT_SEQ" property="currentSeq" />
        <result column="MODIFIED_DATE_TIME" property="modifiedDateTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        HANDLE, SITE, NEXT_NUMBER_BO, CODE, CURRENT_SEQ, MODIFIED_DATE_TIME
    </sql>

    <!-- BaseMapper标准查询/修改/删除 -->
    <select id="selectById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include> FROM Z_NEXT_NUMBER_CODE WHERE HANDLE=#{handle}
    </select>

    <select id="selectForUpdateById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include> FROM Z_NEXT_NUMBER_CODE WHERE HANDLE=#{handle} FOR UPDATE
    </select>

    <select id="selectByMap" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include>
        FROM Z_NEXT_NUMBER_CODE
        <if test="cm!=null and !cm.isEmpty">
            <where>
                <foreach collection="cm.keys" item="k" separator="AND">
                    <if test="cm[k] != null">
                        ${k} = #{cm[${k}]}
                    </if>
                </foreach>
            </where>
        </if>
    </select>

    <select id="selectBatchIds" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include>
        FROM Z_NEXT_NUMBER_CODE WHERE HANDLE IN (
        <foreach item="item" index="index" collection="coll" separator=",">#{item}
        </foreach>)
    </select>

    <select id="selectOne" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include> FROM Z_NEXT_NUMBER_CODE
        <where>
            <if test="ew.entity.handle!=null">
                HANDLE=#{ew.handle}
            </if>
            <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
            <if test="ew.entity.nextNumberBo!=null"> AND NEXT_NUMBER_BO=#{ew.entity.nextNumberBo}</if>
            <if test="ew.entity.code!=null"> AND CODE=#{ew.entity.code}</if>
            <if test="ew.entity.currentSeq!=null"> AND CURRENT_SEQ=#{ew.entity.currentSeq}</if>
            <if test="ew.entity.modifiedDateTime!=null"> AND MODIFIED_DATE_TIME=#{ew.entity.modifiedDateTime}</if>
        </where>
    </select>

    <select id="selectCount" resultType="Integer">
        SELECT COUNT(1) FROM Z_NEXT_NUMBER_CODE
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.nextNumberBo!=null"> AND NEXT_NUMBER_BO=#{ew.entity.nextNumberBo}</if>
                    <if test="ew.entity.code!=null"> AND CODE=#{ew.entity.code}</if>
                    <if test="ew.entity.currentSeq!=null"> AND CURRENT_SEQ=#{ew.entity.currentSeq}</if>
                    <if test="ew.entity.modifiedDateTime!=null"> AND MODIFIED_DATE_TIME=#{ew.entity.modifiedDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectList" resultMap="BaseResultMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM Z_NEXT_NUMBER_CODE
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.nextNumberBo!=null"> AND NEXT_NUMBER_BO=#{ew.entity.nextNumberBo}</if>
                    <if test="ew.entity.code!=null"> AND CODE=#{ew.entity.code}</if>
                    <if test="ew.entity.currentSeq!=null"> AND CURRENT_SEQ=#{ew.entity.currentSeq}</if>
                    <if test="ew.entity.modifiedDateTime!=null"> AND MODIFIED_DATE_TIME=#{ew.entity.modifiedDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectMaps" resultType="HashMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM Z_NEXT_NUMBER_CODE
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.nextNumberBo!=null"> AND NEXT_NUMBER_BO=#{ew.entity.nextNumberBo}</if>
                    <if test="ew.entity.code!=null"> AND CODE=#{ew.entity.code}</if>
                    <if test="ew.entity.currentSeq!=null"> AND CURRENT_SEQ=#{ew.entity.currentSeq}</if>
                    <if test="ew.entity.modifiedDateTime!=null"> AND MODIFIED_DATE_TIME=#{ew.entity.modifiedDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectObjs" resultType="Object">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM Z_NEXT_NUMBER_CODE
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.nextNumberBo!=null"> AND NEXT_NUMBER_BO=#{ew.entity.nextNumberBo}</if>
                    <if test="ew.entity.code!=null"> AND CODE=#{ew.entity.code}</if>
                    <if test="ew.entity.currentSeq!=null"> AND CURRENT_SEQ=#{ew.entity.currentSeq}</if>
                    <if test="ew.entity.modifiedDateTime!=null"> AND MODIFIED_DATE_TIME=#{ew.entity.modifiedDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectPage" resultMap="BaseResultMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM Z_NEXT_NUMBER_CODE
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.nextNumberBo!=null"> AND NEXT_NUMBER_BO=#{ew.entity.nextNumberBo}</if>
                    <if test="ew.entity.code!=null"> AND CODE=#{ew.entity.code}</if>
                    <if test="ew.entity.currentSeq!=null"> AND CURRENT_SEQ=#{ew.entity.currentSeq}</if>
                    <if test="ew.entity.modifiedDateTime!=null"> AND MODIFIED_DATE_TIME=#{ew.entity.modifiedDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectMapsPage" resultType="HashMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM Z_NEXT_NUMBER_CODE
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.nextNumberBo!=null"> AND NEXT_NUMBER_BO=#{ew.entity.nextNumberBo}</if>
                    <if test="ew.entity.code!=null"> AND CODE=#{ew.entity.code}</if>
                    <if test="ew.entity.currentSeq!=null"> AND CURRENT_SEQ=#{ew.entity.currentSeq}</if>
                    <if test="ew.entity.modifiedDateTime!=null"> AND MODIFIED_DATE_TIME=#{ew.entity.modifiedDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <insert id="insert" parameterType="com.foreverwin.mesnac.common.model.NextNumberCode">
        INSERT INTO Z_NEXT_NUMBER_CODE
        <trim prefix="(" suffix=")" suffixOverrides=",">
            HANDLE,
            <if test="site!=null">SITE,</if>
            <if test="nextNumberBo!=null">NEXT_NUMBER_BO,</if>
            <if test="code!=null">CODE,</if>
            <if test="currentSeq!=null">CURRENT_SEQ,</if>
            <if test="modifiedDateTime!=null">MODIFIED_DATE_TIME,</if>
        </trim> VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #{handle},
            <if test="site!=null">#{site},</if>
            <if test="nextNumberBo!=null">#{nextNumberBo},</if>
            <if test="code!=null">#{code},</if>
            <if test="currentSeq!=null">#{currentSeq},</if>
            <if test="modifiedDateTime!=null">#{modifiedDateTime},</if>
        </trim>
    </insert>

    <insert id="insertAllColumn" parameterType="com.foreverwin.mesnac.common.model.NextNumberCode">
        INSERT INTO Z_NEXT_NUMBER_CODE
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <include refid="Base_Column_List"></include>
        </trim> VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #{handle},
            #{site},
            #{nextNumberBo},
            #{code},
            #{currentSeq},
            #{modifiedDateTime},
        </trim>
    </insert>


    <update id="updateById">
        UPDATE Z_NEXT_NUMBER_CODE <trim prefix="SET" suffixOverrides=",">
        <if test="et.site!=null">SITE=#{et.site},</if>
        <if test="et.nextNumberBo!=null">NEXT_NUMBER_BO=#{et.nextNumberBo},</if>
        <if test="et.code!=null">CODE=#{et.code},</if>
        <if test="et.currentSeq!=null">CURRENT_SEQ=#{et.currentSeq},</if>
        <if test="et.modifiedDateTime!=null">MODIFIED_DATE_TIME=#{et.modifiedDateTime},</if>
    </trim> WHERE HANDLE=#{et.handle} <if test="et instanceof java.util.Map"><if test="et.MP_OPTLOCK_VERSION_ORIGINAL!=null">and ${et.MP_OPTLOCK_VERSION_COLUMN}=#{et.MP_OPTLOCK_VERSION_ORIGINAL}</if></if>
    </update>


    <update id="updateAllColumnById">
        UPDATE Z_NEXT_NUMBER_CODE <trim prefix="SET" suffixOverrides=",">
        SITE=#{et.site},
        NEXT_NUMBER_BO=#{et.nextNumberBo},
        CODE=#{et.code},
        CURRENT_SEQ=#{et.currentSeq},
        MODIFIED_DATE_TIME=#{et.modifiedDateTime},
    </trim> WHERE HANDLE=#{et.handle} <if test="et instanceof java.util.Map"><if test="et.MP_OPTLOCK_VERSION_ORIGINAL!=null">and ${et.MP_OPTLOCK_VERSION_COLUMN}=#{et.MP_OPTLOCK_VERSION_ORIGINAL}</if></if>
    </update>


    <update id="update">
        UPDATE Z_NEXT_NUMBER_CODE <trim prefix="SET" suffixOverrides=",">
        <if test="et.site!=null">SITE=#{et.site},</if>
        <if test="et.nextNumberBo!=null">NEXT_NUMBER_BO=#{et.nextNumberBo},</if>
        <if test="et.code!=null">CODE=#{et.code},</if>
        <if test="et.currentSeq!=null">CURRENT_SEQ=#{et.currentSeq},</if>
        <if test="et.modifiedDateTime!=null">MODIFIED_DATE_TIME=#{et.modifiedDateTime},</if>
    </trim>
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    HANDLE=#{ew.entity.handle}
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.nextNumberBo!=null"> AND NEXT_NUMBER_BO=#{ew.entity.nextNumberBo}</if>
                    <if test="ew.entity.code!=null"> AND CODE=#{ew.entity.code}</if>
                    <if test="ew.entity.currentSeq!=null"> AND CURRENT_SEQ=#{ew.entity.currentSeq}</if>
                    <if test="ew.entity.modifiedDateTime!=null"> AND MODIFIED_DATE_TIME=#{ew.entity.modifiedDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </update>

    <delete id="deleteById">
        DELETE FROM Z_NEXT_NUMBER_CODE WHERE HANDLE=#{handle}
    </delete>

    <delete id="deleteByMap">
        DELETE FROM Z_NEXT_NUMBER_CODE
        <if test="cm!=null and !cm.isEmpty">
            <where>
                <foreach collection="cm.keys" item="k" separator="AND">
                    <if test="cm[k] != null">
                        ${k} = #{cm[${k}]}
                    </if>
                </foreach>
            </where>
        </if>
    </delete>

    <delete id="delete">
        DELETE FROM Z_NEXT_NUMBER_CODE
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.site!=null"> AND SITE=#{ew.entity.site}</if>
                    <if test="ew.entity.nextNumberBo!=null"> AND NEXT_NUMBER_BO=#{ew.entity.nextNumberBo}</if>
                    <if test="ew.entity.code!=null"> AND CODE=#{ew.entity.code}</if>
                    <if test="ew.entity.currentSeq!=null"> AND CURRENT_SEQ=#{ew.entity.currentSeq}</if>
                    <if test="ew.entity.modifiedDateTime!=null"> AND MODIFIED_DATE_TIME=#{ew.entity.modifiedDateTime}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </delete>

    <delete id="deleteBatchIds">
        DELETE FROM Z_NEXT_NUMBER_CODE WHERE HANDLE IN (
        <foreach item="item" index="index" collection="coll" separator=",">#{item}
        </foreach>)
    </delete>
    <!-- BaseMapper标准查询/修改/删除 -->

    <select id="getMaxCurrentSeq" resultType="Long">
        SELECT MAX(CURRENT_SEQ) FROM Z_NEXT_NUMBER_CODE WHERE NEXT_NUMBER_BO = #{nextNumberBo}
    </select>
</mapper>
