<?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="org.sky.scientific.mapper.AttendanceMapper">

    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.AttendanceEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="unicode" property="unicode"/>
        <result column="userName" property="userName"/>
         <result column="work_date" property="workDate"/>
        <result column="work_hours" property="workHours"/>

        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>

    <resultMap id="ResultVoMap" type="org.sky.scientific.pojo.vo.AttendanceVO">
        <result column="unicode" property="unicode"/>
        <result column="name" property="name"/>
        <result column="number" property="number"/>
        <result column="id_card" property="idCard"/>
        <result column="personnel_type" property="personnelType"/>
    </resultMap>

    <select id="selectDailyAttendanceByUnicodeAndMonth" resultType="org.sky.scientific.pojo.vo.DailyAttendance">
        select unicode,work_date, work_hours
        from kd_attendance_records
        where left(work_date,6) = #{yearAndMonth}
        <if test="unicode != null">
            and unicode = #{unicode}
        </if>
        order by work_date asc
    </select>

    <select id="selectAttendancePage" resultMap="ResultVoMap" parameterType="org.sky.scientific.pojo.dto.AttendanceDto">
        SELECT xt.unicode, t.name, t.number, t.id_card, t.personnel_type
        FROM kd_attendance_records xt
        LEFT JOIN kd_technician t on xt.unicode = t.unicode and t.year_and_month = #{ew.yearAndMonth}
        WHERE left(xt.work_date,6) = #{ew.yearAndMonth}
        <if test="ew.name != null">
            and t.name like concat('%',#{ew.name},'%')
        </if>
        <if test="ew.number != null">
            and t.number like concat('%',#{ew.number},'%')
        </if>
        <if test="ew.idCard != null">
            and t.id_card like concat('%',#{ew.idCard},'%')
        </if>
        <if test="ew.personnelType != null">
            and t.personnel_type = #{ew.personnelType}
        </if>
        <if test="ew.xmId!=null and ew.xmId > 0">
            and xt.unicode in (select distinct unicode from kd_xm_technician where xm_id = #{ew.xmId} and year_and_month = #{ew.yearAndMonth})
        </if>
        group by xt.unicode
     </select>

    <update id="batchUpdate" parameterType="java.util.List">
        <foreach collection="list" item="record" separator=";">
            update kd_attendance_records
            set work_hours = #{record.workHours}
            where id = #{record.id}
        </foreach>
    </update>

    <select id="selectPrimaryKeyByUnicodeAndWorkDate" resultType="java.lang.Long">
        select id from kd_attendance_records
        where unicode = #{unicode} and work_date = #{workDate}
    </select>

    <insert id="batchInsert" parameterType="java.util.List">
        insert into kd_attendance_records(tenant_id,unicode,work_date,work_hours,create_user,create_dept)
        VALUES
        <foreach collection="list" item="a" separator=",">
            (#{a.tenantId},#{a.unicode},#{a.workDate},#{a.workHours},#{a.createUser},#{a.createDept})
        </foreach>
    </insert>

    <select id="selectByYearAndMonth" resultMap="ResultMap" parameterType="java.lang.String">
        select id,unicode, work_date, work_hours
        from kd_attendance_records
        where left(work_date,6) = #{yearAndMonth}
    </select>

    <select id="countByUnicodeAndMonth" resultType="java.lang.Long">
        select count(*) from kd_attendance_records where unicode = #{unicode} and left(work_date,6) = #{yearAndMonth}
    </select>

    <update id="clearUnicodeAndMonthly">
        update kd_attendance_records
        set work_hours = 0
        where unicode = #{unicode} and left(work_date,6) = #{yearAndMonth}
    </update>

    <delete id="deleteByUnicodeAndMonthly">
        delete
        from kd_attendance_records
        where unicode = #{unicode} and left(work_date,6) = #{yearAndMonth}
    </delete>

    <select id="selectInfoByUnicodeAndDate" resultMap="ResultMap" parameterType="org.sky.scientific.pojo.entity.AttendanceEntity">
        SELECT xt.unicode, t.name as userName ,xt.work_date, xt.work_hours
        FROM kd_attendance_records xt
        LEFT JOIN kd_technician t on xt.unicode = t.unicode and t.year_and_month = left(#{workDate},6)
        WHERE xt.work_date = #{workDate} and xt.unicode = #{unicode}
    </select>
</mapper>
