<?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.staff.StaffMapper">
    <resultMap id="BaseResultMap" type="com.engine.organization.entity.staff.po.StaffPO">
        <result column="id" property="id"/>
        <result column="plan_id" property="planId"/>
        <result column="comp_id" property="compId"/>
        <result column="ec_company" property="ecCompany"/>
        <result column="ec_department" property="ecDepartment"/>
        <result column="job_id" property="jobId"/>
        <result column="staff_num" property="staffNum"/>
        <result column="control_policy" property="controlPolicy"/>
        <result column="permanent_num" property="permanentNum"/>
        <result column="freeze_num" property="freezeNum"/>
        <result column="lack_status" property="lackStatus"/>
        <result column="staff_desc" property="staffDesc"/>
        <result column="description" property="description"/>
        <result column="creator" property="creator"/>
        <result column="delete_type" property="deleteType"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
        t
        .
        id
        , t.plan_id
        , t.comp_id
        , t.ec_company
        , t.dept_id
        , t.ec_department
        , t.job_id
        , t.staff_num
        , t.control_policy
        , t.permanent_num
        , t.freeze_num
        , t.lack_status
        , t.staff_desc
        , t.description
    </sql>
    <select id="getStaffByID" parameterType="com.engine.organization.entity.staff.po.StaffPO"
            resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from jcl_org_staff t where id = #{id} AND delete_type = 0
    </select>
    <select id="listUsedId" resultType="java.lang.String">
        select staff_id
        from jcl_org_staffs
        where delete_type = 0
    </select>
    <select id="getStaffByFilter" resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from jcl_org_staff t
        where delete_type = 0
        <if test="companyId != null">
            and ec_company = #{companyId}
        </if>
        <if test="departmentId != null">
            and ec_department = #{departmentId}
        </if>
        <if test="jobId != null">
            and job_id = #{jobId}
        </if>
        order by update_time desc
    </select>

    <select id="customSelect" resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from jcl_org_staff t
        where delete_type = 0
        <if test="planId != null">
            and plan_id = #{planId}
        </if>
        <if test="companyId != null">
            and ec_company = #{companyId}
        </if>
        <if test="departmentId != null">
            and ec_department = #{departmentId}
        </if>
        <if test="jobId != null">
            and job_id = #{jobId}
        </if>
    </select>


    <select id="getStaffsByIds" resultMap="BaseResultMap">
        select
        <include refid="baseColumns"/>
        from jcl_org_staff t
        WHERE delete_type = 0
        AND id IN
        <foreach collection="ids" open="(" item="id" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <update id="updateStaff" parameterType="com.engine.organization.entity.staff.po.StaffPO">
        update jcl_org_staff
        <set>
            creator=#{creator},
            update_time=#{updateTime},
            plan_id=#{planId},
            comp_id=#{compId},
            ec_company=#{ecCompany},
            dept_id=#{deptId},
            ec_department=#{ecDepartment},
            job_id=#{jobId},
            staff_num=#{staffNum},
            control_policy=#{controlPolicy},
            permanent_num=#{permanentNum},
            freeze_num=#{freezeNum},
            lack_status=#{lackStatus},
            staff_desc=#{staffDesc},
            description=#{description},
        </set>
        WHERE id = #{id} AND delete_type = 0
    </update>

    <insert id="insertIgnoreNull" parameterType="com.engine.organization.entity.staff.po.StaffPO" keyProperty="id"
            keyColumn="id" useGeneratedKeys="true">
        INSERT INTO jcl_org_staff
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="creator != null">
                creator,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="planId != null ">
                plan_id,
            </if>
            <if test="compId != null ">
                comp_id,
            </if>
            <if test="deptId != null ">
                dept_id,
            </if>
            <if test="ecCompany != null ">
                ec_company,
            </if>
            <if test="ecDepartment != null ">
                ec_department,
            </if>
            <if test="jobId != null ">
                job_id,
            </if>
            <if test="staffNum != null ">
                staff_num,
            </if>
            <if test="controlPolicy != null ">
                control_policy,
            </if>
            <if test="permanentNum != null ">
                permanent_num,
            </if>
            <if test="freezeNum != null ">
                freeze_num,
            </if>
            <if test="lackStatus != null ">
                lack_status,
            </if>
            <if test="staffDesc != null ">
                staff_desc,
            </if>
            <if test="description != null ">
                description,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="creator != null">
                #{creator},
            </if>
            <if test="deleteType != null">
                #{deleteType},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>

            <if test="planId != null ">
                #{planId},
            </if>
            <if test="compId != null ">
                #{compId},
            </if>
            <if test="deptId != null ">
                #{deptId},
            </if>
            <if test="ecCompany != null ">
                #{ecCompany},
            </if>
            <if test="ecDepartment != null ">
                #{ecDepartment},
            </if>
            <if test="jobId != null ">
                #{jobId},
            </if>
            <if test="staffNum != null ">
                #{staffNum},
            </if>
            <if test="controlPolicy != null ">
                #{controlPolicy},
            </if>
            <if test="permanentNum != null ">
                #{permanentNum},
            </if>
            <if test="freezeNum != null ">
                #{freezeNum},
            </if>
            <if test="lackStatus != null ">
                #{lackStatus},
            </if>
            <if test="staffDesc != null ">
                #{staffDesc},
            </if>
            <if test="description != null ">
                #{description},
            </if>
        </trim>
    </insert>

    <insert id="insertIgnoreNull" parameterType="com.engine.organization.entity.staff.po.StaffPO" databaseId="oracle">
        <selectKey keyProperty="id" resultType="long" order="AFTER">
            select JCL_ORG_STAFF_ID.currval from dual
        </selectKey>
        INSERT INTO jcl_org_staff
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="creator != null">
                creator,
            </if>
            <if test="deleteType != null">
                delete_type,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="planId != null ">
                plan_id,
            </if>
            <if test="compId != null ">
                comp_id,
            </if>
            <if test="deptId != null ">
                dept_id,
            </if>
            <if test="ecCompany != null ">
                ec_company,
            </if>
            <if test="ecDepartment != null ">
                ec_department,
            </if>
            <if test="jobId != null ">
                job_id,
            </if>
            <if test="staffNum != null ">
                staff_num,
            </if>
            <if test="controlPolicy != null ">
                control_policy,
            </if>
            <if test="permanentNum != null ">
                permanent_num,
            </if>
            <if test="freezeNum != null ">
                freeze_num,
            </if>
            <if test="lackStatus != null ">
                lack_status,
            </if>
            <if test="staffDesc != null ">
                staff_desc,
            </if>
            <if test="description != null ">
                description,
            </if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="creator != null">
                #{creator},
            </if>
            <if test="deleteType != null">
                #{deleteType},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>

            <if test="planId != null ">
                #{planId},
            </if>
            <if test="compId != null ">
                #{compId},
            </if>
            <if test="deptId != null ">
                #{deptId},
            </if>
            <if test="ecCompany != null ">
                #{ecCompany},
            </if>
            <if test="ecDepartment != null ">
                #{ecDepartment},
            </if>
            <if test="jobId != null ">
                #{jobId},
            </if>
            <if test="staffNum != null ">
                #{staffNum},
            </if>
            <if test="controlPolicy != null ">
                #{controlPolicy},
            </if>
            <if test="permanentNum != null ">
                #{permanentNum},
            </if>
            <if test="freezeNum != null ">
                #{freezeNum},
            </if>
            <if test="lackStatus != null ">
                #{lackStatus},
            </if>
            <if test="staffDesc != null ">
                #{staffDesc},
            </if>
            <if test="description != null ">
                #{description},
            </if>
        </trim>
    </insert>

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

    <select id="getStaffsByParamId" resultMap="BaseResultMap">
        select sum(a.staff_num) as staff_num from jcl_org_staff a
        inner join jcl_org_staffplan b on a.plan_id = b.id
        WHERE a.delete_type = 0 and b.control_dimension = #{ftype}
        <if test="compId != null">
            and a.comp_id = #{compId}
        </if>
        <if test="deptId != null">
            and a.dept_id = #{deptId}
        </if>
        <if test="jobId != null">
            and a.job_id = #{jobId}
        </if>
    </select>

    <select id="selectByEcCompany" resultMap="BaseResultMap">
        SELECT <include refid="baseColumns"/>
        FROM jcl_org_staff t
        where t.delete_type = 0
        AND t.ec_company IN
        <foreach collection="companyIds" open="(" item="companyId" separator="," close=")">
            #{companyId}
        </foreach>
        AND t.plan_id IN (SELECT id from jcl_org_staffplan s where s.control_dimension = 1)
    </select>

    <select id="selectByEcDepartment" resultMap="BaseResultMap">
        SELECT <include refid="baseColumns"/>
        FROM jcl_org_staff t
        where t.delete_type = 0
        AND t.ec_department IN
        <foreach collection="departmentIds" open="(" item="departmentId" separator="," close=")">
                #{departmentId}
        </foreach>
        AND t.plan_id IN (SELECT id from jcl_org_staffplan s where s.control_dimension = 2)
    </select>

    <select id="selectByPlanIds" resultMap="BaseResultMap">
        SELECT <include refid="baseColumns"/>
        FROM jcl_org_staff t
        where t.delete_type = 0
        AND t.plan_id IN
        <foreach collection="planIds" open="(" item="planId" separator="," close=")">
            #{planId}
        </foreach>
    </select>

</mapper>