<?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.srm.bpmserver.infra.dao.UserCenterDao">

    <select id="selectOrg" resultType="com.srm.bpmserver.infra.po.OrganizationPO">
        select org_id id,org_parent_id pid,org_name name from sp_org
        <where>
            <if test="q!=null and q!=''">
                AND org_name LIKE #{q}
            </if>
            <if test="pid!=null and pid!=''">
                AND org_parent_id=#{pid}
            </if>
        </where>
    </select>
    <select id="selectUsers" resultType="com.srm.bpmserver.infra.po.UserOrgPO">
        SELECT ep.id, ep.username name, so.org_id orgId
        FROM sp_user ep
        LEFT JOIN sp_org so ON so.org_id = ep.orgid
        WHERE ep.deletestatus = 0
        AND ep.supplier_id is null
        <if test="pid!=null and pid!=''">
            AND so.org_id =#{pid}
        </if>
        ORDER BY ep.create_date DESC
    </select>
    <select id="selectAllPosition" resultType="com.srm.bpmserver.infra.po.PositionPO">
        select id,name,parent_id pid from org_position
        WHERE delete_flag = 0
        <if test="pid!=null and pid!=''">
            AND parent_id=#{pid}
        </if>
        <if test="q!=null and q!=''">
            AND name LIKE #{q}
        </if>
        ORDER BY sort
    </select>
    <select id="selectPositionUser" resultType="com.srm.bpmserver.infra.po.PositionUserPO">
        SELECT ep.id, ep.username name, og.position_id pid
        FROM org_position_employee og
        RIGHT JOIN sp_user ep ON og.employee_id = ep.id
        WHERE og.delete_flag = 0
        <if test="pid!=null and pid!=''">
            AND og.position_id = #{pid}
        </if>
    </select>
    <select id="selectUserOrgByUserId1" resultType="java.lang.String">
        SELECT so.org_id orgId
        FROM sp_user ep
                 LEFT JOIN sp_org so ON so.org_code = ep.dept_code
        where ep.id = #{userId}
          AND ep.deletestatus = 0
          AND ep.user_type = 0
    </select>
    <select id="selectUserByPositionAndOrg" resultType="java.lang.String">
        select employee_id from org_position_employee ope
        where position_id=#{positionId} AND ope.delete_flag=0
        <if test="orgId!=null and orgId!=''">
            AND ope.org_id=#{orgId}
        </if>
    </select>
    <select id="selectUserInfoByUserId"
            resultType="com.srm.bpm.logic.dto.UserInfoDTO">
        select id id, id code, username nickname
        from sp_user
        where id IN
        <foreach collection="userIds" item="i" open="(" separator="," close=")">
            #{i}
        </foreach>
    </select>
    <select id="selectUserOrgByUserId" resultType="com.srm.bpm.logic.dto.UserOrgDTO">
        SELECT so.org_id orgId,org_name orgName,ep.id userId,so.org_id orgCode
        FROM sp_user ep
        LEFT JOIN sp_org so ON so.org_id = ep.orgid
        where ep.id IN
        <foreach collection="userIds" item="i" open="(" separator="," close=")">
            #{i}
        </foreach>
        AND ep.deletestatus = 0
        AND ep.supplier_id is null
    </select>
    <select id="selectUserPositionByUserAndOrg"
            resultType="com.srm.bpm.logic.dto.UserPositionDTO">
        select ope.position_id,
               ope.employee_id userId,
               ope.org_id,
               op.name         positionName,
               op.id           positionCode
        from org_position_employee ope
                 LEFT JOIN org_position op on ope.position_id = op.id
        where ope.org_id = #{orgId}
          and ope.employee_id = #{userId} AND ope.delete_flag=0
    </select>
    <select id="findChooseUsers" resultType="com.srm.bpm.logic.dto.UserChooseDTO">
        SELECT
        oe.id,
        oe.username,
        op.name `position`,
        oo. org_name AS orgName
        FROM
        sp_user oe
        LEFT JOIN sp_org oo ON oe.orgid = oo.org_id
        LEFT JOIN org_position_employee ope ON ope.employee_id = oe.id
        LEFT JOIN org_position op ON op.id = ope.position_id
        WHERE oe.deletestatus = 0
        AND oe.user_type = 0
        <if test="query!=null">
            <if test="query.username != null and query.username != ''">
                AND oe.username LIKE #{query.usernameLike}
            </if>
            <if test="query.positionId>0">
                AND ope.position_id = #{query.positionId}
            </if>
            <if test="query.orgId > 0">
                AND oo.org_id=#{query.orgId}
            </if>
        </if>
    </select>
</mapper>
