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

    <resultMap type="com.shkj.wms.domain.BaseMold" id="BaseMoldResult">
            <result property="id" column="id"/>
            <result property="moldNo" column="mold_no"/>
            <result property="moldName" column="mold_name"/>
            <result property="type" column="type"/>
            <result property="model" column="model"/>
            <result property="length" column="length"/>
            <result property="width" column="width"/>
            <result property="height" column="height"/>
            <result property="volume" column="volume"/>
            <result property="serviceInterval" column="service_interval"/>
            <result property="serviceLife" column="service_life"/>
            <result property="isDel" column="is_del"/>
            <result property="isActivity" column="is_activity"/>
            <result property="barCode" column="bar_code"/>
            <result property="branchId" column="branch_id"/>
            <result property="updateBy" column="update_by"/>
            <result property="createBy" column="create_by"/>
            <result property="updateTime" column="update_time"/>
            <result property="createTime" column="create_time"/>
    </resultMap>
    <select id="queryMoldByBarCode" resultType="com.shkj.wms.vo.BaseMoldVo"
            parameterType="com.shkj.wms.bo.BaseMoldBarcodeQueryBo">

        SELECT
            id,mold_no,mold_name,type,model,length,width,height,volume,service_interval,service_life,
            is_del,is_activity,branch_id,update_by,create_by,update_time,create_time,bar_code as moldBarcode,ifnull(weight,0) as weight
        FROM
        base_mold m
        WHERE
        m.is_activity = 1
        AND m.is_del = 0
        <if test="moldBarcode != null and moldBarcode != ''">
            AND m.bar_code  like CONCAT('%', #{moldBarcode},'%')
        </if>

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


    </select>
    <select id="queryStockMoldByBarCode" resultType="com.shkj.wms.vo.BaseMoldVo"
            parameterType="com.shkj.wms.bo.BaseMoldBarcodeQueryBo">
        SELECT
        m.id,
        m.mold_no,
        m.mold_name,
        m.type,
        m.model,
        m.length,
        m.width,
        m.height,
        m.volume,
        m.service_interval,
        m.service_life,
        m.is_del,
        m.is_activity,
        m.branch_id,
        m.update_by,
        m.create_by,
        m.update_time,
        m.create_time,
        bar_code AS mold_barcode,
        c.container_name
        FROM
        base_mold m
        INNER JOIN sys_stock_detail s ON m.id = s.mold_id   AND s.qty - s.out_qty > 0
        INNER JOIN base_container c on c.id =s.container_id
        LEFT JOIN ( SELECT plan.mold_id FROM busin_out_in_plan plan WHERE plan_status NOT IN ( '2', '3' ) AND plan.type = 'out' ) AS planout ON planout.mold_id = m.id
        WHERE
        planout.mold_id IS NULL
        AND m.is_activity = 1
        AND m.is_del = 0
        <if test="moldBarcode != null and moldBarcode != ''">
            and m.bar_code like CONCAT('%', #{moldBarcode},'%')
        </if>

        <if test="moldNo != null and moldNo != ''">
            and m.mold_no like CONCAT('%', #{moldNo},'%')
        </if>

        <if test="model != null and model != ''">
            and m.model like CONCAT('%', #{model},'%')
        </if>

        <if test="moldName != null and moldName != ''">
            and m.mold_name like CONCAT('%', #{moldName},'%')
        </if>

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

      --  order by c.container_name

    </select>

    <select id="queryStockMoldByBarCodePage" resultType="com.shkj.wms.vo.BaseMoldVo"
            parameterType="com.shkj.wms.bo.BaseMoldBarcodeQueryBo">
        SELECT
        m.id,
        m.mold_no,
        m.mold_name,
        m.type,
        m.model,
        m.length,
        m.width,
        m.height,
        m.volume,
        m.service_interval,
        m.service_life,
        m.is_del,
        m.is_activity,
        m.branch_id,
        m.update_by,
        m.create_by,
        m.update_time,
        m.create_time,
        bar_code AS mold_barcode,
        c.container_name
        FROM
        base_mold m
        INNER JOIN sys_stock_detail s ON m.id = s.mold_id   AND s.qty - s.out_qty > 0
        INNER JOIN base_container c on c.id =s.container_id
        LEFT JOIN ( SELECT plan.mold_id FROM busin_out_in_plan plan WHERE plan_status NOT IN ( '2', '3' ) AND plan.type = 'out' ) AS planout ON planout.mold_id = m.id
        WHERE
        planout.mold_id IS NULL
        AND m.is_activity = 1
        AND m.is_del = 0

        <if test="moldBarcode != null and moldBarcode != ''">
            and m.bar_code like CONCAT('%', #{moldBarcode},'%')
        </if>

        <if test="moldNo != null and moldNo != ''">
            and m.mold_no like CONCAT('%', #{moldNo},'%')
        </if>

        <if test="model != null and model != ''">
            and m.model like CONCAT('%', #{model},'%')
        </if>

        <if test="moldName != null and moldName != ''">
            and m.mold_name like CONCAT('%', #{moldName},'%')
        </if>

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

    </select>

    <select id="getBaseMoldNotinStockDetail" resultType="com.shkj.wms.vo.BaseMoldVo"
            parameterType="com.shkj.wms.bo.BaseMoldQueryBo">
        select
            id,mold_no,mold_name,type,model,length,width,height,volume,service_interval,service_life,
            is_del,is_activity,branch_id,update_by,create_by,update_time,create_time,bar_code as mold_barcode
        from base_mold m
        where
         m.is_activity = 1
          and m.is_del = 0
          and not EXISTS   (select 1 from sys_stock_detail s where s.barcode = m.bar_code and s.qty + s.in_qty > 0)
        <if test="moldNo != null and moldNo != ''">
            AND m.mold_no  like CONCAT('%', #{moldNo},'%')
        </if>

        <if test="model != null and model != ''">
            AND m.model  like CONCAT('%', #{model},'%')
        </if>

        <if test="moldName != null and moldName != ''">
            AND m.mold_name  like CONCAT('%', #{moldName},'%')
        </if>

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