<?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.ghy.worker.mapper.WorkerTeamMapper">

    <resultMap id="WorkerTeamResult" type="com.ghy.worker.domain.WorkerTeam">
        <id property="workerTeamId"         column="worker_team_id"/>
        <result property="leaderId"         column="leader_id"/>
        <result property="workerId"         column="worker_id"/>
        <result property="workerName"       column="worker_name"/>
        <result property="name"       column="name"/>
        <result property="workerLogoUrl"       column="worker_logo_url"/>
        <result property="phone"       column="phone"/>
        <result property="workerStatus"       column="worker_status"/>
        <result property="createBy"         column="create_by"/>
        <result property="createTime"       column="create_time"/>
        <result property="updateBy"         column="update_by"/>
        <result property="updateTime"       column="update_time"/>
        <result property="remark"           column="remark"/>
    </resultMap>

    <insert id="insertWorkerTeam" parameterType="com.ghy.worker.domain.WorkerTeam" useGeneratedKeys="true"
            keyProperty="workerTeamId">
        INSERT INTO worker_team(
        <if test="leaderId != null and leaderId != 0">leader_id,</if>
        <if test="workerId != null and workerId != 0">worker_id,</if>
        <if test="workerName != null and workerName != ''">worker_name,</if>
        <if test="remark != null and remark != ''">remark,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )values(
        <if test="leaderId != null and leaderId != 0">#{leaderId},</if>
        <if test="workerId != null and workerId != 0">#{workerId},</if>
        <if test="workerName != null and workerName != ''">#{workerName},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        sysdate()
        )
    </insert>

    <sql id="selectWorkerTeam">
        SELECT wt.worker_team_id, wt.leader_id, wt.worker_name, wt.worker_id, w.phone, w.name, w.worker_logo_url, w.status as worker_status
        FROM worker_team wt
        left join worker w on wt.worker_id = w.worker_id
    </sql>

    <select id="getWorkerTeamList"  parameterType="com.ghy.worker.domain.WorkerTeam" resultMap="WorkerTeamResult">
        <include refid="selectWorkerTeam"/>
        <where>
            <if test="leaderId != null and leaderId != ''">
                AND wt.leader_id = #{leaderId}
            </if>
            <if test="workerId != null">
                AND wt.worker_id = #{workerId}
            </if>
            <if test="workerStatus != null">
                AND w.status = #{workerStatus}
            </if>
            <if test="hasRegistered != null and hasRegistered != ''">
                AND w.phone IS NOT NULL
            </if>
            <if test="workerName != null and workerName != ''">
                AND ( wt.worker_name like concat('%',#{workerName},'%')
                or w.name like concat('%',#{workerName},'%') )
            </if>
        </where>
    </select>

    <delete id="deleteWorkerTeamByIds" parameterType="Long">
        DELETE FROM worker_team WHERE worker_team_id IN
        <foreach collection="array" item="workerTeamId" open="(" separator="," close=")">
            #{workerTeamId}
        </foreach>
    </delete>

    <update id="updateWorkerTeam" parameterType="com.ghy.worker.domain.WorkerTeam">
        update worker_team
        <set>
            <if test="workerName != null and workerName != ''">worker_name = #{workerName}</if>
        </set>
        where worker_team_id = #{workerTeamId}
    </update>
</mapper>
