<?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.tofly.base.mapper.UserMapper">

    <resultMap id="userMap" type="com.tofly.base.entity.User">
        <id property="id" column="ID"/>
        <result property="username" column="USERNAME"/>
        <result property="password" column="PASSWORD"/>
        <result property="salt" column="SALT"/>
        <result property="phone" column="PHONE"/>
        <result property="avatar" column="AVATAR"/>
        <result property="departmentId" column="DEPARTMENT_ID"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="updateTime" column="UPDATE_TIME"/>
        <result property="lockFlag" column="LOCK_FLAG"/>
        <result property="statusFlag" column="STATUS_FLAG"/>
        <result property="wxOpenid" column="WX_OPENID"/>
        <result property="qqOpenid" column="QQ_OPENID"/>
        <result property="email" column="EMAIL"/>
        <result property="realName" column="REAL_NAME"/>
        <result property="loginTime" column="LOGIN_TIME"/>
        <result property="lastIp" column="LAST_IP"/>
        <result property="pwderrtimes" column="PWDERRTIMES"/>
        <result property="worknumber" column="WORKNUMBER"/>
        <result property="findpwdkey" column="FINDPWDKEY"/>
        <result property="aliasname" column="ALIASNAME"/>
        <result property="birthday" column="BIRTHDAY"/>
        <result property="belong" column="BELONG"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="esignature" column="ESIGNATURE"/>
        <result property="userLevel" column="USER_LEVEL"/>
        <result property="createType" column="CREATE_TYPE"/>
        <result property="firstlog" column="FIRSTLOG"/>
        <result property="applyer" column="APPLYER"/>
        <result property="applytime" column="APPLYTIME"/>
        <result property="applystate" column="APPLYSTATE"/>
        <result property="auditor" column="AUDITOR"/>
        <result property="accountstate" column="ACCOUNTSTATE"/>
        <result property="audittime" column="AUDITTIME"/>
        <result property="auditstatus" column="AUDITSTATUS"/>
        <result property="auditstate" column="AUDITSTATE"/>
        <result property="note" column="NOTE"/>
        <result property="enableFlag" column="ENABLE_FLAG"/>
        <result property="job" column="JOB"/>
        <result property="originalPassword" column="ORIGINAL_PASSWORD"/>
        <result property="parentCompanyId" column="PARENT_COMPANY_ID"/>
        <result property="companyId" column="COMPANY_ID"/>
        <result property="nowVersion" column="NOW_VERSION"/>
        <result property="lastAppUpdateTime" column="LAST_APP_UPDATE_TIME"/>
        <result property="shortNumber" column="SHORT_NUMBER"/>
        <result property="sex" column="SEX"/>
        <result property="birth" column="BIRTH"/>
        <result property="serialNumber" column="SERIAL_NUMBER"/>
        <association property="parentCompanyName" column="PARENT_COMPANY_ID" select="getCompanyName"/>
        <association property="companyName" column="COMPANY_ID" select="getCompanyName"/>
        <association property="departmentName" column="DEPARTMENT_ID" select="getDName"/>
        <association property="roleList" column="ID" select="getRoleList"/>
        <collection property="deptList" column="ID" select="getDeptList"/>
    </resultMap>

    <select id="getCompanyName" resultType="string">
        select COMPANY_NAME as companyName
        from TF_SMPN_COMPANY_X
        where id = #{departmentId}
    </select>
    <select id="getDName" resultType="java.lang.Object" parameterType="java.lang.String">
        select name as departmentName
        from TF_SMPN_DEPARTMENT_X
        where id = #{departmentId}
    </select>
    <select id="getRoleList" resultType="com.tofly.base.entity.Role">
        SELECT
            *
        FROM
            TF_SMPN_ROLE_X
        WHERE
            ID IN ( SELECT ROLE_ID FROM TF_SMPN_USERROLE_X WHERE USER_ID = #{id} )
    </select>
    <select id="getDeptList" resultType="com.tofly.base.entity.Department">
        SELECT
            *
        FROM
            TF_SMPN_DEPARTMENT_X
        WHERE
            ID IN ( SELECT DEPT_ID FROM TF_SMPN_USERDEPT_X WHERE USER_ID = #{id} )
    </select>

    <select id="getUserById" resultMap="userMap">
        select id,
               username,
               phone,
               avatar,
               department_id,
               create_time,
               update_time,
               lock_flag,
               status_flag,
               wx_openid,
               qq_openid,
               email,
               real_name,
               login_time,
               last_ip,
               pwderrtimes,
               worknumber,
               findpwdkey,
               aliasname,
               birthday,
               belong,
               create_user,
               CREATE_TYPE,
               FIRSTLOG,
               APPLYER,
               APPLYTIME,
               APPLYSTATE,
               AUDITOR,
               ACCOUNTSTATE,
               AUDITTIME,
               AUDITSTATE,
               AUDITSTATUS,
               ESIGNATURE,
               NOTE,
               ENABLE_FLAG,
               SHORT_NUMBER,
               SEX,
               BIRTH,
               JOB,
               SERIAL_NUMBER
        from TF_SMPN_USER_X
        where id = #{id}
    </select>

    <select id="getExistUserListByRole" resultMap="userMap">

        select id,
               username,
               phone,
               avatar,
               department_id,
               create_time,
               update_time,
               lock_flag,
               status_flag,
               wx_openid,
               qq_openid,
               email,
               real_name,
               login_time,
               last_ip,
               pwderrtimes,
               worknumber,
               findpwdkey,
               aliasname,
               birthday,
               belong,
               create_user,
               CREATE_TYPE,
               FIRSTLOG,
               APPLYER,
               APPLYTIME,
               APPLYSTATE,
               AUDITOR,
               ACCOUNTSTATE,
               AUDITTIME,
               AUDITSTATE,
               AUDITSTATUS,
               ESIGNATURE,
               NOTE,
               SHORT_NUMBER,
               SEX,
               BIRTH,
               JOB,
               SERIAL_NUMBER
        from TF_SMPN_USER_X u
        where status_flag = '1'
          and exists(select 1 from TF_SMPN_USERROLE_X where user_id = u.id and role_id = #{roleId})
    </select>

    <select id="getNoExistUserListByRole" resultMap="userMap">

        select id,
               username,
               phone,
               avatar,
               department_id,
               create_time,
               update_time,
               lock_flag,
               status_flag,
               wx_openid,
               qq_openid,
               email,
               real_name,
               login_time,
               last_ip,
               pwderrtimes,
               worknumber,
               findpwdkey,
               aliasname,
               birthday,
               belong,
               create_user
                   CREATE_TYPE,
               FIRSTLOG,
               APPLYER,
               APPLYTIME,
               APPLYSTATE,
               AUDITOR,
               ACCOUNTSTATE,
               AUDITTIME,
               AUDITSTATE,
               AUDITSTATUS,
               ESIGNATURE,
               NOTE,
               SHORT_NUMBER,
               SEX,
               BIRTH,
               JOB,
               SERIAL_NUMBER
        from TF_SMPN_USER_X u
        where status_flag = '1'
          and not exists(select 1 from TF_SMPN_USERROLE_X where user_id = u.id and role_id = #{roleId})
    </select>

    <select id="getSources" parameterType="java.lang.Long" resultType="map">
        select s.*
        from TF_SMPN_DEPARTMENTSOURCE_X ds,
             TF_SMPN_SOURCE_X s
        where ds.source_id = s.id
          and ds.department_id = #departmentId#
    </select>


    <select id="getUserPage" resultType="java.util.LinkedHashMap" databaseId="oracle">
        select id,
        username,
        phone,
        avatar,
        department_id,
        create_time,
        lock_flag,
        decode(lock_flag,'0','锁定','未锁定') lock_flag_name,
        status_flag,
        wx_openid,
        qq_openid,
        email,
        real_name,
        login_time,
        last_ip,
        pwderrtimes,
        worknumber,
        findpwdkey,
        aliasname,
        birthday,
        belong,
        create_user,
        user_level,
        CREATE_TYPE,
        FIRSTLOG,
        APPLYER,
        APPLYTIME,
        APPLYSTATE,
        AUDITOR,
        ACCOUNTSTATE,
        AUDITTIME,
        AUDITSTATE,
        AUDITSTATUS,
        ESIGNATURE,
        NOTE,
        ENABLE_FLAG,
        JOB,
        PARENT_COMPANY_ID,
        COMPANY_ID,
        NOW_VERSION,
        LAST_APP_UPDATE_TIME,
        SHORT_NUMBER,
        SEX,
        BIRTH,
        (select wmsys.wm_concat(u2.real_name)
        from TF_SMPN_USER_ASCRIPTION u1 left join TF_SMPN_USER_X u2
        on u1.parent_User_Id = u2.id
        where u1.user_id = l.id) userGroup,
        (select COMPANY_NAME
        from TF_SMPN_COMPANY_X
        where id = PARENT_COMPANY_ID) as parentCompanyName,
        (select COMPANY_NAME
        from TF_SMPN_COMPANY_X
        where id = COMPANY_ID) as companyName,
        (select real_name from TF_SMPN_USER_X
        where username=l.AUDITOR) as auditorName,
        (select real_name from TF_SMPN_USER_X
        where id=l.create_user) as create_user_name,
        (select m.name
        from TF_SMPN_DEPARTMENT_X m
        where m.id = l.department_id) as dept_name,
        to_char((select wmsys.wm_concat(n.role_id)
        from TF_SMPN_USERROLE_X n
        where n.user_id = l.id)) as role_id_list,
        to_char((select wmsys.wm_concat(to_char(o.role_name))
        from TF_SMPN_ROLE_X o, TF_SMPN_USERROLE_X n
        where n.role_id = o.id
        and l.id = n.user_id)) as role_name,
        (select notes
        from tf_smpn_code_x
        where key = '0001'
        and value = l.status_flag) as status_flag_name
        from TF_SMPN_USER_X l
        where
        1=1 and status_flag = 1
        <if test="user.auditstatus != null and user.auditstatus.trim() != ''">
            and AUDITSTATUS = #{user.auditstatus}
        </if>
        <if test="user.id != null">
            and id = #{user.id}
        </if>
        <if test="user.realName != null and user.realName.trim() != ''">
            and real_Name like '%'||#{user.realName}||'%'
        </if>
        <if test="user.departmentId != null">
            AND ID IN (SELECT a.USER_ID FROM TF_SMPN_USERDEPT_X a WHERE a.DEPT_ID = #{user.departmentId})
        </if>
        <if test="user.enableFlag != null and user.enableFlag.trim() != ''">
            and ENABLE_FLAG = #{user.enableFlag}
        </if>
        <if test="user.lockFlag != null and user.lockFlag.trim() != ''">
            and lock_flag = #{user.lockFlag}
        </if>
        <if test="user.userLevel != null and user.userLevel.trim() != ''">
            and user_level = #{user.userLevel}
        </if>
        <if test="user.username != null">
            and USERNAME like '%'||#{user.username}||'%'
        </if>
        <if test="user.companyId != null">
            AND ID IN (SELECT a.USER_ID FROM TF_SMPN_USERDEPT_X a WHERE a.COMPANY_ID = #{user.companyId})
        </if>
        <if test="user.deptIds != null">
            AND ID IN (SELECT a.USER_ID
            FROM TF_SMPN_USERDEPT_X a
            WHERE to_char(a.DEPT_ID) IN
            <foreach item="nodeCodestr" collection="user.deptIds.split(',')" open="(" separator="," close=")">
                #{nodeCodestr}
            </foreach>
            )
        </if>
        <if test="user.companyIds != null">
            AND ID IN (SELECT a.USER_ID
            FROM TF_SMPN_USERDEPT_X a
            WHERE to_char(a.COMPANY_ID) IN
            <foreach item="nodeCodestr" collection="user.companyIds.split(',')" open="(" separator="," close=")">
                #{nodeCodestr}
            </foreach>
            )
        </if>
        ORDER BY CREATE_TIME DESC
    </select>
    <select id="getUserPage" resultType="map" databaseId="mysql">
        select id,
        username,
        phone,
        avatar,
        department_id,
        create_time,
        lock_flag,
        case lock_flag when '0' then '锁定' else '未锁定' end lock_flag_name,
        status_flag,
        wx_openid,
        qq_openid,
        email,
        real_name,
        login_time,
        last_ip,
        pwderrtimes,
        worknumber,
        findpwdkey,
        aliasname,
        birthday,
        belong,
        create_user,
        user_level,
        CREATE_TYPE,
        FIRSTLOG,
        APPLYER,
        APPLYTIME,
        APPLYSTATE,
        AUDITOR,
        ACCOUNTSTATE,
        AUDITTIME,
        AUDITSTATE,
        AUDITSTATUS,
        ESIGNATURE,
        NOTE,
        SHORT_NUMBER,
        SEX,
        BIRTH,
        SERIAL_NUMBER,
        (select real_name from TF_SMPN_USER_X
        where id=l.create_user) as create_user_name,
        (select m.name
        from TF_SMPN_DEPARTMENT_X m
        where m.id = l.department_id) as dept_name,
        (select group_concat(n.role_id)
        from TF_SMPN_USERROLE_X n
        where n.user_id = l.id group by l.id) as role_id_list,
        (select group_concat(o.role_name)
        from TF_SMPN_ROLE_X o, TF_SMPN_USERROLE_X n
        where n.role_id = o.id
        and l.id = n.user_id GROUP BY l.id) as role_name,
        (select notes
        from tf_smpn_code_x
        where `key` = '0011'
        and `value` = l.status_flag) as status_flag_name
        from TF_SMPN_USER_X l where 1=1
        <if test="user.auditstatus != null">
            and AUDITSTATUS = #{user.auditstatus}
        </if>
        <if test="user.realName != null and user.realName.trim() != ''">
            and (real_name like CONCAT('%',#{user.realName},'%') or real_name like CONCAT('%',#{user.realName},'%'))
        </if>
        <if test="user.username != null and user.username.trim() != ''">
            and (username like CONCAT('%',#{user.username},'%') or username like CONCAT('%',#{user.username},'%'))
        </if>
        <if test="user.departmentId != null">
            and department_id = #{user.departmentId}
        </if>
        <if test="user.statusFlag != null">
            and status_flag = #{user.statusFlag}
        </if>
        <if test="user.lockFlag != null">
            and lock_flag = #{user.lockFlag}
        </if>
        <if test="user.userLevel != null">
            and user_level = #{userLevel}
        </if>
    </select>

    <update id="updateUserInfo">
        update TF_SMPN_USER_X
        <set>
            <if test="lastIp != null and lastIp.trim() != ''">
                LAST_IP = #{lastIp} ,
            </if>
            <if test="pwderrtimes != null">
                PWDERRTIMES = #{pwderrtimes} ,
            </if>
            <if test="loginTime != null">
                LOGIN_TIME = #{loginTime} ,
            </if>
            <if test="lockFlag != null and lockFlag.trim() != ''">
                lockFlag = #{lockFlag} ,
            </if>
        </set>
        where id=#{id}
    </update>

    <select id="getUserByMenu" resultType="map">
        select u.id, u.real_name
        from TF_SMPN_USER_X u,
             TF_SMPN_USERROLE_X ur,
             TF_SMPN_ROLE_X r,
             TF_SMPN_ROLEMENU_X rm,
             TF_SMPN_MENU_X m
        where u.id = ur.user_id
          and ur.role_id = r.id
          and rm.role_id = r.id
          and m.id = rm.menu_id
          and m.name = #{menuName}
    </select>


    <select id="getListHistory" resultType="map" databaseId="oracle">
        select u.USERNAME,u.REAL_NAME,u.PHONE,u.EMAIL,u.CREATE_TYPE,
        u.STATUS_FLAG,u.LOCK_FLAG,u.AUDITSTATE,u.CREATE_TIME,u.CREATE_USER,u.AUDITTIME,
        u.AUDITOR,u.AUDITSTATUS,u.AVATAR,u.ESIGNATURE,u.NOTE,u.APPLYER,u.APPLYSTATE,u.SHORT_NUMBER,
        u.SEX,u.BIRTH,u.JOB,SERIAL_NUMBER,
        (select real_name from TF_SMPN_USER_X where u.APPLYER = username) as applyerName,
        (select real_name from TF_SMPN_USER_X
        where username=u.AUDITOR) as auditorName,
        (select m.name
        from TF_SMPN_DEPARTMENT_X m
        where m.id = u.department_id) as dept_name,
        to_char((select wmsys.wm_concat(to_char(o.role_name))
        from TF_SMPN_ROLE_X o, TF_SMPN_USERROLE_X n
        where n.role_id = o.id
        and u.id = n.user_id)) as role_name
        FROM
        TF_SMPN_USER_X u
        where
        u.CREATE_TYPE=1
        <if test="map.userName != null and map.userName.trim() != ''">
            and u.APPLYER like '%'||#{map.userName}||'%'
        </if>
        <if test="map.realName != null and map.realName.trim() != ''">
            and u.REAL_NAME like '%'||#{map.realName}||'%'
        </if>
        <if test="map.auditStatus != null and map.auditStatus.trim() != ''">
            and u.AUDITSTATUS = #{map.auditStatus}
        </if>
        <if test="map.StartAuditTime != null and map.StartAuditTime.trim() != ''">
            and to_char(u.CREATE_TIME,'yyyy-MM-dd hh:mm:ss') >= #{map.StartAuditTime}
        </if>
        <if test="map.endAuditTime != null and map.endAuditTime.trim() != ''">
            <![CDATA[   and to_char(u.CREATE_TIME,'yyyy-MM-dd hh:mm:ss') <= #{map.endAuditTime}   ]]>
        </if>
        order by u.CREATE_TIME desc
    </select>

    <update id="updateByIdList" parameterType="java.util.List">
        update TF_SMPN_USER_X set LOCK_FLAG=0
        where
        id in
        <foreach item="item" index="index" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

    <update id="updateByIdListToUnlock" parameterType="java.util.List">
        update TF_SMPN_USER_X
        set LOCK_FLAG=1,
        pwderrtimes = 0
        where
        id in
        <foreach item="item" index="index" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

    <update id="updateByIdStatusFlag" parameterType="java.util.List">
        update TF_SMPN_USER_X set ENABLE_FLAG=0
        where
        id in
        <foreach item="item" index="index" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

    <update id="updateUnStatusFlag" parameterType="java.util.List">
        update TF_SMPN_USER_X set ENABLE_FLAG=1
        where
        id in
        <foreach item="item" index="index" collection="list" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

    <select id="sysUserReport" resultType="Map">
        select t2.NAME as dptName,count(1) as num from TF_SMPN_USER_X t1
                                                           inner join
                                                       TF_SMPN_DEPARTMENT_X t2
                                                       on t1.DEPARTMENT_ID=t2.id
        where 1=1
          and t1.STATUS_FLAG=1
        GROUP BY t2.NAME

    </select>

    <select id="getDeptUserIdList" resultType="long">
        select ID from TF_SMPN_USER_X
        <where>
            STATUS_FLAG=1
            <if test="deptIdList!=null and deptIdList.size()>0">
                and department_id IN
                <foreach item="deptId" index="idx" collection="deptIdList" separator="," open="(" close=")">
                    #{deptId}
                </foreach>
            </if>
        </where>
    </select>
</mapper>
