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

    <resultMap type="FmsTaskpick" id="FmsTaskpickResult">
        <result property="taskpickId"    column="taskpickId"    />
        <result property="taskId"    column="taskId"    />
        <result property="pickArea"    column="pickArea"    />
        <result property="pickWeight"    column="pickWeight"    />
        <result property="pickDate"    column="pickDate"    />
        <result property="pickRemark"    column="pickRemark"    />
    </resultMap>

    <select id="selectFmsTaskpickList" parameterType="AgmTaskPickListReqVo" resultType="AgmTaskPickListResVo">
        SELECT c.cropName, c.cropType, i.pickWeight, i.pickNo, i.taskpickId, s.taskrcdCompletedate, h.warehouseName, l.landName, b.blockName, p.planNo, i.pickChangestatus,
        (select GROUP_CONCAT(resUrl) from SysRes r where r.resBusId = c.cropId) cropPreview
        FROM
        FmsTaskpick i
        LEFT JOIN FmsTask t ON t.taskId = i.taskId
        LEFT JOIN FmsLand l ON t.landId = l.landId
        LEFT JOIN FmsBlock b ON t.blockId = b.blockId
        LEFT JOIN FmsPlan p ON t.planId = p.planId
        LEFT JOIN FmsCrop c ON t.cropId = c.cropId
        LEFT JOIN FmsTaskrcd s ON t.taskId = s.taskId
        LEFT JOIN WmsWarehouse h ON s.warehouseId = h.warehouseId
        WHERE (i.pickChangestatus is null or i.pickChangestatus = '0') and s.taskrcdAuditstatus = '2'

        <if test="warehouseId != null and warehouseId != ''">and h.warehouseId = #{warehouseId}</if>
        <if test="goodsName != null and goodsName != ''">and c.cropName like concat('%', #{goodsName}, '%')</if>

    </select>

    <select id="selectFmsTaskpickByTaskpickId" parameterType="String" resultType="AgmTaskPickListResVo">
        SELECT c.cropName, c.cropId, c.cropType, i.pickWeight, i.pickNo, i.taskpickId, i.pickChangestatus, s.taskrcdCompletedate,
        h.warehouseName, l.landName, b.blockName, p.planNo, p.planId,p.planPlanstartdate, p.planPlanenddate, p.planStartdate, p.planEnddate,
        (select GROUP_CONCAT(resUrl) from SysRes r where r.resBusId = c.cropId) cropPreview
        FROM
        FmsTaskpick i
        LEFT JOIN FmsTask t ON t.taskId = i.taskId
        LEFT JOIN FmsLand l ON t.landId = l.landId
        LEFT JOIN FmsBlock b ON t.blockId = b.blockId
        LEFT JOIN FmsPlan p ON t.planId = p.planId
        LEFT JOIN FmsCrop c ON t.cropId = c.cropId
        LEFT JOIN FmsTaskrcd s ON t.taskId = s.taskId and s.taskrcdAuditstatus = '2'
        LEFT JOIN WmsWarehouse h ON s.warehouseId = h.warehouseId
        where taskpickId = #{taskpickId} limit 1
    </select>

    <select id="selectWmsFinishgoodsList" parameterType="AgmTaskPickListReqVo" resultType="WmsFinishgoodsListResVo">
        SELECT * from WmsFinishgoods fg
        LEFT JOIN WmsGoods g on fg.goodsId = g.goodsId
        LEFT JOIN TssSourceinfo si on fg.sourceinfoId = si.sourceinfoId
        LEFT JOIN MppQualitycheck c on c.finishgoodsId = fg.finishgoodsId
        LEFT JOIN WmsStock s on s.goodsId = g.goodsId
        LEFT JOIN WmsWarehouse h on h.warehouseId = s.warehouseId
        <where>
            <if test="warehouseId != null and warehouseId != ''">and h.warehouseId = #{warehouseId}</if>
            <if test="finishgoodscode != null and finishgoodscode != ''">and fg.finishgoodscode = #{finishgoodscode}</if>
            <if test="finishgoodsType != null and finishgoodsType != ''">and fg.finishgoodsType = #{finishgoodsType}</if>
            <if test="goodsName != null and goodsName != ''">and (g.goodsName like concat('%', #{goodsName}, '%')
                or fg.finishgoodPickno like concat('%', #{goodsName}, '%'))
            </if>
            <if test="finishgoodNo != null and finishgoodNo != ''">and fg.finishgoodNo = #{finishgoodNo}</if>
            <if test="goodsSpec != null and goodsSpec != ''">and g.goodsSpec = #{goodsSpec}</if>
            <if test="goodsUnit != null and goodsUnit != ''">and g.goodsUnit = #{goodsUnit}</if>
            <if test="hasStock != null and hasStock == '1'.toString()">and s.stockAmount > 0</if>
        </where>
    </select>


</mapper>