<?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.engine.salary.mapper.formula.FormulaVarMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.salaryformula.po.FormulaVar">
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="formula_id" property="formulaId"/>
        <result column="field_id" property="fieldId"/>
        <result column="field_name" property="fieldName"/>
        <result column="field_type" property="fieldType"/>
        <result column="source" property="source"/>
        <result column="order_index" property="orderIndex"/>
        <result column="creator" property="creator"/>
        <result column="delete_type" property="deleteType"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        , t.name
        , t.formula_id
        , t.field_id
        , t.field_name
        , t.field_type
        , t.source
        , t.order_index
        , t.creator
        , t.delete_type
        , t.create_time
        , t.update_time
    </sql>

    <!-- 查询全部 -->
    <select id="listAll" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_formula_var t
        WHERE delete_type = 0
    </select>

    <!-- 根据主键获取单条记录 -->
    <select id="getById" resultMap="BaseResultMap" parameterType="Long">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_formula_var t
        WHERE id = #{id} AND delete_type = 0
    </select>

    <!-- 条件查询 -->
    <select id="listSome" resultMap="BaseResultMap"
            parameterType="com.engine.salary.entity.salaryformula.po.FormulaVar">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_formula_var t
        WHERE delete_type = 0
        <if test="name != null">
            AND name = #{name}
        </if>
        <if test="formulaId != null">
            AND formula_id = #{formulaId}
        </if>
        <if test="fieldId != null">
            AND field_id = #{fieldId}
        </if>
        <if test="fieldName != null">
            AND field_name = #{fieldName}
        </if>
        <if test="fieldType != null">
            AND field_type = #{fieldType}
        </if>
        <if test="source != null">
            AND source = #{source}
        </if>
        <if test="orderIndex != null">
            AND order_index = #{orderIndex}
        </if>
        <if test="creator != null">
            AND creator = #{creator}
        </if>
        <if test="deleteType != null">
            AND delete_type = #{deleteType}
        </if>
        <if test="createTime != null">
            AND create_time = #{createTime}
        </if>
        <if test="updateTime != null">
            AND update_time = #{updateTime}
        </if>
        <if test="formulaIds != null and formulaIds.size()>0">
            AND formula_id IN
            <foreach collection="formulaIds" open="(" item="formulaId" separator="," close=")">
                #{formulaId}
            </foreach>
        </if>
        ORDER BY id DESC
    </select>


    <!-- 插入不为NULL的字段 -->
    <insert id="insertIgnoreNull" parameterType="com.engine.salary.entity.salaryformula.po.FormulaVar">
        INSERT INTO hrsa_formula_var
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="formulaId != null">
                formula_id,
            </if>
            <if test="fieldId != null">
                field_id,
            </if>
            <if test="fieldName != null">
                field_name,
            </if>
            <if test="fieldType != null">
                field_type,
            </if>
            <if test="source != null">
                source,
            </if>
            <if test="orderIndex != null">
                order_index,
            </if>
            <if test="creator != null">
                creator,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="name != null">
                #{name},
            </if>
            <if test="formulaId != null">
                #{formulaId},
            </if>
            <if test="fieldId != null">
                #{fieldId},
            </if>
            <if test="fieldName != null">
                #{fieldName},
            </if>
            <if test="fieldType != null">
                #{fieldType},
            </if>
            <if test="source != null">
                #{source},
            </if>
            <if test="orderIndex != null">
                #{orderIndex},
            </if>
            <if test="creator != null">
                #{creator},
            </if>
            <if test="deleteType != null">
                #{deleteType},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
        </trim>
    </insert>


    <!-- 更新不为NULL的字段 -->
    <update id="updateIgnoreNull" parameterType="com.engine.salary.entity.salaryformula.po.FormulaVar">
        UPDATE hrsa_formula_var
        <set>
            <if test="name != null">
                name=#{name},
            </if>
            <if test="formulaId != null">
                formula_id=#{formulaId},
            </if>
            <if test="fieldId != null">
                field_id=#{fieldId},
            </if>
            <if test="fieldName != null">
                field_name=#{fieldName},
            </if>
            <if test="fieldType != null">
                field_type=#{fieldType},
            </if>
            <if test="source != null">
                source=#{source},
            </if>
            <if test="orderIndex != null">
                order_index=#{orderIndex},
            </if>
            <if test="creator != null">
                creator=#{creator},
            </if>
            <if test="deleteType != null">
                delete_type=#{deleteType},
            </if>
            <if test="createTime != null">
                create_time=#{createTime},
            </if>
            <if test="updateTime != null">
                update_time=#{updateTime},
            </if>
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <delete id="deleteByFormulaId" parameterType="com.engine.salary.entity.salaryformula.po.FormulaVar">
        UPDATE hrsa_formula_var
        SET delete_type=1
        WHERE formula_id = #{formulaId}
          AND delete_type = 0
    </delete>

    <delete id="deleteInFormulaIds">
        UPDATE hrsa_formula_var
        SET delete_type=1
        WHERE delete_type = 0
        AND formula_id IN
        <foreach collection="formulaIds" open="(" item="formulaId" separator="," close=")">
            #{formulaId}
        </foreach>
        and create_time <![CDATA[ < ]]>  #{yesterday}
    </delete>

</mapper>