<?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.InsuranceSchemeDetailMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.sischeme.po.InsuranceSchemeDetailPO">
        <result column="id" property="id"/>
        <result column="primary_id" property="primaryId"/>
        <result column="insurance_id" property="insuranceId"/>
        <result column="effective_time" property="effectiveTime"/>
        <result column="expiration_time" property="expirationTime"/>
        <result column="is_payment" property="isPayment"/>
        <result column="payment_scope" property="paymentScope"/>
        <result column="upper_limit" property="upperLimit"/>
        <result column="lower_limit" property="lowerLimit"/>
        <result column="payment_proportion" property="paymentProportion"/>
        <result column="fixed_cost" property="fixedCost"/>
        <result column="valid_num" property="validNum"/>
        <result column="rentention_rule" property="rententionRule"/>
        <result column="payment_cycle" property="paymentCycle"/>
        <result column="account_type" property="accountType"/>
        <result column="cycle_setting" property="cycleSetting"/>
        <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.primary_id
        , t.insurance_id
        , t.effective_time
        , t.expiration_time
        , t.is_payment
        , t.payment_scope
        , t.upper_limit
        , t.lower_limit
        , t.payment_proportion
        , t.fixed_cost
        , t.valid_num
        , t.rentention_rule
        , t.payment_cycle
        , t.account_type
        , t.cycle_setting
        , t.create_time
        , t.update_time
        , t.creator
        , t.delete_type
        , t.tenant_key
    </sql>


    <sql id="formColumns">
        t.id
        , t.primary_id
        , t.insurance_id
        , t.is_payment
        , t.payment_scope
        , t.payment_proportion
        , t.fixed_cost
        , t.valid_num
        , t.rentention_rule
        , t.create_time
        , t.update_time
        , t.creator
        , t.delete_type
        , t.tenant_key
    </sql>


    <select id="queryListBySchemeId" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_scheme_detail t
        WHERE t.primary_id = #{schemeId} AND delete_type = 0
    </select>


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

    <sql id="paramSql">
        <if test="primaryId != null and primaryId != ''">
            AND primary_id = #{primaryId}
        </if>
    </sql>

    <select id="getByPPI" resultMap="BaseResultMap" >
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_scheme_detail t
        WHERE t.insurance_id = #{insuranceId}
        AND t.payment_scope = #{paymentScope}
        AND t.primary_id = #{primaryId}
        AND delete_type = 0
    </select>

    <select id="getByPI" resultMap="BaseResultMap" >
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_scheme_detail t
        WHERE t.insurance_id = #{insuranceId}
        AND t.primary_id = #{primaryId}
        AND delete_type = 0
    </select>

    <!-- 插入全部字段 -->
    <insert id="insert" parameterType="com.engine.salary.entity.sischeme.po.InsuranceSchemeDetailPO"
            keyProperty="id" keyColumn="id" useGeneratedKeys="true"
    >
        INSERT INTO hrsa_scheme_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            primary_id,
            insurance_id,
            effective_time,
            expiration_time,
            is_payment,
            payment_scope,
            upper_limit,
            lower_limit,
            payment_proportion,
            fixed_cost,
            valid_num,
            rentention_rule,
            payment_cycle,
            account_type,
            cycle_setting,
            create_time,
            update_time,
            creator,
            delete_type,
            tenant_key,
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            #{primaryId},
            #{insuranceId},
            #{effectiveTime},
            #{expirationTime},
            #{isPayment},
            #{paymentScope},
            #{upperLimit},
            #{lowerLimit},
            #{paymentProportion},
            #{fixedCost},
            #{validNum},
            #{rententionRule},
            #{paymentCycle},
            #{accountType},
            #{cycleSetting},
            #{createTime},
            #{updateTime},
            #{creator},
            #{deleteType},
            #{tenantKey},
        </trim>
    </insert>

    <insert id="insert" parameterType="com.engine.salary.entity.sischeme.po.InsuranceSchemeDetailPO" databaseId="oracle"
    >
        <selectKey  keyProperty="id" resultType="long" order="AFTER">
            select hrsa_scheme_detail_id.currval from dual
        </selectKey>
        INSERT INTO hrsa_scheme_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            primary_id,
            insurance_id,
            effective_time,
            expiration_time,
            is_payment,
            payment_scope,
            upper_limit,
            lower_limit,
            payment_proportion,
            fixed_cost,
            valid_num,
            rentention_rule,
            payment_cycle,
            account_type,
            cycle_setting,
            create_time,
            update_time,
            creator,
            delete_type,
            tenant_key,
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            #{primaryId},
            #{insuranceId},
            #{effectiveTime},
            #{expirationTime},
            #{isPayment},
            #{paymentScope},
            #{upperLimit},
            #{lowerLimit},
            #{paymentProportion},
            #{fixedCost},
            #{validNum},
            #{rententionRule},
            #{paymentCycle},
            #{accountType},
            #{cycleSetting},
            #{createTime},
            #{updateTime},
            #{creator},
            #{deleteType},
            #{tenantKey},
        </trim>
    </insert>

    <delete id="batchDeleteByPrimaryIds">
        UPDATE hrsa_scheme_detail
        SET delete_type = 1
        WHERE delete_type = 0
        AND primary_id IN
        <foreach collection="primaryIds" open="(" item="primaryIds" separator="," close=")">
            #{primaryIds}
        </foreach>
    </delete>

    <!-- 根据险种id和是否缴费查询社保方案明细表 -->
    <select id="queryListByInsuranceIdIsPayment" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_scheme_detail t
        WHERE t.insurance_id = #{insuranceId} AND t.is_payment = #{isPayment} AND delete_type = 0
    </select>


    <select id="queryListByPrimaryIdIsPayment" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_scheme_detail t,hrsa_insurance_category b
        WHERE t.insurance_id = b.id
        AND t.primary_id = #{primaryId}
        AND t.is_payment = #{isPayment}
        AND b.welfare_type = #{welfareType}
        AND t.delete_type = 0
    </select>


    <select id="queryInsuranceSchemeDetailList" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_scheme_detail t
        WHERE t.primary_id = #{primaryId}
        AND t.is_payment = #{isPayment}
        AND delete_type = 0
    </select>

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

    <select id="listAll" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_scheme_detail t
        WHERE delete_type = 0
    </select>

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

    <update id="batchUpdate" parameterType="java.util.List">
        update hrsa_scheme_detail
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="upper_limit =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.upperLimit!=null">
                        when id=#{item.id} then #{item.upperLimit}
                    </if>
                </foreach>
            </trim>
            <trim prefix="fixed_cost =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.fixedCost!=null">
                        when id=#{item.id} then #{item.fixedCost}
                    </if>
                </foreach>
            </trim>
            <trim prefix="lower_limit =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.lowerLimit!=null">
                        when id=#{item.id} then #{item.lowerLimit}
                    </if>
                </foreach>
            </trim>
            <trim prefix="delete_type =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.deleteType!=null">
                        when id=#{item.id} then #{item.deleteType}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        id in
        <foreach collection="collection" item="item" index="index" separator="," open="(" close=")">
            #{item.id}
        </foreach>
    </update>

    <update id="updateAll" parameterType="com.engine.salary.entity.sischeme.po.InsuranceSchemeDetailPO">
        update hrsa_scheme_detail
        <set>
            <if test="paymentScope != null" >
                payment_scope=#{paymentScope},
            </if>
            <if test="isPayment != null" >
                is_payment=#{isPayment},
            </if>
            <if test="deleteType != null" >
                delete_type=#{deleteType},
            </if>
            <if test="paymentProportion != null" >
                payment_proportion=#{paymentProportion},
            </if>
            <if test="upperLimit != null" >
                upper_limit=#{upperLimit},
            </if>
            <if test="lowerLimit != null" >
                lower_limit=#{lowerLimit},
            </if>
            <if test="updateTime != null" >
                update_time=#{updateTime},
            </if>
            <if test="fixedCost != null" >
                fixed_cost=#{fixedCost},
            </if>
            <if test="validNum != null" >
                valid_num=#{validNum},
            </if>
            <if test="rententionRule != null" >
                rentention_rule=#{rententionRule},
            </if>
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>
</mapper>