<?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.siarchives.OtherSchemeMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.siarchives.po.InsuranceArchivesOtherSchemePO">
        <result column="id" property="id"/>
        <result column="employee_id" property="employeeId"/>
        <result column="non_payment" property="nonPayment"/>
        <result column="welfare_type" property="welfareType"/>
        <result column="other_start_time" property="otherStartTime"/>
        <result column="other_end_time" property="otherEndTime"/>
        <result column="other_scheme_id" property="otherSchemeId"/>
        <result column="payment_organization" property="paymentOrganization"/>
        <result column="under_take" property="underTake"/>
        <result column="other_payment_base_string" property="otherPaymentBaseString"/>
        <result column="other_payment_com_base_string" property="otherPaymentComBaseString"/>
        <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.employee_id
        , t.non_payment
        , t.welfare_type
        , t.other_start_time
        , t.other_end_time
        , t.other_scheme_id
        , t.payment_organization
        , t.under_take
        , t.other_payment_base_string
        , t.other_payment_com_base_string
        , t.create_time
        , t.update_time
        , t.creator
        , t.delete_type
        , t.tenant_key
    </sql>


    <!-- 根据人员获取记录 -->
    <select id="getOtherByEmployeeId" resultMap="BaseResultMap" >
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_other_archives t
        WHERE delete_type = 0
        <if test="employeeIds != null and employeeIds.size()>0">
            AND employee_id IN
            <foreach collection="employeeIds" open="(" item="employeeId" separator="," close=")">
                #{employeeId}
            </foreach>
        </if>

    </select>

    <!-- 根据人员id和个税扣缴人id获取记录 -->
    <select id="getOtherByEmployeeIdAndPayOrg" resultMap="BaseResultMap" >
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_other_archives t
        WHERE delete_type = 0
        AND employee_id = #{param.employeeId}
        AND payment_organization = #{param.paymentOrganization}
    </select>

    <!-- 根据id获取记录 -->
    <select id="getOtherById" resultMap="BaseResultMap" >
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_other_archives t
        WHERE delete_type = 0
        <if test="ids != null and ids.size()>0">
            AND id IN
            <foreach collection="ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>

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

    <!-- 批量删除 -->
    <delete id="batchDeleteByEmployeeIds">
        UPDATE hrsa_other_archives
        SET delete_type = 1
        WHERE delete_type = 0
        AND employee_id IN
        <if test="employeeIds != null and employeeIds.size()>0">
            <foreach collection="employeeIds" open="(" item="employeeId" separator="," close=")">
                #{employeeId}
            </foreach>
        </if>

    </delete>

    <delete id="deleteByIds">
        UPDATE hrsa_other_archives
        SET delete_type = 1
        WHERE delete_type = 0
        AND id IN
        <if test="ids != null and ids.size()>0">
            <foreach collection="ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </delete>

    <!-- 根据人员id和个税扣缴义务人删除档案 -->
    <update id="deleteByEmployeeIdAndPayOrg" parameterType="com.engine.salary.entity.siarchives.po.InsuranceArchivesOtherSchemePO">
        UPDATE hrsa_other_archives
        SET delete_type = 1
        WHERE employee_id = #{param.employeeId}
        AND payment_organization = #{param.paymentOrganization}
        AND delete_type = 0
    </update>

    <!-- 批量保存 -->
    <insert id="batchSave">
        INSERT INTO hrsa_other_archives(
        other_scheme_id,
        other_start_time,
        under_take,
        tenant_key,
        welfare_type,
        other_end_time,
        employee_id,
        delete_type,
        update_time,
        create_time,
        non_payment,
        creator,
        payment_organization,
        other_payment_base_string,
        other_payment_com_base_string)
        VALUES
        <foreach collection="otherSchemePOS" item="item" separator=",">
            (
            #{item.otherSchemeId},
            #{item.otherStartTime},
            #{item.underTake},
            #{item.tenantKey},
            #{item.welfareType},
            #{item.otherEndTime},
            #{item.employeeId},
            #{item.deleteType},
            #{item.updateTime},
            #{item.createTime},
            #{item.nonPayment},
            #{item.creator},
            #{item.paymentOrganization},
            #{item.otherPaymentBaseString},
            #{item.otherPaymentComBaseString}
            )
        </foreach>
    </insert>
    <insert id="batchSave" databaseId="oracle">
        INSERT INTO hrsa_other_archives(
        other_scheme_id,
        other_start_time,
        under_take,
        tenant_key,
        welfare_type,
        other_end_time,
        employee_id,
        delete_type,
        update_time,
        create_time,
        non_payment,
        creator,
        payment_organization,
        other_payment_base_string,
        other_payment_com_base_string)
        <foreach collection="otherSchemePOS" item="item" separator="union all">
            select
            #{item.otherSchemeId,jdbcType=DOUBLE},
            #{item.otherStartTime,jdbcType=VARCHAR},
            #{item.underTake,jdbcType=INTEGER},
            #{item.tenantKey,jdbcType=VARCHAR},
            #{item.welfareType,jdbcType=INTEGER},
            #{item.otherEndTime,jdbcType=VARCHAR},
            #{item.employeeId,jdbcType=DOUBLE},
            #{item.deleteType,jdbcType=INTEGER},
            #{item.updateTime,jdbcType=DATE},
            #{item.createTime,jdbcType=DATE},
            #{item.nonPayment,jdbcType=INTEGER},
            #{item.creator,jdbcType=DOUBLE},
            #{item.paymentOrganization,jdbcType=DOUBLE},
            #{item.otherPaymentBaseString,jdbcType=VARCHAR},
            #{item.otherPaymentComBaseString,jdbcType=VARCHAR}
            from dual
        </foreach>
    </insert>
    <insert id="batchSave" databaseId="sqlserver">
        <foreach collection="otherSchemePOS" item="item" separator=";">
        INSERT INTO hrsa_other_archives(
        other_scheme_id,
        other_start_time,
        under_take,
        tenant_key,
        welfare_type,
        other_end_time,
        employee_id,
        delete_type,
        update_time,
        create_time,
        non_payment,
        creator,
        payment_organization,
        other_payment_base_string,
        other_payment_com_base_string)
        VALUES
            (
            #{item.otherSchemeId},
            #{item.otherStartTime},
            #{item.underTake},
            #{item.tenantKey},
            #{item.welfareType},
            #{item.otherEndTime},
            #{item.employeeId},
            #{item.deleteType},
            #{item.updateTime},
            #{item.createTime},
            #{item.nonPayment},
            #{item.creator},
            #{item.paymentOrganization},
            #{item.otherPaymentBaseString},
            #{item.otherPaymentComBaseString}
            )
        </foreach>
    </insert>

    <!-- 根据个税扣缴义务人id获取目标账单月可以进行缴纳的人员id -->
    <select id="listCanPayEmpIds" resultType="long">
        SELECT
        t.employee_id
        FROM hrsa_other_archives t
        WHERE delete_type = 0
        AND payment_organization = #{paymentOrganization}
        AND other_scheme_id is not null
        AND other_start_time is not null AND other_start_time <![CDATA[ <= ]]> #{billMonth}
        AND (other_end_time is null OR other_end_time <![CDATA[ >= ]]> #{billMonth} OR other_end_time ='')
    </select>

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

    <select id="listBySchemeId"
            resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesOtherSchemePO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_other_archives t
        left join hrsa_insurance_base_info i on i.other_archives_id  = t.id
        WHERE t.delete_type = 0
        AND other_scheme_id = #{schemeId}
        <if test="runStatuses != null and runStatuses.size()>0">
            AND i.run_status IN
            <foreach collection="runStatuses" open="(" item="runStatus" separator="," close=")">
                #{runStatus}
            </foreach>
        </if>
    </select>

    <update id="batchUpdate" parameterType="java.util.List">
        update hrsa_other_archives
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="employee_id =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.employeeId!=null">
                        when id=#{item.id} then #{item.employeeId}
                    </if>
                </foreach>
            </trim>
            <trim prefix="other_payment_base_string =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.otherPaymentBaseString!=null">
                        when id=#{item.id} then #{item.otherPaymentBaseString}
                    </if>
                </foreach>
            </trim>
            <trim prefix="other_payment_com_base_string =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.otherPaymentComBaseString!=null">
                        when id=#{item.id} then #{item.otherPaymentComBaseString}
                    </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="batchUpdateEndTime">
        UPDATE hrsa_other_archives
        SET other_end_time = #{endTime}
        WHERE delete_type = 0
        <if test="ids != null and ids.size()>0">
            AND id IN
            <foreach collection="ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </update>

    <!-- 更新,更新全部字段 -->
    <update id="updateById" parameterType="com.engine.salary.entity.siarchives.po.InsuranceArchivesOtherSchemePO">
        UPDATE hrsa_other_archives
        <set>
            welfare_type = #{welfareType},
            other_payment_base_string = #{otherPaymentBaseString},
            other_payment_com_base_string = #{otherPaymentComBaseString},
            other_scheme_id = #{otherSchemeId},
            other_end_time = #{otherEndTime},
            other_start_time = #{otherStartTime},
            non_payment = #{nonPayment},
            tenant_key = #{tenantKey},
            employee_id = #{employeeId},
            update_time = #{updateTime},
            under_take = #{underTake},
            payment_organization = #{paymentOrganization},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>

    <!-- 批量更新最后缴纳月为null -->
    <update id="batchUpdateEndTimeToNull">
        UPDATE hrsa_other_archives
        SET other_end_time = null
        WHERE delete_type = 0
        <if test="ids != null and ids.size()>0">
            AND id IN
            <foreach collection="ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </update>

    <update id="updateEndTime">
        UPDATE hrsa_other_archives
        SET other_end_time = #{endTime}
        WHERE delete_type = 0
          AND id = #{id}
    </update>

    <insert id="insert" parameterType="com.engine.salary.entity.siarchives.po.InsuranceArchivesOtherSchemePO">
        INSERT INTO hrsa_other_archives(
            other_scheme_id,
            other_start_time,
            under_take,
            tenant_key,
            welfare_type,
            other_end_time,
            employee_id,
            delete_type,
            update_time,
            create_time,
            non_payment,
            creator,
            payment_organization,
            other_payment_base_string,
            other_payment_com_base_string)
        VALUES
        (
            #{otherSchemeId},
            #{otherStartTime},
            #{underTake},
            #{tenantKey},
            #{welfareType},
            #{otherEndTime},
            #{employeeId},
            #{deleteType},
            #{updateTime},
            #{createTime},
            #{nonPayment},
            #{creator},
            #{paymentOrganization},
            #{otherPaymentBaseString},
            #{otherPaymentComBaseString}
        )
    </insert>
</mapper>