<?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.datacollection.AttendQuoteFieldMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.datacollection.po.AttendQuoteFieldPO">
        <result column="id" property="id"/>
        <result column="field_name" property="fieldName"/>
        <result column="source_type" property="sourceType"/>
        <result column="field_type" property="fieldType"/>
        <result column="enable_status" property="enableStatus"/>
        <result column="code" property="code"/>
        <result column="description" property="description"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="creator" property="creator"/>
        <result column="delete_type" property="deleteType"/>
        <result column="tenant_key" property="tenantKey"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        , t.field_name
        , t.source_type
        , t.field_type
        , t.enable_status
        , t.code
        , t.description
        , t.create_time
        , t.update_time
        , t.creator
        , t.delete_type
        , t.tenant_key
    </sql>

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

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

    <!-- 插入不为NULL的字段 -->
    <insert id="insertIgnoreNull" parameterType="com.engine.salary.entity.datacollection.po.AttendQuoteFieldPO"
            keyProperty="id" keyColumn="id" useGeneratedKeys="true"
    >
        INSERT INTO hrsa_attend_quote_field
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="id != null">
                id,
            </if>
            <if test="fieldName != null">
                field_name,
            </if>
            <if test="sourceType != null">
                source_type,
            </if>
            <if test="fieldType != null">
                field_type,
            </if>
            <if test="enableStatus != null">
                enable_status,
            </if>
            <if test="code != null">
                code,
            </if>
            <if test="description != null">
                description,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="creator != null">
                creator,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="tenantKey != null">
                tenant_key,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="fieldName != null">
                #{fieldName},
            </if>
            <if test="sourceType != null">
                #{sourceType},
            </if>
            <if test="fieldType != null">
                #{fieldType},
            </if>
            <if test="enableStatus != null">
                #{enableStatus},
            </if>
            <if test="code != null">
                #{code},
            </if>
            <if test="description != null">
                #{description},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="creator != null">
                #{creator},
            </if>
            <if test="deleteType != null">
                #{deleteType},
            </if>
            <if test="tenantKey != null">
                #{tenantKey},
            </if>
        </trim>
    </insert>

    <insert id="insertIgnoreNull" parameterType="com.engine.salary.entity.datacollection.po.AttendQuoteFieldPO" databaseId="oracle"
    >
        <selectKey  keyProperty="id" resultType="long" order="AFTER">
            select hrsa_attend_quote_field_id.currval from dual
        </selectKey>
        INSERT INTO hrsa_attend_quote_field
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="id != null">
                id,
            </if>
            <if test="fieldName != null">
                field_name,
            </if>
            <if test="sourceType != null">
                source_type,
            </if>
            <if test="fieldType != null">
                field_type,
            </if>
            <if test="enableStatus != null">
                enable_status,
            </if>
            <if test="code != null">
                code,
            </if>
            <if test="description != null">
                description,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="creator != null">
                creator,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="tenantKey != null">
                tenant_key,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="fieldName != null">
                #{fieldName},
            </if>
            <if test="sourceType != null">
                #{sourceType},
            </if>
            <if test="fieldType != null">
                #{fieldType},
            </if>
            <if test="enableStatus != null">
                #{enableStatus},
            </if>
            <if test="code != null">
                #{code},
            </if>
            <if test="description != null">
                #{description},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="creator != null">
                #{creator},
            </if>
            <if test="deleteType != null">
                #{deleteType},
            </if>
            <if test="tenantKey != null">
                #{tenantKey},
            </if>
        </trim>
    </insert>

    <!-- 更新,更新全部字段 -->
    <update id="update" parameterType="com.engine.salary.entity.datacollection.po.AttendQuoteFieldPO">
        UPDATE hrsa_attend_quote_field
        <set>
            field_name=#{fieldName},
            source_type=#{sourceType},
            field_type=#{fieldType},
            enable_status=#{enableStatus},
            code=#{code},
            description=#{description},
            create_time=#{createTime},
            update_time=#{updateTime},
            creator=#{creator},
            delete_type=#{deleteType},
            tenant_key=#{tenantKey},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <!-- 更新不为NULL的字段 -->
    <update id="updateIgnoreNull" parameterType="com.engine.salary.entity.datacollection.po.AttendQuoteFieldPO">
        UPDATE hrsa_attend_quote_field
        <set>
            <if test="fieldName != null">
                field_name=#{fieldName},
            </if>
            <if test="sourceType != null">
                source_type=#{sourceType},
            </if>
            <if test="fieldType != null">
                field_type=#{fieldType},
            </if>
            <if test="enableStatus != null">
                enable_status=#{enableStatus},
            </if>
            <if test="code != null">
                code=#{code},
            </if>
            <if test="description != null">
                description=#{description},
            </if>
            <if test="createTime != null">
                create_time=#{createTime},
            </if>
            <if test="updateTime != null">
                update_time=#{updateTime},
            </if>
            <if test="creator != null">
                creator=#{creator},
            </if>
            <if test="deleteType != null">
                delete_type=#{deleteType},
            </if>
            <if test="tenantKey != null">
                tenant_key=#{tenantKey},
            </if>
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <!-- 根据主键删除记录 -->
    <delete id="delete" parameterType="com.engine.salary.entity.datacollection.po.AttendQuoteFieldPO">
        UPDATE hrsa_attend_quote_field
        SET delete_type=1
        WHERE id = #{id}
          AND delete_type = 0
    </delete>

    <delete id="deleteAttendByCode"
            parameterType="com.engine.salary.entity.datacollection.param.AttendQuoteFieldQueryParam">
        UPDATE hrsa_attend_quote_field
        SET delete_type=1
        WHERE delete_type = 0
        AND  source_type= 2
        AND code IN
        <foreach collection="param.codes" open="(" item="code" separator="," close=")">
            #{code}
        </foreach>

    </delete>


    <sql id="attendQuoteFieldColumn">
        t1
        .
        id
        ,
        t1.field_name,
        t1.source_type,
        t1.field_type,
        t1.enable_status,
        t1.description
    </sql>

    <sql id="paramSql">
        <if test="param.ids != null and param.ids.size()>0">
            AND t1.id IN
            <foreach collection="param.ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="param.fieldName != null and param.fieldName != ''">
            AND t1.field_name like CONCAT('%',#{param.fieldName},'%')
        </if>
    </sql>
    <sql id="paramSql" databaseId="oracle">
        <if test="param.ids != null and param.ids.size()>0">
            AND t1.id IN
            <foreach collection="param.ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="param.fieldName != null and param.fieldName != ''">
            AND t1.field_name like '%'||#{param.fieldName}||'%'
        </if>
    </sql>
    <sql id="paramSql" databaseId="sqlserver">
        <if test="param.ids != null and param.ids.size()>0">
            AND t1.id IN
            <foreach collection="param.ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="param.fieldName != null and param.fieldName != ''">
            AND t1.field_name like '%'+#{param.fieldName}+'%'
        </if>
    </sql>

    <select id="list" resultType="com.engine.salary.entity.datacollection.dto.AttendQuoteFieldListDTO">
        SELECT
        <include refid="attendQuoteFieldColumn"/>
        FROM
        hrsa_attend_quote_field t1
        WHERE
        t1.delete_type = 0
        <include refid="paramSql"/>
        <if test="param.enableStatus != null">
            AND enable_status = #{param.enableStatus}
        </if>
        ORDER BY t1.source_type,t1.id DESC
    </select>

    <insert id="saveBatch">
        INSERT INTO hrsa_attend_quote_field
        (field_name,source_type,field_type,enable_status,code,description,create_time,update_time,creator,delete_type,tenant_key)
        VALUES
        <foreach collection="saves" item="item" separator=",">
            (
            #{item.fieldName},
            #{item.sourceType},
            #{item.fieldType},
            #{item.enableStatus},
            #{item.code},
            #{item.description},
            #{item.createTime},#{item.updateTime}, #{item.creator}, 0, #{item.tenantKey}
            )
        </foreach>
    </insert>
    <insert id="saveBatch" databaseId="oracle">
        INSERT INTO hrsa_attend_quote_field
        (field_name,source_type,field_type,enable_status,code,description,create_time,update_time,creator,delete_type,tenant_key)

        <foreach collection="saves" item="item" separator="union all">
            select
            #{item.fieldName,jdbcType=VARCHAR},
            #{item.sourceType,jdbcType=INTEGER},
            #{item.fieldType,jdbcType=INTEGER},
            #{item.enableStatus,jdbcType=INTEGER},
            #{item.code,jdbcType=VARCHAR},
            #{item.description,jdbcType=VARCHAR},
            #{item.createTime,jdbcType=DATE},
            #{item.updateTime,jdbcType=DATE},
            #{item.creator,jdbcType=DOUBLE},
            0,
            #{item.tenantKey,jdbcType=VARCHAR}
            from dual
        </foreach>
    </insert>
    <insert id="saveBatch" databaseId="sqlserver">
        <foreach collection="saves" item="item" separator=";">
        INSERT INTO hrsa_attend_quote_field
        (field_name,source_type,field_type,enable_status,code,description,create_time,update_time,creator,delete_type,tenant_key)
        VALUES
            (
            #{item.fieldName},
            #{item.sourceType},
            #{item.fieldType},
            #{item.enableStatus},
            #{item.code},
            #{item.description},
            #{item.createTime},#{item.updateTime}, #{item.creator}, 0, #{item.tenantKey}
            )
        </foreach>
    </insert>

    <update id="updateNameByCode" parameterType="java.util.List">
        update hrsa_attend_quote_field
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="field_name =case" suffix="end,">
                <foreach collection="updates" item="item" index="index">
                    <if test="item.fieldName!=null">
                        when code=#{item.code} then #{item.fieldName}
                    </if>
                </foreach>
            </trim>
            <trim prefix="update_time =case" suffix="end,">
                <foreach collection="updates" item="item" index="index">
                    <if test="item.updateTime!=null">
                        when code=#{item.code} then #{item.updateTime}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        <!--        <foreach collection="updates" separator="or" item="item" index="index" >-->
        <!--            code=#{item.code}-->
        <!--        </foreach>-->
        code in
        <foreach collection="updates" item="item" index="index" separator="," open="(" close=")">
            #{item.code}
        </foreach>
    </update>

    <update id="updateNameByCode" parameterType="java.util.List" databaseId="postgresql">
        <foreach collection="updates" item="item" separator=";">
            UPDATE hrsa_attend_quote_field
            <set>
                <if test="item.fieldName != null">
                    field_name=#{item.fieldName},
                </if>
                <if test="item.updateTime != null">
                    update_time=#{item.updateTime},
                </if>
            </set>
            WHERE code = #{item.code}

        </foreach>

    </update>

    <select id="listSome" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_attend_quote_field t
        WHERE delete_type = 0
        <if test="param.fieldName != null and param.fieldName != ''">
            AND field_name = #{param.fieldName}
        </if>
        <if test="param.sourceType != null and param.sourceType != ''">
            AND source_type = #{param.sourceType}
        </if>
    </select>

    <delete id="deleteByIds">
        UPDATE hrsa_attend_quote_field
        SET delete_type=1
        WHERE
        id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
        AND delete_type = 0
    </delete>


    <select id="getAttendanceSerials" resultType="string">
        select id from 	kq_ShiftManagement where isdelete is null or isdelete <![CDATA[ <> ]]>  '1'
    </select>

</mapper>