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

    <resultMap type="com.shkj.wms.domain.BaseContainer" id="BaseContainerResult">
        <result property="id" column="id"/>
        <result property="containerCode" column="container_code"/>
        <result property="containerName" column="container_name"/>
        <result property="containerType" column="container_type"/>
        <result property="status" column="status"/>
        <result property="extend1" column="extend1"/>
        <result property="extend2" column="extend2"/>
        <result property="extend3" column="extend3"/>
        <result property="extend4" column="extend4"/>
        <result property="extend5" column="extend5"/>
        <result property="remark" column="remark"/>
        <result property="branchId" column="branch_id"/>
        <result property="isDel" column="is_del"/>
        <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>
    <resultMap type="com.shkj.wms.vo.BaseContainerVo" id="BaseContainerVoResult">
        <result property="id" column="id"/>
        <result property="containerCode" column="container_code"/>
        <result property="containerName" column="container_name"/>
        <result property="containerType" column="container_type"/>
        <result property="status" column="status"/>
        <result property="extend1" column="extend1"/>
        <result property="extend2" column="extend2"/>
        <result property="extend3" column="extend3"/>
        <result property="extend4" column="extend4"/>
        <result property="extend5" column="extend5"/>
        <result property="remark" column="remark"/>
        <result property="branchId" column="branch_id"/>
        <result property="isDel" column="is_del"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remainQty" column="remainQty"/>
    </resultMap>
    <select id="queryNoUserContainer" resultType="com.shkj.wms.domain.BaseContainer">
        SELECT * from base_container c
        join busin_in_billing_d bd on c.id = bd.container_id
        join busin_in_receiving_d rd on c.id = rd.container_id
        join busin_in_shelf_d sd on c.id = sd.container_id

        where c.id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and bd.line_status != 2 and bd.is_draft = 0
        and rd.line_status != 2 and rd.is_draft = 0
        and sd.line_status != 2 and sd.is_draft = 0
    </select>



    <select id="queryBaseContainerList" resultMap="BaseContainerVoResult">
        SELECT
        bc.*, container_type- ifnull(useContainer.useqty,0) remainQty
        FROM
        base_container bc
        left join (
        select count(*) useqty,container_id from sys_stock_detail
        group by container_id
        )as useContainer on bc.id =useContainer.container_id
        where is_del = '0'
        <if test="containerCode != null and containerCode != ''">
            AND (
            LOWER(bc.container_code) LIKE LOWER(CONCAT('%',#{containerCode},'%'))
                or  LOWER(bc.container_name) LIKE LOWER(CONCAT('%',#{containerCode},'%'))
            )
        </if>
        <if test="containerType != null">
            AND bc.container_type = #{containerType}
        </if>
        <if test="status != null">
            AND bc.status = #{status}
        </if>
        ${branchParams.dataScopeBranch}
        ORDER BY bc.create_time desc
    </select>

    <select id="queryContainerInWWarehoseList" resultType="com.shkj.wms.vo.BaseContainerInWWarehoseVo"
            parameterType="com.shkj.wms.bo.BaseContainerQueryListBo">
        SELECT CONSIGNOR.CONTAINER_CODE,CONSIGNOR.CONTAINER_NAME,
        BRANCH.BRANCH_NAME,STORAGES.STORAGE_NAME,LOCATION.LOCATION_NAME,STOCK.IN_TIME,DATEDIFF(CURTIME(),STOCK.IN_TIME),UPKEEP.NEXT_UPKEEP
        FROM SYS_STOCK_DETAIL STOCK
        INNER JOIN BASE_CONTAINER CONSIGNOR ON STOCK.CONTAINER_CODE=CONSIGNOR.ID
        INNER JOIN BASE_BRANCH BRANCH ON BRANCH.ID=CONSIGNOR.BRANCH_ID
        INNER JOIN BASE_LOCATION LOCATION ON STOCK.LOCATION_ID=LOCATION.ID
        INNER JOIN BASE_STORAGE STORAGES ON STORAGES.ID=STOCK.STORAGE_ID
        LEFT JOIN (
        SELECT * FROM BASE_CONTAINER_UPKEEP WHERE IFNULL(IS_UPKEEP,0)='0'
        ) UPKEEP ON UPKEEP.CONTAINER_ID =CONSIGNOR.ID

        <where>
            <if test="locationCode != null">
                AND IFNULL(LOCATION.location_name,'') like (concat('%',#{locationCode},'%'))
            </if>

            <if test="containerCode != null">
                AND IFNULL(CONSIGNOR.CONTAINER_CODE,'') like (concat('%',#{containerCode},'%'))
            </if>
        </where>
    </select>


    <select id="getContainerUpkeepDay" resultType="com.shkj.wms.vo.BusinContainerUpkeepVo"
            parameterType="com.shkj.wms.bo.BusinContainerUpkeepQueryBo">
        select conttainer.id as container_id, upkeep.id,conttainer.container_name,conttainer.container_code,
        next_upkeep,ifnull(upkeep_time,create_time) as upkeep_time,DATEDIFF(CURTIME(),ifnull(upkeep_time,create_time))
        days_used from base_container conttainer
        left join
        (
        select id, container_id,next_upkeep,upkeep_time
        from busin_container_upkeep
        where ifnull(is_upkeep,0) =0
        )upkeep on conttainer.id=upkeep.container_id
        <where>
            <if test="containerCode !=null and containerCode !='' ">
                conttainer.container_code =#{containerCode}
            </if>
            <if test="branchId != null">
                and conttainer.branch_id = #{branchId}
            </if>
        </where>
    </select>

    <select id="getContainerNoInv" resultType="com.shkj.wms.vo.BaseContainerVo">
        select distinct
        a.container_code,b.container_name from sys_stock_detail a
        inner join base_container b on a.container_code=b.container_code
        where ifnull(mold_no,'')='' and out_qty=0
    </select>



</mapper>
