<?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.SocialSchemeMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.siarchives.po.InsuranceArchivesSocialSchemePO">
        <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="social_start_time" property="socialStartTime"/>
        <result column="social_end_time" property="socialEndTime"/>
        <result column="social_scheme_id" property="socialSchemeId"/>
        <result column="social_account" property="socialAccount"/>
        <result column="payment_organization" property="paymentOrganization"/>
        <result column="under_take" property="underTake"/>
        <result column="social_payment_base_string" property="socialPaymentBaseString"/>
        <result column="social_payment_com_base_string" property="socialPaymentComBaseString"/>
        <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.social_start_time
        , t.social_end_time
        , t.social_scheme_id
        , t.social_account
        , t.payment_organization
        , t.under_take
        , t.social_payment_base_string
        , t.social_payment_com_base_string
        , t.create_time
        , t.update_time
        , t.creator
        , t.delete_type
        , t.tenant_key
    </sql>


    <!-- 根据人员id获取记录 -->
    <select id="getSocialByEmployeeId" resultMap="BaseResultMap" >
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_social_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="getSocialByEmployeeIdAndPayOrg" resultMap="BaseResultMap" >
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_social_archives t
        WHERE delete_type = 0
        AND employee_id = #{param.employeeId}
        AND payment_organization = #{param.paymentOrganization}
    </select>

    <!-- 根据ids获取多条记录 -->
    <select id="getSocialById" resultMap="BaseResultMap" >
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_social_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_social_archives t
        WHERE delete_type = 0
        AND id = #{id}
    </select>

    <!-- 批量删除 -->
    <delete id="batchDeleteByEmployeeIds">
        UPDATE hrsa_social_archives
        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_social_archives
        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.InsuranceArchivesSocialSchemePO">
        UPDATE hrsa_social_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_social_archives(
        welfare_type,
        delete_type,
        social_payment_base_string,
        social_payment_com_base_string,
        social_scheme_id,
        create_time,
        social_end_time,
        social_start_time,
        creator,
        non_payment,
        tenant_key,
        employee_id,
        update_time,
        under_take,
        social_account,
        payment_organization)
        VALUES
        <foreach collection="socialSchemePOS" item="item" separator=",">
            (
            #{item.welfareType},
            #{item.deleteType},
            #{item.socialPaymentBaseString},
            #{item.socialPaymentComBaseString},
            #{item.socialSchemeId},
            #{item.createTime},
            #{item.socialEndTime},
            #{item.socialStartTime},
            #{item.creator},
            #{item.nonPayment},
            #{item.tenantKey},
            #{item.employeeId},
            #{item.updateTime},
            #{item.underTake},
            #{item.socialAccount},
            #{item.paymentOrganization}
            )
        </foreach>
    </insert>
    <insert id="batchSave" databaseId="oracle">
        INSERT INTO hrsa_social_archives(
        welfare_type,
        delete_type,
        social_payment_base_string,
        social_payment_com_base_string,
        social_scheme_id,
        create_time,
        social_end_time,
        social_start_time,
        creator,
        non_payment,
        tenant_key,
        employee_id,
        update_time,
        under_take,
        social_account,
        payment_organization)
        <foreach collection="socialSchemePOS" item="item" separator="union all">
            select
            #{item.welfareType,jdbcType=INTEGER},
            #{item.deleteType,jdbcType=INTEGER},
            #{item.socialPaymentBaseString,jdbcType=VARCHAR},
            #{item.socialPaymentComBaseString,jdbcType=VARCHAR},
            #{item.socialSchemeId,jdbcType=DOUBLE},
            #{item.createTime,jdbcType=DATE},
            #{item.socialEndTime,jdbcType=VARCHAR},
            #{item.socialStartTime,jdbcType=VARCHAR},
            #{item.creator,jdbcType=DOUBLE},
            #{item.nonPayment,jdbcType=INTEGER},
            #{item.tenantKey,jdbcType=VARCHAR},
            #{item.employeeId,jdbcType=DOUBLE},
            #{item.updateTime,jdbcType=DATE},
            #{item.underTake,jdbcType=INTEGER},
            #{item.socialAccount,jdbcType=VARCHAR},
            #{item.paymentOrganization,jdbcType=DOUBLE}
            from dual
        </foreach>
    </insert>
    <insert id="batchSave" databaseId="sqlserver">
        <foreach collection="socialSchemePOS" item="item" separator=";">
        INSERT INTO hrsa_social_archives(
        welfare_type,
        delete_type,
        social_payment_base_string,
        social_payment_com_base_string,
        social_scheme_id,
        create_time,
        social_end_time,
        social_start_time,
        creator,
        non_payment,
        tenant_key,
        employee_id,
        update_time,
        under_take,
        social_account,
        payment_organization)
        VALUES
            (
            #{item.welfareType},
            #{item.deleteType},
            #{item.socialPaymentBaseString},
            #{item.socialPaymentComBaseString},
            #{item.socialSchemeId},
            #{item.createTime},
            #{item.socialEndTime},
            #{item.socialStartTime},
            #{item.creator},
            #{item.nonPayment},
            #{item.tenantKey},
            #{item.employeeId},
            #{item.updateTime},
            #{item.underTake},
            #{item.socialAccount},
            #{item.paymentOrganization}
            )
        </foreach>
    </insert>


    <!-- 人员档案信息 -->

    <sql id="paramSql">
        <!-- 关键字（姓名、工号 -->
        <if test="param.keyword != null and param.keyword != ''">
            AND
            (
            a.lastname like CONCAT('%',#{param.keyword},'%')
            OR a.workcode like CONCAT('%',#{param.keyword},'%')
            )
        </if>
    </sql>
    <sql id="paramSql" databaseId="oracle">
        <if test="param.keyword != null and param.keyword != ''">
            AND
            (
            e.lastname  like '%'||#{param.keyword}||'%'
            OR e.workcode like '%'||#{param.keyword}||'%'
            )
        </if>
    </sql>
    <sql id="paramSql" databaseId="sqlserver">
        <if test="param.keyword != null and param.keyword != ''">
            AND
            (
            e.lastname like '%'+#{param.keyword}+'%'
            OR e.workcode like '%'+#{param.keyword}+'%'
            )
        </if>
    </sql>

    <sql id="paramSqlCommon">
        <!--部门 多选-->
        <if test="param.departmentIds != null and param.departmentIds.size()>0">
            AND a.departmentid IN
            <foreach collection="param.departmentIds" open="(" item="departmentId" separator="," close=")">
                #{departmentId}
            </foreach>
        </if>
        <!--状态 多选-->
        <if test="param.statuses != null and param.statuses.size()>0">
            AND a.status IN
            <foreach collection="param.statuses" open="(" item="status" separator="," close=")">
                #{status}
            </foreach>
        </if>
        <!--岗位 多选-->
        <if test="param.positions != null and param.positions.size()>0">
            AND a.jobtitle IN
            <foreach collection="param.positions" open="(" item="position" separator="," close=")">
                #{position}
            </foreach>
        </if>
        <!--入职日期-->
        <if test="param.hiredateStart != null and param.hiredateEnd != null">
            AND  a.companystartdate between #{param.hiredateStart} and #{param.hiredateEnd}
        </if>
        <!--离职日期-->
        <if test="param.dimissionDateStart != null and param.dimissionDateEnd != null">
            AND  f.dismissdate between #{param.dimissionDateStart} and #{param.dimissionDateEnd}
        </if>
        <!--方案主键-->
        <if test ="param.siSchemeId != null">
            AND c.social_scheme_id = #{param.siSchemeId}
        </if>
        <if test ="param.fundSchemeId != null">
            AND d.fund_scheme_id = #{param.fundSchemeId}
        </if>
        <if test ="param.otherSchemeId != null">
            AND e.other_scheme_id = #{param.otherSchemeId}
        </if>
    </sql>



    <select id="listPageEmployeePOS" resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesEmployeePO">
        SELECT
          a.id as employeeId,
          a.lastname as userName,
          b.departmentname as departmentName,
          a.mobile as telephone
        FROM
        hrmresource a
        LEFT JOIN hrmdepartment b ON a.departmentid = b.id
        LEFT JOIN hrsa_social_archives c ON a.id = c.employee_id
        LEFT JOIN hrsa_fund_archives d ON a.id = d.employee_id
        LEFT JOIN hrsa_other_archives e ON a.id = e.employee_id
        LEFT JOIN bill_hrmdismiss f on a.id = f.resource_n
        WHERE
        a.status not in (7)
        <include refid="paramSql"/>
        <include refid="paramSqlCommon"/>
        ORDER BY a.id asc
    </select>


    <select id="tips" resultType="long">
        SELECT
          employee_id
        FROM
          hrsa_social_archives
        WHERE
        social_start_time is not null and social_end_time is null
    </select>


    <select id="queryEmployeeList" resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesEmployeePO">
        SELECT base.id AS baseInfoId, e.ID AS employeeId, e.lastname AS userName, e.departmentid AS departmentId, e.workcode AS jobNum, e.MOBILE AS telephone,
               d.departmentname AS departmentName, e.jobtitle AS position, e.STATUS AS userStatus,e.companystartdate AS hiredate,e.certificatenum as idNo,
        e.enddate as dimissionDate,
        social.siSchemeId,
        base.payment_organization AS paymentOrganization,
        fund.fundSchemeId,
        other.otherSchemeId,
        social.id AS socialId,
        fund.id AS fundId,
        other.id AS otherId,
        e.companystartdate as companystartdate,
        e.enddate as dismissdate,
        c.subcompanyname as subcompanyName

        FROM hrsa_insurance_base_info base
        LEFT JOIN hrmresource e ON base.employee_id = e.id
        LEFT JOIN hrmdepartment d ON e.departmentid = d.ID
        LEFT JOIN hrmsubcompany c ON c.id = e.subcompanyid1
        LEFT JOIN( SELECT social.id, social.employee_id, social.social_scheme_id AS siSchemeId, social.payment_organization FROM hrsa_social_archives social WHERE social.delete_type = 0 )social ON e.id  = social.employee_id AND base.payment_organization = social.payment_organization
        LEFT JOIN( SELECT fund.id, fund.employee_id, fund.fund_scheme_id AS fundSchemeId, fund.payment_organization FROM hrsa_fund_archives fund WHERE fund.delete_type = 0 )fund ON e.id  = fund.employee_id AND base.payment_organization = fund.payment_organization
        LEFT JOIN( SELECT other.id, other.employee_id, other.other_scheme_id AS otherSchemeId, other.payment_organization FROM hrsa_other_archives other WHERE other.delete_type = 0 )other ON e.id  = other.employee_id AND base.payment_organization = other.payment_organization
        WHERE
        1=1
        AND base.delete_type = 0
        AND base.employee_type is null
        <include refid="condition"></include>
        <if test="param.orderRule != null">
            ORDER BY ${param.orderRule.orderRule} ${param.orderRule.ascOrDesc}
        </if>
    </select>
    <!--]]>-->
    <select id="queryExtEmployeeList" resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesEmployeePO">
        SELECT base.id AS baseInfoId, e.id AS employeeId, e.username AS userName, e.department_id AS departmentId, e.workcode AS jobNum, e.mobile AS telephone,
        d.departmentname AS departmentName, e.jobtitle_id AS position, e.status AS userStatus,e.companystartdate AS hiredate,
        social.siSchemeId,
        base.payment_organization AS paymentOrganization,
        fund.fundSchemeId,
        other.otherSchemeId,
        social.id AS socialId,
        fund.id AS fundId,
        other.id AS otherId,
        e.companystartdate as companystartdate,
        c.subcompanyname as subcompanyName

        FROM hrsa_insurance_base_info base
        LEFT JOIN hrsa_external_employee e ON base.employee_id = e.id
        LEFT JOIN hrmdepartment d ON e.department_id = d.ID
        LEFT JOIN hrmsubcompany c ON c.id = e.subcompany_id
        LEFT JOIN( SELECT social.id, social.employee_id, social.social_scheme_id AS siSchemeId, social.payment_organization FROM hrsa_social_archives social WHERE social.delete_type = 0 )social ON e.id  = social.employee_id AND base.payment_organization = social.payment_organization
        LEFT JOIN( SELECT fund.id, fund.employee_id, fund.fund_scheme_id AS fundSchemeId, fund.payment_organization FROM hrsa_fund_archives fund WHERE fund.delete_type = 0 )fund ON e.id  = fund.employee_id AND base.payment_organization = fund.payment_organization
        LEFT JOIN( SELECT other.id, other.employee_id, other.other_scheme_id AS otherSchemeId, other.payment_organization FROM hrsa_other_archives other WHERE other.delete_type = 0 )other ON e.id  = other.employee_id AND base.payment_organization = other.payment_organization
        WHERE
        1=1
        AND base.delete_type = 0
        AND base.employee_type = 1
        <include refid="extCondition"></include>

        ORDER BY base.create_time desc

    </select>


    <sql id="condition">
        <if test="param.keyword != null and param.keyword != ''">
            AND
            (
            e.lastname like CONCAT('%',#{param.keyword},'%')
            )
        </if>
        <if test="param.userName != null and param.userName != ''">
            AND e.lastname like CONCAT('%',#{param.userName},'%')
        </if>
        <if test="param.jobNum != null and param.jobNum != ''">
            AND e.workcode like CONCAT('%',#{param.jobNum},'%')
        </if>
        <if test="param.departmentIds != null and param.departmentIds.size()>0">
            AND e.departmentid IN
            <foreach collection="param.departmentIds" open="(" item="departmentId" separator="," close=")">
                #{departmentId}
            </foreach>
        </if>
        <if test="param.subcompanyIds != null and param.subcompanyIds.size()>0">
            AND e.subcompanyid1 IN
            <foreach collection="param.subcompanyIds" open="(" item="subcompanyId" separator="," close=")">
                #{subcompanyId}
            </foreach>
        </if>
        <if test="param.statuses != null and param.statuses.size()>0">
            AND e.STATUS IN
            <foreach collection="param.statuses" open="(" item="userStatus" separator="," close=")">
                #{userStatus}
            </foreach>
        </if>
        <if test="param.positions != null and param.positions.size()>0">
            AND e.jobtitle IN
            <foreach collection="param.positions" open="(" item="position" separator="," close=")">
                #{position}
            </foreach>
        </if>
        <if test="param.hiredateStart != null">
            AND e.companystartdate &gt; #{param.hiredateStart}
        </if>
        <if test="param.hiredateEnd != null">
            AND e.companystartdate &lt; #{param.hiredateEnd}
        </if>
        <if test="param.dimissionDateStart != null">
            AND e.enddate &gt; #{param.dimissionDateStart}
        </if>
        <if test="param.dimissionDateEnd != null">
            AND e.enddate &lt; #{param.dimissionDateEnd}
        </if>
        <if test="param.siSchemeId != null and param.siSchemeId != 0">
            AND siSchemeId = #{param.siSchemeId}
        </if>
        <if test="param.fundSchemeId != null and param.fundSchemeId != 0">
            AND fundSchemeId = #{param.fundSchemeId}
        </if>
        <if test="param.otherSchemeId != null and param.otherSchemeId != 0">
            AND otherSchemeId = #{param.otherSchemeId}
        </if>
        <if test="param.taxAgentId != null and param.taxAgentId != 0">
            AND base.payment_organization = #{param.taxAgentId}
        </if>
        <if test="param.runStatuses != null and param.runStatuses.size()>0">
            AND base.run_status IN
            <foreach collection="param.runStatuses" open="(" item="runStatus" separator="," close=")">
                #{runStatus}
            </foreach>
        </if>

        <if test="param.employeeIds != null and param.employeeIds.size() > 0">
            AND e.ID IN
            <foreach collection="param.employeeIds" open="(" separator="," item="item" close=")">
                #{item}
            </foreach>
        </if>

        <if test="param.ids != null and param.ids.size() > 0">
            AND base.id IN
            <foreach collection="param.ids" open="(" separator="," item="item" close=")">
                #{item}
            </foreach>
        </if>
    </sql>

    <sql id="condition" databaseId="sqlserver">
        <if test="param.keyword != null and param.keyword != ''">
            AND
            (
            e.lastname like '%'+#{param.keyword}+'%'
            )
        </if>
        <if test="param.userName != null and param.userName != ''">
            AND e.lastname like '%'+#{param.userName}+'%'
        </if>
        <if test="param.jobNum != null and param.jobNum != ''">
            AND e.workcode like '%'+#{param.jobNum}+'%'
        </if>
        <if test="param.departmentIds != null and param.departmentIds.size()>0">
            AND e.departmentid IN
            <foreach collection="param.departmentIds" open="(" item="departmentId" separator="," close=")">
                #{departmentId}
            </foreach>
        </if>
        <if test="param.subcompanyIds != null and param.subcompanyIds.size()>0">
            AND e.subcompanyid1 IN
            <foreach collection="param.subcompanyIds" open="(" item="subcompanyId" separator="," close=")">
                #{subcompanyId}
            </foreach>
        </if>
        <if test="param.statuses != null and param.statuses.size()>0">
            AND e.STATUS IN
            <foreach collection="param.statuses" open="(" item="userStatus" separator="," close=")">
                #{userStatus}
            </foreach>
        </if>
        <if test="param.positions != null and param.positions.size()>0">
            AND e.jobtitle IN
            <foreach collection="param.positions" open="(" item="position" separator="," close=")">
                #{position}
            </foreach>
        </if>
        <if test="param.hiredateStart != null">
            AND e.companystartdate &gt; #{param.hiredateStart}
        </if>
        <if test="param.hiredateEnd != null">
            AND e.companystartdate &lt; #{param.hiredateEnd}
        </if>
        <if test="param.dimissionDateStart != null">
            AND e.enddate &gt; #{param.dimissionDateStart}
        </if>
        <if test="param.dimissionDateEnd != null">
            AND e.enddate &lt; #{param.dimissionDateEnd}
        </if>
        <if test="param.siSchemeId != null and param.siSchemeId != 0">
            AND siSchemeId = #{param.siSchemeId}
        </if>
        <if test="param.fundSchemeId != null and param.fundSchemeId != 0">
            AND fundSchemeId = #{param.fundSchemeId}
        </if>
        <if test="param.otherSchemeId != null and param.otherSchemeId != 0">
            AND otherSchemeId = #{param.otherSchemeId}
        </if>
        <if test="param.taxAgentId != null and param.taxAgentId != 0">
            AND base.payment_organization = #{param.taxAgentId}
        </if>
        <if test="param.runStatuses != null and param.runStatuses.size()>0">
            AND base.run_status IN
            <foreach collection="param.runStatuses" open="(" item="runStatus" separator="," close=")">
                #{runStatus}
            </foreach>
        </if>
        <if test="param.employeeIds != null and param.employeeIds.size() > 0">
            AND e.ID IN
            <foreach collection="param.employeeIds" open="(" separator="," item="item" close=")">
                #{item}
            </foreach>
        </if>
        <if test="param.ids != null and param.ids.size() > 0">
            AND base.id IN
            <foreach collection="param.ids" open="(" separator="," item="item" close=")">
                #{item}
            </foreach>
        </if>
    </sql>


    <sql id="condition" databaseId="oracle">
        <if test="param.keyword != null and param.keyword != ''">
            AND
            (
            e.lastname like '%'||#{param.keyword}||'%'
            )
        </if>
        <if test="param.userName != null and param.userName != ''">
            AND e.lastname like '%'||#{param.userName}||'%'
        </if>
        <if test="param.jobNum != null and param.jobNum != ''">
            AND e.workcode like '%'||#{param.jobNum}||'%'
        </if>
        <if test="param.departmentIds != null and param.departmentIds.size()>0">
            AND e.departmentid IN
            <foreach collection="param.departmentIds" open="(" item="departmentId" separator="," close=")">
                #{departmentId}
            </foreach>
        </if>
        <if test="param.subcompanyIds != null and param.subcompanyIds.size()>0">
            AND e.subcompanyid1 IN
            <foreach collection="param.subcompanyIds" open="(" item="subcompanyId" separator="," close=")">
                #{subcompanyId}
            </foreach>
        </if>
        <if test="param.statuses != null and param.statuses.size()>0">
            AND e.STATUS IN
            <foreach collection="param.statuses" open="(" item="userStatus" separator="," close=")">
                #{userStatus}
            </foreach>
        </if>
        <if test="param.positions != null and param.positions.size()>0">
            AND e.jobtitle IN
            <foreach collection="param.positions" open="(" item="position" separator="," close=")">
                #{position}
            </foreach>
        </if>
        <if test="param.hiredateStart != null">
            AND e.companystartdate &gt; #{param.hiredateStart}
        </if>
        <if test="param.hiredateEnd != null">
            AND e.companystartdate &lt; #{param.hiredateEnd}
        </if>
        <if test="param.dimissionDateStart != null">
            AND e.enddate &gt; #{param.dimissionDateStart}
        </if>
        <if test="param.dimissionDateEnd != null">
            AND e.enddate &lt; #{param.dimissionDateEnd}
        </if>
        <if test="param.siSchemeId != null and param.siSchemeId != 0">
            AND siSchemeId = #{param.siSchemeId}
        </if>
        <if test="param.fundSchemeId != null and param.fundSchemeId != 0">
            AND fundSchemeId = #{param.fundSchemeId}
        </if>
        <if test="param.otherSchemeId != null and param.otherSchemeId != 0">
            AND otherSchemeId = #{param.otherSchemeId}
        </if>
        <if test="param.taxAgentId != null and param.taxAgentId != 0">
            AND base.payment_organization = #{param.taxAgentId}
        </if>
        <if test="param.runStatuses != null and param.runStatuses.size()>0">
            AND base.run_status IN
            <foreach collection="param.runStatuses" open="(" item="runStatus" separator="," close=")">
                #{runStatus}
            </foreach>
        </if>
        <if test="param.employeeIds != null and param.employeeIds.size() > 0">
            AND e.ID IN
            <foreach collection="param.employeeIds" open="(" separator="," item="item" close=")">
                #{item}
            </foreach>
        </if>
        <if test="param.ids != null and param.ids.size() > 0">
            AND base.id IN
            <foreach collection="param.ids" open="(" separator="," item="item" close=")">
                #{item}
            </foreach>
        </if>
    </sql>

    <sql id="extCondition">
        <if test="param.keyword != null and param.keyword != ''">
            AND
            (
            e.username like CONCAT('%',#{param.keyword},'%')
            )
        </if>
        <if test="param.userName != null and param.userName != ''">
            AND e.username like CONCAT('%',#{param.userName},'%')
        </if>
        <if test="param.jobNum != null and param.jobNum != ''">
            AND e.workcode like CONCAT('%',#{param.jobNum},'%')
        </if>
        <if test="param.departmentIds != null and param.departmentIds.size()>0">
            AND e.department_id IN
            <foreach collection="param.departmentIds" open="(" item="departmentId" separator="," close=")">
                #{departmentId}
            </foreach>
        </if>
        <if test="param.subcompanyIds != null and param.subcompanyIds.size()>0">
            AND e.subcompany_id IN
            <foreach collection="param.subcompanyIds" open="(" item="subcompanyId" separator="," close=")">
                #{subcompanyId}
            </foreach>
        </if>
        <if test="param.statuses != null and param.statuses.size()>0">
            AND e.status IN
            <foreach collection="param.statuses" open="(" item="userStatus" separator="," close=")">
                #{userStatus}
            </foreach>
        </if>
        <if test="param.positions != null and param.positions.size()>0">
            AND e.jobtitle_id IN
            <foreach collection="param.positions" open="(" item="position" separator="," close=")">
                #{position}
            </foreach>
        </if>
        <if test="param.hiredateStart != null">
            AND e.companystartdate &gt; #{param.hiredateStart}
        </if>
        <if test="param.hiredateEnd != null">
            AND e.companystartdate &lt; #{param.hiredateEnd}
        </if>
        <if test="param.siSchemeId != null and param.siSchemeId != 0">
            AND siSchemeId = #{param.siSchemeId}
        </if>
        <if test="param.fundSchemeId != null and param.fundSchemeId != 0">
            AND fundSchemeId = #{param.fundSchemeId}
        </if>
        <if test="param.otherSchemeId != null and param.otherSchemeId != 0">
            AND otherSchemeId = #{param.otherSchemeId}
        </if>
        <if test="param.taxAgentId != null and param.taxAgentId != 0">
            AND base.payment_organization = #{param.taxAgentId}
        </if>
        <if test="param.runStatuses != null and param.runStatuses.size()>0">
            AND base.run_status IN
            <foreach collection="param.runStatuses" open="(" item="runStatus" separator="," close=")">
                #{runStatus}
            </foreach>
        </if>

        <if test="param.employeeIds != null and param.employeeIds.size() > 0">
            AND e.id IN
            <foreach collection="param.employeeIds" open="(" separator="," item="item" close=")">
                #{item}
            </foreach>
        </if>

        <if test="param.ids != null and param.ids.size() > 0">
            AND base.id IN
            <foreach collection="param.ids" open="(" separator="," item="item" close=")">
                #{item}
            </foreach>
        </if>
    </sql>

    <sql id="extCondition" databaseId="sqlserver">
        <if test="param.keyword != null and param.keyword != ''">
            AND
            (
            e.username like '%'+#{param.keyword}+'%'
            )
        </if>
        <if test="param.userName != null and param.userName != ''">
            AND e.username like '%'+#{param.userName}+'%'
        </if>
        <if test="param.jobNum != null and param.jobNum != ''">
            AND e.workcode like '%'+#{param.jobNum}+'%'
        </if>
        <if test="param.departmentIds != null and param.departmentIds.size()>0">
            AND e.department_id IN
            <foreach collection="param.departmentIds" open="(" item="departmentId" separator="," close=")">
                #{departmentId}
            </foreach>
        </if>
        <if test="param.subcompanyIds != null and param.subcompanyIds.size()>0">
            AND e.subcompany_id IN
            <foreach collection="param.subcompanyIds" open="(" item="subcompanyId" separator="," close=")">
                #{subcompanyId}
            </foreach>
        </if>
        <if test="param.statuses != null and param.statuses.size()>0">
            AND e.status IN
            <foreach collection="param.statuses" open="(" item="userStatus" separator="," close=")">
                #{userStatus}
            </foreach>
        </if>
        <if test="param.positions != null and param.positions.size()>0">
            AND e.jobtitle_id IN
            <foreach collection="param.positions" open="(" item="position" separator="," close=")">
                #{position}
            </foreach>
        </if>
        <if test="param.hiredateStart != null">
            AND e.companystartdate &gt; #{param.hiredateStart}
        </if>
        <if test="param.hiredateEnd != null">
            AND e.companystartdate &lt; #{param.hiredateEnd}
        </if>

        <if test="param.siSchemeId != null and param.siSchemeId != 0">
            AND siSchemeId = #{param.siSchemeId}
        </if>
        <if test="param.fundSchemeId != null and param.fundSchemeId != 0">
            AND fundSchemeId = #{param.fundSchemeId}
        </if>
        <if test="param.otherSchemeId != null and param.otherSchemeId != 0">
            AND otherSchemeId = #{param.otherSchemeId}
        </if>
        <if test="param.taxAgentId != null and param.taxAgentId != 0">
            AND base.payment_organization = #{param.taxAgentId}
        </if>
        <if test="param.runStatuses != null and param.runStatuses.size()>0">
            AND base.run_status IN
            <foreach collection="param.runStatuses" open="(" item="runStatus" separator="," close=")">
                #{runStatus}
            </foreach>
        </if>
        <if test="param.employeeIds != null and param.employeeIds.size() > 0">
            AND e.id IN
            <foreach collection="param.employeeIds" open="(" separator="," item="item" close=")">
                #{item}
            </foreach>
        </if>
        <if test="param.ids != null and param.ids.size() > 0">
            AND base.id IN
            <foreach collection="param.ids" open="(" separator="," item="item" close=")">
                #{item}
            </foreach>
        </if>
    </sql>


    <sql id="extCondition" databaseId="oracle">
        <if test="param.keyword != null and param.keyword != ''">
            AND
            (
            e.username like '%'||#{param.keyword}||'%'
            )
        </if>
        <if test="param.userName != null and param.userName != ''">
            AND e.username like '%'||#{param.userName}||'%'
        </if>
        <if test="param.jobNum != null and param.jobNum != ''">
            AND e.workcode like '%'||#{param.jobNum}||'%'
        </if>
        <if test="param.departmentIds != null and param.departmentIds.size()>0">
            AND e.department_id IN
            <foreach collection="param.departmentIds" open="(" item="departmentId" separator="," close=")">
                #{departmentId}
            </foreach>
        </if>
        <if test="param.subcompanyIds != null and param.subcompanyIds.size()>0">
            AND e.subcompany_id IN
            <foreach collection="param.subcompanyIds" open="(" item="subcompanyId" separator="," close=")">
                #{subcompanyId}
            </foreach>
        </if>
        <if test="param.statuses != null and param.statuses.size()>0">
            AND e.status IN
            <foreach collection="param.statuses" open="(" item="userStatus" separator="," close=")">
                #{userStatus}
            </foreach>
        </if>
        <if test="param.positions != null and param.positions.size()>0">
            AND e.jobtitle_id IN
            <foreach collection="param.positions" open="(" item="position" separator="," close=")">
                #{position}
            </foreach>
        </if>
        <if test="param.hiredateStart != null">
            AND e.companystartdate &gt; #{param.hiredateStart}
        </if>
        <if test="param.hiredateEnd != null">
            AND e.companystartdate &lt; #{param.hiredateEnd}
        </if>
        <if test="param.siSchemeId != null and param.siSchemeId != 0">
            AND siSchemeId = #{param.siSchemeId}
        </if>
        <if test="param.fundSchemeId != null and param.fundSchemeId != 0">
            AND fundSchemeId = #{param.fundSchemeId}
        </if>
        <if test="param.otherSchemeId != null and param.otherSchemeId != 0">
            AND otherSchemeId = #{param.otherSchemeId}
        </if>
        <if test="param.taxAgentId != null and param.taxAgentId != 0">
            AND base.payment_organization = #{param.taxAgentId}
        </if>
        <if test="param.runStatuses != null and param.runStatuses.size()>0">
            AND base.run_status IN
            <foreach collection="param.runStatuses" open="(" item="runStatus" separator="," close=")">
                #{runStatus}
            </foreach>
        </if>
        <if test="param.employeeIds != null and param.employeeIds.size() > 0">
            AND e.id IN
            <foreach collection="param.employeeIds" open="(" separator="," item="item" close=")">
                #{item}
            </foreach>
        </if>
        <if test="param.ids != null and param.ids.size() > 0">
            AND base.id IN
            <foreach collection="param.ids" open="(" separator="," item="item" close=")">
                #{item}
            </foreach>
        </if>
    </sql>

    <!-- 根据社保缴纳组织id获取记录 -->
    <select id="getSocialByPaymentOrganization" resultMap="BaseResultMap" >
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_social_archives t
        WHERE delete_type = 0
        AND payment_organization = #{paymentOrganization}
    </select>

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

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

    <select id="listBySchemeId"
            resultType="com.engine.salary.entity.siarchives.po.InsuranceArchivesSocialSchemePO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_social_archives t
        left join hrsa_insurance_base_info i on i.social_archives_id  = t.id
        WHERE t.delete_type = 0
        AND t.social_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_social_archives
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="social_payment_base_string =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.socialPaymentBaseString!=null">
                        when id=#{item.id} then #{item.socialPaymentBaseString}
                    </if>
                </foreach>
            </trim>
            <trim prefix="social_payment_com_base_string =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.socialPaymentComBaseString!=null">
                        when id=#{item.id} then #{item.socialPaymentComBaseString}
                    </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_social_archives
        SET social_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.InsuranceArchivesSocialSchemePO">
        UPDATE hrsa_social_archives
        <set>
            welfare_type = #{welfareType},
            social_payment_base_string = #{socialPaymentBaseString},
            social_payment_com_base_string = #{socialPaymentComBaseString},
            social_scheme_id = #{socialSchemeId},
            social_end_time = #{socialEndTime},
            social_start_time = #{socialStartTime},
            non_payment = #{nonPayment},
            tenant_key = #{tenantKey},
            employee_id = #{employeeId},
            update_time = #{updateTime},
            under_take = #{underTake},
            social_account = #{socialAccount},
            payment_organization = #{paymentOrganization},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>

    <!-- 批量更新最后缴纳月为null -->
    <update id="batchUpdateEndTimeToNull">
        UPDATE hrsa_social_archives
        SET social_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_social_archives
        SET social_end_time = #{endTime}
        WHERE delete_type = 0 AND id = #{id}
    </update>


    <insert id="insert" parameterType="com.engine.salary.entity.siarchives.po.InsuranceArchivesSocialSchemePO">
        INSERT INTO hrsa_social_archives(
            welfare_type,
            delete_type,
            social_payment_base_string,
            social_payment_com_base_string,
            social_scheme_id,
            create_time,
            social_end_time,
            social_start_time,
            creator,
            non_payment,
            tenant_key,
            employee_id,
            update_time,
            under_take,
            social_account,
            payment_organization)
        VALUES
        (
            #{welfareType},
            #{deleteType},
            #{socialPaymentBaseString},
            #{socialPaymentComBaseString},
            #{socialSchemeId},
            #{createTime},
            #{socialEndTime},
            #{socialStartTime},
            #{creator},
            #{nonPayment},
            #{tenantKey},
            #{employeeId},
            #{updateTime},
            #{underTake},
            #{socialAccount},
            #{paymentOrganization}
        )

    </insert>
</mapper>