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

    <!-- 通用查询映射结果 -->
    <resultMap id="userDepartmentPostMap" type="com.dayou.vo.UserVO">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="name" property="name" />
        <result column="id_no" property="idNo" />
        <result column="user_type" property="userType" />
        <result column="mobile" property="mobile" />
        <result column="sex" property="sex" />
        <result column="password" property="password" />
        <result column="salt" property="salt" />
        <result column="birthday" property="birthday" />
        <result column="enable" property="enable" />
        <result column="staff_no" property="staffNo"/>
        <result column="nation" property="nation"/>
        <result column="politics" property="politics"/>
        <result column="home_address" property="homeAddress"/>
        <result column="resign_state" property="resignState"/>
        <result column="hire_date" property="hireDate"/>
        <result column="resign_date" property="resignDate"/>
        <result column="remark" property="remark"/>
        <result column="native_place" property="nativePlace"/>
        <collection property="departmentPostVOList" ofType="com.dayou.vo.DepartmentPostVO" select="selectDepartmentPostVO" column="id"/>
        <collection property="userPosts" ofType="com.dayou.vo.IdNameVO" select="selectUserPosts" column="id"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        name, id_no, user_type, mobile, sex, password, salt, birthday, enable,staff_no,nation,politics,home_address,resign_state,hire_date,resign_date,remark,native_place,settle_password
    </sql>

    <insert id="saveOrUpdateById" parameterType="com.dayou.entity.User">
        insert into user(`id`,`name`, `staff_no`,`password`,`user_type`,`sex`) values
            (#{user.id},#{user.name},#{user.staffNo},#{user.password},#{user.userType},#{user.sex})
            on duplicate key update
            id=id
    </insert>

    <select id="xPage" parameterType="com.dayou.vo.UserVO" resultMap="userDepartmentPostMap">
        select <include refid="Base_Column_List" /> from user 
        <where>
            deleted = 0
            <if test="userVO!=null and userVO.sex!=null and userVO.sex!='' ">
                and sex = #{userVO.sex}
            </if>
            <if test="userVO!=null and userVO.resignState!=null">
                and resign_state = #{userVO.resignState}
            </if>
            <if test="userVO!=null and userVO.staffNo!=null and userVO.staffNo!='' ">
                and staff_no like concat('%',#{userVO.staffNo},'%')
            </if>
            <if test="userVO!=null and userVO.name!=null and userVO.name!='' ">
                and NAME like concat('%',#{userVO.name},'%')
            </if>
            <if test="userVO!=null and userVO.keyword!=null and userVO.keyword!='' ">
                and NAME like concat('%',#{userVO.keyword},'%') or staff_no like concat('%',#{userVO.keyword},'%')
            </if>
            <if test="userVO!=null and userVO.departmentIds!=null and userVO.departmentIds.size!=0 ">
                and id in (
                select u.id from (select user_id from user_post where post_id in ( select id from post where department_id
                in
                <foreach collection="userVO.departmentIds" open="(" close=")" separator="," item="departmentId">
                    #{departmentId}
                </foreach>

                and deleted = 0) and deleted = 0) u1
                left join user u on u.id = u1.user_id where u.deleted = 0
                )
            </if>
        </where>
    </select>

    <select id="selectDepartmentPostVO" parameterType="java.lang.Long" resultType="com.dayou.vo.DepartmentPostVO">
        SELECT
            d.NAME AS departmentName,
            p.name as postName
        FROM
            user_post up
                left join
            post p
            on up.post_id = p.id
                LEFT JOIN department d ON p.department_id = d.id
        where up.user_id = #{id} and up.deleted = 0 and d.deleted=0 and p.deleted=0
    </select>

    <select id="detail" parameterType="java.lang.Long" resultMap="userDepartmentPostMap">
        select <include refid="Base_Column_List" /> from user where deleted = 0 and id=#{id}
    </select>

    <select id="selectUserPosts" parameterType="java.lang.Long" resultType="com.dayou.vo.IdNameVO">
        SELECT
            p.id,
            CONCAT( p.NAME, '-', d.NAME ) AS NAME
        FROM
            user_post up
                LEFT JOIN post p ON up.post_id = p.id
                LEFT JOIN department d ON p.department_id = d.id
        WHERE
            up.user_id = #{id}
          AND p.deleted = 0
          AND d.deleted =0
        AND up.deleted = 0
    </select>

    <select id="getDepartmentIdByUserId" parameterType="java.lang.Long" resultType="java.lang.Long">
        SELECT
            p.department_id
        FROM
            user_post up
                LEFT JOIN post p ON p.id = up.post_id
                left join user u on u.id = up.user_id
        WHERE
            u.id = #{userId}
          AND p.deleted = 0
          AND up.deleted = 0
          and u.deleted =0
    </select>

    <select id="simplePostName" parameterType="java.lang.String" resultType="com.dayou.entity.User">
        SELECT
            u.id,u.name,enable
        FROM
            user_post up
                LEFT JOIN post p ON p.id = up.post_id
                left join user u on u.id = up.user_id
        WHERE
            p.NAME = #{postName}
          AND p.deleted = 0
          AND up.deleted = 0
          and u.deleted =0
            and resign_state = 0
          and `enable` = 1
            order by u.id
    </select>

    <select id="listPostName" parameterType="com.dayou.vo.UserVO" resultMap="userDepartmentPostMap">
        select u.id,u.staff_no,u.name ,u.sex FROM
        user_post up
        LEFT JOIN post p ON p.id = up.post_id
        left join user u on u.id = up.user_id
        WHERE
        p.NAME = #{postName}
        AND p.deleted = 0
        AND up.deleted = 0
        and u.deleted =0
          and
            not exists (
                    select 1 from team_member where team_member.deleted = 0 and team_member.member_id = u.id
                )
    </select>

    <select id="usersByDepartment" parameterType="java.lang.String" resultType="com.dayou.vo.IdNameVO">
        SELECT DISTINCT
            u.id,
            u.name
        FROM
            user_post up
                INNER JOIN ( SELECT id FROM post WHERE department_id =( SELECT id FROM department WHERE NAME = #{departmentName} AND deleted = 0 ) AND deleted = 0 ) p ON up.post_id = p.id
                LEFT JOIN user u ON u.id = up.user_id
        WHERE
            up.deleted = 0
          AND u.deleted = 0
    </select>

    <select id="usersByDepartments" parameterType="java.util.List" resultType="com.dayou.vo.IdNameVO">
        SELECT DISTINCT
            u.id,
            u.name
        FROM
            user_post up
                INNER JOIN ( SELECT id FROM post WHERE department_id in ( SELECT id FROM department WHERE NAME
                in (
                    <foreach collection="list" index="index" item="item" separator=",">
                        #{item}
                    </foreach>
                )
          AND deleted = 0 ) AND deleted = 0 ) p ON up.post_id = p.id
                LEFT JOIN user u ON u.id = up.user_id
        WHERE
            up.deleted = 0
          AND u.deleted = 0
    </select>

    <select id="usersByDepartmentIds" parameterType="java.util.Set" resultType="com.dayou.vo.IdNameVO">
        SELECT DISTINCT
        u.id,
        u.name
        FROM
        user_post up
        INNER JOIN ( SELECT id FROM post WHERE department_id in (
        <foreach collection="list" index="index" item="item" separator=",">
            #{item}
        </foreach>
        )
         AND deleted = 0 ) p ON up.post_id = p.id
        LEFT JOIN user u ON u.id = up.user_id
        WHERE
        up.deleted = 0
        AND u.deleted = 0
    </select>
    <select id="byDepartmentId" parameterType="java.lang.Long" resultType="com.dayou.entity.User">
        SELECT DISTINCT
            u.id,
            u.NAME
        FROM
            user_post up
                LEFT JOIN user u ON u.id = up.user_id
        WHERE
                up.post_id IN ( SELECT id FROM post WHERE department_id = #{departmentId} AND deleted = 0 )
          AND up.deleted = 0
          AND u.deleted = 0
          AND u.resign_state = 0
    </select>

    <select id="allotDepartmentUsers" parameterType="java.util.Set" resultType="com.dayou.common.PullDownModel">
        SELECT DISTINCT
            u.id,
            u.NAME
        FROM
            user_post up
                LEFT JOIN user u ON u.id = up.user_id
        WHERE
                up.post_id IN ( SELECT id FROM post WHERE department_id in (
                    <foreach collection="departmentIds" index="index" separator="," item="item">
                            #{item}
                    </foreach>
                    )
                                                      AND deleted = 0 )
          AND u.deleted = 0
    </select>

    <select id="infoPostName" parameterType="java.lang.String" resultType="com.dayou.entity.User">
        SELECT
            u.id,u.name,u.mobile
        FROM
            user_post up
                LEFT JOIN post p ON p.id = up.post_id
                left join user u on u.id = up.user_id
        WHERE
            p.NAME = #{postName}
          AND p.deleted = 0
          AND up.deleted = 0
          and u.deleted =0
        order by u.id
    </select>

    <!--获取资产评估师用户-->
    <select id="byAssetEvaluator" resultType="com.dayou.entity.User">
        SELECT user.id,
               user.name
        FROM user, user_archive
        WHERE user.id = user_archive.user_id
        AND user_archive.asset_evaluator_registration_no IS NOT NULL
        AND user.deleted = 0
        AND user_archive.deleted = 0
    </select>

    <!--根据部门名称获取部门及其下辖部门人员-->
    <select id="byDepartmentName" resultType="com.dayou.entity.User">
        SELECT DISTINCT user.id, user.name
        FROM (SELECT user.id,user.name,post.department_id FROM user, user_post, post WHERE user.id = user_post.user_id AND user_post.post_id = post.id AND user.deleted = 0 AND user_post.deleted = 0 AND post.deleted = 0) AS user
        INNER JOIN (SELECT id
        FROM (SELECT id,parent_id FROM department WHERE deleted = 0 ORDER BY parent_id, id ) org_query,
             (SELECT @id := (SELECT id FROM department WHERE name = #{depName} AND deleted = 0)) initialisation
        WHERE (FIND_IN_SET(parent_id, @id) > 0 OR FIND_IN_SET(id, @id) > 0)
                  AND @id := CONCAT(@id, ',', id)) AS depId ON depId.id = user.department_id
    </select>
</mapper>
