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

    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmAssetHoursEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="xm_id" property="xmId"/>
        <result column="asset_id" property="assetId"/>
        <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.XmAssetHoursVO">
        <result column="xm_id" property="xmId"/>
        <result column="xmbh" property="xmbh"/>
        <result column="xmmc" property="xmmc"/>
        <result column="asset_id" property="assetId"/>
        <result column="zcbm" property="zcbm"/>
        <result column="zcmc" property="zcmc"/>
        <result column="zclb" property="zclb"/>
        <result column="yt" property="yt"/>
         <result column="zgs" property="zgs"/>
        <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="selectXmAssetHoursPage" resultMap="ResultVoMap" parameterType="org.sky.scientific.pojo.dto.XmAssetHoursDto">
        SELECT xa.xm_id,xm.xmbh,xm.xmmc,asset.id as asset_id,asset.zcbm,asset.zcmc,asset.zclb,asset.yt,asset.ZGS,kxah.monthly_hours
        FROM kd_xm_fixed_asset xa
        LEFT JOIN kd_asset asset ON xa.zcbm = asset.zcbm and xa.year_and_month = asset.year_and_month
        LEFT JOIN
        (
            select xm_id, asset_id, left(work_date,6) as year_and_month,sum(work_hours) as monthly_hours
            from kd_xm_asset_hours
            group by xm_id, asset_id, left(work_date,6)
        )kxah on kxah.xm_id = xa.xm_id and kxah.asset_id = asset.id and kxah.year_and_month = xa.year_and_month
        LEFT JOIN
        (

            SELECT
            xm.id,xm.xmbh,
            COALESCE(get_xm_info(xm.id,'XMMC',#{ew.yearAndMonth}),XMMC) AS XMMC,
            xm.XMKSSJ,
            COALESCE(get_xm_info(xm.id,'XMJSSJ',#{ew.yearAndMonth}),XMJSSJ) AS XMJSSJ
            FROM kd_xm xm
            WHERE xm.is_deleted = 0 and DATE_FORMAT(XMKSSJ, '%Y%m') <![CDATA[<=]]> #{ew.yearAndMonth}
        ) xm on xm.id = xa.xm_id
        WHERE xa.year_and_month = #{ew.yearAndMonth} and asset.ZCLB = '仪器设备'
        <if test="ew.xmId !=null">and xa.xm_id = #{ew.xmId}</if>
        <if test="ew.zcmc != null">and asset.zcmc like concat('%',#{ew.zcmc},'%')</if>
        <if test="ew.xmmc != null">and xm.xmmc like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.yt != null">and asset.yt = #{ew.yt}</if>
        <if test="ew.monthlyHoursStart!=null and ew.monthlyHoursEnd!=null">
            and kxah.monthly_hours between #{ew.monthlyHoursStart} and #{ew.monthlyHoursEnd}
        </if>
    </select>

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

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

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

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

    <select id="countByZcbmAfterMonth" resultType="java.lang.Long">
        select count(*)
        from kd_xm_asset_hours h
        left join kd_asset a on h.asset_id = a.id
        where a.zcbm = #{zcbm} and left(h.work_date,6) <![CDATA[>=]]> #{yearAndMonth}
    </select>

    <delete id="clearXmAssetHours">
        DELETE FROM kd_xm_asset_hours
               WHERE left(work_date,6) = #{yearAndMonth} and xm_id = #{xmId} and asset_id = #{assetId}
    </delete>
</mapper>
