<?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.dk.mdm.mapper.mst.StaffMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        t.staff_id
        , t.staff_code, t.staff_name,t.staff_phone, t.org_id, t.role_ids,  t.wx_user_id, t.remarks,
        t.flg_valid,t.flg_can_login,t.hr_status,
        t.cp_id, t.op_create_time, t.op_create_user_id, t.op_update_time, t.op_update_user_id
    </sql>


    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.common.model.response.mst.StaffResponse">
        <id column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
        <result column="staff_code" property="staffCode"/>
        <result column="staff_name" property="staffName"/>
        <result column="staff_phone" property="staffPhone"/>
        <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
        <result column="org_name" property="orgName"/>
        <result column="flg_business" property="flgBusiness"/>
        <result column="hr_status_name" property="hrStatusName"/>
        <result column="role_names" property="roleNames"/>
        <result column="role_ids" property="roleIds" typeHandler="UuidListTypeHandler"/>
        <result column="wx_user_id" property="wxUserId" typeHandler="UuidTypeHandler"/>
        <result column="remarks" property="remarks"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>
        <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
        <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
        <result column="op_app_code" property="opAppCode"/>
        <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
        <result column="op_db_user" property="opDbUser"/>
        <result column="flg_can_login" property="flgCanLogin"/>
        <result column="hr_status" property="hrStatus"/>
        <result column="default_wh_id" property="defaultWhId" typeHandler="UuidTypeHandler" />
        <result column="default_wh_name" property="defaultWhName"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="staffId != null">
                AND t.staff_id = #{staffId}
            </if>
            <if test="staffCode != null and staffCode != ''">
                AND t.staff_code LIKE concat('%', my_ex.likequery(#{staffCode}), '%')
            </if>
            <if test="staffName != null and staffName != ''">
                AND t.staff_name LIKE concat('%', my_ex.likequery(#{staffName}), '%')
            </if>
            <if test="staffPhone != null and staffPhone != ''">
                AND t.staff_phone LIKE concat('%', my_ex.likequery(#{staffPhone}), '%')
            </if>

            <if test="orgId != null">
                AND t.org_id = #{orgId}
            </if>
            <if test="remarks != null and remarks != ''">
                AND t.remarks = #{remarks}
            </if>
            <if test="flgValid != null">
                AND t.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null" >
                AND t.cp_id = #{cpId}
            </if>
<!--            <if test="staffCode=='东科智云-体验'" >-->
<!--                AND t.cp_id = 2-->
<!--            </if>-->
            <if test="hrStatus != null">
                AND t.hr_status = #{hrStatus}
            </if>
            <if test="roleIds != null and roleIds.size()>0">
                AND t.role_ids &amp;&amp;
                #{roleIds, typeHandler=UuidListTypeHandler}
            </if>
            <if test="orgIds != null and orgIds.size()>0">
                AND t.org_id =
                any(#{orgIds, typeHandler=UuidListTypeHandler})
            </if>
            <if test="searchText !=null">
                AND ( t.staff_name LIKE concat('%', my_ex.likequery(#{searchText}), '%')
                or t.staff_phone LIKE concat('%', my_ex.likequery(#{searchText}), '%'))
            </if>
        </where>
    </sql>

    <!-- 查询表t_mst_staff，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        , org.org_name,
          case when t.hr_status = 1 then '在职' else '离职' end as  hr_status_name,
           (SELECT  COALESCE(array_to_string(array_agg(role_name), ','),'')
            FROM dkic_b.t_mst_role
            WHERE role_id = ANY (t.role_ids)) AS role_names
        FROM dkic_b.t_mst_staff t
        LEFT JOIN dkic_b.t_mst_org org
        ON t.org_Id = org.org_Id
        <include refid="Condition"/>
        ORDER BY  t.op_create_time desc
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_mst_staff，（条件查询+分页）列表 -->
    <select id="selectByCondByCode" resultMap="BaseResultMap">
        SELECT t.* FROM dkic_b.t_mst_staff t
        <where>
            <if test="staffCode != null and staffCode != ''">
                AND t.staff_code  = #{staffCode}
            </if>

            <if test="cpId != null">
                AND t.cp_id = #{cpId}
            </if>
        </where>
    </select>

    <!-- 查询表t_mst_staff，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_mst_staff t
        <include refid="Condition"/>
    </select>

    <!-- 根据主键查询表t_mst_staff的一行数据 -->
    <select id="selectById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        , org.org_name,
        case when t.hr_status = 1 then '在职' else '离职' end as  hr_status_name,
            (SELECT  COALESCE(array_to_string(array_agg(role_name), ','),'')
            FROM dkic_b.t_mst_role
            WHERE role_id = ANY (t.role_ids)) AS role_names
        FROM dkic_b.t_mst_staff t
        LEFT JOIN dkic_b.t_mst_org org ON t.org_Id = org.org_Id
        WHERE t.staff_id = #{id}::uuid
    </select>

    <!-- 根据用户Id查询表t_mst_staff的一行数据 -->
    <select id="selectByUserId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        , org.org_name,
        org.flg_business,
        CASE
        WHEN tmow.wh_id IS NULL THEN
        (SELECT wh_id FROM dkic_b.t_mst_warehouse where cp_id = T.cp_id and flg_default)
        ELSE  tmow.wh_id
        END AS default_wh_id,
        CASE
        WHEN tmw.wh_name IS NULL THEN
        (SELECT wh_name FROM dkic_b.t_mst_warehouse where cp_id = T.cp_id and flg_default)
        ELSE tmw.wh_name
        END AS default_wh_name,
        case when t.hr_status = 1 then '在职' else '离职' end as  hr_status_name,

        (SELECT  COALESCE(array_to_string(array_agg(role_name), ','),'')
        FROM dkic_b.t_mst_role
        WHERE role_id = ANY (t.role_ids)) AS role_names
        FROM dkic_b.t_mst_staff t
        LEFT JOIN dkic_b.t_mst_org org ON t.org_Id = org.org_Id
        LEFT JOIN dkic_b.t_mst_org_wh tmow ON tmow.cp_id = T.cp_id
        AND tmow.org_id = T.org_id
        AND tmow.flg_default
        LEFT JOIN dkic_b.t_mst_warehouse tmw ON tmw.wh_id = tmow.wh_id
        WHERE t.wx_user_id = #{id}::uuid
        AND t.cp_id = #{cpId}
    </select>

    <!-- 根据用户Id更新dkic_a.t_wx_user的cpid-->
<!--    <update id="updateWxInfoByUserId">-->
<!--        update dkic_a.t_wx_user -->
<!--        set current_cp = #{currentCp},joined_cps = #{currentCp}-->
<!--        where user_id = #{userId}::uuid-->
<!--    </update>-->

    <!-- 批量插入数 -->
    <insert id="insertBatch">
        insert into dkic_b.t_mst_staff
        (
        <trim suffixOverrides=",">
            staff_code,
            staff_name,
            staff_phone,
            org_id,
            role_ids,
            wx_user_id,
            remarks,
            cp_id,
            op_app_code,
            flg_can_login,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.staffCode},
                #{item.staffName},
                #{item.staffPhone},
                #{item.orgId}::uuid,
                #{item.roleIds},
                #{item.wxUserId}::uuid,
                #{item.remarks},
                #{item.cpId},
                #{item.opAppCode},
                #{item.flgCanLogin},
            </trim>
            )
        </foreach>
    </insert>
</mapper>
