<?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.datacollection.VariableArchiveMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.datacollection.po.VariableArchivePO">
        <result column="id" property="id" />
        <result column="employee_id" property="employeeId" />
        <result column="salary_month" property="salaryMonth" />
        <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.tax_agent_id
        , t.salary_month
        , t.create_time
        , t.update_time
        , t.creator
        , t.delete_type
        , t.tenant_key
    </sql>

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

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

    <!-- 条件查询 -->
    <select id="listSome" resultMap="BaseResultMap" parameterType="com.engine.salary.entity.datacollection.po.VariableArchivePO">
        SELECT
        <include refid="baseColumns" />
        FROM hrsa_variable_archive t
        WHERE delete_type = 0
        <if test="id != null">
            AND id = #{id}
        </if>
        <if test="employeeId != null">
            AND employee_id = #{employeeId}
        </if>
        <if test="taxAgentId != null">
            AND tax_agent_id = #{taxAgentId}
        </if>
        <if test="salaryMonth != null">
            AND salary_month = #{salaryMonth}
        </if>
        <if test="createTime != null">
            AND create_time = #{createTime}
        </if>
        <if test="updateTime != null">
            AND update_time = #{updateTime}
        </if>
        <if test="creator != null">
            AND creator = #{creator}
        </if>
        <if test="deleteType != null">
            AND delete_type = #{deleteType}
        </if>
        <if test="tenantKey != null">
            AND tenant_key = #{tenantKey}
        </if>
        <if test="ids != null and ids.size()>0">
            AND id IN
            <foreach collection="ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        ORDER BY id DESC
    </select>


    <sql id="variableArchiveColumn">
        t.id
        , t.employee_id
        , t.tax_agent_id
        , t.salary_month
        , t.create_time
        , t.update_time
        , t.creator
        , t.delete_type
        , e.mobile
        , e.workcode
        , e.certificatenum as idNo
        , e.lastname as username
        , e.certificatenum as idNo
        , e.companystartdate as companystartdate
        , e.enddate as dismissdate
        , d.departmentname AS departmentName
        , c.subcompanyname AS subcompanyName
        , a.name AS taxAgentName
        , a.id AS taxAgentId
    </sql>

    <select id="list" resultType="com.engine.salary.entity.datacollection.dto.VariableArchiveListDTO">
        SELECT
        <include refid="variableArchiveColumn" />
        FROM hrsa_variable_archive 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
        LEFT JOIN hrsa_tax_agent a ON t.tax_agent_id = a.id
        WHERE t.delete_type = 0
        and e.status not in (7)

        <!-- 个税扣缴义务人 -->
        <if test="param.taxAgentIds != null and param.taxAgentIds.size()>0">
            AND t.tax_agent_id IN
            <foreach collection="param.taxAgentIds" open="(" item="taxAgentId" separator="," close=")">
                #{taxAgentId}
            </foreach>
        </if>
        <!-- 薪资所属月 -->
        <if test="param.salaryMonth != null">
            AND t.salary_month = #{param.salaryMonthDate}
        </if>
        <if test="param.id != null">
            AND t.id = #{param.id}
        </if>
        <if test="param.ids != null and param.ids.size()>0">
            AND t.id IN
            <foreach collection="param.ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <!-- 姓名 -->
        <if test="param.username != null and param.username != ''">
            AND e.lastname like CONCAT('%',#{param.username},'%')
        </if>
        <!-- 工号 -->
        <if test="param.workcode != null and param.workcode != ''">
            AND e.workcode like CONCAT('%',#{param.workcode},'%')
        </if>
        <!-- 部门 -->
        <if test="param.departmentIds != null and param.departmentIds.size()>0">
            AND d.id IN
            <foreach collection="param.departmentIds" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="param.employeeIds != null and param.employeeIds.size()>0">
            AND t.employee_id IN
            <foreach collection="param.employeeIds" open="(" item="empId" separator="," close=")">
                #{empId}
            </foreach>
        </if>
        <!-- 排序 -->
        <if test="param.orderRule != null">
            ORDER BY ${param.orderRule.orderRule} ${param.orderRule.ascOrDesc}
        </if>
    </select>
    <select id="list" resultType="com.engine.salary.entity.datacollection.dto.VariableArchiveListDTO" databaseId="oracle">
    SELECT
        <include refid="variableArchiveColumn" />
        FROM hrsa_variable_archive 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
        LEFT JOIN hrsa_tax_agent a ON t.tax_agent_id = a.id
        WHERE t.delete_type = 0
        and e.status not in (7)

        <!-- 个税扣缴义务人 -->
        <if test="param.taxAgentIds != null and param.taxAgentIds.size()>0">
            AND t.tax_agent_id IN
            <foreach collection="param.taxAgentIds" open="(" item="taxAgentId" separator="," close=")">
                #{taxAgentId}
            </foreach>
        </if>
        <!-- 薪资所属月 -->
        <if test="param.salaryMonth != null">
            AND t.salary_month = #{param.salaryMonthDate}
        </if>
        <if test="param.id != null">
            AND t.id = #{param.id}
        </if>
        <if test="param.ids != null and param.ids.size()>0">
            AND t.id IN
            <foreach collection="param.ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <!-- 姓名 -->
        <if test="param.username != null and param.username != ''">
            AND e.lastname like '%'||#{param.username}||'%'
        </if>
        <!-- 工号 -->
        <if test="param.workcode != null and param.workcode != ''">
            AND e.workcode like '%'||#{param.workcode}||'%'
        </if>
        <!-- 部门 -->
        <if test="param.departmentIds != null and param.departmentIds.size()>0">
            AND d.id IN
            <foreach collection="param.departmentIds" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="param.employeeIds != null and param.employeeIds.size()>0">
            AND t.employee_id IN
            <foreach collection="param.employeeIds" open="(" item="empId" separator="," close=")">
                #{empId}
            </foreach>
        </if>
        <!-- 排序 -->
        <if test="param.orderRule != null">
            ORDER BY ${param.orderRule.orderRule} ${param.orderRule.ascOrDesc}
        </if>
    </select>
    <select id="list" resultType="com.engine.salary.entity.datacollection.dto.VariableArchiveListDTO" databaseId="sqlserver">
        SELECT
        <include refid="variableArchiveColumn" />
        FROM hrsa_variable_archive 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
        LEFT JOIN hrsa_tax_agent a ON t.tax_agent_id = a.id
        WHERE t.delete_type = 0
        and e.status not in (7)

        <!-- 个税扣缴义务人 -->
        <if test="param.taxAgentIds != null and param.taxAgentIds.size()>0">
            AND t.tax_agent_id IN
            <foreach collection="param.taxAgentIds" open="(" item="taxAgentId" separator="," close=")">
                #{taxAgentId}
            </foreach>
        </if>
        <!-- 薪资所属月 -->
        <if test="param.salaryMonth != null">
            AND t.salary_month = #{param.salaryMonthDate}
        </if>
        <if test="param.id != null">
            AND t.id = #{param.id}
        </if>
        <if test="param.ids != null and param.ids.size()>0">
            AND t.id IN
            <foreach collection="param.ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <!-- 姓名 -->
        <if test="param.username != null and param.username != ''">
            AND e.lastname like '%'+#{param.username}+'%'
        </if>
        <!-- 工号 -->
        <if test="param.workcode != null and param.workcode != ''">
            AND e.workcode like '%'+#{param.workcode}+'%'
        </if>
        <!-- 部门 -->
        <if test="param.departmentIds != null and param.departmentIds.size()>0">
            AND d.id IN
            <foreach collection="param.departmentIds" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="param.employeeIds != null and param.employeeIds.size()>0">
            AND t.employee_id IN
            <foreach collection="param.employeeIds" open="(" item="empId" separator="," close=")">
                #{empId}
            </foreach>
        </if>
        <!-- 排序 -->
        <if test="param.orderRule != null">
            ORDER BY ${param.orderRule.orderRule} ${param.orderRule.ascOrDesc}
        </if>
    </select>


    <!-- 插入不为NULL的字段 -->
    <insert id="insertIgnoreNull" parameterType="com.engine.salary.entity.datacollection.po.VariableArchivePO">
        INSERT INTO hrsa_variable_archive
        <trim prefix="(" suffix=")" suffixOverrides=",">

            <if test="id != null">
                id,
            </if>
            <if test="taxAgentId != null">
                tax_agent_id,
            </if>
            <if test="employeeId != null">
                employee_id,
            </if>
            <if test="salaryMonth != null">
                salary_month,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="creator != null">
                creator,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="tenantKey != null">
                tenant_key,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="id != null" >
                #{id},
            </if>
            <if test="taxAgentId != null">
                #{taxAgentId},
            </if>
            <if test="employeeId != null" >
                #{employeeId},
            </if>
            <if test="salaryMonth != null" >
                #{salaryMonth},
            </if>
            <if test="createTime != null" >
                #{createTime},
            </if>
            <if test="updateTime != null" >
                #{updateTime},
            </if>
            <if test="creator != null" >
                #{creator},
            </if>
            <if test="deleteType != null" >
                #{deleteType},
            </if>
            <if test="tenantKey != null" >
                #{tenantKey},
            </if>
        </trim>
    </insert>



    <!-- 更新,更新全部字段 -->
    <update id="update" parameterType="com.engine.salary.entity.datacollection.po.VariableArchivePO">
        UPDATE hrsa_variable_archive
        <set>
            employee_id=#{employeeId},
            salary_month=#{salaryMonth},
            create_time=#{createTime},
            update_time=#{updateTime},
            creator=#{creator},
            delete_type=#{deleteType},
            tenant_key=#{tenantKey},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <!-- 更新不为NULL的字段 -->
    <update id="updateIgnoreNull" parameterType="com.engine.salary.entity.datacollection.po.VariableArchivePO">
        UPDATE hrsa_variable_archive
        <set>
            <if test="employeeId != null" >
                employee_id=#{employeeId},
            </if>
            <if test="salaryMonth != null" >
                salary_month=#{salaryMonth},
            </if>
            <if test="createTime != null" >
                create_time=#{createTime},
            </if>
            <if test="updateTime != null" >
                update_time=#{updateTime},
            </if>
            <if test="creator != null" >
                creator=#{creator},
            </if>
            <if test="deleteType != null" >
                delete_type=#{deleteType},
            </if>
            <if test="tenantKey != null" >
                tenant_key=#{tenantKey},
            </if>
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <!-- 根据主键删除记录 -->
    <update id="delete" parameterType="com.engine.salary.entity.datacollection.po.VariableArchivePO">
        UPDATE hrsa_variable_archive
        SET delete_type=1
        WHERE id = #{id} AND delete_type = 0
    </update>

    <update id="deleteByIds">
        UPDATE hrsa_variable_archive
        SET delete_type=1
        WHERE delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </update>


</mapper>