<?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.XmTechnicianHoursMapper">

    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmTechnicianHours">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="xm_id" property="xmId"/>
        <result column="unicode" property="unicode"/>
        <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.XmTechnicianHoursVO">
        <result column="xm_id" property="xmId"/>
        <result column="xmbh" property="xmbh"/>
        <result column="xmmc" property="xmmc"/>
        <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"/>
        <result column="monthly_hours" property="monthlyHours"/>
        <collection property="dailyAttendances" ofType="org.sky.scientific.pojo.vo.DailyAttendance">
            <result column="work_date" property="workDate"/>
            <result column="work_hours" property="workHours"/>
        </collection>
    </resultMap>

    <select id="selectHoursPage" resultMap="ResultVoMap" >
        SELECT
            xm.xmmc,xm.xmbh,xm.xm_id,
            t.name, xt.unicode, t.number,t.id_card,t.department,t.nature,t.situation,t.personnel_type
        FROM kd_xm_technician_hours xt
        LEFT JOIN kd_technician t on xt.unicode = t.unicode and t.year_and_month = #{ew.yearAndMonth}
        LEFT JOIN
        (
            SELECT
                temp.id as xm_id, XMBH,
                COALESCE(get_xm_info(temp.id,'XMMC', #{ew.yearAndMonth}), temp.XMMC ) AS XMMC
            FROM kd_xm temp
        ) xm ON xt.xm_id = xm.xm_id
        WHERE left(xt.work_date,6) = #{ew.yearAndMonth}
        <if test="ew.xmId!=null">and xt.xm_id = #{ew.xmId}</if>
        <if test="ew.xmmc!=null and ew.xmmc!=''">and xm.XMMC like concat(concat('%', #{ew.xmmc}), '%')</if>
        <if test="ew.name!=null and ew.name!=''">and t.name like concat(concat('%', #{ew.name}), '%')</if>
        <if test="ew.number!=null and ew.number!=''">and t.number like concat(concat('%', #{ew.number}), '%')</if>
        <if test="ew.idCard!=null and ew.idCard!=''">and t.id_card like concat(concat('%', #{ew.idCard}), '%')</if>
        <if test="ew.personnelType!=null and ew.personnelType!=''">and t.personnel_type =#{personnelType}</if>
        group by xt.xm_id,xt.unicode
    </select>

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

    <update id="batchUpdateHours" parameterType="java.util.List">
        <foreach collection="list" item="record" separator=";">
            update kd_xm_technician_hours
            set work_hours = #{record.workHours}
            where tenant_id = #{record.tenantId} and xm_id = #{record.xmId} and unicode = #{record.unicode} and work_date = #{record.workDate}
        </foreach>
    </update>

    <select id="selectByXmIdAndUnicode" resultType="org.sky.scientific.pojo.vo.DailyAttendance">
        select work_date,work_hours
        from kd_xm_technician_hours
        where xm_id = #{xmId} and unicode = #{unicode} and left(work_date,6) = #{yearAndMonth}
        order by work_date asc
    </select>

    <select id="selectByYearAndMonth" resultMap="ResultMap">
        select id, xm_id, unicode, work_date, work_hours
        from kd_xm_technician_hours
        where left(work_date,6) = #{yearAndMonth}
        <if test="xmId!=null">and xm_id = #{xmId}</if>
        <if test="unicode!=null">and unicode = #{unicode}</if>
    </select>

    <select id="countByUnicodeAndWorkDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            unicode,
            work_date as workDate,
            ifnull(sum(work_hours), 0 ) AS hoursTotal
        FROM
            kd_xm_technician_hours
        WHERE
            LEFT ( work_date, 6 ) = #{yearAndMonth}
        GROUP BY
            unicode,
            work_date
    </select>

    <select id="selectListForPrint" resultType="com.alibaba.fastjson.JSONObject">

    </select>

    <select id="selectHoursListByUnicodeAndYear" resultType="org.sky.scientific.pojo.vo.DailyAttendance">
        SELECT
            LEFT(tmp.work_date,6) as workDate ,
            COUNT(DISTINCT work_date) as workHours
        from kd_xm_technician_hours tmp
        where tmp.unicode = #{unicode} and LEFT(tmp.work_date,4)=#{year} and work_hours > 0
        group by left(tmp.work_date,6)
    </select>

    <resultMap id="TechnicianHoursMonthlyVOMap" type="org.sky.scientific.pojo.vo.TechnicianHoursMonthlyVO">
        <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"/>
        <result column="unicode" property="unicode"/>
    </resultMap>
    <select id="meiyueyanfagongshifenpeibiao" resultMap="TechnicianHoursMonthlyVOMap" parameterType="org.sky.scientific.pojo.dto.TechnicianDTO">
        SELECT
            DISTINCT xt.unicode, name, number, id_card, personnel_type
        FROM kd_xm_technician_hours xt
        LEFT JOIN  kd_technician t on t.unicode = xt.unicode and t.year_and_month=#{ew.yearAndMonth}
        WHERE left(xt.work_date,6) = #{ew.yearAndMonth}
        <if test="ew.name!=null and ew.name!=''">and t.name like concat(concat('%', #{ew.name}), '%')</if>
        <if test="ew.number!=null and ew.number!=''">and t.number like concat(concat('%', #{ew.number}), '%')</if>
        <if test="ew.idCard!=null and ew.idCard!=''">and t.id_card like concat(concat('%', #{ew.idCard}), '%')</if>
        <if test="ew.personnelType!=null and ew.personnelType!=''">and t.personnel_type =#{ew.personnelType}</if>
     </select>

    <select id="selectDailyHoursByUnicodeAndMonth" resultType="org.sky.scientific.pojo.vo.DailyHours">
        SELECT
            work_date as workDate,SUM(work_hours) as workHours
        FROM kd_xm_technician_hours h
        WHERE left(work_date,6)= #{yearAndMonth} and unicode = #{unicode}
        GROUP BY work_date
    </select>

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

    <select id="sumByUnicodeAndDay" resultType="java.lang.Double">
        select ifnull(sum(work_hours),0)
        from kd_xm_technician_hours
        where unicode = #{unicode} and work_date = #{day}
    </select>

    <select id="sumByUnicodeAndMonth" resultType="java.lang.Double">
        select ifnull(sum(work_hours),0)
        from kd_xm_technician_hours
        where unicode = #{unicode} and left(work_date,6) = #{month}
    </select>

    <select id="selectListForSync" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            h.tenant_id as tenantId, xm.id as xmId, xm.XMBH as xmbh,h.unicode, h.work_date as workDate , h.work_hours as workHours
        FROM kd_xm_technician_hours h
        left join
        (
            SELECT m.id, m.XMKSSJ, m.xmbh,
                COALESCE(get_xm_info(m.id, 'XMJSSJ', #{yearAndMonth}),XMJSSJ) AS XMJSSJ
            FROM kd_xm m
            WHERE m.is_deleted = 0 and DATE_FORMAT(XMKSSJ, '%Y%m') <![CDATA[<=]]> #{yearAndMonth}
        ) xm on xm.id = h.xm_id
         WHERE LEFT(h.work_date, 6) = #{yearAndMonth}
    </select>

    <select id="sumHoursByXmAndUnicodeAndYear" resultType="java.lang.Double">
        select
            ifnull(sum(work_hours),0 )
        from kd_xm_technician_hours
        where xm_id = #{xmId} and unicode = #{unicode} and left(work_date,4) = #{year}
    </select>

    <update id="clearByXmUnicodeMonth">
        update kd_xm_technician_hours
        set work_hours = 0
        WHERE left(work_date,6) = #{yearAndMonth} and xm_id = #{xmId} and unicode = #{unicode}
    </update>

    <delete id="deleteByXmUnicodeMonth">
        DELETE FROM kd_xm_technician_hours
        WHERE left(work_date,6) = #{yearAndMonth} and xm_id = #{xmId} and unicode = #{unicode}
    </delete>
</mapper>
