<?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.SysCodeRuleMapper">
    
    <resultMap type="SysCodeRule" id="SysCodeRuleResult">
        <result property="ruleId"    column="rule_id"    />
        <result property="ruleCode"    column="rule_code"    />
        <result property="ruleName"    column="rule_name"    />
        <result property="codePrefix"    column="code_prefix"    />
        <result property="dateFormat"    column="date_format"    />
        <result property="serialLength"    column="serial_length"    />
        <result property="currentSerial"    column="current_serial"    />
        <result property="resetCycle"    column="reset_cycle"    />
        <result property="lastSerialKey"    column="last_serial_key"    />
        <result property="enabled"    column="enabled"    />
        <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="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectSysCodeRuleVo">
        select rule_id, rule_code, rule_name, code_prefix, date_format, serial_length, current_serial, reset_cycle, last_serial_key, enabled, create_by, create_time, update_by, update_time, remark, del_flag from sys_code_rule
    </sql>

    <select id="selectSysCodeRuleList" parameterType="SysCodeRule" resultMap="SysCodeRuleResult">
        <include refid="selectSysCodeRuleVo"/>
        <where>
            del_flag = '0'
            <if test="ruleCode != null  and ruleCode != ''"> and rule_code = #{ruleCode}</if>
            <if test="ruleName != null  and ruleName != ''"> and rule_name like concat('%', #{ruleName}, '%')</if>
            <if test="enabled != null  and enabled != ''"> and enabled = #{enabled}</if>
        </where>
    </select>
    
    <select id="selectSysCodeRuleByRuleId" parameterType="Long" resultMap="SysCodeRuleResult">
        <include refid="selectSysCodeRuleVo"/>
        where rule_id = #{ruleId} and del_flag = '0'
    </select>

    <select id="selectSysCodeRuleByRuleCode" parameterType="String" resultMap="SysCodeRuleResult">
        <include refid="selectSysCodeRuleVo"/>
        where rule_code = #{ruleCode}
        limit 1
    </select>

    <select id="selectSysCodeRuleByRuleCodeForUpdate" parameterType="String" resultMap="SysCodeRuleResult">
        <include refid="selectSysCodeRuleVo"/>
        where rule_code = #{ruleCode} and del_flag = '0'
        for update
    </select>

    <select id="selectSysCodeRuleByRuleIdForUpdate" parameterType="Long" resultMap="SysCodeRuleResult">
        <include refid="selectSysCodeRuleVo"/>
        where rule_id = #{ruleId} and del_flag = '0'
        for update
    </select>

    <insert id="insertSysCodeRule" parameterType="SysCodeRule" useGeneratedKeys="true" keyProperty="ruleId">
        insert into sys_code_rule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="ruleCode != null and ruleCode != ''">rule_code,</if>
            <if test="ruleName != null and ruleName != ''">rule_name,</if>
            <if test="codePrefix != null and codePrefix != ''">code_prefix,</if>
            <if test="dateFormat != null and dateFormat != ''">date_format,</if>
            <if test="serialLength != null">serial_length,</if>
            <if test="currentSerial != null">current_serial,</if>
            <if test="resetCycle != null and resetCycle != ''">reset_cycle,</if>
            <if test="lastSerialKey != null">last_serial_key,</if>
            <if test="enabled != null and enabled != ''">enabled,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="ruleCode != null and ruleCode != ''">#{ruleCode},</if>
            <if test="ruleName != null and ruleName != ''">#{ruleName},</if>
            <if test="codePrefix != null and codePrefix != ''">#{codePrefix},</if>
            <if test="dateFormat != null and dateFormat != ''">#{dateFormat},</if>
            <if test="serialLength != null">#{serialLength},</if>
            <if test="currentSerial != null">#{currentSerial},</if>
            <if test="resetCycle != null and resetCycle != ''">#{resetCycle},</if>
            <if test="lastSerialKey != null">#{lastSerialKey},</if>
            <if test="enabled != null and enabled != ''">#{enabled},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateSysCodeRule" parameterType="SysCodeRule">
        update sys_code_rule
        <trim prefix="SET" suffixOverrides=",">
            <if test="ruleCode != null and ruleCode != ''">rule_code = #{ruleCode},</if>
            <if test="ruleName != null and ruleName != ''">rule_name = #{ruleName},</if>
            code_prefix = #{codePrefix},
            <if test="dateFormat != null and dateFormat != ''">date_format = #{dateFormat},</if>
            <if test="serialLength != null">serial_length = #{serialLength},</if>
            <if test="resetCycle != null and resetCycle != ''">reset_cycle = #{resetCycle},</if>
            <if test="enabled != null and enabled != ''">enabled = #{enabled},</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 rule_id = #{ruleId} and del_flag = '0'
    </update>

    <update id="updateSerialState">
        update sys_code_rule
        set current_serial = #{currentSerial},
            last_serial_key = #{lastSerialKey},
            update_time = #{updateTime}
        where rule_id = #{ruleId} and del_flag = '0'
    </update>

    <update id="deleteSysCodeRuleByRuleId" parameterType="Long">
        update sys_code_rule set del_flag = '1' where rule_id = #{ruleId} and del_flag = '0'
    </update>

    <update id="deleteSysCodeRuleByRuleIds" parameterType="String">
        update sys_code_rule set del_flag = '1' where del_flag = '0' and rule_id in
        <foreach item="ruleId" collection="array" open="(" separator="," close=")">
            #{ruleId}
        </foreach>
    </update>

</mapper>
