<?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.taxdeclaration.TaxDeclarationDetailMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.taxdeclaration.po.TaxDeclarationDetailPO">
        <result column="create_time" property="createTime"/>
        <result column="creator" property="creator"/>
        <result column="delete_type" property="deleteType"/>
        <result column="employee_id" property="employeeId"/>
        <result column="field_code" property="fieldCode"/>
        <result column="field_value" property="fieldValue"/>
        <result column="id" property="id"/>
        <result column="tax_declaration_id" property="taxDeclarationId"/>
        <result column="tenant_key" property="tenantKey"/>
        <result column="update_time" property="updateTime"/>
        <result column="employee_type" property="employeeType"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        create_time
        , t.creator
        , t.delete_type
        , t.employee_id
        , t.field_code
        , t.field_value
        , t.id
        , t.tax_declaration_id
        , t.tenant_key
        , t.update_time
        , t.employee_type
    </sql>


    <resultMap id="resultMap" type="com.engine.salary.entity.taxdeclaration.dto.TaxDeclarationEmployeeDTO">
        <result column="employee_type" property="employeeType"/>
        <result column="employee_id" property="employeeId"/>
    </resultMap>

    <select id="listPage4EmployeeId" resultMap="resultMap">
        SELECT  employee_type, employee_id
        FROM hrsa_tax_declaration_detail t
        LEFT JOIN hrmresource e ON e.id = t.employee_id
        LEFT JOIN hrmdepartment d ON d.id = e.DEPARTMENTID
        LEFT JOIN hrmsubcompany c ON c.id = e.subcompanyid1
        WHERE delete_type = 0
          AND tax_declaration_id = #{param.taxDeclarationId}
        <if test="param.orderRule != null">
            ORDER BY ${param.orderRule.orderRule} ${param.orderRule.ascOrDesc}
        </if>
    </select>

    <select id="list4EmployeeId" resultMap="resultMap">
        SELECT DISTINCT employee_type, employee_id
        FROM hrsa_tax_declaration_detail
        WHERE delete_type = 0
          AND tax_declaration_id = #{taxDeclarationId}
    </select>

    <select id="countEmployeeId" resultType="int">
        SELECT count(DISTINCT employee_id)
        FROM hrsa_tax_declaration_detail
        WHERE delete_type = 0
          AND tax_declaration_id = #{taxDeclarationId}
    </select>
    <select id="listEmployeeId" resultType="long">
        SELECT DISTINCT employee_id
        FROM hrsa_tax_declaration_detail
        WHERE delete_type = 0
          AND tax_declaration_id = #{taxDeclarationId}
    </select>
    <select id="listByTaxDeclarationIdAndEmployeeIds"
            resultType="com.engine.salary.entity.taxdeclaration.po.TaxDeclarationDetailPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_tax_declaration_detail t
        WHERE delete_type = 0
        AND tax_declaration_id = #{taxDeclarationId}
        <if test="collection != null and collection.size()>0">
            AND employee_id IN
            <foreach collection="collection" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>


    <insert id="batchInsert">
        INSERT INTO hrsa_tax_declaration_detail
        (id, tax_declaration_id, employee_id, field_code, field_value, creator,
        create_time, update_time, delete_type, tenant_key, employee_type)
        VALUES
        <foreach collection="collection" item="item" separator=",">
            (
            #{item.id},
            #{item.taxDeclarationId},
            #{item.employeeId},
            #{item.fieldCode},
            #{item.fieldValue},
            #{item.creator},
            #{item.createTime},
            #{item.updateTime},
            #{item.deleteType},
            #{item.tenantKey},
            #{item.employeeType}
            )
        </foreach>
    </insert>

    <insert id="batchInsert" databaseId="oracle">
        INSERT INTO hrsa_tax_declaration_detail
        (id, tax_declaration_id, employee_id, field_code, field_value, creator,
        create_time, update_time, delete_type, tenant_key, employee_type)

        <foreach collection="collection" item="item" separator="union all">
            select
            #{item.id,jdbcType=DOUBLE},
            #{item.taxDeclarationId,jdbcType=DOUBLE},
            #{item.employeeId,jdbcType=DOUBLE},
            #{item.fieldCode,jdbcType=VARCHAR},
            #{item.fieldValue,jdbcType=VARCHAR},
            #{item.creator,jdbcType=DOUBLE},
            #{item.createTime,jdbcType=VARCHAR},
            #{item.updateTime,jdbcType=VARCHAR},
            #{item.deleteType,jdbcType=INTEGER},
            #{item.tenantKey,jdbcType=VARCHAR},
            #{item.employeeType,jdbcType=INTEGER}
            from dual
        </foreach>
    </insert>
    <insert id="batchInsert" databaseId="sqlserver">
        <foreach collection="collection" item="item" separator=";">
            INSERT INTO hrsa_tax_declaration_detail
            (id,tax_declaration_id, employee_id, field_code, field_value, creator,
            create_time, update_time, delete_type, tenant_key, employee_type)
            VALUES
            (
            #{item.id},
            #{item.taxDeclarationId},
            #{item.employeeId},
            #{item.fieldCode},
            #{item.fieldValue},
            #{item.creator},
            #{item.createTime},
            #{item.updateTime},
            #{item.deleteType},
            #{item.tenantKey},
            #{item.employeeType}
            )
        </foreach>
    </insert>


    <update id="deleteByTaxDeclarationIds">
        UPDATE hrsa_tax_declaration_detail
        SET delete_type = 1
        WHERE delete_type = 0
        AND tax_declaration_id IN
        <foreach collection="taxDeclarationIds" open="(" item="taxDeclarationIds" separator="," close=")">
            #{taxDeclarationIds}
        </foreach>
    </update>

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


    <update id="batchUpdate" parameterType="java.util.List">
        update hrsa_tax_declaration_detail
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="field_value =case" suffix="end,">
                <foreach collection="collection" item="item" index="index">
                    <if test="item.fieldValue!=null">
                        when id=#{item.id} then #{item.fieldValue}
                    </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>
</mapper>