<?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.yunfeiyun.agmp.fms.mapper.FmsTaskmachineMapper">
    
    <resultMap type="FmsTaskmachine" id="FmsTaskmachineResult">
        <result property="taskmachId"    column="taskmachId"    />
        <result property="taskrcdId"    column="taskrcdId"    />
        <result property="machId"    column="machId"    />
        <result property="taskmachUsedcount"    column="taskmachUsedcount"    />
    </resultMap>

    <sql id="selectFmsTaskmachineVo">
        select taskmachId, taskrcdId, machId, taskmachUsedcount from FmsTaskmachine
    </sql>

    <select id="selectFmsTaskmachineList" parameterType="FmsTaskmachine" resultMap="FmsTaskmachineResult">
        <include refid="selectFmsTaskmachineVo"/>
        <where>  
            <if test="taskrcdId != null  and taskrcdId != ''"> and taskrcdId = #{taskrcdId}</if>
            <if test="machId != null  and machId != ''"> and machId = #{machId}</if>
            <if test="taskmachUsedcount != null "> and taskmachUsedcount = #{taskmachUsedcount}</if>
        </where>
    </select>
    
    <select id="selectFmsTaskmachineByTaskmachId" parameterType="String" resultMap="FmsTaskmachineResult">
        <include refid="selectFmsTaskmachineVo"/>
        where taskmachId = #{taskmachId}
    </select>

    <select id="selectFmsTaskmachineByRcdId" parameterType="String" resultType="FmsTaskmachineResVo">
        select * from FmsTaskmachine tm left join FmsMach m on tm.machId = m.machId
        where tm.taskrcdId = #{taskrcdId}
    </select>

    <select id="selectFmsTaskmachineStat" parameterType="FmsTaskListReqVo" resultType="FmsTaskmachineResVo">
        SELECT sum(tm.taskmachUsedcount) taskmachUsedcount ,m.machId, m.machType, m.machName, m.machSpec, m.machBrand
        FROM
        FmsTaskmachine tm
        LEFT JOIN FmsTaskrcd r ON r.taskrcdId = tm.taskrcdId
        LEFT JOIN FmsMach m ON m.machId = tm.machId
        LEFT JOIN FmsTask t ON t.taskId = r.taskId
        <where>
            <if test="landId != null  and landId != ''"> and t.landId = #{landId}</if>
            <if test="blockId != null  and blockId != ''"> and t.blockId = #{blockId}</if>
            <if test="planId != null and planId != '' ">and t.planId = #{planId}</if>
            <if test="startDate != null  and startDate != ''"> and t.taskCompletedate <![CDATA[ >= ]]> #{startDate}</if>
            <if test="endDate != null  and endDate != ''"> and t.taskCompletedate <![CDATA[ <= ]]> #{endDate}</if>
            <if test="dataFilter == true and landIds != null and landIds.size() > 0">
                AND t.landId in
                <foreach collection="landIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY m.machId
    </select>
        
    <insert id="insertFmsTaskmachine" parameterType="FmsTaskmachine">
        insert into FmsTaskmachine
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskmachId != null">taskmachId,</if>
            <if test="taskrcdId != null">taskrcdId,</if>
            <if test="machId != null">machId,</if>
            <if test="taskmachUsedcount != null">taskmachUsedcount,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskmachId != null">#{taskmachId},</if>
            <if test="taskrcdId != null">#{taskrcdId},</if>
            <if test="machId != null">#{machId},</if>
            <if test="taskmachUsedcount != null">#{taskmachUsedcount},</if>
         </trim>
    </insert>

    <insert id="insertFmsTaskmachineBatch" parameterType="FmsTaskmachine">
        insert into FmsTaskmachine
        <trim prefix="(" suffix=")" suffixOverrides=",">
            taskmachId,
            taskrcdId,
            machId,
            taskmachUsedcount,
            taskmachInvestmentAmount,
        </trim>
        values
        <foreach item="data" collection="list" separator=",">
            (
            #{data.taskmachId},
            #{data.taskrcdId},
            #{data.machId},
            #{data.taskmachUsedcount},
            #{data.taskmachInvestmentAmount}
            )
        </foreach>
    </insert>

    <update id="updateFmsTaskmachine" parameterType="FmsTaskmachine">
        update FmsTaskmachine
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskrcdId != null">taskrcdId = #{taskrcdId},</if>
            <if test="machId != null">machId = #{machId},</if>
            <if test="taskmachUsedcount != null">taskmachUsedcount = #{taskmachUsedcount},</if>
        </trim>
        where taskmachId = #{taskmachId}
    </update>

    <update id="updateFmsTaskmachineBatch" parameterType="FmsTaskmachine">
        <foreach collection="list" item="taskmachine" separator=";">
            update FmsTaskmachine
            <trim prefix="SET" suffixOverrides=",">
                <if test="taskmachine.taskrcdId != null">taskrcdId = #{taskmachine.taskrcdId},</if>
                <if test="taskmachine.machId != null">machId = #{taskmachine.machId},</if>
                <if test="taskmachine.taskmachUsedcount != null">taskmachUsedcount = #{taskmachine.taskmachUsedcount},</if>
            </trim>
            where taskmachId = #{taskmachine.taskmachId}
        </foreach>
    </update>

    <delete id="deleteFmsTaskmachineByTaskmachId" parameterType="String">
        delete from FmsTaskmachine where taskmachId = #{taskmachId}
    </delete>

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