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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.vo.TeamVO">
        <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="supervisor_id" property="supervisorId" />
        <result column="cultivate_id" property="cultivateId" />
        <result column="department_id" property="departmentId" />
        <result column="supervisorName" property="supervisorName" />
        <result column="cultivateName" property="cultivateName" />
        <result column="departmentName" property="departmentName" />
        <result column="cultivatePost" property="cultivatePost"/>
        <collection property="members" ofType="java.lang.Long" select="selectTeamMember" column="id"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        t.id,
        t.deleted,
        t.created,
        t.modified,
        t.name, t.supervisor_id,t.cultivate_id, t.department_id
    </sql>

    <select id="getPage" parameterType="com.dayou.vo.TeamVO" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> ,u.name as supervisorName ,u1.name as cultivateName ,d.name as departmentName,p.name as cultivatePost
        from team t left join user u on u.id = t.supervisor_id left join user u1 on u1.id = t.cultivate_id
        left join department d on d.id = t.department_id LEFT JOIN user_post up ON up.user_id = t.cultivate_id
        LEFT JOIN post p ON p.id = up.post_id
        where t.deleted = 0 and u.deleted =0 and u1.deleted = 0 and d.deleted = 0 and up.deleted = 0 and d.name = #{vo.departmentName}
        <if test="vo!=null and vo.name!=null and vo.name!= ''">
            and t.name like concat ('%',#{vo.name},'%')
        </if>
        <if test="vo!=null and vo.cultivateName!=null and vo.cultivateName!= ''">
            and u1.name like concat ('%',#{vo.cultivateName},'%')
        </if>
        order by t.id DESC
    </select>

    <select id="selectTeamMember" parameterType="java.lang.Long" resultType="com.dayou.vo.TeamMemberVO">
        SELECT
            tm.*,
            t.name as teamName,
            t.supervisor_id as supervisorId,
            u1.name as supervisorName,
            u.name as memberName,
            p.name as memberPost
        FROM
            team_member tm
                LEFT JOIN team t ON t.id = tm.team_id
                LEFT JOIN user u ON u.id = tm.member_id
                LEFT JOIN user u1 ON u1.id = t.supervisor_id
                LEFT JOIN user_post up ON up.user_id = tm.member_id
                LEFT JOIN post p ON p.id = up.post_id
        WHERE
             tm.deleted = 0 and u.deleted = 0 and u1.deleted = 0 and up.deleted = 0 and p.deleted = 0
            and tm.team_id =#{id}
    </select>
</mapper>
