<?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.shkj.wms.mapper.BusinDataTaskMapper">

    <resultMap type="com.shkj.wms.domain.BusinDataTask" id="BusinDataTaskInResult">
        <result property="taskCode" column="task_Code"/>
        <result property="taskStatus" column="task_Status"/>
        <result property="taskType" column="task_Type"/>
        <result property="taskPath" column="task_Path"/>
        <result property="isUrgent" column="is_Urgent"/>
        <result property="isTop" column="is_Top"/>
        <result property="priority" column="priority"/>
        <result property="fromRow" column="fromR_ow"/>
        <result property="fromColumn" column="from_Column"/>
        <result property="fromLayer" column="from_Layer"/>
        <result property="fromSide" column="from_Side"/>
        <result property="toRow" column="to_Row"/>
        <result property="toColumn" column="to_Column"/>
        <result property="toLayer" column="to_Layer"/>
        <result property="toSide" column="to_Side"/>
        <result property="plcId" column="plc_id"/>
        <result property="isDown" column="is_down"/>
        <result property="newDestination" column="new_destination"/>
    </resultMap>

    <select id="queryinTask" resultType="com.shkj.wms.domain.BusinDataTask">
        select * from
        busin_data_task
        where
            (task_type='12' or  task_type='15')
        and
            task_status='1'
    </select>

    <select id="selectBusinDataTaskByQrcode" resultMap="BusinDataTaskInResult"
            parameterType="String">
        SELECT
        *
        FROM
        busin_data_task
        <if test="qrCode !=null ">
            WHERE
            busin_data_task.qr_code =#{qrCode}
        </if>
    </select>

    <select id="getBusinDataTaskByTraverseDate" resultMap="BusinDataTaskInResult"
            parameterType="String">
        SELECT
        *
        FROM
        busin_data_task
        <if test="traverseDate !=null ">
            WHERE busin_data_task.traverse_date =#{traverseDate}
        </if>
    </select>

    <select id="selectBusinDataTask" resultType="com.shkj.wms.vo.BusinDataTaskVo" parameterType="com.shkj.wms.bo.BusinTaskQueryBo">
        select ifnull(is_top,'0') as is_top ,busin_data_task.* from busin_data_task
        where task_status in ('0','-1')

        <if test="taskType !=null and  taskType !=''">
            and task_type = #{taskType}
        </if>
        order by is_top,is_urgent,create_time asc
        limit 5
    </select>
    <select id="getCurDateTaskQty" resultType="com.shkj.wms.vo.DataScreeTaskqtyVo" >
        select count(*) taskQty,'out' as taskType ,
               CASE task_status WHEN '-1' THEN '0' ELSE task_status END as task_status
        from busin_data_task where create_time>=CURDATE()
                               and IFNULL(from_row,'')!='' and task_type in(4,5) and task_status not in(99,-3)
        GROUP BY task_status
        union
        select count(*) taskQty,'in' as taskType ,
               Case task_status when '-1' then '0' Else task_status End as task_status
        from busin_data_task where create_time>=CURDATE()
                               and IFNULL(to_row,'')!=''  and task_type in(0,7)
        GROUP BY task_status
    </select>


    <select id="selectBusinDataTask1" resultType="com.shkj.wms.vo.BusinDataTaskVo" parameterType="com.shkj.wms.bo.BusinTaskQueryBo">
        select ifnull(is_top,'0') as is_top ,busin_data_task.* from busin_data_task
        where task_status in ('0','-1')

        <if test="taskType !=null and  taskType !=''">
            and task_type = #{taskType}
        </if>
        order by is_top,is_urgent,create_time asc
        limit 1
    </select>

    <select id="selectOutBusinDataTask" resultType="com.shkj.wms.vo.BusinDataTaskVo" parameterType="com.shkj.wms.bo.BusinTaskQueryBo">
        select ifnull(is_top,'0') as is_top ,busin_data_task.* from busin_data_task
        where task_status in ('0','-1')

        <if test="taskType !=null and  taskType !=''">
            and task_type = #{taskType}
        </if>
        order by is_top,is_urgent, create_time asc
        <choose>
            <when test="limit !=null">
                limit #{limit}
            </when>
            <otherwise>
                limit 10
            </otherwise>
        </choose>
    </select>


    <select id="getEmptyCallPalletDataTask" resultType="com.shkj.wms.vo.BusinDataTaskVo" parameterType="com.shkj.wms.bo.BusinTaskQueryBo">
        select task.*,pointGroup.max_task_qty from busin_data_task task
        INNER JOIN base_point point on task.task_path =point.point_no and point.is_active=1
        inner join base_point_group pointGroup on point.point_group=pointGroup.id
        where task_status=1
        <if test="taskType !=null and  taskType !=''">
            and task_type = #{taskType}
        </if>

        <if test="taskPath !=null and  taskPath !=''">
            and task_path = #{taskPath}
        </if>

    </select>

    <select id="selectOutBusinDataTaskBySkipOver" resultType="com.shkj.wms.vo.BusinDataTaskVo" parameterType="com.shkj.wms.bo.BusinTaskQueryBo">
        select distinct task.* from busin_data_task task
        inner join busin_data_task_goods taskgoods on task.id =taskgoods.task_code
        where task.task_type in(4,5)    and task_status in(0,-1) and taskgoods.goods_no in

        <foreach item="goodsNo" collection="goodsNoList" open="(" separator="," close=")">
            #{goodsNo}
        </foreach>
        order by is_top,is_urgent, create_time asc
        limit 1
    </select>



    <select id="selectBusinDataTaskByKeys" resultType="com.shkj.wms.vo.BusinDataTaskVo" parameterType="com.shkj.wms.bo.BusinTaskQueryBo">
        select task.* from busin_data_task task inner join
        base_container container on task.container_id=container.id
        where  task_status !=2
        <if test="keywords !=null ">
            and container.container_code =#{keywords}
        </if>
        ORDER BY task.id
        limit 1
    </select>

    <select id="selectfrontById" resultType="com.shkj.wms.vo.BusinDataTaskVo" parameterType="com.shkj.wms.domain.BusinDataTask">
        select id

        FROM busin_data_task task

        WHERE id!=#{id}
          AND task.update_time>=SUBDATE(now()
            , INTERVAL 4 HOUR)
        ORDER BY task.update_time DESC
        limit 1


    </select>

    <insert id="insertBusinDataTask" parameterType="BusinDataTask" useGeneratedKeys="true" keyProperty="id">
        insert into busin_data_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskCode != null">task_code,</if>
            <if test="taskStatus != null">task_status,</if>
            <if test="taskType != null">task_type,</if>
            <if test="exceptionMsg != null">exception_msg,</if>
            <if test="taskPath != null">task_path,</if>
            <if test="batch != null">batch,</if>
            <if test="isUrgent != null">is_urgent,</if>
            <if test="isTop != null">is_top,</if>
            <if test="priority != null">priority,</if>
            <if test="fromRow != null">from_row,</if>
            <if test="fromColumn != null">from_column,</if>
            <if test="fromLayer != null">from_layer,</if>
            <if test="fromSide != null">from_side,</if>
            <if test="toRow != null">to_row,</if>
            <if test="toColumn != null">to_column,</if>
            <if test="toLayer != null">to_layer,</if>
            <if test="toSide != null">to_side,</if>
            <if test="taskStarttime != null">task_starttime,</if>
            <if test="taskEntime != null">task_entime,</if>
            <if test="remark != null">remark,</if>
            <if test="plcId != null">plc_id,</if>
            <if test="agvNo != null">agv_no,</if>
            <if test="punchNo != null">punch_no,</if>
            <if test="dpsNoOne != null">dps_no_one,</if>
            <if test="dpsNoTwo != null">dps_no_two,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="traverseDate != null">traverse_date,</if>
            <if test="containerId != null">container_id,</if>
            <if test="transId != null">trans_id,</if>
            <if test="branchId != null">branch_id,</if>
            <if test="points != null">points,</if>
            <if test="isDown != null">is_down,</if>
            <if test="distinction != null">distinction,</if>
            <if test="tunnel != null">tunnel,</if>
            <if test="warehouseType != null">warehouse_type,</if>
            <if test="isReturn != null">is_return,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskCode != null">#{taskCode},</if>
            <if test="taskStatus != null">#{taskStatus},</if>
            <if test="taskType != null">#{taskType},</if>
            <if test="exceptionMsg != null">#{exceptionMsg},</if>
            <if test="taskPath != null">#{taskPath},</if>
            <if test="batch != null">#{batch},</if>
            <if test="isUrgent != null">#{isUrgent},</if>
            <if test="isTop != null">#{isTop},</if>
            <if test="priority != null">#{priority},</if>
            <if test="fromRow != null">#{fromRow},</if>
            <if test="fromColumn != null">#{fromColumn},</if>
            <if test="fromLayer != null">#{fromLayer},</if>
            <if test="fromSide != null">#{fromSide},</if>
            <if test="toRow != null">#{toRow},</if>
            <if test="toColumn != null">#{toColumn},</if>
            <if test="toLayer != null">#{toLayer},</if>
            <if test="toSide != null">#{toSide},</if>
            <if test="taskStarttime != null">#{taskStarttime},</if>
            <if test="taskEntime != null">#{taskEntime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="plcId != null">#{plcId},</if>
            <if test="agvNo != null">#{agvNo},</if>
            <if test="punchNo != null">#{punchNo},</if>
            <if test="dpsNoOne != null">#{dpsNoOne},</if>
            <if test="dpsNoTwo != null">#{dpsNoTwo},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="traverseDate != null">#{traverseDate},</if>
            <if test="containerId != null">#{containerId},</if>
            <if test="transId != null">#{transId},</if>
            <if test="branchId != null">#{branchId},</if>
            <if test="points != null">#{points},</if>
            <if test="isDown != null">#{isDown},</if>
            <if test="distinction != null">#{distinction},</if>
            <if test="tunnel != null">#{tunnel},</if>
            <if test="warehouseType != null">#{warehouseType},</if>
            <if test="isReturn != null">#{isReturn},</if>
        </trim>

        <selectKey resultType="java.lang.Long" keyProperty="id">
            SELECT LAST_INSERT_ID()
        </selectKey>

    </insert>

    <select id="selectOutBoundDataTask" parameterType="java.lang.String" resultType="com.shkj.wms.vo.BusinDataTaskOutVo">
        select task.id as taskId,task.task_code ,task_status,task_type,batch,is_top,is_urgent,priority,goods_no
        from busin_data_task task
        inner join busin_data_task_goods goods on task.id=goods.task_code
        where ifnull(task_status,0)=0

        <if test="taskType =! null  and taskType !='' ">
           and  task_type in( '4','5')
        </if>

        <!--<foreach collection="list" item="goodsno" open="(" separator="," close=")">
            #{goods}
        </foreach>-->
        and  goods.goods_no= #{goodsNo}
        order by task.traverse_date,task.create_time asc,task.id
        limit 1
    </select>

    <select id="selectTaskManagement" resultType="com.shkj.wms.vo.BusinDataTaskManageVo" parameterType="com.shkj.wms.bo.BusinTaskManageQueryBo">
        select task.id as taskCode
        ,task.create_time,goodsData.vehiche_model,goodsData.color,task.task_status,task_type from busin_data_task
        task
        left join (select task_code,min(goods_no) goods_no from busin_data_task_goods where 1=1
        <if test="createTimeS !=null and createTimeS !=''">
            and DATE_FORMAT(create_time,'%Y-%m-%d')>=#{createTimeS}
        </if>

        <if test="createTimeE !=null and createTimeE !=''">
            and DATE_FORMAT(create_time,'%Y-%m-%d') &lt;=#{createTimeE}
        </if>
        group by task_code) taskGoods
        on task.id=taskGoods.task_code
        left   join base_goods_data goodsData on goodsData.goods_no =taskGoods.goods_no

        <where>
            <if test="createTimeS !=null and createTimeS !=''">
                and DATE_FORMAT(task.create_time,'%Y-%m-%d')>=#{createTimeS}
            </if>

            <if test="createTimeE !=null and createTimeE !=''">
                and DATE_FORMAT(task.create_time,'%Y-%m-%d') &lt;=#{createTimeE}
            </if>

            <if test="taskCode !=null and taskCode !='' ">
                and task.id like concat('%', #{taskCode}, '%')
            </if>

            <if test="taskType !=null and taskType !=''">
                and task_type =#{taskType}
            </if>

            <if test="taskStatus !=null and taskStatus !=''">
                and task_status =#{taskStatus}
            </if>

            <if test="exeDev !=null and exeDev !=''">
                and task_status =#{taskStatus}
            </if>

            <if test="exceStatus !=null and exceStatus =='-1'">
                and task_status ='-1'
            </if>

            <if test="exceStatus !=null and exceStatus =='1'">
                and task_status !='-1'
            </if>

            <if test="vehicheModel !=null and vehicheModel!=''  ">
                and goodsData.vehiche_model =#{vehicheModel}
            </if>
            <if test="color !=null and color !=''">
                and goodsData.color =#{color}
            </if>

        </where>
        order by task.id desc
    </select>

    <select id="callContainerCount" parameterType="java.lang.String" resultType="Integer">
        select count(*) callCounts from busin_data_task  where task_status='0' and task_type='8'
        <if test="taskPath !=null and taskPath !=''">
            and task_path= #{taskPath}
        </if>

    </select>

    <select id="gettaskAbnormal" resultType="com.shkj.wms.vo.BusinDataTaskVo" parameterType="String">
        SELECT *
        FROM
            busin_data_task
        WHERE
            task_type='15'
          AND
            task_path=#{point}
          AND
            task_status='2'
          AND
            associated_tasks_id is not null
        ORDER BY
            update_time DESC
            limit 1
    </select>

    <select id="AGVNullReach" resultType="java.lang.String" parameterType="java.lang.String">
        SELECT
            id
        FROM
            busin_data_task
        WHERE
            task_type='15'
          AND
            task_path=#{point}
          AND
            task_status='2'
          AND
            associated_tasks_id is not null
    </select>

    <select id="selectOutBoundTaskGoods" parameterType="java.lang.Long" resultType="com.shkj.wms.vo.BusinDataTaskGoodsVo">
        SELECT
	        task.id AS taskId,task.task_code,task_status,task_type,goods.qr_code,goods.goods_no ,task.container_id
        FROM
	        busin_data_task task
	    INNER JOIN
	        busin_data_task_goods goods ON task.id = goods.task_code
	    <where>
            <if test="taskId !=null ">
                and task.id= #{taskId}
            </if>
        </where>
    </select>

    <select id="getTaskCountByTunnel" resultType="com.shkj.wms.bo.TunnelTask">
        SELECT t.tunnel,COUNT(id) AS count FROM busin_data_task t WHERE t.task_type IN('0','11','20')  and  (t.`task_status` = 0 OR t.task_status = 1) AND t.tunnel IS NOT NULL GROUP BY t.tunnel
    </select>
    <select id="getOutTaskCountByTunnel" resultType="com.shkj.wms.bo.TunnelTask">
        SELECT t.tunnel,COUNT(id) AS count FROM busin_data_task t WHERE t.task_type IN('4','15')  and  (t.`task_status` = 0 OR t.task_status = 1 OR t.task_status = -1) AND t.tunnel IS NOT NULL GROUP BY t.tunnel
    </select>
    <select id="getGoodLocationNum" parameterType="java.lang.String" resultType="com.shkj.wms.bo.TunnelTask">
        SELECT t.tunnel,COUNT(t.id) AS count FROM sys_stock_detail t
        <where>
            <if test="goodNo !=null ">
                and t.good_no= #{goodNo}
            </if>
        </where>
        GROUP BY t.tunnel
    </select>

    <select id="callPalletQty" parameterType="java.lang.String" resultType="com.shkj.wms.vo.CallPalletQtyVo">
        select count(*) qty,task_status from busin_data_task
        where  task_type=8 and task_status !=2
        <if test="pointNo !=null ">
            and task_path= #{pointNo}
        </if>
        group by task_status
        order by task_status
    </select>

</mapper>
