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


    <select id="selectVinOutboundList" resultType="com.shkj.wms.bo.BaseBarcodesOutAddBo" parameterType="com.shkj.wms.bo.BaseBarcodesOutQueryBo">
      select id,vin, derive, traverse_date, color, bumper_type, vehicle_code, out_status ,'0' as isUrgent from base_barcodes_out
        where out_status="0"

        <if test="taskMinutes !=null and taskMinutes !='' ">
            and  TIMESTAMPDIFF(MINUTE,traverse_date,now()) between 0 and #{taskMinutes}
        </if>

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

        <if test="vehicleCode !=null and vehicleCode !='' ">
            and vehicle_code=#{vehicleCode}
        </if>

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

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

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

        order by traverse_date asc
        <if test="taskQty !=null and taskQty !=0 ">
            limit #{taskQty}
        </if>

    </select>

    <select id="selectVinOutboundListOne" resultType="com.shkj.wms.bo.BaseBarcodesOutAddBo" parameterType="com.shkj.wms.bo.BaseBarcodesOutQueryBo">
      select id,vin, derive, traverse_date, color, bumper_type, vehicle_code, out_status ,'0' as isUrgent from base_barcodes_out
        where out_status="4"

        <if test="traverseDate !=null">
            and traverse_date >= #{traverseDate}
        </if>

        <if test="endTraverseDate !=null">
            and traverse_date &lt;= #{endTraverseDate}
        </if>

        <if test="bumperType !=null">
            and bumper_type = #{bumperType}
        </if>

        <if test="color !=null">
            and color = #{color}
        </if>

        <if test="vehicleCode !=null">
            and vehicle_code = #{vehicleCode}
        </if>

        <if test="vin !=null and vin !='' ">
            and vin=#{vin}
        </if>
        order by traverse_date asc limit 1
    </select>

    <select id="getBarcodesOutData" resultType="com.shkj.wms.vo.BaseBarcodesOutVo">
        select outs.vin,derive,vehicle_code,bumper_type,color,outs.batch,head.id as transId,out_status,outs.traverse_date,'1' as isUrgent
        from base_barcodes_out outs left join busin_billhead head
        on outs.vin=head.vin
        where ifnull(audit_flg,0)!=1
        <if test="queryKeywords !=null and queryKeywords !='' ">
            and ( outs.vehicle_code like concat('%', #{queryKeywords}, '%') or outs.derive like concat('%', #{queryKeywords}, '%'))
        </if>

    </select>
    <select id="getBarcodesnoOutData" resultType="com.shkj.wms.vo.BaseBarcodesOutVo">
        SELECT o.derive,
               o.goods_no,
               o.color,
               o.bumper_type,
               o.vehicle_code,
               o.traverse_date,
               b.batch,
               b.is_cancle,
               o.id
        FROM base_barcodes_out o
                 LEFT JOIN busin_billhead b ON o.vin = b.vin
        WHERE out_status = '1'
          and ifnull(audit_flg, '0')!='1'
	      and ifnull(is_cancle,'0')!='1'
          ORDER BY batch ASC
    </select>

    <select id="getTraverseDateInfoByTraverseDate"  resultType="com.shkj.wms.vo.BaseBarcodesOutVo" parameterType="String">
        select * from  base_barcodes_out
        where traverse_date > #{correctDate}
    </select>

    <select id="selectBarcodesOutDataTaskInfo2" resultType="com.shkj.wms.vo.BaseBarcodesOutTaskVo" parameterType="com.shkj.wms.bo.BaseBarcodesOutQueryBo">
        select distinct  allouts.derive,allouts.bumper_type,allouts.vehicle_code,allouts.color,doneqty,noqty,noouts.traverse_date
        from base_barcodes_out  allouts
        left join(
        select count(*) doneqty ,derive,bumper_type,vehicle_code,color from base_barcodes_out
        where out_status!='0'
        group by derive,bumper_type,vehicle_code,color
        ) doneouts
        on allouts.derive=doneouts.derive and allouts.bumper_type= doneouts.bumper_type and allouts.vehicle_code=doneouts.vehicle_code and allouts.color=doneouts.color
        left join(
        select count(*) noqty ,derive,bumper_type,vehicle_code,color, min(traverse_date) traverse_date from base_barcodes_out
        where out_status='0'
        group by derive,bumper_type,vehicle_code,color
        ) noouts
        on allouts.derive=noouts.derive and allouts.bumper_type= noouts.bumper_type and allouts.vehicle_code=noouts.vehicle_code and allouts.color=noouts.color
        <where>
            <if test="derive !=null and derive !='' ">
            and allouts.derive=#{derive}
            </if>

            <if test="vehicleCode !=null and vehicleCode !='' ">
                and allouts.vehicle_code=#{vehicleCode}
            </if>

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

        </where>

    </select>

    <select id="selectBarcodesOutDataTaskInfo" resultType="com.shkj.wms.vo.BaseBarcodesOutTaskVo" parameterType="com.shkj.wms.bo.BaseBarcodesOutQueryBo">
        select   batch,
        max((case out_status when '0'  then qtys else 0 end)) noqty,
        max((case out_status when '1'  then qtys else 0 end))  +max((case out_status when '2'  then qtys else 0 end))+max((case out_status when '3'  then qtys else 0 end)) doneqty,
        max((case out_status when '4'  then qtys else 0 end)) cancelqty,
        max((case out_status when '5'  then qtys else 0 end)) pauseqty
        from (
            select count(*)qtys,batch,out_status
            from base_barcodes_out  allouts
            group by batch,out_status
        ) as tempstatus
        group by batch
        <where>
            <if test="batch !=null and batch !='' ">
                and batch=#{batch}
            </if>
        </where>

    </select>

    <select id="selectBarcodesOutDataTaskInfoDetail" resultType="com.shkj.wms.vo.BaseBarcodesOutVo" parameterType="com.shkj.wms.bo.BaseBarcodesOutQueryBo">
        select vin, derive,traverse_date,color,bumper_type,vehicle_code,out_status,is_cancel from base_barcodes_out
        where out_status=0 and is_cancel is null

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

        <if test="bumperType !=null and bumperType !='' ">
            and bumper_type=#{bumperType}
        </if>

        <if test="vehicleCode !=null and vehicleCode !='' ">
            and vehicle_code=#{vehicleCode}
        </if>

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

        <if test="batch !=null and batch !='' ">
            and batch=#{batch}
        </if>
        order by traverse_date
    </select>
    <select id="getOutEmptyInfo" resultType="map">
        SELECT
            CASE
                bdr.is_state
                WHEN 0 THEN '执行中'
                WHEN 1 THEN '执行中'
                WHEN 2 THEN '呼叫AGV失败'
                WHEN 3 THEN '已完成'
                END AS isState,
            bp.point_name AS pointName,
            bb.branch_name AS branchName,
            bdr.create_time AS createTime,
            ifnull(bdt.task_code,'') AS taskCode,
            ifnull(bc.container_code,'') AS containerCode
        FROM
            busin_dpscontainer_record bdr
                LEFT JOIN base_point bp ON bdr.point_no = bp.point_no
                LEFT JOIN base_branch bb ON bdr.branch_id = bb.id
                LEFT JOIN busin_data_task bdt ON bdr.task_code = bdt.id
                LEFT JOIN base_container bc ON bdr.container_id = bc.id
        where bdr.is_state != 1 and date_format(bdr.create_time,'%Y-%m-%d')  >=CURDATE()
        order by bdr.id desc,bp.point_name desc
    </select>
</mapper>
