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

    <resultMap type="com.shkj.wms.domain.BusinOutInPlan" id="BusinOutInPlanResult">
        <result property="id" column="id"/>
        <result property="orderNo" column="order_no"/>
        <result property="moldBarcode" column="mold_barcode"/>
        <result property="moldId" column="mold_id"/>
        <result property="moldNo" column="mold_no"/>
        <result property="containerId" column="container_id"/>
        <result property="containerCode" column="container_code"/>
        <result property="type" column="type"/>
        <result property="planStatus" column="plan_status"/>
        <result property="locationId" column="location_id"/>
        <result property="errorMsg" column="error_msg"/>
        <result property="qty" column="qty"/>
        <result property="branchId" column="branch_id"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>
    <select id="queryList" resultType="com.shkj.wms.vo.BusinOutInPlanVo"
            parameterType="com.shkj.wms.bo.BusinOutInPlanQueryBo">

        SELECT
        p.*,
        l.location_code,
        l.location_name,
        m.mold_name,
        m.type as moldType,
        m.model,
        m.length,
        m.width,
        m.height,
        m.volume,
        m.service_interval,
        m.service_life,
        c.container_name,
        p.plan_time
        FROM
        busin_out_in_plan p
        JOIN base_mold m ON p.mold_id = m.id
        left join base_container c on c.id = p.container_id
        left join base_location l on l.id = p.location_id
        WHERE
        1=1

        <if test="moldType != null and moldType != ''">
            and m.type = #{moldType}
        </if>


        <if test="arrType != null and arrType != ''">
            and p.type in
            <foreach collection="arrType" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>

        <if test="planStatus != null and planStatus != ''">
            and p.plan_status = #{planStatus}
        </if>
        <if test="orderNo != null and orderNo != ''">
            AND p.order_no LIKE CONCAT('%', #{orderNo}, '%')
        </if>
        <if test="containerCode != null and containerCode != ''">
            and c.container_code = #{containerCode}
        </if>
        <if test="locationCode != null and locationCode != ''">
            and l.location_name LIKE CONCAT('%',#{locationCode}, '%')
        </if>
        <if test="moldBarcode != null and moldBarcode != ''">
            AND (p.mold_barcode LIKE CONCAT('%', #{moldBarcode}, '%')
            or m.mold_name like CONCAT('%', #{moldBarcode}, '%')
            or m.mold_no like CONCAT('%', #{moldBarcode}, '%'))
        </if>

        <if test="startTime != null">
            AND DATE(p.create_time) &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND DATE(p.create_time) &lt;= #{endTime}
        </if>
        order by p.create_time desc

    </select>



    <select id="getTaskManagement" resultType="com.shkj.wms.vo.BusinTaskManageVo">
        SELECT
        count( DISTINCT order_no ) qty,
        'in' AS type
        FROM
        busin_out_in_plan
        WHERE
        plan_status = 0
        AND type IN ( 'in', 'back' ) UNION
        SELECT
        count( DISTINCT order_no ) qty,
        'out' AS type
        FROM
        busin_out_in_plan
        WHERE
        plan_status = 0
        AND type IN ( 'out', 'call' ) UNION ALL
        SELECT
        count( DISTINCT order_no ) qty,
        'timeout' AS type
        FROM
        busin_out_in_plan
        WHERE
        plan_status NOT IN ( 2, 3 )
        AND type = 'out'
        AND ifnull(plan_time,now()) &lt; now() UNION ALL
        select count(distinct inv.inv_order_no) qty ,'stockinv' from busin_stock_inv inv INNER JOIN
        sys_third_exception_request exec on inv.inv_order_no = exec.`code`
        where exec.`status`='fail'
    </select>

    <select id="getTaskInfo" resultType="com.shkj.wms.vo.BusinDataTaskVo" parameterType="com.shkj.wms.bo.BusinTaskQueryBo">
        select p.order_no,p.plan_status,c.container_name,count(qty) moldQty,max(p.create_time) create_time
        from busin_out_in_plan p
                 INNER JOIN base_container c on p.container_id=c.id
        where type in('in','back','inv_back') and plan_status='1'
        <if test="taskSDate != null and taskSDate !='' " >
            AND DATE(p.create_time) &gt;= #{taskSDate}
        </if>
        <if test="taskEDate != null and taskEDate !='' " >
            AND DATE(p.create_time) &lt;= #{taskEDate}
        </if>
        <if test="containerCode != null and containerCode != ''">
            and c.container_name  LIKE CONCAT('%',#{containerCode}, '%')
        </if>
        GROUP BY  p.order_no,p.plan_status,c.container_name

    </select>
</mapper>