<?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.FmsMachMapper">
    
    <resultMap type="FmsMach" id="FmsMachResult">
        <result property="machId"    column="machId"    />
        <result property="machType"    column="machType"    />
        <result property="machName"    column="machName"    />
        <result property="machNo"    column="machNo"    />
        <result property="machBrand"    column="machBrand"    />
        <result property="machSpec"    column="machSpec"    />
        <result property="machBuydate"    column="machBuydate"    />
        <result property="machUsestatus"    column="machUsestatus"    />
        <result property="machOwnername"    column="machOwnername"    />
        <result property="machOwnermobile"    column="machOwnermobile"    />
    </resultMap>

    <sql id="selectFmsMachVo">
        select machId, machType, machName, machNo, machBrand, machSpec, machBuydate, machUsestatus, machOwnername, machOwnermobile from FmsMach
    </sql>

    <select id="selectFmsMachList" parameterType="FmsMachListReqVo" resultMap="FmsMachResult">
        <include refid="selectFmsMachVo"/>
        <where>  
            <if test="machType != null  and machType != ''"> and machType = #{machType}</if>
            <if test="machName != null  and machName != ''"> and machName like concat('%', #{machName}, '%')</if>
            <if test="machBrand != null  and machBrand != ''"> and machBrand like concat('%', #{machBrand}, '%')</if>
            <if test="machUsestatus != null  and machUsestatus != ''"> and machUsestatus = #{machUsestatus}</if>
        </where>
        ORDER BY machId DESC
    </select>
    
    <select id="selectFmsMachByMachId" parameterType="String" resultMap="FmsMachResult">
        <include refid="selectFmsMachVo"/>
        where machId = #{machId}
    </select>

    <select id="selectFmsMachByMachNo" parameterType="String" resultMap="FmsMachResult">
        <include refid="selectFmsMachVo"/>
        where machNo = #{machNo}
    </select>

    <select id="selectFmsMachRecordList" parameterType="FmsMachRecordListReqVo" resultType="FmsMachRecordListResVo">
        SELECT ma.machName, ma.machType, p.planNo, t.taskId, t.taskNo, c.cropName, c.cropType, c.cropMaintype, m.taskmachUsedcount,
            ma.machNo, ma.machSpec, tc.taskrcdId, tc.taskrcdCompletedate, t.taskManager, u.userName AS taskManagername
        FROM FmsTaskmachine AS m
            JOIN FmsTaskrcd AS tc ON m.taskrcdId = tc.taskrcdId AND tc.taskrcdAuditstatus = #{taskrcdAuditstatus}
            JOIN FmsTask AS t ON tc.taskId = t.taskId
            JOIN FmsCrop AS c ON t.cropId = c.cropId
            JOIN FmsMach AS ma ON m.machId = ma.machId
            JOIN SysUser AS u ON t.taskManager = u.userId
            JOIN FmsPlan AS p ON p.planId = t.planId
        <where>
            <if test="machType != null  and machType != ''"> and machType = #{machType}</if>
            <if test="blockId != null  and blockId != ''"> and t.blockId = #{blockId}</if>
            <if test="machName != null  and machName != ''"> and machName like concat('%', #{machName}, '%')</if>
            <if test="planNo != null  and planNo != ''"> and p.planNo like concat('%', #{planNo}, '%')</if>
            <if test="taskNo != null  and taskNo != ''"> and t.taskNo like concat('%', #{taskNo}, '%')</if>
            <if test="cropName != null  and cropName != ''"> and c.cropName like concat('%', #{cropName}, '%')</if>
            <if test="taskManagername != null  and taskManagername != ''"> and u.userName like concat('%', #{taskManagername}, '%')</if>
            <if test="startDate != null  and startDate != ''">
                and tc.taskrcdCompletedate >=  #{startDate}
            </if>
            <if test="endDate != null  and endDate != ''">
                and tc.taskrcdCompletedate <![CDATA[ <= ]]>  #{endDate}
            </if>
            <if test="dataFilter == true and blockIds != null and blockIds.size() > 0">
                AND t.blockId in
                <foreach collection="blockIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY tc.taskrcdCompletedate DESC
    </select>
        
    <insert id="insertFmsMach" parameterType="FmsMach">
        insert into FmsMach
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="machId != null">machId,</if>
            <if test="machType != null">machType,</if>
            <if test="machName != null">machName,</if>
            <if test="machNo != null">machNo,</if>
            <if test="machBrand != null">machBrand,</if>
            <if test="machSpec != null">machSpec,</if>
            <if test="machBuydate != null">machBuydate,</if>
            <if test="machUsestatus != null">machUsestatus,</if>
            <if test="machOwnername != null">machOwnername,</if>
            <if test="machOwnermobile != null">machOwnermobile,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="machId != null">#{machId},</if>
            <if test="machType != null">#{machType},</if>
            <if test="machName != null">#{machName},</if>
            <if test="machNo != null">#{machNo},</if>
            <if test="machBrand != null">#{machBrand},</if>
            <if test="machSpec != null">#{machSpec},</if>
            <if test="machBuydate != null">#{machBuydate},</if>
            <if test="machUsestatus != null">#{machUsestatus},</if>
            <if test="machOwnername != null">#{machOwnername},</if>
            <if test="machOwnermobile != null">#{machOwnermobile},</if>
         </trim>
    </insert>

    <update id="updateFmsMach" parameterType="FmsMach">
        update FmsMach
        <trim prefix="SET" suffixOverrides=",">
            <if test="machType != null">machType = #{machType},</if>
            <if test="machName != null">machName = #{machName},</if>
            <if test="machNo != null">machNo = #{machNo},</if>
            <if test="machBrand != null">machBrand = #{machBrand},</if>
            <if test="machSpec != null">machSpec = #{machSpec},</if>
            <if test="machBuydate != null">machBuydate = #{machBuydate},</if>
            <if test="machUsestatus != null">machUsestatus = #{machUsestatus},</if>
            <if test="machOwnername != null">machOwnername = #{machOwnername},</if>
            <if test="machOwnermobile != null">machOwnermobile = #{machOwnermobile},</if>
        </trim>
        where machId = #{machId}
    </update>

    <delete id="deleteFmsMachByMachId" parameterType="String">
        delete from FmsMach where machId = #{machId}
    </delete>

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