<?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.FmsTaskrcdMapper">
    
    <resultMap type="FmsTaskrcd" id="FmsTaskrcdResult">
        <result property="taskrcdId"    column="taskrcdId"    />
        <result property="taskId"    column="taskId"    />
        <result property="warehouseId"    column="warehouseId"    />
        <result property="taskrcdSubmitdate"    column="taskrcdSubmitdate"    />
        <result property="taskrcdCompletedate"    column="taskrcdCompletedate"    />
        <result property="taskrcdHour"    column="taskrcdHour"    />
        <result property="taskrcdWeight"    column="taskrcdWeight"    />
        <result property="taskrcdSubmitcontent"    column="taskrcdSubmitcontent"    />
        <result property="taskrcdPatrolway"    column="taskrcdPatrolway"    />
        <result property="taskrcdAuditstatus"    column="taskrcdAuditstatus"    />
        <result property="taskrcdAuditdate"    column="taskrcdAuditdate"    />
        <result property="taskrcdAuditor"    column="taskrcdAuditor"    />
        <result property="taskrcdAuditcontent"    column="taskrcdAuditcontent"    />
        <result property="taskrcdExecutor"    column="taskrcdExecutor"    />
        <result property="taskrcdExecutorname"    column="taskrcdExecutorname"    />
    </resultMap>

    <resultMap type="FmsTaskrcdListDto" id="FmsTaskrcdListResult">
        <result property="taskrcdId"    column="taskrcdId"    />
        <result property="taskId"    column="taskId"    />
        <result property="warehouseId"    column="warehouseId"    />
        <result property="taskrcdSubmitdate"    column="taskrcdSubmitdate"    />
        <result property="taskrcdCompletedate"    column="taskrcdCompletedate"    />
        <result property="taskrcdHour"    column="taskrcdHour"    />
        <result property="taskrcdWeight"    column="taskrcdWeight"    />
        <result property="taskrcdSubmitcontent"    column="taskrcdSubmitcontent"    />
        <result property="taskrcdPatrolway"    column="taskrcdPatrolway"    />
        <result property="taskrcdAuditstatus"    column="taskrcdAuditstatus"    />
        <result property="taskrcdAuditdate"    column="taskrcdAuditdate"    />
        <result property="taskrcdAuditor"    column="taskrcdAuditor"    />
        <result property="taskrcdAuditorName"    column="taskrcdAuditorName"    />
        <result property="taskrcdAuditcontent"    column="taskrcdAuditcontent"    />
        <result property="taskrcdExecutor"    column="taskrcdExecutor"    />
        <result property="taskrcdExecutorname"    column="taskrcdExecutorname"    />
        <result property="taskrcdCost"    column="taskrcdCost"    />
        <result property="taskrcdAssetInvestment"    column="taskrcdAssetInvestment"    />
        <result property="taskrcdMachineInvestment"    column="taskrcdMachineInvestment"    />

        <result property="warehouseName"    column="warehouseName"    />
        <result property="taskmachUsedcount"    column="taskmachUsedcount"    />
        <result property="machName"    column="machName"    />
        <result property="machSpec"    column="machSpec"    />
        <result property="goodsName"    column="goodsName"    />
        <result property="goodsUnit"    column="goodsUnit"    />
        <result property="goodsSpec"    column="goodsSpec"    />
        <result property="resUrl"    column="resUrl"    />
        <result property="taskassetInvestmentAmount"    column="taskassetInvestmentAmount"    />
        <result property="taskmachInvestmentAmount"    column="taskmachInvestmentAmount"    />
    </resultMap>

    <sql id="selectFmsTaskrcdVo">
        select taskrcdId, taskId, warehouseId, taskrcdCompletedate, taskrcdHour, taskrcdWeight, taskrcdSubmitcontent, taskrcdPatrolway, taskrcdAuditstatus, taskrcdAuditdate, taskrcdAuditor, taskrcdAuditcontent, taskrcdExecutor, taskrcdExecutorname from FmsTaskrcd
    </sql>

    <select id="selectTaskrcdList" parameterType="FmsTaskListReqVo" resultMap="FmsTaskrcdListResult">
        <include refid="selectFmsTaskrcdVo"/>
        <where>
            <if test="taskId != null  and taskId != ''"> and taskId = #{taskId}</if>
            <if test="taskrcdAuditstatus != null  and taskrcdAuditstatus != ''"> and taskrcdAuditstatus = #{taskrcdAuditstatus}</if>
        </where>
    </select>

    <select id="selectFmsTaskrcdList" parameterType="FmsTaskListReqVo" resultMap="FmsTaskrcdListResult">
        SELECT
        t.*,
        h.warehouseName,
        m.taskmachUsedcount,
        ma.machName,
        ma.machSpec,
        ma.machType,
        g.goodsName,
        a.taskassetUsedcount,
        g.goodsUnit,
        g.goodsSpec,
        ag.assetGoodsType as goodsType,
        r.resUrl,
        m.taskmachId,
        a.taskassetId,
        u.userName taskrcdAuditorName,
        a.taskassetInvestmentAmount,
        m.taskmachInvestmentAmount
        FROM
        FmsTaskrcd AS t
        LEFT JOIN FmsTaskasset AS a ON t.taskrcdId = a.taskrcdId
        LEFT JOIN FmsTaskmachine AS m ON t.taskrcdId = m.taskrcdId
        LEFT JOIN WmsAssetgoods ag ON ag.assetgoodsId = a.assetgoodsId
        LEFT JOIN WmsGoods g ON g.goodsId = ag.goodsId
        LEFT JOIN FmsMach AS ma ON m.machId = ma.machId
        LEFT JOIN WmsWarehouse AS h ON t.warehouseId = h.warehouseId
        LEFT JOIN SysRes AS r ON t.taskrcdId = r.resBusId
        LEFT JOIN SysUser AS u ON t.taskrcdAuditor = u.userId
        LEFT JOIN FmsTask ft on ft.taskId = t.taskId
        <where>
            <if test="taskId != null  and taskId != ''"> and t.taskId = #{taskId}</if>
            <if test="taskrcdAuditstatus != null  and taskrcdAuditstatus != ''"> and t.taskrcdAuditstatus = #{taskrcdAuditstatus}</if>
            <if test="dataFilter == true and blockIds != null and blockIds.size() > 0">
                AND ft.blockId in
                <foreach collection="blockIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by t.taskrcdSubmitdate desc
    </select>

    <select id="selectFmsTaskrcdByTaskId" parameterType="String" resultMap="FmsTaskrcdListResult">
        SELECT
        t.*,
        h.warehouseName,
        m.taskmachUsedcount,
        ma.machName,
        ma.machSpec,
        g.goodsName,
        a.taskassetUsedcount,
        g.goodsUnit,
        g.goodsSpec,
        ag.assetgoodsType as goodsType,
        r.resUrl,
        m.taskmachId,
        a.taskassetId
        FROM
        FmsTaskrcd AS t
        LEFT JOIN FmsTaskasset AS a ON t.taskrcdId = a.taskrcdId
        LEFT JOIN FmsTaskmachine AS m ON t.taskrcdId = m.taskrcdId
        LEFT JOIN WmsAssetgoods as ag on ag.assetgoodsId = a.assetgoodsId
        LEFT JOIN WmsGoods AS g ON  g.goodsId = ag.goodsId
        LEFT JOIN FmsMach AS ma ON m.machId = ma.machId
        LEFT JOIN WmsWarehouse AS h ON t.warehouseId = h.warehouseId
        LEFT JOIN SysRes AS r ON t.taskrcdId = r.resBusId
        <where>
            <if test="taskId != null  and taskId != ''"> and taskId = #{taskId}</if>
            and t.taskrcdId = (
                SELECT t2.taskrcdId
                FROM FmsTaskrcd AS t2
                WHERE t2.taskId = #{taskId}
                ORDER BY t2.taskrcdSubmitdate DESC
                LIMIT 1
            )
        </where>
    </select>
    
    <select id="selectFmsTaskrcdByTaskrcdId" parameterType="String" resultMap="FmsTaskrcdResult">
        <include refid="selectFmsTaskrcdVo"/>
        where taskrcdId = #{taskrcdId}
    </select>

    <select id="selectFmsTaskrcdInfoByTaskrcdId" parameterType="String" resultType="FmsTaskrcdByTaskrcdIdDto">
        SELECT
        t.landId,
        la.landName,
        t.blockId,
        b.blockName,
        p.planId,
        p.planNo,
        p.planArea,
        t.taskId,
        t.taskNo,
        t.taskType,
        t.taskPlanstartdate,
        t.taskPlanenddate,
        t.taskManager,
        u.userName AS taskManagername,
        t.taskCreateddate,
        t.taskContent,
        c.cropId,
        c.cropName,
        c.cropType,
        c.cropMaintype,
        tc.taskrcdId,
        tc.taskrcdExecutorname,
        m.machId,
        m.machName,
        m.machType,
        m.machSpec,
        ma.taskmachId,
        ma.taskmachUsedcount,
        a.taskassetUsedcount,
        g.goodsId,
        g.goodsName,
        ag.assetgoodsType as goodsType,
        g.goodsUnit,
        g.goodsSpec,
        a.taskassetId,
        a.taskassetUsedcount,
        w.warehouseId,
        w.warehouseName
        FROM
        FmsTaskrcd AS tc
        LEFT JOIN FmsTask AS t ON tc.taskId = t.taskId
        LEFT JOIN FmsLand AS la ON la.landId = t.landId
        LEFT JOIN FmsBlock AS b ON b.blockId = t.blockId
        LEFT JOIN FmsPlan AS p ON p.planId = t.planId
        LEFT JOIN FmsCrop AS c ON c.cropId = t.cropId
        LEFT JOIN FmsTaskmachine AS ma ON ma.taskrcdId = tc.taskrcdId
        LEFT JOIN FmsMach AS m ON m.machId = ma.machId
        LEFT JOIN FmsTaskasset AS a ON a.taskrcdId = tc.taskrcdId
        LEFT JOIN WmsAssetgoods as ag on ag.assetgoodsId = a.assetgoodsId
        LEFT JOIN WmsGoods AS g ON g.goodsId = ag.goodsId
        LEFT JOIN WmsWarehouse AS w ON w.warehouseId = tc.warehouseId
        LEFT JOIN SysUser AS u ON u.userId = t.taskManager
        LEFT JOIN SysRes AS r ON tc.taskrcdId = r.resBusId
        where tc.taskrcdId = #{taskrcdId}
    </select>
        
    <insert id="insertFmsTaskrcd" parameterType="FmsTaskrcd">
        insert into FmsTaskrcd
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskrcdId != null">taskrcdId,</if>
            <if test="taskId != null">taskId,</if>
            <if test="warehouseId != null">warehouseId,</if>
            <if test="taskrcdSubmitdate != null">taskrcdSubmitdate,</if>
            <if test="taskrcdCompletedate != null">taskrcdCompletedate,</if>
            <if test="taskrcdHour != null">taskrcdHour,</if>
            <if test="taskrcdWeight != null">taskrcdWeight,</if>
            <if test="taskrcdSubmitcontent != null">taskrcdSubmitcontent,</if>
            <if test="taskrcdPatrolway != null">taskrcdPatrolway,</if>
            <if test="taskrcdAuditstatus != null">taskrcdAuditstatus,</if>
            <if test="taskrcdAuditdate != null">taskrcdAuditdate,</if>
            <if test="taskrcdAuditor != null">taskrcdAuditor,</if>
            <if test="taskrcdAuditcontent != null">taskrcdAuditcontent,</if>
            <if test="taskrcdExecutor != null">taskrcdExecutor,</if>
            <if test="taskrcdExecutorname != null">taskrcdExecutorname,</if>
            <if test="taskrcdCost != null">taskrcdCost,</if>
            <if test="taskrcdAssetInvestment != null">taskrcdAssetInvestment,</if>
            <if test="taskrcdMachineInvestment != null">taskrcdMachineInvestment,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskrcdId != null">#{taskrcdId},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="taskrcdSubmitdate != null">#{taskrcdSubmitdate},</if>
            <if test="taskrcdCompletedate != null">#{taskrcdCompletedate},</if>
            <if test="taskrcdHour != null">#{taskrcdHour},</if>
            <if test="taskrcdWeight != null">#{taskrcdWeight},</if>
            <if test="taskrcdSubmitcontent != null">#{taskrcdSubmitcontent},</if>
            <if test="taskrcdPatrolway != null">#{taskrcdPatrolway},</if>
            <if test="taskrcdAuditstatus != null">#{taskrcdAuditstatus},</if>
            <if test="taskrcdAuditdate != null">#{taskrcdAuditdate},</if>
            <if test="taskrcdAuditor != null">#{taskrcdAuditor},</if>
            <if test="taskrcdAuditcontent != null">#{taskrcdAuditcontent},</if>
            <if test="taskrcdExecutor != null">#{taskrcdExecutor},</if>
            <if test="taskrcdExecutorname != null">#{taskrcdExecutorname},</if>
            <if test="taskrcdCost != null">#{taskrcdCost},</if>
            <if test="taskrcdAssetInvestment != null">#{taskrcdAssetInvestment},</if>
            <if test="taskrcdMachineInvestment != null">#{taskrcdMachineInvestment},</if>
         </trim>
    </insert>

    <update id="updateFmsTaskrcd" parameterType="FmsTaskrcd">
        update FmsTaskrcd
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">taskId = #{taskId},</if>
            <if test="warehouseId != null">warehouseId = #{warehouseId},</if>
            <if test="taskrcdSubmitdate != null">#{taskrcdSubmitdate},</if>
            <if test="taskrcdCompletedate != null">taskrcdCompletedate = #{taskrcdCompletedate},</if>
            <if test="taskrcdHour != null">taskrcdHour = #{taskrcdHour},</if>
            <if test="taskrcdWeight != null">taskrcdWeight = #{taskrcdWeight},</if>
            <if test="taskrcdSubmitcontent != null">taskrcdSubmitcontent = #{taskrcdSubmitcontent},</if>
            <if test="taskrcdPatrolway != null">taskrcdPatrolway = #{taskrcdPatrolway},</if>
            <if test="taskrcdAuditstatus != null">taskrcdAuditstatus = #{taskrcdAuditstatus},</if>
            <if test="taskrcdAuditdate != null">taskrcdAuditdate = #{taskrcdAuditdate},</if>
            <if test="taskrcdAuditor != null">taskrcdAuditor = #{taskrcdAuditor},</if>
            <if test="taskrcdAuditcontent != null">taskrcdAuditcontent = #{taskrcdAuditcontent},</if>
            <if test="taskrcdExecutor != null">taskrcdExecutor = #{taskrcdExecutor},</if>
            <if test="taskrcdExecutorname != null">taskrcdExecutorname = #{taskrcdExecutorname},</if>
        </trim>
        where taskrcdId = #{taskrcdId}
    </update>

    <delete id="deleteFmsTaskrcdByTaskrcdId" parameterType="String">
        delete from FmsTaskrcd where taskrcdId = #{taskrcdId}
    </delete>

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