<?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.sischeme.InsuranceSchemeMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.sischeme.po.InsuranceSchemePO">
        <result column="id" property="id"/>
        <result column="payment_area" property="paymentArea"/>
        <result column="payment_type" property="paymentType"/>
        <result column="scheme_name" property="schemeName"/>
        <result column="welfare_type" property="welfareType"/>
        <result column="is_use" property="isUse"/>
        <result column="remarks" property="remarks"/>
        <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"/>
        <result column="shared_type" property="sharedType" />
        <result column="tax_agent_ids" property="taxAgentIds" />
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t.id
        , t.payment_area
        , t.payment_type
        , t.scheme_name
        , t.welfare_type
        , t.is_use
        , t.remarks
        , t.create_time
        , t.update_time
        , t.creator
        , t.delete_type
        , t.tenant_key
        , t.shared_type
        , t.tax_agent_ids
    </sql>

    <sql id="paramSql">
        <!-- 关键字（方案名称） -->
        <if test="schemeName != null and schemeName != ''">
            AND scheme_name like CONCAT('%',#{schemeName},'%')
        </if>
    </sql>
    <sql id="paramSql" databaseId="oracle">
        <if test="schemeName != null and schemeName != ''">
            AND scheme_name like '%'||#{schemeName}||'%'
        </if>
    </sql>
    <sql id="paramSql" databaseId="sqlserver">
        <if test="schemeName != null and schemeName != ''">
            AND scheme_name like '%'+#{schemeName}+'%'
        </if>
    </sql>

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

    <!-- 根据主键获取名称 -->
    <select id="querySchemeName" resultType="java.lang.String">
        SELECT
          scheme_name
        FROM hrsa_social_security_scheme
        WHERE id = #{schemeId}
    </select>


    <!-- 根据名称获取记录 -->
    <select id="listByName" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_social_security_scheme t
        WHERE scheme_name = #{schemeName} AND delete_type = 0
        ORDER BY id DESC
    </select>

    <select id="listAll" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_social_security_scheme t
        WHERE delete_type = 0
        ORDER BY id DESC
    </select>

    <select id="listBySchemeIds" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_social_security_scheme t
        WHERE delete_type = 0
        AND id IN
        <foreach collection="schemeIds" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
        ORDER BY id DESC
    </select>

    <!-- 插入全部字段 -->
    <insert id="insert" parameterType="com.engine.salary.entity.sischeme.po.InsuranceSchemePO"
            keyProperty="id" keyColumn="id" useGeneratedKeys="true"
    >
        INSERT INTO hrsa_social_security_scheme
        <trim prefix="(" suffix=")" suffixOverrides=",">
            payment_area,
            payment_type,
            scheme_name,
            welfare_type,
            is_use,
            remarks,
            create_time,
            update_time,
            creator,
            delete_type,
            tenant_key,
            shared_type,
            tax_agent_ids
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            #{paymentArea},
            #{paymentType},
            #{schemeName},
            #{welfareType},
            #{isUse},
            #{remarks},
            #{createTime},
            #{updateTime},
            #{creator},
            #{deleteType},
            #{tenantKey},
            #{sharedType},
            #{taxAgentIds}
        </trim>
    </insert>

    <insert id="insert" parameterType="com.engine.salary.entity.sischeme.po.InsuranceSchemePO" databaseId="oracle"
    >
        <selectKey  keyProperty="id" resultType="long" order="AFTER">
            select hrsa_social_s_s_id.currval from dual
        </selectKey>
        INSERT INTO hrsa_social_security_scheme
        <trim prefix="(" suffix=")" suffixOverrides=",">
            payment_area,
            payment_type,
            scheme_name,
            welfare_type,
            is_use,
            remarks,
            create_time,
            update_time,
            creator,
            delete_type,
            tenant_key,
            shared_type,
            tax_agent_ids
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            #{paymentArea},
            #{paymentType},
            #{schemeName},
            #{welfareType},
            #{isUse},
            #{remarks},
            #{createTime},
            #{updateTime},
            #{creator},
            #{deleteType},
            #{tenantKey},
            #{sharedType},
            #{taxAgentIds}
        </trim>
    </insert>

    <!-- 更新,更新修改字段 -->
    <update id="update" parameterType="com.engine.salary.entity.sischeme.po.InsuranceSchemePO">
        UPDATE hrsa_social_security_scheme
        <set>
            update_time=#{updateTime},
            payment_area=#{paymentArea},
            payment_type=#{paymentType},
            scheme_name=#{schemeName},
            remarks=#{remarks},
            shared_type=#{sharedType},
            tax_agent_ids=#{taxAgentIds}
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <select id="listByWelfareType" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_social_security_scheme t
        WHERE t.delete_type = 0
        <if test="welfareType != null and welfareType != ''">
            AND
              t.welfare_type = #{welfareType}
        </if>
        order by id desc
    </select>

    <select id="list" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_social_security_scheme t
        WHERE t.delete_type = 0
        <include refid="paramSql"/>
        <if test="welfareType != null and welfareType != ''">
            AND
            t.welfare_type = #{welfareType}
        </if>
        <if test="paymentType != null">
            AND
            t.payment_type = #{paymentType}
        </if>
        order by id desc
    </select>

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

</mapper>