<?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.UserPostMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.UserPost">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="user_id" property="userId" />
        <result column="post_id" property="postId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        user_id, post_id
    </sql>

    <select id="getPostList" parameterType="java.lang.Long" resultType="com.dayou.dto.SimplePostModel">
        select p.id as id,p.name as name,p.department_id as departmentId,p.parent_id as parentId
        from user_post t
                 join
             post p
             on t.post_id = p.id
        where t.user_id = #{userId}
          and t.deleted = false
          and p.deleted = false
    </select>

    <insert id="saveOrUpdateById" parameterType="com.dayou.entity.UserPost">
        insert into user_post(`id`,`user_id`, `post_id`)
        values
        (#{userPost.id},#{userPost.userId},#{userPost.postId})
        on duplicate key update
        id=id
    </insert>

    <select id="getPostUserId" parameterType="java.util.Set" resultType="java.lang.Long">
        select id from user u right join (select distinct user_id from user_post where post_id in (
            <foreach collection="juniorPost" item="item" index="index" separator=",">
                #{item}
            </foreach>
            ) and deleted = 0 ) up on up.user_id = u.id where u.deleted = 0 and enable = 1
    </select>

    <select id="getMarketPostList" parameterType="java.lang.Long" resultType="com.dayou.dto.SimplePostModel">
        select p.id as id,p.name as name,p.department_id as departmentId,p.parent_id as parentId
        from user_post t
                 join
             post p
             on t.post_id = p.id
        left join department d on d.id = p.department_id
        where t.user_id = #{userId} and d.name = '市场部'
          and t.deleted = false
          and p.deleted = false
    </select>
</mapper>
