<?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.cm.central.control.dao.projectleader.IProjectLeaderDao">

    <resultMap id="projectLeaderDTO" type="com.cm.central.control.pojo.dtos.projectleader.ProjectLeaderDTO">
        <id column="project_leader_id" property="projectLeaderId"/>
        <result column="leader_user_id" property="leaderUserId"/>
        <result column="leader_user_name" property="leaderUserName"/>
        <result column="leader_name" property="leaderName"/>
        <result column="leader_phone" property="leaderPhone"/>
        <result column="leader_email" property="leaderEmail"/>
    </resultMap>

    <resultMap id="clientProjectLeaderDTO" type="com.cm.central.control.pojo.dtos.projectleader.ClientProjectLeaderDTO" extends="projectLeaderDTO">
        <result column="client_name" property="clientName"/>
        <result column="customer_name" property="customerName"/>
        <result column="server_ip" property="serverIp"/>
    </resultMap>

    <!-- 新增项目负责人 -->
    <insert id="saveProjectLeader" parameterType="map">
        INSERT INTO central_project_leader(
            project_leader_id,
            leader_user_id,
            leader_user_name,
            leader_name,
            leader_phone,
            leader_email,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{projectLeaderId},
            #{leaderUserId},
            #{leaderUserName},
            #{leaderName},
            #{leaderPhone},
            #{leaderEmail},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除项目负责人 -->
    <update id="removeProjectLeader" parameterType="map">
        UPDATE
            central_project_leader
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            project_leader_id IN
        <foreach collection="projectLeaderIds" index="index" open="(" separator="," close=")">
            #{projectLeaderIds[${index}]}
        </foreach>
    </update>

    <!-- 删除项目负责人（物理） -->
    <update id="deleteProjectLeader" parameterType="map">
        DELETE FROM
            central_project_leader
        WHERE
            project_leader_id IN
        <foreach collection="projectLeaderIds" index="index" open="(" separator="," close=")">
            #{projectLeaderIds[${index}]}
        </foreach>
    </update>

    <!-- 修改项目负责人 -->
    <update id="updateProjectLeader" parameterType="map">
        UPDATE
            central_project_leader
        SET
        <if test="leaderUserId != null and leaderUserId != ''">
            leader_user_id = #{leaderUserId},
        </if>
        <if test="leaderUserName != null and leaderUserName != ''">
            leader_user_name = #{leaderUserName},
        </if>
        <if test="leaderName != null and leaderName != ''">
            leader_name = #{leaderName},
        </if>
        <if test="leaderPhone != null and leaderPhone != ''">
            leader_phone = #{leaderPhone},
        </if>
        <if test="leaderEmail != null and leaderEmail != ''">
            leader_email = #{leaderEmail},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            project_leader_id = #{projectLeaderId}
    </update>

    <!-- 项目负责人详情 -->
    <select id="getProjectLeader" parameterType="map" resultMap="projectLeaderDTO">
        SELECT
            t1.leader_user_id,
            t1.leader_user_name,
            t1.leader_name,
            t1.leader_phone,
            t1.leader_email,
            t1.project_leader_id
        FROM
            central_project_leader t1
        WHERE
            t1.is_delete = 0
        <if test="projectLeaderId != null and projectLeaderId != ''">
        AND
            t1.project_leader_id = #{projectLeaderId}
        </if>
    </select>

    <!-- 项目负责人列表 -->
    <select id="listProjectLeader" parameterType="map" resultMap="projectLeaderDTO">
        SELECT
            t1.leader_user_id,
            t1.leader_user_name,
            t1.leader_name,
            t1.leader_phone,
            t1.leader_email,
            t1.project_leader_id
        FROM
            central_project_leader t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.leader_user_name LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.leader_name LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="projectLeaderIds != null and projectLeaderIds.size > 0">
        AND
            t1.project_leader_id IN
            <foreach collection="projectLeaderIds" index="index" open="(" separator="," close=")">
            #{projectLeaderIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 项目负责人统计  -->
    <select id="countProjectLeader" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            central_project_leader t1
        WHERE
            t1.is_delete = 0
    </select>

    <!-- 获取项目负责人（通过客户端ID） -->
    <select id="getProjectLeaderByClientId" parameterType="java.lang.String" resultMap="clientProjectLeaderDTO">
        SELECT
            t1.client_name,
            t1.server_ip,
            t2.leader_user_id,
            t2.leader_user_name,
            t2.leader_name,
            t2.leader_phone,
            t2.leader_email,
            t2.project_leader_id,
            t3.customer_name
        FROM
            central_client t1
        LEFT JOIN
            central_project_leader t2
        ON
            t2.project_leader_id = t1.project_leader_id
        AND
            t1.is_delete = 0
        LEFT JOIN
            central_customer t3
        ON
            t1.customer_id = t3.customer_id
        AND
            t3.is_delete = 0
        WHERE
            t2.is_delete = 0
        AND
            t1.client_id = #{_parameter}
    </select>

</mapper>