<?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.sundot.airport.attendance.mapper.AttendancePostRecordMapper">

    <resultMap type="AttendancePostRecord" id="AttendancePostRecordResult">
        <result property="tenantId" column="tenant_id"/>
        <result property="revision" column="revision"/>
        <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="userId" column="user_id"/>
        <result property="channelCode" column="channel_code"/>
        <result property="shiftCode" column="shift_code"/>
        <result property="checkInTime" column="check_in_time"/>
        <result property="checkOutTime" column="check_out_time"/>
        <result property="workDuration" column="work_duration"/>
        <result property="status" column="status"/>
        <result property="attendanceDate" column="attendance_date"/>
        <result property="attendanceTeamId" column="attendance_team_id"/>
        <result property="overDuration" column="over_duration"/>
        <result property="id" column="id"/>
        <result property="attendanceTeamName" column="attendance_team_name"/>
        <result property="attendanceDepartmentId" column="attendance_department_id"/>
        <result property="attendanceDepartmentName" column="attendance_department_name"/>
        <result property="attendanceBrigadeId" column="attendance_brigade_id"/>
        <result property="attendanceBrigadeName" column="attendance_brigade_name"/>
        <result property="attendanceStationId" column="attendance_station_id"/>
        <result property="attendanceStationName" column="attendance_station_name"/>
        <result property="channelName" column="channel_name"/>
        <result property="regionalCode" column="regional_code"/>
        <result property="regionalName" column="regional_name"/>
        <result property="terminlCode" column="terminl_code"/>
        <result property="terminlName" column="terminl_name"/>
        <result property="userName" column="user_name"/>
        <result property="shiftName" column="shift_name"/>
        <result property="remark" column="remark"/>
        <result property="statusDesc" column="status_desc"/>
        <result property="positionCode" column="position_code"/>
        <result property="positionName" column="position_name"/>
        <result property="locked" column="locked"/>
    </resultMap>

    <sql id="selectAttendancePostRecordVo">
        select tenant_id,
               revision,
               create_by,
               create_time,
               update_by,
               update_time,
               user_id,
               channel_code,
               shift_code,
               check_in_time,
               check_out_time,
               work_duration,
               status,
               attendance_date,
               attendance_team_id,
               over_duration,
               id,
               attendance_team_name,
               attendance_department_id,
               attendance_department_name,
               attendance_brigade_id,
               attendance_brigade_name,
               attendance_station_id,
               attendance_station_name,
               channel_name,
               regional_code,
               regional_name,
               terminl_code,
               terminl_name,
               user_name,
               shift_name,
               remark,
               status_desc,
               position_code,
               position_name,
               locked
        from attendance_post_record
    </sql>

    <select id="selectAttendancePostRecordList" parameterType="AttendancePostRecord"
            resultMap="AttendancePostRecordResult">
        <include refid="selectAttendancePostRecordVo"/>
        <where>
            <if test="tenantId != null  and tenantId != ''">and tenant_id = #{tenantId}</if>
            <if test="revision != null ">and revision = #{revision}</if>
            <if test="createBy != null ">and create_by = #{createBy}</if>
            <if test="userId != null ">and user_id = #{userId}</if>
            <if test="channelCode != null  and channelCode != ''">and channel_code = #{channelCode}</if>
            <if test="shiftCode != null  and shiftCode != ''">and shift_code = #{shiftCode}</if>
            <if test="checkInTime != null ">and check_in_time = #{checkInTime}</if>
            <if test="checkInTimeStart != null">and check_in_time >= #{checkInTimeStart}</if>
            <if test="checkInTimeEnd != null">and check_in_time &lt;= #{checkInTimeEnd}</if>
            <!-- 修改未签退记录的查询条件 -->
            <if test="checkOutTimeType == 1">
                AND (check_out_time IS NULL
                OR check_out_time = '2000-01-01 00:00:00'
                OR check_out_time = '')
            </if>
            <if test="checkOutTime != null ">and check_out_time = #{checkOutTime}</if>
            <if test="workDuration != null ">and work_duration = #{workDuration}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="attendanceDate != null ">and DATE(attendance_date) = DATE(#{attendanceDate})</if>
            <if test="attendanceTeamId != null ">and attendance_team_id = #{attendanceTeamId}</if>
            <if test="overDuration != null ">and over_duration = #{overDuration}</if>
            <if test="attendanceTeamName != null  and attendanceTeamName != ''">and attendance_team_name like
                concat('%', #{attendanceTeamName}, '%')
            </if>
            <if test="attendanceDepartmentId != null ">and attendance_department_id = #{attendanceDepartmentId}</if>
            <if test="attendanceDepartmentName != null  and attendanceDepartmentName != ''">and
                attendance_department_name like concat('%', #{attendanceDepartmentName}, '%')
            </if>
            <if test="attendanceBrigadeId != null ">and attendance_brigade_id = #{attendanceBrigadeId}</if>
            <if test="attendanceBrigadeName != null  and attendanceBrigadeName != ''">and
                attendance_brigade_name like concat('%', #{attendanceBrigadeName}, '%')
            </if>
            <if test="attendanceStationId != null ">and attendance_station_id = #{attendanceStationId}</if>
            <if test="attendanceStationName != null  and attendanceStationName != ''">and attendance_station_name like
                concat('%', #{attendanceStationName}, '%')
            </if>
            <if test="channelName != null  and channelName != ''">and channel_name like concat('%', #{channelName},
                '%')
            </if>
            <if test="regionalCode != null  and regionalCode != ''">and regional_code = #{regionalCode}</if>
            <if test="regionalName != null  and regionalName != ''">and regional_name like concat('%', #{regionalName},
                '%')
            </if>
            <if test="terminlCode != null  and terminlCode != ''">and terminl_code = #{terminlCode}</if>
            <if test="terminlName != null  and terminlName != ''">and terminl_name like concat('%', #{terminlName},
                '%')
            </if>
            <if test="userName != null  and userName != ''">and user_name like concat('%', #{userName}, '%')</if>
            <if test="shiftName != null  and shiftName != ''">and shift_name like concat('%', #{shiftName}, '%')</if>
            <if test="statusDesc != null  and statusDesc != ''">and status_desc = #{statusDesc}</if>
            <if test="positionCode != null  and positionCode != ''">and position_code = #{positionCode}</if>
            <if test="positionName != null  and positionName != ''">and position_name like concat('%', #{positionName},
                '%')
            </if>
            <if test="beginTime != null">and attendance_date >= #{beginTime}</if>
            <if test="endTime != null">and attendance_date &lt;= #{endTime}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectAttendancePostRecordById" parameterType="Long" resultMap="AttendancePostRecordResult">
        <include refid="selectAttendancePostRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertAttendancePostRecord" parameterType="AttendancePostRecord" useGeneratedKeys="true"
            keyProperty="id">
        insert into attendance_post_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tenantId != null">tenant_id,</if>
            <if test="revision != null">revision,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="userId != null">user_id,</if>
            <if test="channelCode != null and channelCode != ''">channel_code,</if>
            <if test="shiftCode != null and shiftCode != ''">shift_code,</if>
            <if test="checkInTime != null">check_in_time,</if>
            <if test="checkOutTime != null">check_out_time,</if>
            <if test="workDuration != null">work_duration,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="attendanceDate != null">attendance_date,</if>
            <if test="attendanceTeamId != null">attendance_team_id,</if>
            <if test="overDuration != null">over_duration,</if>
            <if test="attendanceTeamName != null and attendanceTeamName != ''">attendance_team_name,</if>
            <if test="attendanceDepartmentId != null">attendance_department_id,</if>
            <if test="attendanceDepartmentName != null and attendanceDepartmentName != ''">attendance_department_name,
            </if>
            <if test="attendanceBrigadeId != null">attendance_brigade_id,</if>
            <if test="attendanceBrigadeName != null and attendanceBrigadeName != ''">attendance_brigade_name,</if>
            <if test="attendanceStationId != null">attendance_station_id,</if>
            <if test="attendanceStationName != null and attendanceStationName != ''">attendance_station_name,</if>
            <if test="channelName != null and channelName != ''">channel_name,</if>
            <if test="regionalCode != null and regionalCode != ''">regional_code,</if>
            <if test="regionalName != null and regionalName != ''">regional_name,</if>
            <if test="terminlCode != null and terminlCode != ''">terminl_code,</if>
            <if test="terminlName != null and terminlName != ''">terminl_name,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="shiftName != null and shiftName != ''">shift_name,</if>
            <if test="remark != null">remark,</if>
            <if test="statusDesc != null and statusDesc != ''">status_desc,</if>
            <if test="positionCode != null">position_code,</if>
            <if test="positionName != null">position_name,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tenantId != null">#{tenantId},</if>
            <if test="revision != null">#{revision},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="userId != null">#{userId},</if>
            <if test="channelCode != null and channelCode != ''">#{channelCode},</if>
            <if test="shiftCode != null and shiftCode != ''">#{shiftCode},</if>
            <if test="checkInTime != null">#{checkInTime},</if>
            <if test="checkOutTime != null">#{checkOutTime},</if>
            <if test="workDuration != null">#{workDuration},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="attendanceDate != null">#{attendanceDate},</if>
            <if test="attendanceTeamId != null">#{attendanceTeamId},</if>
            <if test="overDuration != null">#{overDuration},</if>
            <if test="attendanceTeamName != null and attendanceTeamName != ''">#{attendanceTeamName},</if>
            <if test="attendanceDepartmentId != null">#{attendanceDepartmentId},</if>
            <if test="attendanceDepartmentName != null and attendanceDepartmentName != ''">
                #{attendanceDepartmentName},
            </if>
            <if test="attendanceBrigadeId != null">#{attendanceBrigadeId},</if>
            <if test="attendanceBrigadeName != null and attendanceBrigadeName != ''">#{attendanceBrigadeName},</if>
            <if test="attendanceStationId != null">#{attendanceStationId},</if>
            <if test="attendanceStationName != null and attendanceStationName != ''">#{attendanceStationName},</if>
            <if test="channelName != null and channelName != ''">#{channelName},</if>
            <if test="regionalCode != null and regionalCode != ''">#{regionalCode},</if>
            <if test="regionalName != null and regionalName != ''">#{regionalName},</if>
            <if test="terminlCode != null and terminlCode != ''">#{terminlCode},</if>
            <if test="terminlName != null and terminlName != ''">#{terminlName},</if>
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="shiftName != null and shiftName != ''">#{shiftName},</if>
            <if test="remark != null">#{remark},</if>
            <if test="statusDesc != null and statusDesc != ''">#{statusDesc},</if>
            <if test="positionCode != null">#{positionCode},</if>
            <if test="positionName != null">#{positionName},</if>
        </trim>
    </insert>

    <update id="updateAttendancePostRecord" parameterType="AttendancePostRecord">
        update attendance_post_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="revision != null">revision = #{revision},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="channelCode != null and channelCode != ''">channel_code = #{channelCode},</if>
            <if test="shiftCode != null and shiftCode != ''">shift_code = #{shiftCode},</if>
            <if test="checkInTime != null">check_in_time = #{checkInTime},</if>
            <if test="checkOutTime != null">check_out_time = #{checkOutTime},</if>
            <if test="workDuration != null">work_duration = #{workDuration},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="attendanceDate != null">attendance_date = #{attendanceDate},</if>
            <if test="attendanceTeamId != null">attendance_team_id = #{attendanceTeamId},</if>
            <if test="overDuration != null">over_duration = #{overDuration},</if>
            <if test="attendanceTeamName != null and attendanceTeamName != ''">attendance_team_name =
                #{attendanceTeamName},
            </if>
            <if test="attendanceDepartmentId != null">attendance_department_id = #{attendanceDepartmentId},</if>
            <if test="attendanceDepartmentName != null and attendanceDepartmentName != ''">attendance_department_name =
                #{attendanceDepartmentName},
            </if>
            <if test="attendanceBrigadeId != null">attendance_brigade_id = #{attendanceBrigadeId},</if>
            <if test="attendanceBrigadeName != null and attendanceBrigadeName != ''">attendance_brigade_name =
                #{attendanceBrigadeName},
            </if>
            <if test="attendanceStationId != null">attendance_station_id = #{attendanceStationId},</if>
            <if test="attendanceStationName != null and attendanceStationName != ''">attendance_station_name =
                #{attendanceStationName},
            </if>
            <if test="channelName != null and channelName != ''">channel_name = #{channelName},</if>
            <if test="regionalCode != null and regionalCode != ''">regional_code = #{regionalCode},</if>
            <if test="regionalName != null and regionalName != ''">regional_name = #{regionalName},</if>
            <if test="terminlCode != null and terminlCode != ''">terminl_code = #{terminlCode},</if>
            <if test="terminlName != null and terminlName != ''">terminl_name = #{terminlName},</if>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="shiftName != null and shiftName != ''">shift_name = #{shiftName},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="statusDesc != null and statusDesc != ''">status_desc = #{statusDesc},</if>
            <if test="positionCode != null">position_code = #{positionCode},</if>
            <if test="positionName != null">position_name = #{positionName},</if>
            <if test="locked != null">locked = #{locked},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAttendancePostRecordById" parameterType="Long">
        delete
        from attendance_post_record
        where id = #{id}
    </delete>

    <delete id="deleteAttendancePostRecordByIds" parameterType="String">
        delete from attendance_post_record where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectTodayRecordsByUserId" resultMap="AttendancePostRecordResult">
        <include refid="selectAttendancePostRecordVo"/>
        <where>
            <if test="createBy != null">
                and create_by=#{createBy}
            </if>
            <if test="userId != null">
                and user_id=#{userId}
            </if>
            <if test="today != null">
                AND DATE(attendance_date) = DATE(#{today})
            </if>
        </where>
        ORDER BY check_in_time
    </select>

    <select id="selectTodayRecordsByShiftCodes" resultMap="AttendancePostRecordResult">
        <include refid="selectAttendancePostRecordVo"/>
        <where>
            DATE(attendance_date) = DATE(#{today})
            <if test="shiftCodes != null and shiftCodes.size() > 0">
                AND shift_code IN
                <foreach collection="shiftCodes" item="shiftCode" open="(" separator="," close=")">
                    #{shiftCode}
                </foreach>
            </if>
        </where>
        ORDER BY check_in_time, user_name
    </select>

    <select id="selectLocationsByTime" resultMap="AttendancePostRecordResult">
        <include refid="selectAttendancePostRecordVo"/>
        <where>
            user_id = #{userId}
            AND check_in_time &lt;= #{queryTime}
            AND (
            check_out_time >= #{queryTime}
            OR check_out_time = '2000-01-01 00:00:00'
            )
        </where>
        ORDER BY check_in_time DESC
    </select>

    <select id="selectLocationsByTimeAndChannelName" resultMap="AttendancePostRecordResult">
        <include refid="selectAttendancePostRecordVo"/>
        <where>
            check_in_time &lt;= #{endTime}
            AND (check_out_time IS NULL
            OR check_out_time = '2000-01-01 00:00:00'
            OR check_out_time = '')
            <if test="channelCodeList != null and channelCodeList.size() > 0">
                AND channel_code IN
                <foreach collection="channelCodeList" item="channelCode" open="(" separator="," close=")">
                    #{channelCode}
                </foreach>
            </if>
        </where>
        ORDER BY check_in_time DESC
    </select>

    <select id="selectCheckOutTimeByShiftCodes" resultMap="AttendancePostRecordResult">
        <include refid="selectAttendancePostRecordVo"/>
        <where>
            DATE(attendance_date) &lt;= DATE(#{today})
            <if test="shiftCodes != null and shiftCodes.size() > 0">
                AND attendance_team_id IN
                <foreach collection="shiftCodes" item="shiftCode" open="(" separator="," close=")">
                    #{shiftCode}
                </foreach>
            </if>
        </where>
        AND check_out_time IS NOT NULL
        AND check_out_time != '2000-01-01 00:00:00'
        AND check_out_time != ''
        ORDER BY check_out_time DESC
    </select>

    <select id="selectKezhangRecordCount" resultMap="AttendancePostRecordResult">
        <include refid="selectAttendancePostRecordVo"/>
        <where>
            user_id = #{userId}
            and check_in_time &lt;= #{endTime}
            AND (check_out_time IS NULL
            OR check_out_time = '2000-01-01 00:00:00'
            OR check_out_time = '')
        </where>
        ORDER BY check_in_time DESC
    </select>
    <select id="queryLastTime" resultMap="AttendancePostRecordResult">
        <include refid="selectAttendancePostRecordVo"/>
        <where>
            user_id = #{userId}
        </where>
        ORDER BY check_in_time DESC
    </select>
    <select id="queryKeIds" resultType="Long">
        SELECT user_id
        FROM attendance_post_record
        WHERE (channel_code is null or channel_code = '')
          and regional_code is not null
          and check_in_time >= #{startTime}
          and check_in_time &lt;= #{endTime}
          AND (check_out_time IS NULL
            OR check_out_time = '2000-01-01 00:00:00'
            OR check_out_time = '')
    </select>

    <select id="selectTeamOnList" resultMap="AttendancePostRecordResult">
        <include refid="selectAttendancePostRecordVo"/>
        <where>
            attendance_team_id = #{attendanceTeamId}
            AND attendance_date &lt;= DATE(#{date})
            AND (
            check_out_time >= #{date}
            OR check_out_time = '2000-01-01 00:00:00'
            )
        </where>
    </select>

    <select id="selectMaxDateByUserId" resultType="java.util.Date">
        select max(attendance_date) attendance_date
        from attendance_post_record
        <where>
            <if test="createBy != null">
                and create_by=#{createBy}
            </if>
            <if test="userId != null">
                and user_id=#{userId}
            </if>
        </where>
    </select>
</mapper>