<?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.EmployMapper">
    <!-- 员工基本信息 -->
    <select id="listEmployee" resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.ID             as employeeId,
               e.LASTNAME       as username,
               d.DEPARTMENTNAME as departmentName,
               e.status,
               e.mobile,
               e.workcode,
               e.certificatenum as idNo,
               e.accounttype as accountType,
               'false' as extEmp
        from hrmresource e
                 left join hrmdepartment d on e.departmentid = d.id
        where e.status not in (7)

    </select>

    <select id="getEmployeeByIds" resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.id as employeeId,
        e.lastname as username,
        e.certificatenum as idNo,
        e.status as status,
        e.workcode as workcode,
        e.companystartdate as companystartdate,
        e.mobile as mobile,
        e.accounttype as accountType,
        e.workyear as workYear
        from hrmresource e
        where e.status not in (7)
        <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="getEmployeeByIdsIncludeAccountType"
            resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.id as employeeId,
        e.lastname as username,
        e.certificatenum as idNo,
        e.status as status,
        e.workcode as workcode,
        e.companystartdate as companystartdate,
        e.mobile as mobile,
        e.accounttype as accountType,
        e.workyear as workYear
        from hrmresource e
        where e.status not in (7)
        <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="getEmployeeByIdsAll" resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.id as employeeId,
        e.lastname as username,
        d.departmentname as departmentName,
        d.id as departmentId,
        sc.SUBCOMPANYNAME as subcompanyName,
        sc.id as subcompanyid,
        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 jobcallId,
        job.name as jobcall,
        e.birthday as birthday,
        e.certificatenum as idNo,
        e.enddate as dismissdate,
        e.workyear,
        e.accounttype as accountType,
        e.companyworkyear
        from hrmresource e
        left join hrmdepartment d on e.departmentid = d.id
        left join hrmjobtitles c on e.jobtitle = c.id
        left join HrmSubCompany sc on e.SUBCOMPANYID1=sc.id
        left join hrmjobcall job on e.jobcall=job.id
        where e.status not in (7)
        <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="listPositionInfo" resultType="com.engine.salary.entity.hrm.PositionInfo">
        select t.id,
        t.jobtitlename as name
        from hrmjobtitles t
        where 1=1
        <if test="collection != null and collection.size()>0">
            AND t.id IN
            <foreach collection="collection" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="getPositionInfoById" resultType="com.engine.salary.entity.hrm.PositionInfo">
        select t.id,
        t.jobtitlename as name
        from hrmjobtitles t
        where t.id = #{positionId}
    </select>

    <select id="listByParams" resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.id as employeeId,
        e.lastname as username,
        e.status as status,
        e.certificatenum as idNo,
        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,
        e.accounttype as accountType,
        sc.SUBCOMPANYNAME as subcompanyName,
        sc.id as subcompanyid,
        job.id as jobcallId,
        job.name as jobcall
        from hrmresource e
        left join HrmSubCompany sc on e.SUBCOMPANYID1=sc.id
        left join hrmdepartment d on e.departmentid = d.id
        left join hrmjobtitles c on e.jobtitle = c.id
        left join hrmjobcall job on e.jobcall=job.id
        WHERE e.status not in (7)
        <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.subcompanyid1 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>
                )
            </foreach>
            )
        </if>
    </select>

    <select id="listByVirtualParams" resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select h.id as employeeId,
        h.lastname as username,
        h.status as status,
        h.certificatenum as idNo,
        h.workcode as workcode,
        h.companystartdate as companystartdate,
        h.accounttype as accountType,
        h.mobile as mobile
        from hrmresourcevirtual v
        left join hrmresource h on v.resourceid=h.id
        WHERE h.status not in (7)
        <if test="params != null and params.size() > 0">
            AND ( 1=2
            <foreach collection="params" item="param">
                OR
                (
                <if test="param.targetType == 'DEPT'">
                    v.departmentid IN
                    <foreach collection="param.targetIds" open="(" item="targetId" separator="," close=")">
                        #{targetId}
                    </foreach>
                </if>
                <if test="param.targetType == 'SUBCOMPANY'">
                    v.subcompanyid IN
                    <foreach collection="param.targetIds" open="(" item="targetId" separator="," close=")">
                        #{targetId}
                    </foreach>
                </if>
                <if test="param.employeeStatus != null and param.employeeStatus.size() > 0">
                    AND h.status IN
                    <foreach collection="param.employeeStatus" open="(" item="status" separator="," close=")">
                        #{status}
                    </foreach>
                </if>
                )
            </foreach>
            )
        </if>
    </select>


    <select id="getEmployeeById" resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.id               as employeeId,
               e.lastname         as username,
               e.status           as status,
               e.certificatenum   as idNo,
               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,
               e.enddate          as dismissdate,
               e.accounttype as accountType,
               sc.SUBCOMPANYNAME as subcompanyName,
               sc.id as subcompanyid
        from hrmresource e
                 left join hrmdepartment d on e.departmentid = d.id
                 left join HrmSubCompany sc on e.SUBCOMPANYID1=sc.id
                 left join hrmjobtitles c on e.jobtitle = c.id
        where e.status not in (7)

          AND e.id = #{id}
    </select>


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


    <select id="listAllFields" resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.id               as employeeId,
               e.lastname         as username,
               e.status           as status,
               e.workcode         as workcode,
               e.certificatenum   as idNo,
               e.companystartdate as companystartdate,
               e.mobile           as mobile,
               e.accounttype as accountType,
               e.subcompanyid1    as subcompanyid,
               d.departmentname   as departmentName,
               d.id               as departmentId,
               c.jobtitlename     as jobtitleName,
               c.id               as jobtitleId
        from hrmresource e
                 left join hrmdepartment d on e.departmentid = d.id
                 left join hrmjobtitles c on e.jobtitle = c.id
        where e.status not in (7)
    </select>

    <select id="listAll" resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.id               as employeeId,
               e.lastname         as username,
               e.status           as status,
               e.workcode         as workcode,
               e.certificatenum   as idNo,
               e.companystartdate as companystartdate,
               e.mobile           as mobile,
               e.accounttype as accountType,
               e.subcompanyid1    as subcompanyid,
               e.departmentid     as departmentId,
               d.DEPARTMENTNAME   as departmentName,
               e.jobtitle         as jobtitleId
        from hrmresource e
        left join hrmdepartment d on e.departmentid = d.id
        where e.status not in (7)

    </select>


    <select id="listAllForReport" resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.id               as employeeId,
               e.lastname         as username,
               e.status           as status,
               e.workcode         as workcode,
               e.certificatenum   as idNo,
               e.companystartdate as companystartdate,
               e.mobile           as mobile,
               e.departmentid     as departmentId,
               e.subcompanyid1    as subcompanyid,
               e.accounttype as accountType,
               e.costcenterid     as costcenterId,
               e.locationid       as locationId,
               e.jobtitle         as jobtitleId,
               d.departmentname   as departmentName,
               d.id               as departmentId
        from hrmresource e
                 left join hrmdepartment d on e.departmentid = d.id
        where e.status not in (7)

    </select>


    <select id="getDeptInfoList" resultType="com.engine.salary.entity.hrm.DeptInfo">
        select d.departmentname as name,
        d.id as id
        from hrmdepartment d
        where 1=1
        <if test="departmentIds != null and departmentIds.size()>0">
            AND d.id IN
            <foreach collection="departmentIds" open="(" item="departmentId" separator="," close=")">
                #{departmentId}
            </foreach>
        </if>
    </select>

    <select id="getVirtualDeptInfoList" resultType="com.engine.salary.entity.hrm.DeptInfo">
        select d.departmentname as name,
        d.id as id
        from hrmdepartmentvirtual d
        where 1=1
        <if test="virtualDepartmentIds != null and virtualDepartmentIds.size()>0">
            AND d.id IN
            <foreach collection="virtualDepartmentIds" open="(" item="virtualDepartmentId" separator="," close=")">
                #{virtualDepartmentId}
            </foreach>
        </if>
    </select>

    <select id="getSubCompanyInfoList" resultType="com.engine.salary.entity.hrm.SubCompanyInfo">
        select d.subcompanyname as name,
        d.id as id
        from hrmsubcompany d
        where 1=1
        <if test="subDepartmentIds != null and subDepartmentIds.size()>0">
            AND d.id IN
            <foreach collection="subDepartmentIds" open="(" item="subDepartmentId" separator="," close=")">
                #{subDepartmentId}
            </foreach>
        </if>
    </select>

    <select id="getSubCompanyInfoById" resultType="com.engine.salary.entity.hrm.SubCompanyInfo">
        select d.subcompanyname as name,
        d.id as id
        from hrmsubcompany d
        where d.id = #{subDepartmentId}
    </select>

    <select id="getVirtualSubCompanyInfoList" resultType="com.engine.salary.entity.hrm.SubCompanyInfo">
        select d.subcompanyname as name,
        d.id as id
        from hrmsubcompanyvirtual d
        where 1=1
        <if test="virtualSubCompanyIds != null and virtualSubCompanyIds.size()>0">
            AND d.id IN
            <foreach collection="virtualSubCompanyIds" open="(" item="virtualSubCompanyId" separator="," close=")">
                #{virtualSubCompanyId}
            </foreach>
        </if>
    </select>

    <select id="listHrmInfoByIdAndName" resultType="com.engine.salary.entity.hrm.dto.HrmInfoDTO">
        select e.id as employeeId,
        e.lastname as username
        from hrmresource e
        where 1=1
        <if test="param.ids != null and param.ids.size()>0">
            AND e.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>
    </select>

    <select id="listHrmInfoByIdAndName" resultType="com.engine.salary.entity.hrm.dto.HrmInfoDTO" databaseId="sqlserver">
        select e.id as employeeId,
        e.lastname as username
        from hrmresource e
        where 1=1
        <if test="param.ids != null and param.ids.size()>0">
            AND e.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>
    </select>

    <select id="listHrmInfoByIdAndName" resultType="com.engine.salary.entity.hrm.dto.HrmInfoDTO" databaseId="oracle">
        select e.id as employeeId,
        e.lastname as username
        from hrmresource e
        where 1=1
        <if test="param.ids != null and param.ids.size()>0">
            AND e.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>
    </select>

    <select id="getDeptInfosByName" resultType="com.engine.salary.entity.hrm.DeptInfo">
        select d.departmentname as name,
        d.id as id,
        d.subcompanyid1 as subcompanyid1
        from hrmdepartment d
        where d.departmentname = #{name}
    </select>

    <select id="getSubCompanyInfosByName" resultType="com.engine.salary.entity.hrm.SubCompanyInfo">
        select d.subcompanyname as name,
        d.id as id
        from hrmsubcompany d
        where d.subcompanyname = #{name}
    </select>

    <select id="getDeptInfoById" resultType="com.engine.salary.entity.hrm.DeptInfo">
        select d.departmentname as name,
        d.id as id,
        d.subcompanyid1 as subcompanyid1
        from hrmdepartment d
        where d.id = #{id}
    </select>
    <select id="listByDismissDate" resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.id               as employeeId,
               e.lastname         as username,
               e.status           as status,
               e.certificatenum   as idNo,
               e.workcode         as workcode,
               e.companystartdate as companystartdate,
               e.mobile           as mobile,
               e.accounttype as accountType,
               e.enddate          as dismissdate
        from hrmresource e
        where e.status in (4,5,6)
          AND e.enddate >= #{dismissDate}
    </select>

    <select id="listExtHrmInfoByIdAndName" resultType="com.engine.salary.entity.hrm.dto.HrmInfoDTO">
        select e.id as employeeId,
        e.username as username
        from hrsa_external_employee e
        where 1=1
        <if test="param.ids != null and param.ids.size()>0">
            AND e.id IN
            <foreach collection="param.ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>

        <if test="param.userName != null and param.userName != ''">
            AND
            (
            e.username like CONCAT('%',#{param.userName},'%')
            )
        </if>
    </select>

    <select id="listExtHrmInfoByIdAndName" resultType="com.engine.salary.entity.hrm.dto.HrmInfoDTO" databaseId="sqlserver">
        select e.id as employeeId,
        e.username as username
        from hrsa_external_employee e
        where 1=1
        <if test="param.ids != null and param.ids.size()>0">
            AND e.id IN
            <foreach collection="param.ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>

        <if test="param.userName != null and param.userName != ''">
            AND
            (
            e.username like '%'+#{param.userName}+'%'
            )
        </if>
    </select>

    <select id="listExtHrmInfoByIdAndName" resultType="com.engine.salary.entity.hrm.dto.HrmInfoDTO" databaseId="oracle">
        select e.id as employeeId,
        e.username as username
        from hrsa_external_employee e
        where 1=1
        <if test="param.ids != null and param.ids.size()>0">
            AND e.id IN
            <foreach collection="param.ids" open="(" item="id" separator="," close=")">
                #{id}
            </foreach>
        </if>

        <if test="param.userName != null and param.userName != ''">
            AND
            (
            e.username like '%'||#{param.userName}||'%'
            )
        </if>
    </select>
    <select id="listBySubCompanyOrDepartment"
            resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select e.id as employeeId,
        e.lastname as username,
        e.status as status,
        e.certificatenum as idNo,
        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.accounttype as accountType,
        e.mobile as mobile
        from hrmresource e
        left join hrmdepartment d on e.departmentid = d.id
        left join hrmjobtitles c on e.jobtitle = c.id
        WHERE e.status not in (7)
        <if test="subCompanyIds != null and subCompanyIds.size()>0">
            AND e.subcompanyid1 IN
            <foreach collection="subCompanyIds" open="(" item="subCompanyId" separator="," close=")">
                #{subCompanyId}
            </foreach>
        </if>
        <if test="departmentIds != null and departmentIds.size()>0">
            AND d.id IN
            <foreach collection="departmentIds" open="(" item="departmentId" separator="," close=")">
                #{departmentId}
            </foreach>
        </if>
    </select>

    <select id="listVirtualEmpByVirtualDepIds"
            resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select  e.RESOURCEID       as employeeId,
                e.SUBCOMPANYID     as subcompanyid,
                h.accounttype as accountType,
                e.DEPARTMENTID     as departmentId
        from hrmresourcevirtual e
        left join hrmresource h on e.RESOURCEID=h.id
        where h.status not in (7)
        <if test="virtualDepartmentIds != null and virtualDepartmentIds.size()>0">
            AND e.DEPARTMENTID IN
            <foreach collection="virtualDepartmentIds" open="(" item="virtualDepartmentId" separator="," close=")">
                #{virtualDepartmentId}
            </foreach>
        </if>
    </select>

    <select id="listVirtualEmpByVirtualSubCompanyIds"
            resultType="com.engine.salary.entity.datacollection.DataCollectionEmployee">
        select
        e.RESOURCEID       as employeeId,
        e.SUBCOMPANYID     as subcompanyid,
        h.accounttype as accountType,
        e.DEPARTMENTID     as departmentId
        from hrmresourcevirtual e
        left join hrmresource h on e.RESOURCEID=h.id
        where h.status not in (7)
        <if test="virtualSubCompanyIds != null and virtualSubCompanyIds.size()>0">
            AND e.SUBCOMPANYID IN
            <foreach collection="virtualSubCompanyIds" open="(" item="virtualSubCompanyId" separator="," close=")">
                #{virtualSubCompanyId}
            </foreach>
        </if>
    </select>

    <select id="listBySubCompany" resultType="long">
        select
        e.id
        from hrmresource e
        where e.status not in (7)
        AND e.subcompanyid1 IN
        <foreach collection="subCompanyIds" open="(" item="subCompanyId" separator="," close=")">
            #{subCompanyId}
        </foreach>
    </select>

    <select id="listByDepartment" resultType="long">
        select
        e.id
        from hrmresource e
        where e.status not in (7)
        AND e.departmentid IN
        <foreach collection="departmentIds" open="(" item="departmentId" separator="," close=")">
            #{departmentId}
        </foreach>
    </select>

    <select id="listByJob" resultType="long">
        select
        e.id
        from hrmresource e
        where e.status not in (7)
        AND e.jobtitle IN
        <foreach collection="jobIds" open="(" item="jobId" separator="," close=")">
            #{jobId}
        </foreach>
    </select>

    <select id="getJobCallInfoById" resultType="com.engine.salary.entity.hrm.JobCallInfo">
        select job.id,
               job.NAME as name
        from hrmjobcall job
        where job.id = #{jobCallId}
    </select>
</mapper>