<?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.api.manage.dao.operationManagement.SysAttendanceTeamDao">

    <select id="list" parameterType="com.api.model.operationManagement.SearchAttendanceTeam" resultType="com.api.vo.operationManagement.VoAttendanceTeam">
        select sat.*,su.actual_name as create_name,so.name as organization_name
        from sys_attendance_team as sat,sys_user as su,sys_organization as so
        <where>
            sat.create_id = su.id
            and sat.organization_id = so.id
            <if test="name != null and name != ''">
                and sat.name like concat('%',#{name},'%')
            </if>
            <if test="organizationId != null">
                and sat.organization_id = #{organizationId}
            </if>
            <if test="teamMembersId != null">
                and find_in_set(#{teamMembersId},sat.team_members)
            </if>
            <if test="createId != null">
                and sat.create_id = #{createId}
            </if>
        </where>
    </select>
    <select id="findUserNameById" parameterType="java.lang.String" resultType="java.lang.String">
        select actual_name as name from sys_user where id = #{s}
    </select>

    <insert id="insert" parameterType="com.api.model.operationManagement.SysAttendanceTeam">
        insert into sys_attendance_team(
            name,
            organization_id,
            team_members,
            create_id,
            create_date
        )values (
            #{name},
            #{organizationId},
            #{teamMembers},
            #{createId},
            #{createDate}
        )
    </insert>

    <update id="update" parameterType="com.api.model.operationManagement.SysAttendanceTeam">
        update sys_attendance_team
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="organizationId != null">
                organization_id = #{organizationId},
            </if>
            <if test="teamMembers != null and teamMembers != ''">
                team_members = #{teamMembers},
            </if>
            modify_id = #{modifyId},
            modify_date = #{modifyDate}
        </set>
        where id = #{id}
    </update>

    <delete id="delete" parameterType="java.lang.Integer">
        delete from sys_attendance_team where id = #{id}
    </delete>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.model.operationManagement.SysAttendanceTeam">
        select * from sys_attendance_team where id =#{teamId}
    </select>
    <select id="countPlanByUserId" parameterType="java.lang.String" resultType="java.lang.Integer">
        select count(*)
        from sys_attendance_team as sat,sys_attendance_scheduling_plan as sasp
        where sasp.team_id = sat.id and sasp.status = 1 and FIND_IN_SET(#{s},sat.team_members)
    </select>
    <select id="findPeopleById" parameterType="java.lang.String" resultType="com.api.vo.operationManagement.SysAttendancePeople">
        select su.id,su.actual_name as name,su.tel,si.name as identity_name
        from sys_user as su
        LEFT JOIN sys_identity as si on su.position_id = si.id
        where FIND_IN_SET(su.id,#{teamMembers})
    </select>

</mapper>