<?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.zcloud.basic.info.persistence.mapper.sync.SyncUserMapper">
    <select id="listPageSyncUser" resultType="com.zcloud.basic.info.persistence.dataobject.sync.SyncUserDO">
        select
        d.name as department_name,
        p.post_name,
        u.id,
        u.name,
        u.department_id,
        u.email,
        u.phone,
        u.personnel_type_name,
        u.nation_name,
        FROM_BASE64(u.user_id_card) user_id_card,
        CASE
        WHEN LENGTH(FROM_BASE64(u.user_id_card)) > 0
        AND MOD(SUBSTRING(FROM_BASE64(u.user_id_card), 17, 1), 2) = 1 THEN
        '男'
        WHEN LENGTH(FROM_BASE64(u.user_id_card)) > 0
        AND MOD(SUBSTRING(FROM_BASE64(u.user_id_card), 17, 1), 2) = 0 THEN
        '女'
        END AS sex,

        CASE
        WHEN LENGTH(FROM_BASE64(u.user_id_card)) > 0 THEN
        (YEAR(NOW()) - SUBSTRING(FROM_BASE64(u.user_id_card), 7, 4)) ELSE NULL
        END AS age,

        cast(substring(FROM_BASE64(u.user_id_card), 7, 8) AS DATE) AS birthday
        from user u
        left join corp_info c on c.id = u.corpinfo_id
        left join department d on d.id = u.department_id
        left join post p on p.id = u.post_id
        <where>
            and u.id != u.corpinfo_id
            and u.delete_enum = 'FALSE'
            and u.employment_flag in (1, 2)
            and u.corpinfo_id = #{params.corpinfoId}
            <if test="params.name != null and params.name != ''">
                and u.name like concat('%',#{params.name},'%')
            </if>
            <if test="params.departmentId != null">
                and u.department_id = #{params.departmentId}
            </if>
            <if test="params.departmentName != null and params.departmentName != ''">
                and d.name like concat('%',#{params.departmentName},'%')
            </if>
            <if test="params.postName != null and params.postName != ''">
                and p.post_name like concat('%',#{params.postName},'%')
            </if>
            <if test="params.phone != null and params.phone != ''">
                and u.phone like concat('%',#{params.phone},'%')
            </if>
            <if test="params.userIdCard != null and params.userIdCard != ''">
                and u.user_id_card = TO_BASE64(#{params.userIdCard})
            </if>

        </where>
        order by u.sort ,
        u.create_time desc
    </select>

</mapper>

