<?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.hzs.system.base.mapper.BdProductStorehouseMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.system.base.BdProductStorehouse">
        <id column="PK_ID" property="pkId"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_STOREHOUSE" property="pkStorehouse"/>
        <result column="PK_PRODUCT" property="pkProduct"/>
    </resultMap>

    <!--  产品绑定仓库列表-->
    <select id="getProductStorehouseList" parameterType="com.hzs.system.base.vo.BdProductStorehouseVO"
            resultType="com.hzs.system.base.vo.BdProductStorehouseVO">
        select
        bps.pk_id pkId,
        bps.pk_storehouse pkStorehouse,
        bps.pk_product pkProduct,
        bps.creation_time creationTime,
        bp.product_code productCode,
        bp.product_name productName,
        bst.code,
        bst.name,
        bst.ware_address wareAddress,
        bst.province,
        bst.city,
        bst.county,
        bst.mobile_phone mobilePhone,
        bst.contacts,
        pr.name provinceval,
        ci.name cityval,
        co.name countyval
        from BD_PRODUCT_STOREHOUSE bps
        LEFT JOIN BD_STOREHOUSE bst on bps.PK_STOREHOUSE=bst.pk_id
        left join BD_PRODUCT bp on bps.PK_PRODUCT=bp.pk_id
        left JOIN BD_AREA pr on pr.PK_ID=bst.PROVINCE
        left JOIN BD_AREA ci on ci.PK_ID=bst.CITY
        left JOIN BD_AREA co on co.PK_ID=bst.COUNTY
        where bps.DEL_FLAG=0
        and bst.DEL_FLAG=0
        <if test="pkCountry != null">
            and bps.PK_COUNTRY= #{pkCountry}
        </if>
        <if test="code != null and code != ''">
            AND bst.CODE like '%' || #{code} || '%'
        </if>
        <if test="name != null and name != ''">
            AND bst.name like '%' || #{name} || '%'
        </if>

        <if test="productCode != null and productCode != ''">
            AND bp.PRODUCT_CODE like '%' || #{productCode} || '%'
        </if>
        <if test="productName != null and productName != ''">
            AND bp.PRODUCT_NAME like '%' || #{productName} || '%'
        </if>

        <if test="mobilePhone != null and mobilePhone != ''">
            AND bst.MOBILE_PHONE like '%' || #{mobilePhone} || '%'
        </if>
        <if test="contacts != null and contacts != ''">
            AND bst.contacts like '%' || #{contacts} || '%'
        </if>

        <if test="province !=null ">
            AND bst.province = #{province}
        </if>
        <if test="city !=null ">
            AND bst.city = #{city}
        </if>
        <if test="county !=null ">
            AND bst.county = #{county}
        </if>
        <if test="startDate != null and startDate != ''">
            and bps.creation_time >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate != ''">
            and bps.creation_time &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        ORDER BY bps.CREATION_TIME DESC
    </select>

    <select id="queryBdProductStorehouseByCondition" resultType="com.hzs.common.domain.system.base.ext.BdProductStorehouseExt">
        select bs.*,bh.pk_corp from bd_product_storehouse bs
        inner join bd_product_storehouse_area ba
        on bs.pk_id=ba.pk_product_storehouse
        inner join bd_storehouse bh
        on bh.pk_id=bs.pk_storehouse
        where bs.del_flag=0 and ba.del_flag=0 and bh.del_flag=0
        and ba.province=#{province}
        and bs.pk_product in
        <foreach collection="productId" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

</mapper>
