<?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.InsuranceBaseInfoMapper">

    <resultMap id="BaseResultMap" type="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO">
        <result column="id" property="id"/>
        <result column="employee_id" property="employeeId"/>
        <result column="payment_organization" property="paymentOrganization"/>
        <result column="social_archives_id" property="socialArchivesId"/>
        <result column="fund_archives_id" property="fundArchivesId"/>
        <result column="other_archives_id" property="otherArchivesId"/>
        <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="run_status" property="runStatus"/>
        <result column="employee_type" property="employeeType"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t.id
        , t.employee_id
        , t.payment_organization
        , t.social_archives_id
        , t.fund_archives_id
        , t.other_archives_id
        , t.run_status
        , t.create_time
        , t.update_time
        , t.creator
        , t.delete_type
        , t.tenant_key
        , t.employee_type
    </sql>

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

    <select id="listByEmployeeIds"
            resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_base_info t
        WHERE delete_type = 0
        AND employee_id IN
        <foreach collection="employeeIds" open="(" item="employeeId" separator="," close=")">
            #{employeeId}
        </foreach>
    </select>

    <!-- 查询对应id的数据 -->
    <select id="listByIds" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_base_info t
        WHERE id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_base_info t
        WHERE id = #{id}
        AND delete_type = 0
    </select>

    <select id="getInsuranceBaseInfoListByInsuranceDetail" resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO">
        SELECT social.employee_id,
        social.id AS socialArchivesId,
        social.payment_organization,
        fund.id AS fundArchivesId,
        other.id AS otherArchivesId
        FROM hrsa_social_archives social
        LEFT JOIN( SELECT fund.payment_organization, fund.employee_id, fund.id FROM hrsa_fund_archives fund WHERE fund.delete_type = 0 )fund ON social.employee_id  = fund.employee_id AND social.payment_organization = fund.payment_organization
        LEFT JOIN( SELECT other.payment_organization, other.employee_id, other.id FROM hrsa_other_archives other WHERE other.delete_type = 0 )other ON social.employee_id  = other.employee_id AND social.payment_organization = other.payment_organization
        WHERE
        social.delete_type = 0
        <if test="employeeIds != null and employeeIds.size()>0">
            AND (social.employee_id IN
            <!-- 处理in的集合超过1000条时Oracle不支持的情况 -->
            <trim suffixOverrides=" OR social.employee_id IN()">
                <foreach collection="employeeIds " item="Id" index="index" open="(" close=")">
                    <if test="index != 0">
                        <choose>
                            <when test="index % 1000 == 999">) OR social.employee_id IN (</when>
                            <otherwise>,</otherwise>
                        </choose>
                    </if>
                    #{Id}
                </foreach>
            </trim>
            )
        </if>
    </select>

    <select id="getInsuranceBaseInfoList" resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_base_info t
        WHERE
        t.delete_type = 0
    </select>


    <insert id="batchSave">
        INSERT INTO hrsa_insurance_base_info
        (id,employee_id,payment_organization,social_archives_id,fund_archives_id,other_archives_id,tenant_key,creator,delete_type,create_time,update_time,run_status, employee_type)
        VALUES
        <foreach collection="infos" item="item" separator=",">
            (
            #{item.id},
            #{item.employeeId},
            #{item.paymentOrganization},
            #{item.socialArchivesId},
            #{item.fundArchivesId},
            #{item.otherArchivesId},
            #{item.tenantKey},
            #{item.creator},
            #{item.deleteType},
            #{item.createTime},
            #{item.updateTime},
            #{item.runStatus},
            #{item.employeeType}
            )
        </foreach>
    </insert>
    <insert id="batchSave" databaseId="oracle">
        INSERT INTO hrsa_insurance_base_info
        (id,employee_id,payment_organization,social_archives_id,fund_archives_id,other_archives_id,tenant_key,creator,delete_type,create_time,update_time,run_status, employee_type)
        <foreach collection="infos" item="item" separator="union all">
            select
            #{item.id,jdbcType=DOUBLE},
            #{item.employeeId,jdbcType=DOUBLE},
            #{item.paymentOrganization,jdbcType=DOUBLE},
            #{item.socialArchivesId,jdbcType=DOUBLE},
            #{item.fundArchivesId,jdbcType=DOUBLE},
            #{item.otherArchivesId,jdbcType=DOUBLE},
            #{item.tenantKey,jdbcType=VARCHAR},
            #{item.creator,jdbcType=DOUBLE},
            #{item.deleteType},
            #{item.createTime},
            #{item.updateTime},
            #{item.runStatus,jdbcType=VARCHAR},
            #{item.employeeType}
            from dual
        </foreach>
    </insert>
    <insert id="batchSave" databaseId="sqlserver">
        <foreach collection="infos" item="item" separator=";">
            INSERT INTO hrsa_insurance_base_info
            (id,employee_id,payment_organization,social_archives_id,fund_archives_id,other_archives_id,tenant_key,creator,delete_type,create_time,update_time,run_status, employee_type)
            VALUES
            (
            #{item.id},
            #{item.employeeId},
            #{item.paymentOrganization},
            #{item.socialArchivesId},
            #{item.fundArchivesId},
            #{item.otherArchivesId},
            #{item.tenantKey},
            #{item.creator},
            #{item.deleteType},
            #{item.createTime},
            #{item.updateTime},
            #{item.runStatus},
            #{item.employeeType}
            )
        </foreach>
    </insert>

    <!-- 批量删除 -->
    <delete id="batchDeleteByEmployeeIds">
        UPDATE hrsa_insurance_base_info
        SET delete_type = 1
        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>
    </delete>

    <delete id="deleteByIds">
        UPDATE hrsa_insurance_base_info
        SET delete_type = 1
        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>
    </delete>

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

    <!-- 根据个税扣缴义务人id获取记录 -->
    <select id="getSocialByPaymentOrganization" resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_base_info t
        WHERE delete_type = 0
        AND payment_organization = #{paymentOrganization}
    </select>

    <select id="getRePayList" resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_base_info t
        LEFT JOIN( SELECT social.payment_organization, social.employee_id, social.social_end_time FROM hrsa_social_archives social WHERE social.delete_type = 0 )social ON t.employee_id  = social.employee_id AND t.payment_organization = social.payment_organization
        LEFT JOIN( SELECT fund.payment_organization, fund.employee_id, fund.fund_end_time FROM hrsa_fund_archives fund WHERE fund.delete_type = 0 )fund ON t.employee_id  = fund.employee_id AND t.payment_organization = fund.payment_organization
        LEFT JOIN( SELECT other.payment_organization, other.employee_id, other.other_end_time FROM hrsa_other_archives other WHERE other.delete_type = 0 )other ON t.employee_id  = other.employee_id AND t.payment_organization = other.payment_organization
        WHERE t.delete_type = 0
        AND t.run_status = '3'
        AND (
            (social.social_end_time is not null  and social.social_end_time <![CDATA[ > ]]> #{today})
            OR
            (fund.fund_end_time is not null  and fund.fund_end_time <![CDATA[ > ]]> #{today})
            OR
            (other.other_end_time is not null  and other.other_end_time <![CDATA[ > ]]> #{today})
        )
    </select>

    <select id="getReStayDelList" resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_base_info t
        LEFT JOIN( SELECT social.payment_organization, social.employee_id, social.social_end_time, social.social_scheme_id FROM hrsa_social_archives social WHERE social.delete_type = 0 )social ON t.employee_id  = social.employee_id AND t.payment_organization = social.payment_organization
        LEFT JOIN( SELECT fund.payment_organization, fund.employee_id, fund.fund_end_time, fund.fund_scheme_id FROM hrsa_fund_archives fund WHERE fund.delete_type = 0 )fund ON t.employee_id  = fund.employee_id AND t.payment_organization = fund.payment_organization
        LEFT JOIN( SELECT other.payment_organization, other.employee_id, other.other_end_time, other.other_scheme_id FROM hrsa_other_archives other WHERE other.delete_type = 0 )other ON t.employee_id  = other.employee_id AND t.payment_organization = other.payment_organization
        WHERE t.delete_type = 0
        AND t.run_status = '2'
        AND (social.social_scheme_id is null or (social.social_end_time is not null and social.social_end_time <![CDATA[ <= ]]> #{today}))
        AND (fund.fund_scheme_id is null or (fund.fund_end_time is not null and fund.fund_end_time <![CDATA[ <= ]]> #{today}))
        AND (other.other_scheme_id is null or (other.other_end_time is not null and other.other_end_time <![CDATA[ <= ]]> #{today}))
    </select>

    <select id="getAbnormalList" resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_base_info t
        LEFT JOIN hrmresource e ON e.id = t.employee_id
        WHERE t.delete_type = 0
        <!-- 档案状态 -->
        <if test="param.runStatuses != null and param.runStatuses.size()>0">
            AND t.run_status IN
            <foreach collection="param.runStatuses" open="(" item="runStatus" separator="," close=")">
                #{runStatus}
            </foreach>
        </if>
        <!-- 人事状态 -->
        <if test="param.personnelStatuses != null and param.personnelStatuses.size()>0">
            AND e.status IN
            <foreach collection="param.personnelStatuses" open="(" item="personnelStatus" separator="," close=")">
                #{personnelStatus}
            </foreach>
        </if>
    </select>

    <select id="getOneByEmployeeIdAndPayOrg" resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_insurance_base_info t
        WHERE t.delete_type = 0
        AND employee_id = #{employeeId}
        AND payment_organization = #{paymentOrganization}

    </select>
    <select id="listEndDateIsNull"
            resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO">
        select
        a.id
        , a.employee_id
        , a.payment_organization
        , a.social_archives_id
        , a.fund_archives_id
        , a.other_archives_id
        , a.run_status
        , a.create_time
        , a.update_time
        , a.creator
        , a.delete_type
        , a.tenant_key
        , a.employee_type
        from hrsa_insurance_base_info a
        left join hrsa_social_archives s on a.social_archives_id=s.id and s.delete_type=0
        left join hrsa_fund_archives f on a.fund_archives_id=f.id and f.delete_type=0
        left join hrsa_other_archives o on a.other_archives_id=o.id and o.delete_type=0
        where a.delete_type = 0
        and (social_end_time is null or social_end_time ='' or fund_end_time is null or fund_end_time =''
                 or other_end_time is null or other_end_time ='' )
        and a.employee_id in
        <foreach collection="employeeIds" open="(" item="employeeId" separator="," close=")">
            #{employeeId}
        </foreach>
    </select>


    <select id="listStartDateIsNull"
            resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO">
        select
        a.id
        , a.employee_id
        , a.payment_organization
        , a.social_archives_id
        , a.fund_archives_id
        , a.other_archives_id
        , a.run_status
        , a.create_time
        , a.update_time
        , a.creator
        , a.delete_type
        , a.tenant_key
        , a.employee_type
        from hrsa_insurance_base_info a
        left join hrsa_social_archives s on a.social_archives_id=s.id and s.delete_type=0
        left join hrsa_fund_archives f on a.fund_archives_id=f.id and f.delete_type=0
        left join hrsa_other_archives o on a.other_archives_id=o.id and o.delete_type=0
        where a.delete_type = 0
        and (social_start_time is null or social_start_time ='' or fund_start_time is null or fund_start_time =''
        or other_start_time is null or other_start_time ='' )
        <if test="employeeIds != null and employeeIds.size()>0">
            and a.employee_id in
            <foreach collection="employeeIds" open="(" item="employeeId" separator="," close=")">
                #{employeeId}
            </foreach>
        </if>
    </select>

    <update id="updateRunStatusByIds" parameterType="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO">
        UPDATE hrsa_insurance_base_info
        <set>
            run_status=#{runStatus}
        </set>
        WHERE id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>

    </update>

    <update id="updateRunStatusByEmployeeIds" parameterType="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO">
        UPDATE hrsa_insurance_base_info
        <set>
            run_status=#{runStatus}
        </set>
        WHERE employee_id IN
        <foreach collection="employeeIds" open="(" item="employeeId" separator="," close=")">
            #{employeeId}
        </foreach>

    </update>

    <update id="batchUpdate">
        UPDATE hrsa_insurance_base_info
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="run_status = case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    WHEN id = #{item.id} THEN #{item.runStatus}
                </foreach>
            </trim>
            <trim prefix="update_time = case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    WHEN id = #{item.id} THEN #{item.updateTime}
                </foreach>
            </trim>
            <trim prefix="delete_type = case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    WHEN id = #{item.id} THEN #{item.deleteType}
                </foreach>
            </trim>
        </trim>
        WHERE delete_type = 0
        AND id IN
        <foreach collection="collection" item="item" open="(" close=")" separator=",">
            #{item.id}
        </foreach>
    </update>

    <update id="updateById" parameterType="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO">
        UPDATE hrsa_insurance_base_info
        <set>

            <if test="null != employeeId and '' != employeeId">employee_id = #{employeeId},</if>
            <if test="null != paymentOrganization and '' != paymentOrganization">payment_organization = #{paymentOrganization},</if>
            <if test="null != socialArchivesId and '' != socialArchivesId">social_archives_id = #{socialArchivesId},</if>
            <if test="null != fundArchivesId and '' != fundArchivesId">fund_archives_id = #{fundArchivesId},</if>
            <if test="null != otherArchivesId and '' != otherArchivesId">other_archives_id = #{otherArchivesId},</if>
            <if test="null != runStatus and '' != runStatus">run_status = #{runStatus},</if>
            <if test="null != createTime">create_time = #{createTime},</if>
            <if test="null != updateTime">update_time = #{updateTime},</if>
            <if test="null != creator and '' != creator">creator = #{creator},</if>
            <if test="null != deleteType and '' != deleteType">delete_type = #{deleteType},</if>
            <if test="null != tenantKey and '' != tenantKey">tenant_key = #{tenantKey},</if>
        </set>
        WHERE id = #{id}
    </update>

</mapper>