<?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.organization.mapper.hrmresource.SystemDataMapper">

    <resultMap id="CustomFieldMap" type="com.engine.organization.entity.jclimport.po.CusFormFieldPO">
        <result column="fieldlable" property="fieldLabel"/>
        <result column="fieldid" property="fieldId"/>
        <result column="fieldname" property="fieldName"/>
        <result column="ismand" property="required"/>
        <result column="fieldhtmltype" property="fieldHtmlType"/>
        <result column="type" property="type"/>
        <result column="dmlurl" property="dmlUrl"/>
        <result column="scopeid" property="scopeId"/>
        <result column="tablename" property="tableName"/>
        <!--        <result column="fielddbtype" property="fielddbtype"/>-->
        <!--        <result column="fieldorder" property="fieldorder"/>-->
    </resultMap>

    <resultMap id="SelectItemMap" type="com.engine.organization.entity.jclimport.po.JclSelectItem">
        <result column="fieldid" property="fieldId"/>
        <result column="selectvalue" property="selectValue"/>
        <result column="selectname" property="selectName"/>
    </resultMap>

    <resultMap id="RecordMap" type="com.engine.organization.entity.commom.RecordInfo">
        <result column="uuid" property="uuid"/>
        <result column="id" property="id"/>
        <result column="canceled" property="canceled"/>
    </resultMap>


    <update id="updateResourceJobTitleByIds">
        update hrmresource set JOBTITLE = #{jobTitle} where id in
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </update>


    <select id="getScCompanyNameById" resultType="java.lang.String">
        select subcompanyname
        from hrmsubcompany
        where id = #{companyId}
    </select>

    <select id="getBrowserDatas" resultType="java.util.Map">
        select
        subcompanyname as "name",
        id as "id"
        from hrmsubcompany
        where id in
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>


    <select id="getScDepartmentNameById" resultType="java.lang.String">
        select departmentname
        from hrmdepartment
        where id = #{departmentId}
    </select>
    <select id="getScHrmResourceNameById" resultType="java.lang.String">
        select lastname
        from hrmresource
        where id = #{managerId}
    </select>
    <select id="getHrmObjectByID" resultMap="RecordMap">
        select *
        from ${tableName}
        where id = #{id}
    </select>
    <select id="getHrmObjectByUUID" resultMap="RecordMap">
        select *
        from ${tableName}
        where uuid = #{uuid}
    </select>
    <select id="getHrmJobTitleByName" resultMap="RecordMap">
        select *
        from hrmjobtitles
        where jobtitlename = #{name}
        order by created desc limit 1
    </select>
    <select id="getHrmJobTitleByName" resultMap="RecordMap" databaseId="sqlserver">
        select top 1  *
        from hrmjobtitles
        where jobtitlename = #{name}
        order by created desc
    </select>
    <select id="getHrmJobTitleByName" resultMap="RecordMap" databaseId="oracle">
        select *
        from hrmjobtitles
        where jobtitlename = #{name}
          and rownum &lt; 2
        order by created desc
    </select>
    <select id="getHrmResourceIds" resultType="java.lang.Long">
        select id
        from hrmresource
        where departmentid = #{departmentId}
          And jobTitle = #{jobTitle}
    </select>
    <select id="getHrmResourceMaxId" resultType="java.lang.Long">
        select max(id)
        from hrmresource
    </select>
    <select id="getHrmResourceIdsByDept" resultType="java.lang.Long">
        select id
        from jcl_org_hrmresource
        where department_id = #{departmentId}
    </select>

    <select id="getBatchUuidByIds" resultType="java.lang.String">
        select uuid from ${tableName} where id in
        <foreach collection="ecIds" open="(" item="ecId" separator="," close=")">
            #{ecId}
        </foreach>
    </select>
    <select id="getHrmFieldsByScopeId" resultMap="CustomFieldMap">
        SELECT *
        FROM (SELECT t1.fieldid,
                     t2.fieldname,
                     t1.fieldlable,
                     t1.ismand,
                     t2.fielddbtype,
                     t2.fieldhtmltype,
                     t2.type,
                     t1.dmlurl,
                     fieldorder,
                     groupid,
                     #{scopeId} as scopeId,
                     'cus'      as tablename
              FROM cus_formfield t1,
                   cus_formdict t2
              WHERE t1.fieldid = t2.id
                AND t1.isuse = 1
                AND t1.scope = 'HrmCustomFieldByInfoType'
                AND t1.scopeid = #{scopeId}
              UNION ALL
              SELECT fieldid,
                     fieldname,
                     fieldlabel,
                     ismand,
                     fielddbtype,
                     fieldhtmltype,
                     TYPE,
                     dmlurl,
                     fieldorder,
                     groupid,
                     #{scopeId} as scopeId,
                     'hrm'      as tablename
              FROM hrm_formfield) hrmallfield
        WHERE FIELDHTMLTYPE != '6' and groupid IN (SELECT id FROM hrm_fieldgroup WHERE grouptype = #{scopeId})
        ORDER BY hrmallfield.groupid, hrmallfield.fieldorder
    </select>
    <select id="getJobTitleIds" resultType="java.lang.Integer">
        SELECT h.ID
        FROM HRMJOBTITLES h
        INNER JOIN HRMJOBACTIVITIES h1 ON
        h.JOBACTIVITYID = h1.ID
        INNER JOIN HRMJOBGROUPS h2 ON
        h1.JOBGROUPID = h2.ID
        WHERE 1 = 1
        <if test="jobActivityId !=null and jobActivityId != ''">
            AND h1.ID =#{jobActivityId}
        </if>
        <if test="jobGroupId !=null and jobGroupId != ''">
            AND h2.ID =#{jobGroupId}
        </if>
    </select>


</mapper>