<?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.zfire.mall.manager.commonmapper.manage.product.ProductPriceReserveMapper">

    <resultMap id="listResultMap" type="com.zfire.mall.manager.bean.manage.product.price.ProductPriceBean">
        <result column="id" jdbcType="VARCHAR" property="id" />
        <result column="admin_company_id" jdbcType="VARCHAR" property="adminCompanyId" />
        <result column="sale_type_id" jdbcType="VARCHAR" property="saleTypeId" />
        <result column="sale_type_code" jdbcType="VARCHAR" property="saleTypeCode" />
        <result column="sale_type_name" jdbcType="VARCHAR" property="saleTypeName" />
        <result column="material_id" jdbcType="VARCHAR" property="materialId" />
        <result column="material_name" jdbcType="VARCHAR" property="materialName" />
        <result column="material_number" jdbcType="VARCHAR" property="materialNumber" />
        <result column="material_old_number" jdbcType="VARCHAR" property="materialOldNumber" />
        <result column="specification" jdbcType="VARCHAR" property="specification" />
        <result column="unit" jdbcType="VARCHAR" property="unit" />
        <result column="category_id" jdbcType="VARCHAR" property="categoryId" />
        <result column="category_number" jdbcType="VARCHAR" property="categoryNumber" />
        <result column="category_name" jdbcType="VARCHAR" property="categoryName" />
        <result column="batch_price" jdbcType="DECIMAL" property="batchPrice" />
        <result column="retail_price" jdbcType="DECIMAL" property="retailPrice" />
        <result column="start_date" jdbcType="DATE" property="startDate" />
        <result column="end_date" jdbcType="DATE" property="endDate" />
        <result column="is_promote" jdbcType="BOOLEAN" property="isPromote" />
        <result column="qty" jdbcType="DECIMAL" property="qty" />
        <result column="is_revoke" jdbcType="BOOLEAN" property="isRevoke" />
        <collection property="wallets" ofType="com.zfire.mall.manager.bean.manage.product.price.ProductPriceWalletBean">
            <result column="walletItemId" jdbcType="VARCHAR" property="id" />
            <result column="product_price_id" jdbcType="VARCHAR" property="productPriceId" />
            <result column="wallet_id" jdbcType="VARCHAR" property="walletId" />
            <result column="customer_wallet_name" jdbcType="VARCHAR" property="customerWalletName" />
        </collection>
    </resultMap>

    <select id="listPage" resultMap="listResultMap">
        SELECT
            a.*,
            c.category_id,
            c.category_number,
            c.category_name,
            b.id AS walletItemId,
            b.product_price_id,
            b.wallet_id,
            d.customer_wallet_name
        FROM
            product_price a
                LEFT JOIN product_price_wallet b ON a.id = b.product_price_id
                LEFT JOIN king_dee_material c ON a.material_id = c.id
                LEFT JOIN customer_wallet d ON b.wallet_id = d.customer_wallet_id
        <where>
            <if test="adminCompanyIds != null and adminCompanyIds.size > 0">
                AND a.admin_company_id in
                <foreach item="item" index="index" collection="adminCompanyIds" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            <if test="materialNumber != null and materialNumber != ''">
                AND a.material_number = #{materialNumber}
            </if>
            <if test="materialName != null and materialName != ''">
                AND a.material_name like concat('%',#{materialName},'%')
            </if>
            <if test="startDate != null">
                AND a.start_date <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                AND a.end_date <![CDATA[ <= ]]> #{endDate}
            </if>
            <if test="saleTypeId != null and saleTypeId != ''">
                AND a.sale_type_id = #{saleTypeId}
            </if>
            <if test="categoryId != null and categoryId != ''">
                AND c.category_id = #{categoryId}
            </if>
            <if test="specification != null and specification != ''">
                AND a.specification = #{specification}
            </if>
        </where>
        order by a.create_time desc
    </select>

    <select id="queryWallet" resultType="com.zfire.mall.manager.bean.manage.product.price.ProductWalletBean">
        	select
                b.id as 'wallet_id',
                b.name as 'wallet_name',
                a.type as 'type'
            from product_price_wallet a
        join king_dee_wallet b on a.wallet_id=b.id
            where a.type='COMMONLY' and a.product_price_id=#{productPriceId}
    union
        select
                b.wallet_rebate_id as 'wallet_id',
                b.name as 'wallet_name',
                a.type  as 'type'
            from product_price_wallet a
        join wallet_rebate b on a.wallet_id=b.wallet_rebate_id
        where a.type='REBATE' and a.product_price_id=#{productPriceId}
    </select>
    <select id="v2ListPage" resultType="com.zfire.mall.manager.bean.manage.v2.product.price.ProductPriceVO">
        SELECT
            a.*
        FROM
            product_price a
        ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>
    <select id="v2BusinessListPage"
            resultType="com.zfire.mall.manager.bean.manage.v2.product.price.ProductBusinessPriceVO">
        SELECT
            a.*
        FROM
            product_business_price a
        ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>


</mapper>