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

    <resultMap id="BaseResultMap" type="com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseHistoryPO">
        <result column="id" property="id"/>
        <result column="employee_id" property="employeeId"/>
        <result column="payment_organization" property="paymentOrganization"/>
        <result column="welfare_type" property="welfareType"/>
        <result column="adjust_before_scheme_id" property="adjustBeforeSchemeId"/>
        <result column="adjust_after_scheme_id" property="adjustAfterSchemeId"/>
        <result column="adjust_before_base_value" property="adjustBeforeBaseValue"/>
        <result column="adjust_after_base_value" property="adjustAfterBaseValue"/>
        <result column="adjust_welfare_item_id" property="adjustWelfareItemId"/>
        <result column="payment_scope" property="paymentScope"/>
        <result column="operator" property="operator"/>
        <result column="operate_time" property="operateTime"/>
        <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.payment_organization
        , t.welfare_type
        , t.adjust_before_scheme_id
        , t.adjust_after_scheme_id
        , t.adjust_before_base_value
        , t.adjust_after_base_value
        , t.adjust_welfare_item_id
        , t.payment_scope
        , t.operator
        , t.operate_time
        , t.create_time
        , t.update_time
        , t.creator
        , t.delete_type
        , t.tenant_key
    </sql>

    <insert id="batchSave">
        INSERT INTO hrsa_insurance_base_history
        (id,employee_id,payment_organization,welfare_type,
        adjust_before_scheme_id,adjust_after_scheme_id,
        adjust_before_base_value,adjust_after_base_value,
        adjust_welfare_item_id,operator,operate_time,
        tenant_key,creator,delete_type,create_time,update_time,payment_scope)
        VALUES
        <foreach collection="infos" item="item" separator=",">
            (
            #{item.id},
            #{item.employeeId},
            #{item.paymentOrganization},
            #{item.welfareType},
            #{item.adjustBeforeSchemeId},
            #{item.adjustAfterSchemeId},
            #{item.adjustBeforeBaseValue},
            #{item.adjustAfterBaseValue},
            #{item.adjustWelfareItemId},
            #{item.operator},
            #{item.operateTime},
            #{item.tenantKey},
            #{item.creator},
            #{item.deleteType},
            #{item.createTime},
            #{item.updateTime},
            #{item.paymentScope}
            )
        </foreach>
    </insert>
    <insert id="batchSave" databaseId="oracle">
        INSERT INTO hrsa_insurance_base_history
        (id,employee_id,payment_organization,welfare_type,
        adjust_before_scheme_id,adjust_after_scheme_id,
        adjust_before_base_value,adjust_after_base_value,
        adjust_welfare_item_id,operator,operate_time,
        tenant_key,creator,delete_type,create_time,update_time,payment_scope)
        <foreach collection="infos" item="item" separator="union all">
            select
            #{item.id,jdbcType=DOUBLE},
            #{item.employeeId,jdbcType=DOUBLE},
            #{item.paymentOrganization,jdbcType=DOUBLE},
            #{item.welfareType,jdbcType=DOUBLE},
            #{item.adjustBeforeSchemeId,jdbcType=DOUBLE},
            #{item.adjustAfterSchemeId,jdbcType=DOUBLE},
            #{item.adjustBeforeBaseValue,jdbcType=VARCHAR},
            #{item.adjustAfterBaseValue,jdbcType=VARCHAR},
            #{item.adjustWelfareItemId,jdbcType=DOUBLE},
            #{item.operator,jdbcType=DOUBLE},
            #{item.operateTime},
            #{item.tenantKey,jdbcType=VARCHAR},
            #{item.creator,jdbcType=DOUBLE},
            #{item.deleteType},
            #{item.createTime},
            #{item.updateTime},
            #{item.paymentScope}
            from dual
        </foreach>
    </insert>
    <insert id="batchSave" databaseId="sqlserver">
        <foreach collection="infos" item="item" separator=";">
            INSERT INTO hrsa_insurance_base_history
            (id,employee_id,payment_organization,welfare_type,
            adjust_before_scheme_id,adjust_after_scheme_id,
            adjust_before_base_value,adjust_after_base_value,
            adjust_welfare_item_id,operator,operate_time,
            tenant_key,creator,delete_type,create_time,update_time,payment_scope)
            VALUES
            (
            #{item.id},
            #{item.employeeId},
            #{item.paymentOrganization},
            #{item.welfareType},
            #{item.adjustBeforeSchemeId},
            #{item.adjustAfterSchemeId},
            #{item.adjustBeforeBaseValue},
            #{item.adjustAfterBaseValue},
            #{item.adjustWelfareItemId},
            #{item.operator},
            #{item.operateTime},
            #{item.tenantKey},
            #{item.creator},
            #{item.deleteType},
            #{item.createTime},
            #{item.updateTime},
            #{item.paymentScope}
            )
        </foreach>
    </insert>

    <select id="listByEmployeeIdAndPayOrg" resultType="com.engine.salary.entity.siarchives.dto.InsuranceArchivesBaseHistoryDTO">
        SELECT
        t.id
        , t.employee_id
        , t.payment_organization
        , t.welfare_type
        , t.adjust_before_scheme_id
        , t.adjust_after_scheme_id
        , t.adjust_before_base_value
        , t.adjust_after_base_value
        , t.adjust_welfare_item_id
        , t.operator
        , t.operate_time
        , ca.insurance_name as welfareItemName
        , sb.scheme_name as adjustBeforeSchemeName
        , sa.scheme_name as adjustAfterSchemeName
        , eo.lastname as operatorName
        , ee.lastname as employeeName
        , p.name as paymentOrganizationName
        , t.payment_scope
        FROM hrsa_insurance_base_history t
        LEFT JOIN hrmresource eo on eo.id = t.operator
        LEFT JOIN hrmresource ee on ee.id = t.employee_id
        LEFT JOIN hrsa_social_security_scheme sb on sb.id = t.adjust_before_scheme_id
        LEFT JOIN hrsa_social_security_scheme sa on sa.id = t.adjust_after_scheme_id
        LEFT JOIN hrsa_insurance_category ca on ca.id = t.adjust_welfare_item_id
        LEFT JOIN hrsa_tax_agent p on p.id = t.payment_organization
        WHERE t.delete_type = 0
        AND t.employee_id = #{employeeId}
        AND t.payment_organization = #{paymentOrganization}
        ORDER BY t.operate_time DESC, t.adjust_welfare_item_id ASC
    </select>

    <select id="listByEmployeeIdAndOperator" resultType="com.engine.salary.entity.siarchives.dto.InsuranceArchivesBaseHistoryDTO">
        SELECT
        t.id
        , t.employee_id
        , t.payment_organization
        , t.welfare_type
        , t.adjust_before_scheme_id
        , t.adjust_after_scheme_id
        , t.adjust_before_base_value
        , t.adjust_after_base_value
        , t.adjust_welfare_item_id
        , t.operator
        , t.operate_time
        , ca.insurance_name as welfareItemName
        , sb.scheme_name as adjustBeforeSchemeName
        , sa.scheme_name as adjustAfterSchemeName
        , eo.lastname as operatorName
        , ee.lastname as employeeName
        , p.name as paymentOrganizationName
        , t.payment_scope
        FROM hrsa_insurance_base_history t
        LEFT JOIN hrmresource eo on eo.id = t.operator
        LEFT JOIN hrmresource ee on ee.id = t.employee_id
        LEFT JOIN hrsa_social_security_scheme sb on sb.id = t.adjust_before_scheme_id
        LEFT JOIN hrsa_social_security_scheme sa on sa.id = t.adjust_after_scheme_id
        LEFT JOIN hrsa_insurance_category ca on ca.id = t.adjust_welfare_item_id
        LEFT JOIN hrsa_tax_agent p on p.id = t.payment_organization
        WHERE t.delete_type = 0
        <if test="employeeId != null ">
            AND t.employee_id = #{employeeId}
        </if>
        <if test="operator != null ">
            AND t.operator = #{operator}
        </if>
        ORDER BY t.operate_time DESC, t.adjust_welfare_item_id ASC
    </select>

    <select id="listByExtEmpIdAndOperator" resultType="com.engine.salary.entity.siarchives.dto.InsuranceArchivesBaseHistoryDTO">
        SELECT
        t.id
        , t.employee_id
        , t.payment_organization
        , t.welfare_type
        , t.adjust_before_scheme_id
        , t.adjust_after_scheme_id
        , t.adjust_before_base_value
        , t.adjust_after_base_value
        , t.adjust_welfare_item_id
        , t.operator
        , t.operate_time
        , ca.insurance_name as welfareItemName
        , sb.scheme_name as adjustBeforeSchemeName
        , sa.scheme_name as adjustAfterSchemeName
        , eo.lastname as operatorName
        , ee.username as employeeName
        , p.name as paymentOrganizationName
        , t.payment_scope
        FROM hrsa_insurance_base_history t
        LEFT JOIN hrmresource eo on eo.id = t.operator
        LEFT JOIN hrsa_external_employee ee on ee.id = t.employee_id
        LEFT JOIN hrsa_social_security_scheme sb on sb.id = t.adjust_before_scheme_id
        LEFT JOIN hrsa_social_security_scheme sa on sa.id = t.adjust_after_scheme_id
        LEFT JOIN hrsa_insurance_category ca on ca.id = t.adjust_welfare_item_id
        LEFT JOIN hrsa_tax_agent p on p.id = t.payment_organization
        WHERE t.delete_type = 0
        <if test="employeeId != null ">
            AND t.employee_id = #{employeeId}
        </if>
        <if test="operator != null ">
            AND t.operator = #{operator}
        </if>
        ORDER BY t.operate_time DESC, t.adjust_welfare_item_id ASC
    </select>
</mapper>