<?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.extemp.ExternalEmployeeMapper">
    <resultMap id="BaseResultMap" type="com.engine.salary.entity.extemp.po.ExtEmpPO">
        <result column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="department_name" property="departmentName"/>
        <result column="department_id" property="departmentId"/>
        <result column="subcompany_name" property="subcompanyName"/>
        <result column="subcompany_id" property="subcompanyId"/>
        <result column="jobtitle_name" property="jobtitleName"/>
        <result column="jobtitle_id" property="jobtitleId"/>
        <result column="companystartdate" property="companystartdate"/>
        <result column="mobile" property="mobile"/>
        <result column="status" property="status"/>
        <result column="workcode" property="workcode"/>
        <result column="sex" property="sex"/>
        <result column="email" property="email"/>
        <result column="telephone" property="telephone"/>
        <result column="jobcall" property="jobcall"/>
        <result column="birthday" property="birthday"/>
        <result column="id_no" property="idNo"/>
        <result column="bank_card_num" property="bankCardNum"/>
        <result column="bank_name" property="bankName"/>
        <result column="delete_type" property="deleteType"/>
        <result column="creator" property="creator"/>
        <result column="modifier" property="modifier"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="tenant_key" property="tenantKey"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        , t.username
        , t.department_name
        , t.department_id
        , t.subcompany_name
        , t.subcompany_id
        , t.jobtitle_name
        , t.jobtitle_id
        , t.companystartdate
        , t.mobile
        , t.status
        , t.workcode
        , t.sex
        , t.email
        , t.telephone
        , t.jobcall
        , t.birthday
        , t.id_no
        , t.bank_card_num
        , t.bank_name
        , t.delete_type
        , t.creator
        , t.modifier
        , t.create_time
        , t.update_time
        , t.tenant_key
    </sql>

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

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

    <!-- 条件查询 -->
    <select id="listSome" resultMap="BaseResultMap" parameterType="com.engine.salary.entity.extemp.po.ExtEmpPO">
        SELECT
        <include refid="baseColumns"/>
        FROM hrsa_external_employee t
        WHERE delete_type = 0
        <if test="id != null">
            AND id = #{id}
        </if>
        <if test="username != null and username!=''">
            AND username = #{username}
        </if>
        <if test="departmentName != null">
            AND department_name = #{departmentName}
        </if>
        <if test="departmentId != null">
            AND department_id = #{departmentId}
        </if>
        <if test="subcompanyName != null">
            AND subcompany_name = #{subcompanyName}
        </if>
        <if test="subcompanyId != null">
            AND subcompany_id = #{subcompanyId}
        </if>
        <if test="jobtitleName != null">
            AND jobtitle_name = #{jobtitleName}
        </if>
        <if test="jobtitleId != null">
            AND jobtitle_id = #{jobtitleId}
        </if>
        <if test="companystartdate != null">
            AND companystartdate = #{companystartdate}
        </if>
        <if test="mobile != null">
            AND mobile = #{mobile}
        </if>
        <if test="status != null">
            AND status = #{status}
        </if>
        <if test="workcode != null">
            AND workcode = #{workcode}
        </if>
        <if test="sex != null">
            AND sex = #{sex}
        </if>
        <if test="email != null">
            AND email = #{email}
        </if>
        <if test="telephone != null">
            AND telephone = #{telephone}
        </if>
        <if test="jobcall != null">
            AND jobcall = #{jobcall}
        </if>
        <if test="birthday != null">
            AND birthday = #{birthday}
        </if>
        <if test="idNo != null">
            AND id_no = #{idNo}
        </if>
        <if test="bankCardNum != null">
            AND bank_card_num = #{bankCardNum}
        </if>
        <if test="bankName != null">
            AND bank_name = #{bankName}
        </if>
        <if test="deleteType != null">
            AND delete_type = #{deleteType}
        </if>
        <if test="creator != null">
            AND creator = #{creator}
        </if>
        <if test="modifier != null">
            AND modifier = #{modifier}
        </if>
        <if test="createTime != null">
            AND create_time = #{createTime}
        </if>
        <if test="updateTime != null">
            AND update_time = #{updateTime}
        </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>
        <if test="subcompanyIds != null and subcompanyIds.size()>0">
            AND subcompany_id IN
            <foreach collection="subcompanyIds" open="(" item="subcompanyId" separator="," close=")">
                #{subcompanyId}
            </foreach>
        </if>
        <if test="departmentIds != null and departmentIds.size()>0">
            AND department_id IN
            <foreach collection="departmentIds" open="(" item="departmentId" separator="," close=")">
                #{departmentId}
            </foreach>
        </if>
        ORDER BY id DESC
    </select>
    <select id="listByParams" resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.id as employeeId,
        e.username as username,
        e.status as status,
        e.workcode as workcode,
        d.departmentname as departmentName,
        d.id as departmentId,
        c.jobtitlename as jobtitleName,
        c.id as jobtitleId,
        e.companystartdate as companystartdate,
        e.mobile as mobile
        from hrsa_external_employee e
        left join hrmdepartment d on e.department_id = d.id
        left join hrmjobtitles c on e.jobtitle_id = c.id
        WHERE 1=1
        <if test="params != null and params.size() > 0">
            AND ( 1=2
            <foreach collection="params" item="param">
                OR
                (
                <if test="param.targetType == 'EMPLOYEE'">
                    e.id IN
                    <foreach collection="param.targetIds" open="(" item="targetId" separator="," close=")">
                        #{targetId}
                    </foreach>
                </if>
                <if test="param.targetType == 'DEPT'">
                    d.id IN
                    <foreach collection="param.targetIds" open="(" item="targetId" separator="," close=")">
                        #{targetId}
                    </foreach>
                </if>
                <if test="param.targetType == 'POSITION'">
                    c.id IN
                    <foreach collection="param.targetIds" open="(" item="targetId" separator="," close=")">
                        #{targetId}
                    </foreach>
                </if>
                <if test="param.targetType == 'SUBCOMPANY'">
                    e.subcompany_id IN
                    <foreach collection="param.targetIds" open="(" item="targetId" separator="," close=")">
                        #{targetId}
                    </foreach>
                </if>
                <if test="param.employeeStatus != null and param.employeeStatus.size() > 0">
                    AND e.status IN
                    <foreach collection="param.employeeStatus" open="(" item="status" separator="," close=")">
                        #{status}
                    </foreach>
                </if>
                <!--                &#45;&#45; 在职-->
                <!--                <if test="param.employeeStatus != null  and param.employeeStatus == 'normal'">-->
                <!--                    AND em.status in (0,1,2,3)-->
                <!--                </if>-->
                <!--                &#45;&#45; 离职-->
                <!--                <if test="param.employeeStatus != null  and param.employeeStatus == 'unavailable'">-->
                <!--                    AND em.status in (4,5,6)-->
                <!--                </if>-->
                )
            </foreach>
            )
        </if>
    </select>
    <select id="listEmployee" resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.ID             as employeeId,
               e.USERNAME       as username,
               d.DEPARTMENTNAME as departmentName,
               e.status,
               e.mobile,
               e.workcode,
               'true' as extEmp
        from hrsa_external_employee e
                 left join hrmdepartment d on e.department_id = d.id
    </select>

    <select id="getEmployeeByIdsAll"
            resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.id as employeeId,
        e.username as username,
        d.departmentname as departmentName,
        d.id as departmentId,
        sc.SUBCOMPANYNAME as subcompanyName,
        c.jobtitlename as jobtitleName,
        c.id as jobtitleId,
        e.companystartdate as companystartdate,
        e.mobile as mobile,
        e.status as status,
        e.workcode as workcode,
        e.sex as sex,
        e.email as email,
        e.telephone as telephone,
        e.jobcall as jobcall,
        e.birthday as birthday,
        c.id as jobtitleId
        from hrsa_external_employee e
        left join hrmdepartment d on e.department_id = d.id
        left join hrmjobtitles c on e.jobtitle_id = c.id
        left join HrmSubCompany sc on e.SUBCOMPANY_ID=sc.id
        where 1 = 1
        <if test="collection != null and collection.size()>0">
            AND e.id IN
            <foreach collection="collection" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>
    <select id="listAllForReport" resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.id               as employeeId,
               e.username         as username,
               e.status           as status,
               e.workcode         as workcode,
               e.companystartdate as companystartdate,
               e.mobile           as mobile,
               e.department_id     as departmentId,
               e.subcompany_id    as subcompanyid,
               e.jobtitle_id      as jobtitleId,
               d.departmentname   as departmentName,
               d.id               as departmentId
        from hrsa_external_employee e
                 left join hrmdepartment d on e.department_id = d.id
    </select>
    <select id="listSomeDataCollectionEmployee"
            resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        SELECT
        e.id as employeeId,
        e.username as username,
        d.departmentname as departmentName,
        d.id as departmentId,
        sc.SUBCOMPANYNAME as subcompanyName,
        c.jobtitlename as jobtitleName,
        c.id as jobtitleId,
        e.companystartdate as companystartdate,
        e.mobile as mobile,
        e.status as status,
        e.workcode as workcode,
        e.sex as sex,
        e.email as email,
        e.telephone as telephone,
        e.jobcall as jobcall,
        e.birthday as birthday,
        c.id as jobtitleId
        from hrsa_external_employee e
        left join hrmdepartment d on e.department_id = d.id
        left join hrmjobtitles c on e.jobtitle_id = c.id
        left join HrmSubCompany sc on e.SUBCOMPANY_ID=sc.id
        WHERE e.delete_type = 0
        <if test="id != null">
            AND e.id = #{id}
        </if>
        <if test="username != null and username!=''">
            AND e.username = #{username}
        </if>
        <if test="departmentName != null">
            AND e.department_name = #{departmentName}
        </if>
        <if test="departmentId != null">
            AND e.department_id = #{departmentId}
        </if>
        <if test="subcompanyName != null">
            AND e.subcompany_name = #{subcompanyName}
        </if>
        <if test="subcompanyId != null">
            AND e.subcompany_id = #{subcompanyId}
        </if>
        <if test="jobtitleName != null">
            AND e.jobtitle_name = #{jobtitleName}
        </if>
        <if test="jobtitleId != null">
            AND e.jobtitle_id = #{jobtitleId}
        </if>
        <if test="companystartdate != null">
            AND e.companystartdate = #{companystartdate}
        </if>
        <if test="mobile != null">
            AND e.mobile = #{mobile}
        </if>
        <if test="status != null">
            AND e.status = #{status}
        </if>
        <if test="workcode != null">
            AND e.workcode = #{workcode}
        </if>
        <if test="sex != null">
            AND e.sex = #{sex}
        </if>
        <if test="email != null">
            AND e.email = #{email}
        </if>
        <if test="telephone != null">
            AND e.telephone = #{telephone}
        </if>
        <if test="jobcall != null">
            AND e.jobcall = #{jobcall}
        </if>
        <if test="birthday != null">
            AND e.birthday = #{birthday}
        </if>
        <if test="idNo != null">
            AND e.id_no = #{idNo}
        </if>
        <if test="bankCardNum != null">
            AND e.bank_card_num = #{bankCardNum}
        </if>
        <if test="bankName != null">
            AND e.bank_name = #{bankName}
        </if>
        <if test="deleteType != null">
            AND e.delete_type = #{deleteType}
        </if>
        <if test="creator != null">
            AND e.creator = #{creator}
        </if>
        <if test="modifier != null">
            AND e.modifier = #{modifier}
        </if>
        <if test="createTime != null">
            AND e.create_time = #{createTime}
        </if>
        <if test="updateTime != null">
            AND e.update_time = #{updateTime}
        </if>
        <if test="tenantKey != null">
            AND e.tenant_key = #{tenantKey}
        </if>
        <if test="ids != null and ids.size()>0">
            AND e.id IN
            <foreach collection="ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="subcompanyIds != null and subcompanyIds.size()>0">
            AND e.subcompany_id IN
            <foreach collection="subcompanyIds" open="(" item="subcompanyId" separator="," close=")">
                #{subcompanyId}
            </foreach>
        </if>
        <if test="departmentIds != null and departmentIds.size()>0">
            AND e.department_id IN
            <foreach collection="departmentIds" open="(" item="departmentId" separator="," close=")">
                #{departmentId}
            </foreach>
        </if>
        ORDER BY e.id DESC
    </select>


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

            <if test="id != null">
                id,
            </if>
            <if test="username != null">
                username,
            </if>
            <if test="departmentName != null">
                department_name,
            </if>
            <if test="departmentId != null">
                department_id,
            </if>
            <if test="subcompanyName != null">
                subcompany_name,
            </if>
            <if test="subcompanyId != null">
                subcompany_id,
            </if>
            <if test="jobtitleName != null">
                jobtitle_name,
            </if>
            <if test="jobtitleId != null">
                jobtitle_id,
            </if>
            <if test="companystartdate != null">
                companystartdate,
            </if>
            <if test="mobile != null">
                mobile,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="workcode != null">
                workcode,
            </if>
            <if test="sex != null">
                sex,
            </if>
            <if test="email != null">
                email,
            </if>
            <if test="telephone != null">
                telephone,
            </if>
            <if test="jobcall != null">
                jobcall,
            </if>
            <if test="birthday != null">
                birthday,
            </if>
            <if test="idNo != null">
                id_no,
            </if>
            <if test="bankCardNum != null">
                bank_card_num,
            </if>
            <if test="bankName != null">
                bank_name,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="creator != null">
                creator,
            </if>
            <if test="modifier != null">
                modifier,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="tenantKey != null">
                tenant_key,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="username != null">
                #{username},
            </if>
            <if test="departmentName != null">
                #{departmentName},
            </if>
            <if test="departmentId != null">
                #{departmentId},
            </if>
            <if test="subcompanyName != null">
                #{subcompanyName},
            </if>
            <if test="subcompanyId != null">
                #{subcompanyId},
            </if>
            <if test="jobtitleName != null">
                #{jobtitleName},
            </if>
            <if test="jobtitleId != null">
                #{jobtitleId},
            </if>
            <if test="companystartdate != null">
                #{companystartdate},
            </if>
            <if test="mobile != null">
                #{mobile},
            </if>
            <if test="status != null">
                #{status},
            </if>
            <if test="workcode != null">
                #{workcode},
            </if>
            <if test="sex != null">
                #{sex},
            </if>
            <if test="email != null">
                #{email},
            </if>
            <if test="telephone != null">
                #{telephone},
            </if>
            <if test="jobcall != null">
                #{jobcall},
            </if>
            <if test="birthday != null">
                #{birthday},
            </if>
            <if test="idNo != null">
                #{idNo},
            </if>
            <if test="bankCardNum != null">
                #{bankCardNum},
            </if>
            <if test="bankName != null">
                #{bankName},
            </if>
            <if test="deleteType != null">
                #{deleteType},
            </if>
            <if test="creator != null">
                #{creator},
            </if>
            <if test="modifier != null">
                #{modifier},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="tenantKey != null">
                #{tenantKey},
            </if>
        </trim>
    </insert>

    <!-- 更新,更新全部字段 -->
    <update id="update" parameterType="com.engine.salary.entity.extemp.po.ExtEmpPO">
        UPDATE hrsa_external_employee
        <set>
            username=#{username},
            department_name=#{departmentName},
            department_id=#{departmentId},
            subcompany_name=#{subcompanyName},
            subcompany_id=#{subcompanyId},
            jobtitle_name=#{jobtitleName},
            jobtitle_id=#{jobtitleId},
            companystartdate=#{companystartdate},
            mobile=#{mobile},
            status=#{status},
            workcode=#{workcode},
            sex=#{sex},
            email=#{email},
            telephone=#{telephone},
            jobcall=#{jobcall},
            birthday=#{birthday},
            id_no=#{idNo},
            bank_card_num=#{bankCardNum},
            bank_name=#{bankName},
            delete_type=#{deleteType},
            creator=#{creator},
            modifier=#{modifier},
            create_time=#{createTime},
            update_time=#{updateTime},
            tenant_key=#{tenantKey},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <!-- 更新不为NULL的字段 -->
    <update id="updateIgnoreNull" parameterType="com.engine.salary.entity.extemp.po.ExtEmpPO">
        UPDATE hrsa_external_employee
        <set>
            <if test="username != null">
                username=#{username},
            </if>
            <if test="departmentName != null">
                department_name=#{departmentName},
            </if>
                department_id=#{departmentId},
            <if test="subcompanyName != null">
                subcompany_name=#{subcompanyName},
            </if>
                subcompany_id=#{subcompanyId},
            <if test="jobtitleName != null">
                jobtitle_name=#{jobtitleName},
            </if>
            <if test="jobtitleId != null">
                jobtitle_id=#{jobtitleId},
            </if>
            <if test="companystartdate != null">
                companystartdate=#{companystartdate},
            </if>
            <if test="mobile != null">
                mobile=#{mobile},
            </if>
            <if test="status != null">
                status=#{status},
            </if>
            <if test="workcode != null">
                workcode=#{workcode},
            </if>
            <if test="sex != null">
                sex=#{sex},
            </if>
            <if test="email != null">
                email=#{email},
            </if>
            <if test="telephone != null">
                telephone=#{telephone},
            </if>
            <if test="jobcall != null">
                jobcall=#{jobcall},
            </if>
            <if test="birthday != null">
                birthday=#{birthday},
            </if>
            <if test="idNo != null">
                id_no=#{idNo},
            </if>
            <if test="bankCardNum != null">
                bank_card_num=#{bankCardNum},
            </if>
            <if test="bankName != null">
                bank_name=#{bankName},
            </if>
            <if test="deleteType != null">
                delete_type=#{deleteType},
            </if>
            <if test="creator != null">
                creator=#{creator},
            </if>
            <if test="modifier != null">
                modifier=#{modifier},
            </if>
            <if test="createTime != null">
                create_time=#{createTime},
            </if>
            <if test="updateTime != null">
                update_time=#{updateTime},
            </if>
            <if test="tenantKey != null">
                tenant_key=#{tenantKey},
            </if>
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>


    <!-- 根据主键删除记录 -->
    <delete id="delete">
        UPDATE hrsa_external_employee
        SET delete_type=1
        WHERE id = #{id}
          AND delete_type = 0
    </delete>


</mapper>